라라리라

2023.10.27 / Step 8 [DOM_canvas] - 코딩 78 일차 본문

코딩/2023 JavaScript DOM

2023.10.27 / Step 8 [DOM_canvas] - 코딩 78 일차

헤실 2023. 10. 27. 23:31

canvas테스트.html

 

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <canvas id="mycanvas" width="480px" height="320px" style="border: 1px solid black;"></canvas>
    <script src="canvas테스트.js"></script>

</body>
</html>

 


canvas테스트.js

 

//=============================
let canvas = document.querySelector(`#mycanvas`);
let ctx = canvas.getContext("2d");

mycanvas.html

 

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
   <canvas id="myCanvas" width="480" height="320" style="border: 1px solid black;"></canvas>
   <script src="mycanvas.js"></script>
</body>
</html>

 


mycanvas.js

 

//-------------------------------------------------
let canvas = document.getElementById("myCanvas");
let ctx = canvas.getContext("2d");

 


line.html

 

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
   
</head>
<body>
    <canvas id="myCanvas" width="800" height="600" style="border: 1px solid black;"></canvas>
   <script src="line.js"></script>
</body>
</html>

<!--
    참고사이트
-->

 


line.js

 

function drawLine(){
    /*
        [0] 열기
        ctx.beginPath();

        [1] 시작점
        moveTo (start x , start y);
        [2] 끝점
        lineTo (end x , end y)
        [3] 그리기
        ctx.stroke();

        [4] 닫기
        ctx.closePath();
    */
   
    ctx.beginPath();

    ctx.moveTo(100, 75);  // 시작점
    ctx.lineTo(300, 75);  // 끝점
    ctx.stroke();         // 그리기

    ctx.moveTo(300, 75);  // 시작점
    ctx.lineTo(300, 275);  // 끝점
    ctx.stroke();         // 그리기

    ctx.moveTo(20, 175);  // 시작점
    ctx.lineTo(45, 315);  // 끝점
    ctx.stroke();         // 그리기

    ctx.closePath();


}

//-------------------------------------------------
let canvas = document.getElementById("myCanvas");
let ctx = canvas.getContext("2d");
drawLine();