HTML์ canvas๋ฅผ ์ด์ฉํด ๊ฐ์ข
ํจ๊ณผ๋ฅผ ๋ฃ์ด๋ด.
์ด๊ธฐ์ฝ๋
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>HTML5 Canvas</title>
</head>
<body>
<canvas id="draw" width="800" height="800"></canvas>
</body>
<style>
html, body{
margin: 0;
}
</style>
</html>
const ctx = canvas.getContext('2d');
webgl
| experimental-webgl
: 3์ฐจ์ ๋ ๋๋ง ์ปจํ
์คํธwebg2
: 3์ฐจ์ ๋ ๋๋ง ์ปจํ
์คํธ๋ฅผ ๋ํ๋ด๋ `WebGLRenderingContext๊ฐ์ฒด๋ฅผ ์์ฑbitmaprenderer
: ์บ๋ฒ์ค์ ์ปจํ
์ธ ๋ฅผ ์ฃผ์ด์ง ImageBitmap
(en-US)์ผ๋ก ๋์ฒดํ๊ธฐ์ํ ๊ธฐ๋ฅ๋ง์ ์ ๊ณตํ๋ ImageBitmapRenderingContext
(en-US)๋ฅผ ์์ฑ2d์์ ์ง์ฌ๊ฐํ , ํ ์คํธ, ์ ์ ๊ทธ๋ฆด ์ ์๊ณ , ์๋ ๋งํฌ๋ฅผ ์ฐธ๊ณ .
https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D
ctx.strokeStyle
: ๋ํ ์ฃผ์์ ์ ์ ์ฌ์ฉํ ์์ ๋๋ ์คํ์ผ. ๊ธฐ๋ณธ๊ฐ #000ctx.lineJoin
: ๋ ์ ์ด ๋ง๋๋ ๋ชจ์๋ฆฌ ์ ํ์ ์ ์ctx.lineCap
: ์ค ๋์ ์๋ฉ ์ ํ.ctx.lineWidth
: ์ ์ ๋๋น, ๊ธฐ๋ณธ๊ฐ 1.0๊ฐ์ฒด์ ๊ฒฝ๋ก๋ฅผ ์กฐ์ ๊ฐ๋ฅ.
beginPath()
: ํ์ ๊ฒฝ๋ก ๋ชฉ๋ก์ ๋น์ ์ ๊ฒฝ๋ก๋ฅผ ์์. moveTo()
: ์ ํ์ ๊ฒฝ๋ก์ ์์์ ์ (x, y)์ขํ๋ก ์ด๋ํจ.lineTo()
: ํ์ฌ ํ์ ๊ฒฝ๋ก์ ๋ง์ง๋ง ์ ์ ์ง์ ๋ (x, y)์ขํ์ ์ง์ ์ผ๋ก ์ฐ๊ฒฐํจ.stroke
: ํ์ฌ ํ ์คํ์ผ๋ก ํ์ฌ ํ์ ๊ฒฝ๋ก๋ฅผ ํ์ ๊ทธ์. const canvas = document.querySelector("#draw");
const ctx = canvas.getContext('2d');
// canvas resize
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
ctx.strokeStyle = '#BADA55';
ctx.lineJoin = 'round';
ctx.lineCap = 'round'
ctx.lineWidth = 0;
๊ทธ๋ฆด ์บ๋ฒ์ค(#draw)๋ฅผ canvas์ ์ ์ฅํ๊ณ ctx์ canvas์ ๊ทธ๋ฆฌ๊ธฐ ๋์์ด ๋๋ context๋ฅผ ์ป์.
width์ height์๋ฅผ ์ค์ ํ๊ณ ,
์ ์ ์(strokeStyle), ์ ์ ๋๋ชจ์(lineCap), ๋๊ฐ ์ ์ด ๋ง๋๋ ์ง์ ์ ๋ชจ์(lineJoin)๊ณผ ์ ์ ๋๊ผ(lineWidth)์ง์ .
let isDrawing = false;
let lastX = 0;
let lastY = 0;
let hue = 0;
let direction = true;
isDrawing์ ํ์ฌ ๊ทธ๋ฆฌ๊ณ ์๋์ง ์๋์ง check
lastX์ lastY๋ฅผ ์ด์ฉํด ์์์ง์ ์ขํ๋ฅผ ์ง์ .
hue๋ฅผ ํตํด ์ ์ ์์ ๋ณ๊ฒฝ.
direction์ผ๋ก ๊ตต๊ธฐ๋ฅผ ์ ํ๋ ๋ฐฉํฅ์ ์ค์ .
// ๋ง์ฐ์ค๋ฅผ ํด๋ฆญํ ๋ ์์์ง์ ์ฌ์ค์ .
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);
mouse eventํ์ธ.
function draw(e){
// stop the function from running when they are not moused down
// ๋ง์ฐ์ค๋ฅผ ๋๋ฅด์ง ์์ ๋ ๊ธฐ๋ฅ ์คํ ์ค์ง
if(!isDrawing) return;
console.log(e);
ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`;
// ctx.lineWidth = hue;
ctx.beginPath();
// start from
ctx.moveTo(lastX, lastY);
// goto
ctx.lineTo(e.offsetX, e.offsetY);
ctx.stroke();
// ๋ง์ง๋ง ์ง์ ์ ๋์ง์ ์ผ๋ก ์ง์ .
// ๋ค์์ ๊ทธ๋ฆด ๋ ์์์ง์ ์ด ์ด์ ์์
์ ๋์ง์ .
// lastX = e.offsetX;
// lastY = e.offsetY;
[lastX, lastY] = [e.offsetX, e.offsetY];
hue++;
// reset
if(hue >=360){
hue = 0;
}
// ctx.lineWidth++;
if(ctx.lineWidth >= 100 || ctx.lineWidth <= 1){
direction = !direction;
}
if(direction){
ctx.lineWidth++;
}else{
ctx.lineWidth--;
}
}
lastX์ lastY๋ฅผ ์ค์ ํด์ค์ ๋ฐ๋ผ ๋ณํ.
hue๊ฐ ๋ณํ.
๊ทธ ํ direction์ ๋ฐ๋ผ ๊ตต๊ธฐ์กฐ์ .
ctx.globalCompositeOperation = 'multiply';
์์ด ๊ฒน์น๊ฒ ๋ณด์ด๊ฒ ๋จ.
๋ ๋ง์ ์์ฑ์ด ๊ณต์ ๋ฌธ์์ ์์ผ๋ ์ฐธ๊ณ ํ์!