index.html 파일에서 canvas를 생성해주고
index.js에 아래코드를 넣어준다.
//파티클 만들기
class Particle {
constructor(x, y, radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
draw() {
// 원형 그리기
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI / 180 * 360)
ctx.fillStyle = 'red'
ctx.fill()
ctx.closePath()
}
}
const x = 100
const y = 100
const radius = 50
const particle = new Particle(x, y, radius)
particle.draw()
function animate() {
window.requestAnimationFrame(animate)
ctx.clearRect(0, 0, width, height)
particle.draw();
}
animate()
1. Particle 클래스 선언:
2. 파티클 그리기:
3. 파티클 생성:
4. 애니메이션 실행:
이렇게 하면 코드는 파티클을 만들고 캔버스에 애니메이션을 생성하는 데 사용됩니다.
아직 파티클이 되진 못하고 원형이 아래로 내려오는 애니메이션만 만들어집니다.