forked from itorr/sakana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument.js
447 lines (344 loc) · 9.59 KB
/
document.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
const htmlEl = document.documentElement;
let device = String(navigator.userAgent.match(/steam|macos/i)).toLowerCase();
if(
/iPhone|iPad|iPod/i.test(navigator.userAgent)
||
(navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1)
) device = 'ios';
htmlEl.setAttribute('data-device',device)
const sticky = 0.1;
const maxR = 60;
const maxY = 110;
const minY = -maxY;
const el = document.querySelector('.main');
const boxEl = document.querySelector('.single-box');
const inertia = 0.1;
const params = new Proxy(new URLSearchParams(window.location.search), {
get: (searchParams, prop) => searchParams.get(prop),
});
if(params.alpha){
htmlEl.setAttribute('data-alpha',params.alpha);
}
// 自定义背景色
if(params.background){
htmlEl.setAttribute('data-alpha',params.alpha);
htmlEl.style.background = params.background;
}
const Values = {
chisato: {
r: 1, // 角度
y: 40, // 高度
t: 0, // 垂直速度
w: 0, // 横向速度
d: 0.99 // 衰减
},
takina: {
r: 12, // 角度
y: 2, // 高度
t: 0, // 垂直速度
w: 0, // 横向速度
d: 0.988 // 衰减
}
};
const voiceButton = document.querySelector('.set-voice');
const Voices = {
chisato: new Audio('chinanago.mp3'),
takina: new Audio('sakana.mp3'),
isMute: false
};
Voices.takina.volume = Voices.chisato.volume = 0.8;
Voices.takina.muted = Voices.chisato.muted = true;
const toggleVoiceMute = () => {
Voices.isMute = voiceButton.getAttribute('data-active') !== 'true';
voiceButton.setAttribute(
'data-active',
Voices.isMute
);
Voices.takina.muted = Voices.chisato.muted = Voices.isMute;
};
let runing = true;
const deepCopy = v=> JSON.parse(JSON.stringify(v));
el.classList.add(params.v);
let v = deepCopy(Values[params.v] || Values['takina']);
let width;
let height;
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
const resize = _=>{
const { offsetWidth, offsetHeight } = htmlEl;
width = Math.min(offsetWidth,800);
height = 800;
canvas.width = width;
canvas.height = height;
const scalc = offsetWidth / offsetHeight;
const isSuperVertical = scalc < 0.5757;
htmlEl.setAttribute('data-is-super-vertical',isSuperVertical);
};
resize();
const rotatePoint = (cx, cy, x, y, angle)=> {
const radians = (Math.PI / 180) * angle;
const cos = Math.cos(radians);
const sin = Math.sin(radians);
const nx = (cos * (x - cx)) + (sin * (y - cy)) + cx;
const ny = (cos * (y - cy)) - (sin * (x - cx)) + cy;
return {
x: nx,
y: ny
};
}
const draw = _=>{
let { r,y,t,w,d } = v;
const x = r * 1;
const _y = y;// - Math.abs(x);
el.style.transform = `rotate(${r}deg) translateX(${x}px) translateY(${y}px)`;
ctx.clearRect(0,0,width,height);
ctx.save();
ctx.strokeStyle = '#182562';
ctx.lineWidth = 10;
ctx.beginPath();
ctx.translate(
width / 2 ,
640 // height - 160
);
ctx.moveTo(
0,
140
);
const cx = 0;
const cy = -100;
const n = rotatePoint(
cx,
cy,
x,
-y,
r
);
const nx = n.x;
const ny = -n.y - 100;
ctx.quadraticCurveTo(
0,
75,
nx,
ny
);
ctx.stroke();
ctx.restore();
// 这后面是 canvas 绘制角色部分逻辑,没有做 retina 兼容,目前用的 DOM 节点、暂时屏蔽掉
return;
// ctx.clearRect(0,0,width,height);
ctx.save();
ctx.translate(
width/2 ,
height - 160
);
ctx.rotate(r/180*Math.PI);
ctx.translate(
x,
_y
);
ctx.drawImage(
sakanaImageEl,
0,0,
sakanaImageEl.naturalWidth,
sakanaImageEl.naturalHeight,
-150,
-400,
sakanaImageEl.naturalWidth/2,
sakanaImageEl.naturalHeight/2
);
ctx.restore();
};
const loadImage = (src,onOver)=>{
const el = new Image();
el.onload = _=> onOver(el);
el.src = src;
};
let sakanaImageEl;
const init = onOver=>{
// loadImage('sakana.png',el=>{
// sakanaImageEl = el;
onOver();
// })
}
let or = 0;
const run = _=>{
if(!runing) return;
requestAnimationFrame(run);
let { r,y,t,w,d } = v;
w = w - r * 2 - or;
r = r + w * inertia * 1.2;
v.w = w * d;
v.r = r;
t = t - y * 2;
y = y + t * inertia * 2;
v.t = t * d;
v.y = y;
draw();
};
init(_=>{
requestAnimationFrame(run);
});
const move = (x,y)=>{
let r = x * sticky;
r = Math.max(-maxR,r);
r = Math.min(maxR,r);
y = y * sticky * 2;
y = Math.max(minY,y);
y = Math.min(maxY,y);
v.r = r;
v.y = y;
v.w = 0;
v.t = 0;
draw();
}
el.onmousedown = e=>{
e.preventDefault();
runing = false;
const { pageX, pageY } = e;
const _downPageX = pageX;
const _downPageY = pageY;
// 确保通过用户触发事件获得 audio 播放授权
Voices.takina.muted = Voices.chisato.muted = Voices.isMute;
document.onmouseup = e=>{
e.preventDefault();
document.onmousemove = null;
document.onmouseup = null;
runing = true;
playVoice();
run();
};
document.onmousemove = e=>{
const rect = boxEl.getBoundingClientRect();
const leftCenter = rect.left + rect.width / 2;
const topCenter = rect.top;
const { pageX, pageY } = e;
let x = pageX - leftCenter;
let y = pageY - _downPageY;
move(x,y);
};
};
el.ontouchstart = e=>{
e.preventDefault();
runing = false;
if(!e.touches[0]) return;
const { pageX, pageY } = e.touches[0];
const _downPageX = pageX;
const _downPageY = pageY;
// 确保通过用户触发事件获得 audio 播放授权
Voices.takina.muted = Voices.chisato.muted = Voices.isMute;
document.ontouchend = e=>{
document.ontouchmove = null;
document.ontouchend = null;
runing = true;
playVoice();
run();
};
document.ontouchmove = e=>{
if(!e.touches[0]) return;
const rect = boxEl.getBoundingClientRect();
// console.log(rect);
const leftCenter = rect.left + rect.width / 2;
const topCenter = rect.top;
const { pageX, pageY } = e.touches[0];
let x = pageX - leftCenter;
let y = pageY - _downPageY;
move(x,y);
};
};
const playVoice = () => {
if (Voices.isMute) return;
// console.log({ r: v.r, y: v.y })
if (el.classList.contains('chisato')) {
if (
// 'nice chin~a~na~go~' 经验值
Math.abs(v.r) <= 4
&& Math.abs(v.y) >= 20
) {
console.log('nice chin~a~na~go~');
Voices.chisato.play();
};
} else {
if (
// 'nice sakana~' 经验值
v.r >= Values.takina.r
&& (Math.abs(v.y) <= 12 || v.r >= 3 * Math.abs(v.y))
) {
console.log('nice sakana~');
Voices.takina.play();
};
};
};
const canOrientation = !!(
window.DeviceOrientationEvent
&&
typeof window.DeviceOrientationEvent['requestPermission'] === 'function'
);
htmlEl.setAttribute('data-can-orientation',canOrientation);
const getOrientationPermission = onOver=>{
if (!canOrientation) return onOver();
window.DeviceOrientationEvent['requestPermission']().then(permissionState => {
// console.log({permissionState})
if(permissionState !== 'granted') return //alert('获取权限失败');
htmlEl.setAttribute('data-permission-state',true);
onOver();
});
};
const onDeviceOrientation = (e)=> {
const { alpha, beta, gamma, acceleration } = e;
// console.log( { alpha, beta, gamma });
or = -gamma / 2;
// or = or * (alpha > 180?-1:1);
or = Math.min(maxR,or);
or = Math.max(-maxR,or);
};
const setOrientationListener = _=>{
getOrientationPermission(_=>{
if(window.DeviceOrientationEvent){
window.addEventListener('deviceorientation', onDeviceOrientation );
};
});
};
let magicForceTimerHandle = undefined;
let magicForceFlag = false;
const magicForce = _=>{
// 0.1 probability to Switch Character
if(Math.random() < 0.1){
switchValue();
}else{
// Add random velocities in the vertical and horizontal directions
v.t = v.t + (Math.random()-0.5)*150;
v.w = v.w + (Math.random()-0.5)*200;
}
// Set a variable delay between applying magic powers
magicForceTimerHandle = setTimeout(
magicForce,
Math.random()*3000+2000
);
};
const triggerMagicLinkEl = document.querySelector('.trigger-magic-link');
const triggerMagic = _=>{
// Flip the status flag
magicForceFlag = !magicForceFlag;
htmlEl.setAttribute('data-magic-force',magicForceFlag);
triggerMagicLinkEl.setAttribute('data-active',magicForceFlag);
clearTimeout(magicForceTimerHandle);
// Clear the timer or start a timer based on the new flag
if (magicForceFlag)
magicForceTimerHandle = setTimeout(magicForce, Math.random()*1000+500);
};
// setOrientationListener();
const switchValue = _=>{
el.classList.toggle('chisato');
if(el.classList.contains('chisato')){
v = deepCopy(Values['chisato']);
history.replaceState({},'','?v=chisato');
}else{
v = deepCopy(Values['takina']);
history.replaceState({},'','?v=takina');
}
}
document.querySelector('.bed').addEventListener('click',e=>{
e.preventDefault();
switchValue();
})
window.addEventListener('resize',resize);