이번 글에서 다룰 내용
- 캔버스에 공 그리기
- 애니메이션 만들기
ctx.beginPath();
ctx.arc(x, y, radius, startAngle, endAngle, (, anticlockwise));
ctx.stroke();
ctx.fillStyle = 'red';
ctx.fill();
//app.js
import { Ball } from './ball.js';
class App {
constructor() {
this.canvas = document.createElement('canvas');
this.ctx = this.canvas.getContext('2d');
document.body.appendChild(this.canvas);
window.addEventListener('resize', this.resize.bind(this), false);
this.resize();
this.ball = new Ball(200, 200, 30, 15);
this.ball.draw(this.ctx, 'red');
}
}
new App();
//ball.js
export class Ball {
constructor(x, y, radius, speed) {
this.radius = radius;
this.vx = speed;
this.vy = speed;
this.x = x;
this.y = y;
}
draw(ctx, color) {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true);
ctx.fillStyle = color;
ctx.fill();
}
}
raf = window.requestAnimationFrame(callback)
window.cancelAnimationFrame(raf)
//app.js
class App {
constructor() {
...
this.animate();
}
animate() {
window.requestAnimationFrame(this.animate.bind(this));
this.ball.draw(this.ctx, 'red', this.canvas.width, this.canvas.height);
}
}
new App();
//ball.js
export class Ball {
...
draw(ctx, color, canvasWidth, canvasHeight) {
...
this.bounceWindow(canvasWidth, canvasHeight);
...
}
bounceWindow(canvasWidth, canvasHeight) {
if (this.x <= this.radius || this.x >= canvasWidth - this.radius) {
this.vx *= -1;
}
if (this.y <= this.radius || this.y >= canvasHeight - this.radius) {
this.vy *= -1;
}
}
}
//ball.js
bounceWindow(canvasWidth, canvasHeight) {
if (this.x <= this.radius || this.x >= canvasWidth - this.radius) {
this.vx *= -1;
this.x += this.vx;
}
if (this.y <= this.radius || this.y >= canvasHeight - this.radius) {
this.vy *= -1;
this.y += this.vy;
}
}
ctx.clearRect(x, y, width, height);
//app.js
class App() {
...
animate() {
window.requestAnimationFrame(this.animate.bind(this));
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
this.ball.draw(this.ctx, 'red', this.canvas.width, this.canvas.height);
}
}
//ball.js
constructor(this.stageWidth, this.stageHeight, radius, speed) {
...
this.x = this.radius + Math.random() * (stageWidth - this.radius * 2);
this.y = this.radius + Math.random() * (stageHeight - this.radius * 2);
}
//app.js
constructor() {
...
this.ball = new Ball(this.stageWidth, this.stageHeight, 30, 15);
}
//app.js
import {Ball} from './ball.js';
class App {
constructor() {
this.canvas = document.createElement('canvas');
this.ctx = this.canvas.getContext('2d');
document.body.appendChild(this.canvas);
window.addEventListener('resize', this.resize.bind(this), false);
this.resize();
this.ball = new Ball(this.stageWidgth, this.stageHeight, 30, 15);
this.animate();
}
resize() {
this.stageWidth = document.body.clientWidth;
this.stageHeight = document.body.clientHeight;
this.canvas.width = this.stageWidth * 2;
this.canvas.height = this.stageHeight * 2;
this.ctx.scale(2, 2);
}
animate() {
window.requestAnimationFrame(this.animate.bind(this));
this.ctx.clearRect(0, 0, this.stageWidth, this.stageHeight);
this.ball.draw(this.ctx, 'red', this.stageWidth, this.stageHeight);
}
}
new App();
//ball.js
export class Ball {
constructor(stageWidth, stageHeight, radius, speed) {
this.radius = radius;
this.vx = speed;
this.vy = speed;
this.x = this.radius + Math.random() * (stageWidth - this.radius * 2);
this.y = this.radius + Math.random() * (stageHeight - this.radius * 2);
}
draw(ctx, color, canvasWidth, canvasHeight) {
this.x += this.vx;
this.y += this.vy;
this.bounceWindow(canvasWidth, canvasHeight);
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true);
ctx.fillStyle = color;
ctx.fill();
}
bounceWindow(stageWidth, stageHeight) {
if (this.x <= this.radius || this.x >= stageWidth - this.radius) {
this.vx *= -1;
this.x += this.vx;
}
if (this.y <= this.radius || this.y >= stageHeight - this.radius) {
this.vy *= -1;
this.y += this.vy;
}
}
}
/*
stylesheet.css
*/
html {
width: 100%;
height: 100%;
}
body {
width: 100%;
height: 100%;
background-color: #161e38;
}
canvas {
width: 100%;
height: 100%;
}
재미있네요 👍