-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
287 lines (256 loc) · 8.13 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/[email protected]/konva.min.js"></script>
<meta charset="utf-8" />
<title>KPMG Lucky Wheel</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
background-color: #ffffff;
}
p {font-family:Helvetica, sans-serif;font-size:20px;font-style:normal;font-weight:normal;color:#000000;background-color:#ffffff;text-align:center;}
</style>
</head>
<body>
<img src="https://assets.kpmg/is/image/kpmg/KPMG-logo:cq5dam.web.1200.630" alt="KPMG" style="width:200px;height:100px;">
<p>WELCOME TO THE KPMG LUCKY WHEEL. What will you win today?</p>
<div id="container"></div>
<script>
var width = window.innerWidth;
var height = window.innerHeight;
Konva.angleDeg = false;
var angularVelocity = 6;
var angularVelocities = [];
var lastRotation = 0;
var controlled = false;
var numWedges = 20;
var angularFriction = 0.2;
var target, activeWedge, stage, layer, wheel, pointer;
var finished = false;
function getAverageAngularVelocity() {
var total = 0;
var len = angularVelocities.length;
if (len === 0) {
return 0;
}
for (var n = 0; n < len; n++) {
total += angularVelocities[n];
}
return total / len;
}
function purifyColor(color) {
var randIndex = Math.round(Math.random() * 3);
color[randIndex] = 0;
return color;
}
function getRandomColor() {
var r = 100 + Math.round(Math.random() * 55);
var g = 100 + Math.round(Math.random() * 55);
var b = 100 + Math.round(Math.random() * 55);
return purifyColor([r, g, b]);
}
function getRandomReward() {
var mainDigit = Math.round(Math.random() * 4 + 1);
const prizes = ["", "🌊", "🍩", "🖊️", "☕", "🍬"]
return prizes[mainDigit];
}
function addWedge(n) {
var s = getRandomColor();
var reward = getRandomReward();
var r = s[0];
var g = s[1];
var b = s[2];
var angle = (2 * Math.PI) / numWedges;
var endColor = 'rgb(' + r + ',' + g + ',' + b + ')';
r += 100;
g += 100;
b += 100;
var startColor = 'rgb(' + r + ',' + g + ',' + b + ')';
var wedge = new Konva.Group({
rotation: (2 * n * Math.PI) / numWedges,
});
var wedgeBackground = new Konva.Wedge({
radius: 400,
angle: angle,
fillRadialGradientStartPoint: 0,
fillRadialGradientStartRadius: 0,
fillRadialGradientEndPoint: 0,
fillRadialGradientEndRadius: 400,
fillRadialGradientColorStops: [0, startColor, 1, endColor],
fill: '#64e9f8',
fillPriority: 'radial-gradient',
stroke: '#ccc',
strokeWidth: 2,
});
wedge.add(wedgeBackground);
var text = new Konva.Text({
text: reward,
fontFamily: 'Calibri',
fontSize: 50,
fill: 'white',
align: 'center',
stroke: 'yellow',
strokeWidth: 1,
rotation: (Math.PI + angle) / 2,
x: 380,
y: 30,
listening: false,
});
wedge.add(text);
text.cache();
wedge.startRotation = wedge.rotation();
wheel.add(wedge);
}
function animate(frame) {
// handle wheel spin
var angularVelocityChange =
(angularVelocity * frame.timeDiff * (1 - angularFriction)) / 1000;
angularVelocity -= angularVelocityChange;
// activate / deactivate wedges based on point intersection
var shape = stage.getIntersection({
x: stage.width() / 2,
y: 100,
});
if (controlled) {
if (angularVelocities.length > 10) {
angularVelocities.shift();
}
angularVelocities.push(
((wheel.rotation() - lastRotation) * 1000) / frame.timeDiff
);
} else {
var diff = (frame.timeDiff * angularVelocity) / 1000;
if (diff > 0.0001) {
wheel.rotate(diff);
} else if (!finished && !controlled) {
if (shape) {
var text = shape.getParent().findOne('Text').text();
if (text == "🌊") {
text = "a KPMG water bottle";
}
else if (text == "🍩") {
text = "doughnut";
}
else if (text == "🖊️") {
text = "a KPMG pen";
}
else if (text == "☕") {
text = "a KPMG coffee cup";
}
else if (text == "🍬") {
text = "some pick and mix";
}
var price = text.split('\n').join('');
alert('Wahoo! You have won ' + text + "!!!🥳🥳🥳");
}
finished = true;
}
}
lastRotation = wheel.rotation();
if (shape) {
if (shape && (!activeWedge || shape._id !== activeWedge._id)) {
pointer.y(20);
new Konva.Tween({
node: pointer,
duration: 0.3,
y: 30,
easing: Konva.Easings.ElasticEaseOut,
}).play();
if (activeWedge) {
activeWedge.fillPriority('radial-gradient');
}
shape.fillPriority('fill');
activeWedge = shape;
}
}
}
function init() {
stage = new Konva.Stage({
container: 'container',
width: width,
height: height,
});
layer = new Konva.Layer();
wheel = new Konva.Group({
x: stage.width() / 2,
y: 410,
});
for (var n = 0; n < numWedges; n++) {
addWedge(n);
}
pointer = new Konva.Wedge({
fillRadialGradientStartPoint: 0,
fillRadialGradientStartRadius: 0,
fillRadialGradientEndPoint: 0,
fillRadialGradientEndRadius: 30,
fillRadialGradientColorStops: [0, 'white', 1, 'red'],
stroke: 'white',
strokeWidth: 2,
lineJoin: 'round',
angle: 1,
radius: 30,
x: stage.width() / 2,
y: 33,
rotation: -90,
shadowColor: 'black',
shadowOffsetX: 3,
shadowOffsetY: 3,
shadowBlur: 2,
shadowOpacity: 0.5,
});
// add components to the stage
layer.add(wheel);
layer.add(pointer);
stage.add(layer);
// bind events
wheel.on('mousedown touchstart', function (evt) {
angularVelocity = 0;
controlled = true;
target = evt.target;
finished = false;
});
// add listeners to container
stage.addEventListener(
'mouseup touchend',
function () {
controlled = false;
angularVelocity = getAverageAngularVelocity() * 5;
if (angularVelocity > 20) {
angularVelocity = 20;
} else if (angularVelocity < -20) {
angularVelocity = -20;
}
angularVelocities = [];
},
false
);
stage.addEventListener(
'mousemove touchmove',
function (evt) {
var mousePos = stage.getPointerPosition();
if (controlled && mousePos && target) {
var x = mousePos.x - wheel.getX();
var y = mousePos.y - wheel.getY();
var atan = Math.atan(y / x);
var rotation = x >= 0 ? atan : atan + Math.PI;
var targetGroup = target.getParent();
wheel.rotation(
rotation - targetGroup.startRotation - target.angle() / 2
);
}
},
false
);
var anim = new Konva.Animation(animate, layer);
// wait one second and then spin the wheel
setTimeout(function () {
anim.start();
}, 1000);
}
init();
</script>
</body>
</html>