-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathSprite.js
42 lines (36 loc) · 787 Bytes
/
Sprite.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// This Class draws a rectange on a canvas
class Sprite {
constructor(x, y, width, height, color='red') {
this.x = x
this.y = y
this.width = width
this.height = height
this.color = color
this.dx = 0
this.dy = 0
this.rotate = 0
}
move() {
this.x += this.dx
this.y += this.dy
return this
}
moveTo(x, y) {
this.x = x
this.y = y
return this
}
render(ctx) {
ctx.save()
ctx.translate(this.x, this.y)
ctx.rotate(this.rotate * Math.PI / 180)
ctx.beginPath()
// ctx.rect(this.x, this.y, this.width, this.height)
ctx.fillStyle = this.color
ctx.fillRect(-this.width / 2, -this.height / 2, this.width, this.height)
// ctx.fill()
ctx.restore()
return this
}
}
export default Sprite