-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
93 lines (77 loc) · 2.27 KB
/
app.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import Petal from './petal.js'
const IMG_WIDTH = 1944
const IMG_HEIGHT = 1296
const IMG_RATIO = IMG_HEIGHT / IMG_WIDTH
class App {
petals = []
constructor() {
this.canvas = document.createElement('canvas')
document.body.appendChild(this.canvas)
this.ctx = this.canvas.getContext('2d')
this.pixelRatio = window.devicePixelRatio > 1 ? 2 : 1
window.addEventListener('resize', this.resize.bind(this), false)
this.resize()
document.addEventListener('pointerdown', this.handleClick.bind(this), false)
window.requestAnimationFrame(this.animate.bind(this))
}
resize() {
this.stageWidth = document.body.clientWidth
this.stageHeight = document.body.clientHeight
if (this.stageHeight > IMG_HEIGHT * 2 && this.stageWidth > IMG_WIDTH) {
this.treeWidth = IMG_WIDTH
this.treeHeight = IMG_HEIGHT
} else if (this.stageWidth * IMG_RATIO < this.stageHeight / 2) {
this.treeWidth = this.stageWidth
this.treeHeight = this.treeWidth * IMG_RATIO
} else {
this.treeHeight = this.stageHeight / 2
this.treeWidth = this.treeHeight / IMG_RATIO
}
this.canvas.width = this.stageWidth * this.pixelRatio
this.canvas.height = this.stageHeight * this.pixelRatio
this.ctx.scale(this.pixelRatio, this.pixelRatio)
}
animate() {
window.requestAnimationFrame(this.animate.bind(this))
this.ctx.clearRect(0, 0, this.stageWidth, this.stageHeight)
this.petals = this.petals.filter(petal => petal.animate(this.ctx))
}
handleClick(e) {
if (this.stageWidth - this.treeWidth > e.clientX || this.treeHeight < e.clientY) {
return
}
this.petals.push(new Petal(
e.clientX,
e.clientY,
this.stageHeight,
this.treeWidth,
))
this.petals.push(new Petal(
e.clientX + 10,
e.clientY - 10,
this.stageHeight,
this.treeWidth,
))
this.petals.push(new Petal(
e.clientX - 10,
e.clientY - 10,
this.stageHeight,
this.treeWidth,
))
this.petals.push(new Petal(
e.clientX + 10,
e.clientY + 10,
this.stageHeight,
this.treeWidth,
))
this.petals.push(new Petal(
e.clientX - 10,
e.clientY + 10,
this.stageHeight,
this.treeWidth,
))
}
}
window.onload = () => {
new App()
}