-
Notifications
You must be signed in to change notification settings - Fork 9
/
slot.coffee
340 lines (286 loc) · 8.4 KB
/
slot.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
WHEEL_HEIGHT = 100
PADDING_H = 20
PADDING_V = 10
FONT = "Ubuntu"
FONT_HEIGHT = WHEEL_HEIGHT-PADDING_V*2
NUMBER_WIDTH = 80
VELOCITY_MAX = 7.5
ACCELERATION_BASE = 0.01
ACCELERATION_RANDOM = 0.01
STOP_DELAY= 200
DUMMY_IMAGES = 2
#
# offset should be relative to wheel height
#
WINDOW_HEIGHT = 768
WINDOW_WIDTH = 1024
mod = (a, b) ->
r = a % b
r += b if r < 0
return r
class Wheel
constructor: (ctx) ->
@ctx = ctx
@position = 0
@targetPosition = 0
@velocity = 0
@acceleration = Math.random() * ACCELERATION_RANDOM + ACCELERATION_BASE
@rotating = false
@stopped = false
@width = PADDING_H
# tagNames
drawInner: ->
number = Math.round @position
offset = (@position - number) * WHEEL_HEIGHT
@ctx.save()
@ctx.translate 0, offset
@drawTag @tagImageWhite[mod(number, @tagImageWhite.length)]
@ctx.save()
@ctx.translate 0, -WHEEL_HEIGHT
@drawTag @tagImageWhite[mod(number+1, @tagImageWhite.length)]
@ctx.restore()
@ctx.save()
@ctx.translate 0, WHEEL_HEIGHT
@drawTag @tagImageWhite[mod(number-1, @tagImageWhite.length)]
@ctx.restore()
@ctx.restore()
drawOuter: ->
number = Math.round @position
offset = (@position - number) * WHEEL_HEIGHT
displayTagsCount = Math.ceil(((WINDOW_HEIGHT/WHEEL_HEIGHT)-1)/2)+1
@ctx.save()
@ctx.translate 0, offset
@drawTag @tagImage[mod(number, @tagImageWhite.length)]
@ctx.save()
for i in [1..displayTagsCount]
@ctx.translate 0, -WHEEL_HEIGHT
@drawTag @tagImage[mod(number+i, @tagImageWhite.length)]
@ctx.restore()
@ctx.save()
for i in [1..displayTagsCount]
@ctx.translate 0, WHEEL_HEIGHT
@drawTag @tagImage[mod(number-i, @tagImageWhite.length)]
@ctx.restore()
@ctx.restore()
drawTag: (image) ->
@ctx.save()
@ctx.drawImage image, 0, -WHEEL_HEIGHT/2
@ctx.restore()
tick: ->
if @rotating
@position += @velocity
@velocity += @acceleration if @velocity <= VELOCITY_MAX
else
@position += (@targetPosition - @position) * 1.8 # elastically stopped
@velocity = 0
if @stopped and @rotating and Math.round(@position) >= @targetPosition
@rotating = false
if @parent
@parent.childRotateStopped()
start: ->
@stopped = false
@rotating = true
setTarget: (@targetPosition) ->
setParent: (@parent) ->
stop: (@rotating = true) ->
@targetPosition = (@targetPosition % @tagImage.length) + Math.ceil(@position / @tagImage.length) * @tagImage.length
@stopped = true
makeCache: ->
@tagImage = []
@tagImageWhite = []
for key, tagName of @tagNames
c = document.createElement('canvas');
c.width = @width
c.height = WHEEL_HEIGHT
cctx = c.getContext '2d'
cctx.save()
cctx.font = "#{FONT_HEIGHT}px #{FONT}"
cctx.textBaseline = "middle"
cctx.textAlign = "left"
cctx.fillStyle = "#000"
cctx.fillText tagName, 0, WHEEL_HEIGHT/2
cctx.restore()
@tagImage[key] = c
c = document.createElement('canvas');
c.width = @width
c.height = WHEEL_HEIGHT
cctx = c.getContext '2d'
cctx.save()
cctx.font = "#{FONT_HEIGHT}px #{FONT}"
cctx.textBaseline = "middle"
cctx.textAlign = "left"
cctx.fillStyle = "#fff"
cctx.fillText tagName, 0, WHEEL_HEIGHT/2
cctx.restore()
@tagImageWhite[key] = c
class NumberWheel extends Wheel
constructor: (ctx) ->
super ctx
@tagNames = [0..9]
@ctx.font = "#{FONT_HEIGHT}px sans-serif"
@width = @ctx.measureText("0").width
@makeCache()
class AvatarWheel extends Wheel
constructor: (ctx, @tagImage) ->
super ctx
@width += FONT_HEIGHT
@tagImageWhite = @tagImage
drawTag: (image) ->
@ctx.save()
@ctx.drawImage image, 0, -FONT_HEIGHT/2, FONT_HEIGHT, FONT_HEIGHT
@ctx.restore()
class TextWheel extends Wheel
constructor: (ctx, @tagNames, width) ->
super ctx
@ctx.font = "#{FONT_HEIGHT}px sans-serif"
@width += width
@makeCache()
class WheelGroup
constructor: (ctx) ->
@ctx = ctx
@width = 0
@wheels = []
@stopped = true
@rotating = false
add: (wheel) ->
@width += wheel.width
wheel.setParent this
@wheels.push wheel
start: ->
wheel.start() for wheel in @wheels
@stopped = false
@rotating = true
@stopCount = 0
tick: ->
wheel.tick() for wheel in @wheels
drawInner: ->
@ctx.save()
for wheel in @wheels
wheel.drawInner()
@ctx.translate wheel.width, 0
@ctx.restore()
drawOuter: ->
@ctx.save()
for wheel in @wheels
wheel.drawOuter()
@ctx.translate wheel.width, 0
@ctx.restore()
stop: (@rotating = true)->
if @rotating
@wheels[0].stop()
else
wheel.stop(false) for wheel in @wheels
@stopped = true
setParent: (@parent) ->
childRotateStopped: ->
@stopCount += 1
if @stopCount >= @wheels.length
@rotating = false
if @parent
@parent.childRotateStopped()
else
stopWheel = @wheels[@stopCount]
setTimeout ->
stopWheel.stop()
, STOP_DELAY
class NumberWheelGroup extends WheelGroup
constructor: (ctx, digits) ->
super ctx
for i in [1..digits]
newWheel = new NumberWheel @ctx
@add newWheel
@width += PADDING_H
setTarget: (number) ->
for i in [@wheels.length-1..0]
wheel = @wheels[i]
wheel.setTarget number%10
number = Math.floor(number / 10)
digits = 0
count = 0
imageLoadCount = 0
lists = []
window.lists = lists
window.loadSlot = (data) ->
# begin parsing things
count = data.length
lists.avatar = []
lists.id = []
lists.name = []
lists.email = []
for entry, datum of data
digits = datum.id.length if datum.id.length > digits
for key, value of datum
lists[key][entry] = value
avatar = new Image
avatar.onload = imageLoaded
avatar.onerror = imageError
avatar.src = "http://www.gravatar.com/avatar/#{datum.email}?s=#{FONT_HEIGHT}&d=404"
lists.avatar[entry] = avatar
imageErrorCount = 0
imageError = ->
@src = "./dummy/#{imageErrorCount % DUMMY_IMAGES}.png"
imageErrorCount += 1
imageLoaded = ->
imageLoadCount += 1
console.log "avatar loading: #{imageLoadCount}/#{count}"
if imageLoadCount == count
allImagesLoaded()
allImagesLoaded = ->
canvas = document.getElementById("c")
requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
if not requestAnimationFrame
requestAnimationFrame = (fn) ->
setTimeout(fn, 33)
window.onresize = ->
WINDOW_WIDTH = canvas.width = window.innerWidth
WINDOW_HEIGHT = canvas.height = window.innerHeight
WINDOW_WIDTH = canvas.width = window.innerWidth
WINDOW_HEIGHT = canvas.height = window.innerHeight
if canvas.getContext
ctx = canvas.getContext "2d"
wheelGroup = new WheelGroup ctx
numberWheelGroup = new NumberWheelGroup ctx, digits
avatarWheel = new AvatarWheel ctx, lists.avatar
textWheel = new TextWheel ctx, lists.name, 1000
wheelGroup.add numberWheelGroup
wheelGroup.add avatarWheel
wheelGroup.add textWheel
picks = [0..count-1]
pick = ->
if picks.length > 0
choice = Math.floor(Math.random()*picks.length)
picked = picks[choice]
picks.splice(choice, 1)
numberWheelGroup.setTarget parseInt(lists.id[picked])
avatarWheel.setTarget picked
textWheel.setTarget picked
else
window.alert "All people are selected out"
pickAndStop = ->
pick()
wheelGroup.stop()
redraw = ->
wheelGroup.tick()
ctx.clearRect 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT
ctx.save()
ctx.translate 0, WINDOW_HEIGHT/2
ctx.fillStyle = "rgb(127, 63, 63)"
ctx.fillRect -5, -WHEEL_HEIGHT/2, WINDOW_WIDTH+10, WHEEL_HEIGHT
ctx.translate PADDING_H, 0
ctx.globalCompositeOperation = "source-atop"
wheelGroup.drawInner()
ctx.globalCompositeOperation = "destination-over"
wheelGroup.drawOuter()
ctx.globalCompositeOperation = "source-over"
ctx.restore()
requestAnimationFrame redraw
delay = 0
canvas.onclick = ->
if wheelGroup.stopped and not wheelGroup.rotating
wheelGroup.start()
delay = setTimeout pickAndStop, 3000
else if not wheelGroup.stopped and wheelGroup.rotating
clearTimeout delay
pick()
wheelGroup.stop(false)
requestAnimationFrame redraw