시옷스 스터디에서 학습한 내용 정리, 개발하면서 삽질한 것 정리
1
<canvas id="draw" width="800" height="800"></canvas>
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
const canvas = document.querySelector("#draw");// 2D로 랜더링 컨텍스트와 그리기함수 사용const ctx = canvas.getContext("2d");canvas.width = window.innerWidth;canvas.height = window.innerHeight;ctx.strokeStyle = "#BADA55"; // strokeStyle? 색상?ctx.lineJoin = "round"; // 선이 만나는 지점ctx.lineCap = "round"; // 선이 끝나느 지점ctx.lineWidth = 50; // 선의 두께// different brush effects:ctx.globalCompositeOperation = "multiply";let isDrawing = false;let lastX = 0;let lastY = 0;let hue = 0;let direction = true;function draw(e) { // stop the fn from running when they are not moused down if (!isDrawing) return; ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`; ctx.beginPath(); // start from ctx.moveTo(lastX, lastY); // go to ctx.lineTo(e.offsetX, e.offsetY); ctx.stroke(); [lastX, lastY] = [e.offsetX, e.offsetY]; hue++; if (hue >= 360) { hue = 0; } if (ctx.lineWidth >= 100 || ctx.lineWidth <= 1) { direction = !direction; } if (direction) { ctx.lineWidth++; } else { ctx.lineWidth--; }}canvas.addEventListener("mousedown", e => { isDrawing = true; [lastX, lastY] = [e.offsetX, e.offsetY];});canvas.addEventListener("mousemove", draw);canvas.addEventListener("mouseup", () => (isDrawing = false));canvas.addEventListener("mouseout", () => (isDrawing = false));
좀 더 코드를 봐야할듯… 신기함