This repository has been archived by the owner on Feb 6, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 465
/
example.js
232 lines (190 loc) · 5.29 KB
/
example.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
/* global Deck */
var prefix = Deck.prefix
var transform = prefix('transform')
var translate = Deck.translate
var $container = document.getElementById('container')
var $topbar = document.getElementById('topbar')
var $sort = document.createElement('button')
var $shuffle = document.createElement('button')
var $bysuit = document.createElement('button')
var $fan = document.createElement('button')
var $poker = document.createElement('button')
var $flip = document.createElement('button')
$shuffle.textContent = 'Shuffle'
$sort.textContent = 'Sort'
$bysuit.textContent = 'By suit'
$fan.textContent = 'Fan'
$poker.textContent = 'Poker'
$flip.textContent = 'Flip'
$topbar.appendChild($flip)
$topbar.appendChild($shuffle)
$topbar.appendChild($bysuit)
$topbar.appendChild($fan)
$topbar.appendChild($poker)
$topbar.appendChild($sort)
var deck = Deck()
// easter eggs start
var acesClicked = []
var kingsClicked = []
deck.cards.forEach(function (card, i) {
card.enableDragging()
card.enableFlipping()
card.$el.addEventListener('mousedown', onTouch)
card.$el.addEventListener('touchstart', onTouch)
function onTouch () {
var card
if (i % 13 === 0) {
acesClicked[i] = true
if (acesClicked.filter(function (ace) {
return ace
}).length === 4) {
document.body.removeChild($topbar)
deck.$el.style.display = 'none'
setTimeout(function () {
startWinning()
}, 250)
}
} else if (i % 13 === 12) {
if (!kingsClicked) {
return
}
kingsClicked[i] = true
if (kingsClicked.filter(function (king) {
return king
}).length === 4) {
for (var j = 0; j < 3; j++) {
card = Deck.Card(52 + j)
card.mount(deck.$el)
card.$el.style[transform] = 'scale(0)'
card.setSide('front')
card.enableDragging()
card.enableFlipping()
deck.cards.push(card)
}
deck.sort(true)
kingsClicked = false
}
} else {
acesClicked = []
if (kingsClicked) {
kingsClicked = []
}
}
}
})
function startWinning () {
var $winningDeck = document.createElement('div')
$winningDeck.classList.add('deck')
$winningDeck.style[transform] = translate(Math.random() * window.innerWidth - window.innerWidth / 2 + 'px', Math.random() * window.innerHeight - window.innerHeight / 2 + 'px')
$container.appendChild($winningDeck)
var side = Math.floor(Math.random() * 2) ? 'front' : 'back'
for (var i = 0; i < 55; i++) {
addWinningCard($winningDeck, i, side)
}
setTimeout(startWinning, Math.round(Math.random() * 1000))
}
function addWinningCard ($deck, i, side) {
var card = Deck.Card(54 - i)
var delay = (55 - i) * 20
var animationFrames = Deck.animationFrames
var ease = Deck.ease
card.enableFlipping()
if (side === 'front') {
card.setSide('front')
} else {
card.setSide('back')
}
card.mount($deck)
card.$el.style.display = 'none'
var xStart = 0
var yStart = 0
var xDiff = -500
var yDiff = 500
animationFrames(delay, 1000)
.start(function () {
card.x = 0
card.y = 0
card.$el.style.display = ''
})
.progress(function (t) {
var tx = t
var ty = ease.cubicIn(t)
card.x = xStart + xDiff * tx
card.y = yStart + yDiff * ty
card.$el.style[transform] = translate(card.x + 'px', card.y + 'px')
})
.end(function () {
card.unmount()
})
}
// easter eggs end
$shuffle.addEventListener('click', function () {
deck.shuffle()
deck.shuffle()
})
$sort.addEventListener('click', function () {
deck.sort()
})
$bysuit.addEventListener('click', function () {
deck.sort(true) // sort reversed
deck.bysuit()
})
$fan.addEventListener('click', function () {
deck.fan()
})
$flip.addEventListener('click', function () {
deck.flip()
})
$poker.addEventListener('click', function () {
deck.queue(function (next) {
deck.cards.forEach(function (card, i) {
setTimeout(function () {
card.setSide('back')
}, i * 7.5)
})
next()
})
deck.shuffle()
deck.shuffle()
deck.poker()
})
deck.mount($container)
deck.intro()
deck.sort()
// secret message..
var randomDelay = 10000 + 30000 * Math.random()
setTimeout(function () {
printMessage('Psst..I want to share a secret with you...')
}, randomDelay)
setTimeout(function () {
printMessage('...try clicking all kings and nothing in between...')
}, randomDelay + 5000)
setTimeout(function () {
printMessage('...have fun ;)')
}, randomDelay + 10000)
function printMessage (text) {
var animationFrames = Deck.animationFrames
var ease = Deck.ease
var $message = document.createElement('p')
$message.classList.add('message')
$message.textContent = text
document.body.appendChild($message)
$message.style[transform] = translate(window.innerWidth + 'px', 0)
var diffX = window.innerWidth
animationFrames(1000, 700)
.progress(function (t) {
t = ease.cubicInOut(t)
$message.style[transform] = translate((diffX - diffX * t) + 'px', 0)
})
animationFrames(6000, 700)
.start(function () {
diffX = window.innerWidth
})
.progress(function (t) {
t = ease.cubicInOut(t)
$message.style[transform] = translate((-diffX * t) + 'px', 0)
})
.end(function () {
document.body.removeChild($message)
})
}