JS 강의/바닐라 JS로 그림판 만들기

#2 PAINTJS

유호야 2022. 2. 8. 23:07
반응형

#2.0 Canvas Events

캔버스에 마우스를 두면 인식하게끔

mousedown은 내가 마우스를 클릭했을 때

mouseup은 내가 마우스를 클릭한걸 뗐을 때

mouseleave는 아마 마우스가 영역을 벗어났을 때

const canvas = document.getElementById("jsCanvas");

let painting = false;

function stopPainting(){
    painting = false;
}

function onMouseMove(event){
    const x = event.offsetX;
    const y = event.offsetY;
    console.log(x, y);

}

function onMouseDown(event){
    console.log(event);
    const x = event.offsetX;
    const y = event.offsetY;

    painting = true;
}

function onMouseUp(event){
    stopPainting();
}

function onMouseLeave(event){
    stopPainting();
}

if(canvas){
    canvas.addEventListener("mousemove", onMouseMove);
    canvas.addEventListener("mousedown", onMouseDown);
    canvas.addEventListener("mouseup", onMouseUp);
    canvas.addEventListener("mouseleave", stopPainting);
}

#2.1 2D Context

canvas는 html의 한 요소인데, 다른 점은 context를 가진다.

요소안에서 픽셀에 접근할 수 있는 방법이다.

이론적으로는 이 안에 있는 픽셀들인 셈

굉장히 간단하게 context를 만들 수 있다

context 변수를 만드는 것

context의 default 설정 context는 fillcolor를 가지고 있다 

strokeStyle

lineWidth 

const canvas = document.getElementById("jsCanvas");
const ctx = canvas.getContext("2d");

ctx.strokeStyle = "#2c2c2c";
ctx.lineWidth = 2.5;

let painting = false;

function stopPainting(){
    painting = false;
}

function startPainting(){
    painting = true;
}

function onMouseMove(event){
    const x = event.offsetX;
    const y = event.offsetY;
    if(!painting) {
        ctx.beginPath();
        ctx.moveTo(x, y);
    } else {
        ctx.lineTo(x, y);
        ctx.strokeStyle();
    }
}

function onMouseDown(event){
    console.log(event);
    const x = event.offsetX;
    const y = event.offsetY;

    painting = true;
}

function onMouseUp(event){
    stopPainting();
}

function onMouseLeave(event){
    stopPainting();
}

if(canvas){
    canvas.addEventListener("mousemove", onMouseMove);
    canvas.addEventListener("mousedown", startPainting);
    canvas.addEventListener("mouseup", stopPainting);
    canvas.addEventListener("mouseleave", stopPainting);
}

 


#2.2 Recap!

const canvas = document.getElementById("jsCanvas");
const ctx = canvas.getContext("2d");

canvas.width = document.getElementsByClassName("canvas")[0].offsetWidth;
canvas.height = document.getElementsByClassName("canvas")[0].offsetHeight; 

ctx.strokeStyle = "#2c2c2c";
ctx.lineWidth = 2.5;

let painting = false;

function stopPainting(){
    painting = false;
}

function startPainting(){
    painting = true;
}

function onMouseMove(event){
    const x = event.offsetX;
    const y = event.offsetY;
    if(!painting) {
        ctx.beginPath();
        ctx.moveTo(x, y);
    } else {
        ctx.lineTo(x, y);
        ctx.stroke();
    }
}

function onMouseDown(event){
    console.log(event);
    const x = event.offsetX;
    const y = event.offsetY;

    painting = true;
}

function onMouseUp(event){
    stopPainting();
}

function onMouseLeave(event){
    stopPainting();
}

if(canvas){
    canvas.addEventListener("mousemove", onMouseMove);
    canvas.addEventListener("mousedown", startPainting);
    canvas.addEventListener("mouseup", stopPainting);
    canvas.addEventListener("mouseleave", stopPainting);
}

 

#2.3 Changing Color 

const canvas = document.getElementById("jsCanvas");
const ctx = canvas.getContext("2d");
const colors = document.getElementsByClassName("jsColor");

canvas.width = document.getElementsByClassName("canvas")[0].offsetWidth;
canvas.height = document.getElementsByClassName("canvas")[0].offsetHeight; 

ctx.strokeStyle = "#2c2c2c";
ctx.lineWidth = 2.5;

let painting = false;

function stopPainting(){
    painting = false;
}

function startPainting(){
    painting = true;
}

function onMouseMove(event){
    const x = event.offsetX;
    const y = event.offsetY;
    if(!painting) {
        ctx.beginPath();
        ctx.moveTo(x, y);
    } else {
        ctx.lineTo(x, y);
        ctx.stroke();
    }
}

function onMouseUp(event){
    stopPainting();
}

function onMouseLeave(event){
    stopPainting();
}

function handleColorClick(event){
    const color = event.target.style.backgroundColor;
    console.log(color);
    ctx.strokeStyle = color; 
}

if(canvas){
    canvas.addEventListener("mousemove", onMouseMove);
    canvas.addEventListener("mousedown", startPainting);
    canvas.addEventListener("mouseup", stopPainting);
    canvas.addEventListener("mouseleave", stopPainting);
}

Array.from(colors).forEach(color => 
    color.addEventListener("click", handleColorClick)
);

console.log(colors);
console.log(Array.from(colors));

 

 

#2.4 Brush Size

click 처럼 contextmenu는 
우클릭 했을 때 나오는 메뉴

#2.5 Filling Mode

#2.6 Saving the Image

#2.7 Conclusion

 

 

반응형

'JS 강의 > 바닐라 JS로 그림판 만들기' 카테고리의 다른 글

#3 PaintJS 그림판 완성  (0) 2022.02.08
#1 SETUP + STYLES  (0) 2022.02.08
#0 INTRODUCTION  (0) 2022.02.08