-
Notifications
You must be signed in to change notification settings - Fork 1
/
padlock.js
86 lines (72 loc) · 2.02 KB
/
padlock.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
window.addEventListener('load', function () {
AnimateLock()
function AnimateLock() {
const Lock = document.getElementById('Padlock')
Lock.removeEventListener('click', AnimateLock)
const LockShape = Lock.getElementById('PadlockShape')
let delay = 0
let lastRotation = 0
let duration = 400
let rotation = 11
rotateLock(lastRotation, rotation, duration, delay)
lastRotation = rotation
rotation = -12
delay += duration
duration = 300
rotateLock(lastRotation, rotation, duration, delay)
lastRotation = rotation
rotation = 8
delay += duration
duration = 300
rotateLock(lastRotation, rotation, duration, delay)
lastRotation = rotation
rotation = -6
delay += duration
duration = 300
rotateLock(lastRotation, rotation, duration, delay)
lastRotation = rotation
rotation = 4
delay += duration
duration = 300
rotateLock(lastRotation, rotation, duration, delay)
lastRotation = rotation
rotation = -2
delay += duration
duration = 200
rotateLock(lastRotation, rotation, duration, delay)
lastRotation = rotation
rotation = 0
delay += duration
duration = 200
rotateLock(lastRotation, rotation, duration, delay)
setTimeout(function throttle() {
Lock.addEventListener('click', AnimateLock)
}, duration + delay)
function rotateLock(startRotation, endRotation, durationMs, delayMs) {
setTimeout(() => animateRotation({
element: LockShape,
centerX: 75,
centerY: 27,
startRotation,
endRotation,
durationMs
}), delayMs)
}
}
function animateRotation(o) {
animate()
function animate() {
const start = Date.now()
requestAnimationFrame(function anim() {
const time = Math.min(1, (Date.now() - start) / o.durationMs)
const angle = easeOutQuad(time) * (o.endRotation - o.startRotation) + o.startRotation
o.element.setAttribute('transform', `rotate( ${angle}, ${o.centerX}, ${o.centerY})`)
if (time < 1)
requestAnimationFrame(anim)
})
}
}
function easeOutQuad(x) { // https://easings.net/#easeOutQuad
return x * (2 - x)
}
})