

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HTML5 Canvas</title>
<link rel="icon" href="https://fav.farm/🔥" />
</head>
<body>
<canvas id="draw" width="800" height="800"></canvas>
<script>
</script>
<style>
html, body {
margin: 0;
}
</style>
</body>
</html>
canvas의 width와 height를 window의 innerWidth,innerHeight로 할당 const canvas = document.querySelector("#draw");
const ctx = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
window.innerWidth,innerHeight를 할당하면 canvas의 크기가 새로 할당된다
ctx의 strokeStyle(윤곽선의 색상)을 지정하고. lineJoin(선이 연결되는 지점의 모양), lineCap(선의 끝부분 모양) 을 새로 할당한다❓여기서 ctx란?
ctx는context를 뜻한다. 캔버스에 그림을 그릴 때,
getContext메서드를 호출해 캔버스의context를 가져와 사용해야 한다.
context는 캔버스의 그리기 영역이면서 그리기 메서드를 가지는 객체를 뜻함
ctx.strokeStyle = "#BADA55";
ctx.lineJoin = "round";
ctx.lineCap = "round";
let isDrawing = false;
let lastX = 0;
let lastY = 0;
draw()함수에서 isDrawing이 true가 될때 리턴시켜버린다 let isDrawing = false;
function draw(e) {
if (!isDrawing) return;
console.log(e);
}
canvas.addEventListener("mousemove", draw); //mouse를 움직일때
canvas.addEventListener("mousedown", () => (isDrawing = true)); //mouse 버튼을 눌렀을때
canvas.addEventListener("mouseup", () => (isDrawing = false)); //mouse 버튼을 누르고 있는동안 마우커서가 얻은 요소에서 마우스 버튼을 땔 때
canvas.addEventListener("mouseout", () => (isDrawing = false)); //mouse 커서가 요소 밖으로 이동할때
ctx.beginPath()ctx.moveTo(lastX,lastY)moveTo()메서드에 argument로 lastX, lastY를 넘긴다ctx.lineTo(e.offsetX, e.offsetY); function draw(e) {
if (!isDrawing) return;
console.log(e);
ctx.beginPath();
ctx.moveTo(lastX, lastY);
ctx.lineTo(e.offsetX, e.offsetY);
ctx.stroke();
}
[lastX, lastY]를 구조분해 할당을 통해서 [e.offsetX, e.offsetY]에 할당하였고ctx.strokeStyle hue라는 전역변수를 설정해 hue의 선색이 함수가 실행될때마다(마우스가 움직일때마다) 동적으로 증가되면서 값이 변경되게 만들었다(hue++) function draw(e) {
if (!isDrawing) return;
console.log(e);
ctx.beginPath();
ctx.moveTo(lastX, lastY);
ctx.lineTo(e.offsetX, e.offsetY);
ctx.stroke();
[lastX, lastY] = [e.offsetX, e.offsetY];
ctx.strokeStyle = `hsl(${hue}, 100%,50%)`;
hue++
}

hue++덕분에 hue의 색상이 마우스가 움직일때마다 동적으로 움직이고 있다
dircetion이라는 전역변수에 true를 할당하고direction을 false로 만든다true인 상태일땐, 선의 굵기를 계속 증가시키며,false가 되, 선의 굵기를 계속 감소시킨다 let direction = true;
//draw funtino
if (hue >= 360) {
hue = 0;
}
if (ctx.lineWidth >= 100 || ctx.lineWidth <= 1) {
direction = !direction;
}
if (direction) {
ctx.lineWidth++;
} else {
ctx.lineWidth--;
}
}

ctx.lineWidth가 100의 크기가 넘어가면 false가 되서 다시 감소하고 있다
ctx.globalcompositeoperation = "multiply";를 전역변수에 설정하여 흰색을 투명하게하는 기능을 추가할 수 있다.
저도 그림그리고 싶네요오!