-
Notifications
You must be signed in to change notification settings - Fork 0
/
lucky.js
354 lines (287 loc) · 7.76 KB
/
lucky.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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
define(function(require, exports, module) {
var $ = require('./modules/jquery-1.8')
require('./modules/easing')
var CANVAS_HEIGHT = 500
var CANVAS_WIDTH = 900
var BALL_WIDTH = 60
var BALL_HEIGHT = 60
var LUCKY_BALL_WIDTH = 200
var LUCKY_BALL_HEIGHT = 200
var MAX_ZINDEX = 100
var DURATION_MIN = 100
var DURATION_MAX = 500
var ZOOM_DURATION = 500
var HIT_SPEED = 100
var RIGIDITY = 4
function User(name, options) {
this.name = name
this.options = options || {}
this.el = null
this.width = 0
this.height = 0
this.left = 0
this.top = 0
this.x = 0
this.y = 0
this.moving = false
this.lucky = false
this.zooming = false
this.createEl()
this.move()
}
User.prototype.createEl = function() {
this.el = $('<li>' + this.name + '</li>').appendTo('#balls')
this.width = this.el.width()
this.height = this.el.height()
}
User.prototype.move = function(callback) {
this.left = r(0, CANVAS_WIDTH - this.width)
this.top = r(0, CANVAS_HEIGHT - this.height)
this.zIndex = r(0, MAX_ZINDEX)
this.reflow(callback)
}
User.prototype.reflow = function(callback, direct) {
this.x = this.left + this.width / 2
this.y = this.top + this.height / 2
this.el[0].style.zIndex = this.zIndex
if (direct) {
this.el[0].style.left = this.left
this.el[0].style.top = this.top
}
else {
this.el.animate({
'left': this.left,
'top': this.top
}, r(DURATION_MIN, DURATION_MAX), 'easeOutBack', callback)
}
}
User.prototype.start = function() {
this.reset()
this.moving = true
this.autoMove()
}
User.prototype.reset = function() {
this.el.stop(true, true)
this.zooming = false
this.lucky = false
this.el[0].className = ''
this.el[0].style.width = BALL_WIDTH + 'px'
this.el[0].style.height = BALL_HEIGHT + 'px'
this.width = this.el.width()
this.height = this.el.height()
this._maxTop = CANVAS_HEIGHT - this.height
this._maxLeft = CANVAS_WIDTH - this.width
}
User.prototype.autoMove = function() {
var that = this
if (this.moving) {
this.move(function() {
that.autoMove()
})
}
}
User.prototype.stop = function() {
this.el.stop(true, true)
this.moving = false
}
User.prototype.bang = function() {
var that = this
this.lucky = true
this.el[0].className = 'selected'
this.width = LUCKY_BALL_WIDTH
this.height = LUCKY_BALL_HEIGHT
this.left = (CANVAS_WIDTH - this.width) / 2
this.top = (CANVAS_HEIGHT - this.height) / 2
this.zooming = true
this.el.animate({
'left': this.left,
'top': this.top,
'width': this.width,
'height': this.height
}, ZOOM_DURATION, function() {
that.zooming = false
})
}
User.prototype.beginHit = function() {
this._xMove = 0
this._yMove = 0
}
User.prototype.hitMove = function() {
this.left += this._xMove
this.top += this._yMove
this.top = this.top < 0 ? 0 : (this.top > this._maxTop ? this._maxTop : this.top)
this.left = this.left < 0 ? 0 : (this.left > this._maxLeft ? this._maxLeft : this.left)
this.reflow(null, false)
}
module.exports = {
users: [],
init: function(data) {
this.data = data
this.users = data.map(function(name) {
return new User(name)
})
this._bindUI()
},
clear: function() {
this.users.forEach(function(user) {
user = null
})
},
_bindUI: function() {
var that = this
// bind button
var trigger = document.querySelector('#go')
trigger.innerHTML = trigger.getAttribute('data-text-start')
trigger.addEventListener('click', go, false)
function go() {
if (trigger.getAttribute('data-action') === 'start') {
trigger.setAttribute('data-action', 'stop')
trigger.innerHTML = trigger.getAttribute('data-text-stop')
that.start()
}
else {
trigger.setAttribute('data-action', 'start')
trigger.innerHTML = trigger.getAttribute('data-text-start')
that.stop()
}
}
// bind #lucky-balls, remove item
$('#lucky-balls').on('click', 'li', function(e) {
var el = $(e.target)
el.remove()
})
// bind #balls
$('#balls').on('click', 'li', function(e) {
var el = $(e.target)
var name = el.text()
for (var i = 0; i < that.users.length; i++) {
var user = that.users[i]
if (user.name == name) {
that.moveLucky()
if (that.luckyUser !== user) {
that.setLucky(user)
}
break
}
}
})
// bind keydown
document.addEventListener('keydown', function(ev) {
if (ev.keyCode == '32') {
go()
}
else if (ev.keyCode == '27') {
that.moveLucky()
$('#lucky-balls li').eq(0).click()
}
}, false)
},
start: function() {
this.timer && clearTimeout(this.timer)
this.moveLucky()
this.users.forEach(function(user) {
user.start()
})
},
stop: function() {
var users = this.users
var z = 0, lucky = users[0]
users.forEach(function(user) {
user.stop()
if (z < user.zIndex) {
lucky = user
z = user.zIndex
}
})
lucky.bang()
this.hit()
this.luckyUser = lucky
},
removeItem: function(item) {
for (var i = 0; i < this.users.length; i++) {
var user = this.users[i]
if (user === item) {
this.users.splice(i, 1)
}
}
},
addItem: function(name, options) {
this.users.push(new User(name, options))
},
moveLucky: function() {
var luckyUser = this.luckyUser
if (luckyUser) {
luckyUser.el[0].style.cssText = ''
luckyUser.el.prependTo('#lucky-balls')
this.removeItem(luckyUser)
this.luckyUser = null
}
},
setLucky: function(item) {
this.users.forEach(function(user) {
user.stop()
})
this.luckyUser = item
item.bang()
this.hit()
},
hit: function() {
var that = this
var hitCount = 0
var users = this.users
users.forEach(function(user) {
user.beginHit()
})
for (var i = 0; i < users.length; i++) {
for (var j = i + 1; j < users.length; j++) {
if (isOverlap(users[i], users[j])) {
hit(users[i], users[j])
hitCount++
}
}
}
users.forEach(function(user) {
user.hitMove()
})
if (hitCount > 0) {
this.timer = setTimeout(function() {
that.hit()
}, HIT_SPEED)
}
}
}
// Helpers
function r(from, to) {
from = from || 0
to = to || 1
return Math.floor(Math.random() * (to - from + 1) + from)
}
function getOffset(a, b) {
return Math.sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y))
}
function isOverlap(a, b) {
return getOffset(a, b) <= (a.width + b.width) / 2
}
function hit(a, b) {
var yOffset = b.y - a.y
var xOffset = b.x - a.x
var offset = getOffset(a, b)
var power = Math.ceil(((a.width + b.width) / 2 - offset) / RIGIDITY)
var yStep = yOffset > 0 ? Math.ceil(power * yOffset / offset) : Math.floor(power * yOffset / offset)
var xStep = xOffset > 0 ? Math.ceil(power * xOffset / offset) : Math.floor(power * xOffset / offset)
if (a.lucky) {
b._xMove += xStep * 2
b._yMove += yStep * 2
}
else if (b.lucky) {
a._xMove += xStep * -2
a._yMove += yStep * -2
}
else {
a._yMove += -1 * yStep
b._yMove += yStep
a._xMove += -1 * xStep
b._xMove += xStep
}
}
})