라라리라

2023.11.07 / Step 8 [DOM_포인트인 렉트] - 코딩 85 일차 본문

코딩/2023 JavaScript DOM

2023.11.07 / Step 8 [DOM_포인트인 렉트] - 코딩 85 일차

헤실 2023. 11. 7. 17:54

pointInRect.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>
    <h1>네모를 클릭하세요. </h1>
    <canvas id="myCanvas" width="800" height="600" style="border: 1px solid black;">
    </canvas>
    <div class="testText"></div>
    <script src="pointInRect.js"></script>
</body>
</html>

 


pointInRect.js

 

function draw() {
    // 캔버스를 지운다.
    ctx.clearRect(0, 0, canvas.width, canvas.height);

    // 캔버스를 다시 그린다.
    drawRect();
}

function drawRect(){
    ctx.beginPath();

    ctx.rect(x, y, width, height);
    ctx.fill();
    if(check){
        ctx.fillStyle = "red";
    }else{
        ctx.fillStyle = "blue";
    }
    ctx.closePath();
}

window.addEventListener("click", (event) => {

    // 캔버스가 화면 왼쪽 상단에서 일정간격 벗어나있다.
    // 그 간격만큼 조정한다.
    let mx = event.offsetX;
    let my = event.offsetY;
    if(x < mx && mx < x + width && y < my && my < y + height){
        check= true;  
    }else{
        check= false;
    }
    let testText = document.querySelector(".testText");
    testText.innerHTML = ``;
    testText.innerHTML += `x  : ${x} <br>`;
    testText.innerHTML += `y  : ${y} <br>`;
    testText.innerHTML += `mx  : ${mx} <br>`;
    testText.innerHTML += `my : ${my} <br>`;
    testText.innerHTML += `ctx.canvas.offsetLeft  : ${ctx.canvas.offsetLeft} <br>`;
    testText.innerHTML += `ctx.canvas.offsetTop : ${ctx.canvas.offsetTop} <br>`;
});

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

let x = 220;
let y = 220;
let width = 100;
let height = 100;

let check = false;

setInterval(draw, 10);

 


pointtest.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="800px" height="600px" style="border: 1px solid black;"></canvas>
    <script src="pointtest.js"></script>
</body>
</html>

 


pointtest.js

 

function drawrect (){
    cnt.clearRect(0, 0, myCanvas.width, myCanvas.height);
    draw();
}
function draw(){
    cnt.beginPath();
    cnt.rect(x , y, size, size);
   
        cnt.fillStyle = colorList[i];
   
    cnt.fill();
    cnt.closePath();
}
window.addEventListener("click", (a)=>{
    let mx = a.offsetX;
    let my = a.offsetY;
    if(mx >= x && mx <= x + size && my >= y && my <= y + size){
        x = Math.floor(Math.random() * (myCanvas.width - size)) ;
        y = Math.floor(Math.random() * (myCanvas.height - size)) ;
        i++;
        if(i >= 3) i = 0;
    }
})
//===================================================================
let myCanvas = document.querySelector(`#myCanvas`);
let cnt = myCanvas.getContext("2d");
let check = false;
let size = 100;
let x = Math.floor(Math.random() * (myCanvas.width - size));
let y = Math.floor(Math.random() * (myCanvas.height - size));
let i = 0;
let colorList = ["red", "green", "blue"];



setInterval(drawrect, 10);