캔버스를 활용한 눈알 굴리기
<!DOCTYPE html>
<html lang="ko-KO">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Snow</title>
</head>
<body style="width: 100vw;height: 100vh;">
<canvas id="canvas" width="1550" height="1306"></canvas>
<script src="./index.js"></script>
</body>
</html>
- Canvas API는 JavaScript와 HTML canvas 엘리먼트를 통해 그래픽을 그리기위한 수단을 제공합니다. 무엇보다도 애니메이션, 게임 그래픽, 데이터 시각화, 사진 조작 및 실시간 비디오 처리를 위해 사용됩니다.
- Canvas API는 주로 2D 그래픽에 중점을 두고 있습니다. WebGL API 또한 canvas 엘리먼트를 사용하며, 하드웨어 가속 2D 및 3D 그래픽을 그립니다.
const canvas = document.getElementById('canvas');
if (canvas.getContext) {
const ctx = canvas.getContext('2d');
function eye(cx, cy, width, height, ball) {
const PI2 = Math.PI * 2;
const ratio = height / width;
const radius = Math.max(width, height) / 2;
const increment = 1 / radius;
ctx.beginPath();
const x = cx + radius * Math.cos(0);
const y = cy - ratio * radius * Math.sin(0);
ctx.lineTo(x, y);
for (let radians = increment; radians < PI2; radians += increment) {
const dx = cx + radius * Math.cos(radians);
const dy = cy - ratio * radius * Math.sin(radians);
ctx.lineTo(dx, dy);
}
ctx.closePath();
ball ? ctx.fill() : ctx.stroke();
}
function draw(x, y) {
eye(
document.body.clientWidth / 2 - 60,
document.body.clientHeight / 2,
100,
100
);
eye(
document.body.clientWidth / 2 + 60,
document.body.clientHeight / 2,
100,
100
);
eye(
x ? x : document.body.clientWidth / 2 - 60,
y ? y : document.body.clientHeight / 2,
10,
10,
true
);
eye(
x ? x + 120 : document.body.clientWidth / 2 + 60,
y ? y : document.body.clientHeight / 2,
10,
10,
true
);
}
function map(value, start1, stop1, start2, stop2) {
return start2 + (stop2 - start2) * ((value - start1) / (stop1 - start1));
}
function onMouseMove(event) {
canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height);
const x = map(
event.clientX,
0,
document.body.clientWidth,
document.body.clientWidth / 2 - 90,
document.body.clientWidth / 2 - 30
);
const y = map(
event.clientY,
0,
document.body.clientHeight,
document.body.clientHeight / 2 - 20,
document.body.clientHeight / 2 + 30
);
draw(x, y);
}
window.addEventListener('mousemove', onMouseMove);
draw();
}