forked from bra1n/cryptolights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.coffee
235 lines (214 loc) · 7.34 KB
/
engine.coffee
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
class CanvasRenderer
constructor: (@c) ->
@ctx = @c.getContext '2d'
@resize @c
@minFps = 45
@particleMax = 150
@meteorMax = 100
@meteors = []
@blocks = []
@particles = []
@running = no
@lastDraw = Date.now()
@c.addEventListener 'click', @handleClick.bind(@)
@render()
start: -> @running = yes
stop: -> @running = no
resize: (@c) ->
@cw = @c.width = @c.scrollWidth
@ch = @c.height = @c.scrollHeight
@ctx.lineCap = 'round'
rand: (a,b) -> ~~((Math.random()*(b-a+1))+a)
addBlock: (thickness) ->
return if @blocks.length >= 5 or not @running
@blocks.push
y: -50
speed: 5
hue: 190
alpha: .5
thickness: Math.round(thickness)
updateBlocks: ->
i = @blocks.length
while i--
block = @blocks[i]
block.y = Math.round(block.y + block.speed)
block.alpha = Math.max(0, 1-(4*block.y/@ch-3))/2 if block.y / @ch > 0.75
@blocks.splice(i, 1) if block.y > @ch
renderBlocks: ->
i = @blocks.length
while i--
block = @blocks[i]
@ctx.beginPath()
@ctx.moveTo(20, block.y)
@ctx.lineTo(@cw-20, block.y)
@ctx.lineWidth = block.thickness
@ctx.strokeStyle = 'hsla('+block.hue+', 80%, 50%, '+block.alpha+')'
@ctx.stroke()
@ctx.closePath()
@ctx.restore()
createBlockParticles: ->
i = @blocks.length
while i--
block = @blocks[i]
@particles.push
x: Math.round(@rand(20, @cw-20))
y: block.y - 10
vx: (@rand(0, 100)-50)/100
vy: (@rand(-50, 50)-50)/100
radius: Math.round(@rand(2, 6)/2)
alpha: @rand(50, 75)/100
hue: block.hue
light: 50
handleClick: ({pageX, pageY}) ->
cx = pageX - @c.getBoundingClientRect().left
cy = pageY - @c.getBoundingClientRect().top
for {x, y, thickness, link} in @meteors.reverse()
t = thickness * 2
if cx >= x-t and cx <= x+t and cy >= y-t and cy <= y+t
window.open link
break
addMeteor: ({speed, hue, thickness, length, link, donation}) ->
return if not @running
return if (@meteors.length >= @meteorMax or @currentFps < @minFps) and !donation
@meteors.push
x: Math.round(@rand(thickness, @cw-thickness))
y: -50
vy: if donation then speed*0.8 else speed
hue: Math.round(hue)
thickness: if donation then Math.max(20, thickness*2) else thickness
length: Math.max(length, 10)
alpha: 1
timestamp: new Date().getTime()
link: link
donation: donation
@meteors.sort (a,b) -> a.thickness - b.thickness
updateMeteor: (meteor) ->
meteor.x = Math.round(@rand(meteor.thickness, @cw-meteor.thickness)) if meteor.x < meteor.thickness
meteor.y = Math.round(meteor.y + meteor.vy)
meteor.hue = meteor.hue + 3 % 360 if meteor.donation
if meteor.y / @ch > 0.75
meteor.alpha = Math.max(0, 1-(4*meteor.y/@ch-3))
meteor.thickness -= 0.05 if meteor.thickness > 5
renderMeteor: (meteor) ->
@ctx.save()
@ctx.globalAlpha = meteor.alpha
@ctx.translate(meteor.x, meteor.y)
@ctx.beginPath()
@ctx.moveTo(0, 0)
@ctx.lineTo(0, -meteor.length)
@ctx.lineWidth = meteor.thickness
gradient1 = @ctx.createLinearGradient(0, 0, 0, -meteor.length)
gradient1.addColorStop(0, 'hsla('+meteor.hue+', 60%, 50%, .25)')
gradient1.addColorStop(1, 'hsla('+meteor.hue+', 60%, 50%, 0)')
@ctx.strokeStyle = gradient1
@ctx.stroke()
@ctx.closePath()
@ctx.restore()
renderMeteorBorder: (meteor) ->
@ctx.save()
@ctx.globalAlpha = meteor.alpha
@ctx.translate(meteor.x, meteor.y)
@ctx.beginPath()
@ctx.moveTo(0, 0)
@ctx.lineTo(0, -meteor.length)
@ctx.lineWidth = Math.round(meteor.thickness / 5)
gradient2 = @ctx.createLinearGradient(0, meteor.thickness, 0, -meteor.length)
gradient2.addColorStop(0, 'hsla('+meteor.hue+', 100%, 50%, 0)')
gradient2.addColorStop(.1, 'hsla('+meteor.hue+', 100%, 100%, .75)')
gradient2.addColorStop(1, 'hsla('+meteor.hue+', 100%, 50%, 0)')
@ctx.strokeStyle = gradient2
@ctx.stroke()
@ctx.closePath()
@ctx.restore()
renderMeteorFlare: (meteor) ->
@ctx.save()
@ctx.globalAlpha = meteor.alpha
@ctx.translate(meteor.x, meteor.y + meteor.thickness*.5)
@ctx.beginPath()
@ctx.arc(0, 0, meteor.thickness, 0, Math.PI *2, false)
gradient3 = @ctx.createRadialGradient(0, 0, 0, 0, 0, meteor.thickness)
gradient3.addColorStop(0, 'hsla('+(meteor.hue + 30) % 360+', 100%, 50%, .2)')
gradient3.addColorStop(1, 'hsla('+(meteor.hue + 30) % 360+', 50%, 50%, 0)')
@ctx.fillStyle = gradient3
@ctx.fill()
@ctx.closePath()
@ctx.restore()
renderMeteorFlare2: (meteor) ->
@ctx.save()
@ctx.globalAlpha = meteor.alpha
@ctx.translate(meteor.x, meteor.y)
@ctx.scale(1,1.5)
@ctx.beginPath()
@ctx.arc(0, 0, meteor.thickness / 2, 0, Math.PI *2, false)
gradient4 = @ctx.createRadialGradient(0, 0, 0, 0, 0, meteor.thickness / 2)
gradient4.addColorStop(0, 'hsla('+(meteor.hue + 60) % 360+', 80%, 70%, .1)')
gradient4.addColorStop(1, 'hsla('+(meteor.hue + 60) % 360+', 80%, 50%, 0)')
@ctx.fillStyle = gradient4
@ctx.fill()
@ctx.closePath()
@ctx.restore()
createParticles: (meteor) ->
if meteor.donation or (@particles.length < @particleMax - @meteors.length and meteor.thickness > 5)
@particles.push
x: meteor.x + (@rand(0, meteor.thickness) - meteor.thickness/2)
y: meteor.y + (@rand(0, meteor.thickness) - meteor.thickness/2)
vx: (@rand(0, 100)-50)/100
vy: (@rand(-25, 75)-50)/100
radius: if meteor.donation then meteor.thickness*0.2 else Math.round(@rand(1, 6)/2)
alpha: if meteor.donation then 0.5 else @rand(15, 30)/100
hue: meteor.hue
light: if meteor.donation then 0 else 50
updateParticles: () ->
i = @particles.length
while i--
p = @particles[i]
p.vx += (@rand(0, 100)-50)/300
p.vy += (@rand(-25, 75)-50)/300
p.x += p.vx
p.y += p.vy
p.light += 2
p.alpha -= .01
@particles.splice(i, 1) if p.alpha < .02
renderParticles: () ->
i = @particles.length
while i--
p = @particles[i]
@ctx.beginPath()
@ctx.fillStyle = 'hsla('+p.hue+', 100%, '+p.light+'%, '+p.alpha+')'
@ctx.fillRect(Math.round(p.x), Math.round(p.y), p.radius, p.radius)
@ctx.closePath()
clear: () ->
@ctx.globalCompositeOperation = 'destination-out'
@ctx.fillStyle = 'rgba(0, 0, 0, 0.25)'
@ctx.fillRect(0, 0, @cw, @ch)
@ctx.globalCompositeOperation = 'lighter'
render: () ->
requestAnimationFrame @render.bind(@)
return unless @running
@currentFps = Math.round(1000/(Date.now() - @lastDraw))
@lastDraw = Date.now()
@clear()
# debug
#@ctx.font = '48px serif';
#@ctx.fillStyle = 'rgba(255, 255, 255, 1)'
#@ctx.fillText(@currentFps, 10, 50);
# blocks
@updateBlocks()
@renderBlocks()
@createBlockParticles()
# meteors
i = @meteors.length
while i--
meteor = @meteors[i]
if new Date().getTime() - meteor.timestamp > 15*1000 or meteor.y - meteor.length > @ch
@meteors.splice i, 1
continue
@updateMeteor(meteor)
@renderMeteor(meteor)
@renderMeteorBorder(meteor)
@renderMeteorFlare(meteor)
@renderMeteorFlare2(meteor)
@createParticles(meteor)
# meteor particles
@updateParticles()
@renderParticles()