-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
83 lines (71 loc) · 1.9 KB
/
index.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
module.exports = class Ripple {
constructor() {
this.x = 0;
this.y = 0;
this.z = 0;
}
findFurthestPoint(
clickPointX,
elementWidth,
offsetX,
clickPointY,
elementHeight,
offsetY,
) {
this.x = clickPointX - offsetX > elementWidth / 2 ? 0 : elementWidth;
this.y = clickPointY - offsetY > elementHeight / 2 ? 0 : elementHeight;
this.z = Math.hypot(
this.x - (clickPointX - offsetX),
this.y - (clickPointY - offsetY),
);
return this.z;
}
appyStyles(element, color, rect, radius, event) {
element.classList.add('ripple');
element.style.backgroundColor =
color === 'dark' ? 'rgba(0,0,0, 0.2)' : 'rgba(255,255,255, 0.3)';
element.style.borderRadius = '50%';
element.style.pointerEvents = 'none';
element.style.position = 'absolute';
element.style.left = event.clientX - rect.left - radius + 'px';
element.style.top = event.clientY - rect.top - radius + 'px';
element.style.width = element.style.height = radius * 2 + 'px';
}
applyAnimation(element) {
element.animate(
[
{
transform: 'scale(0)',
opacity: 1,
},
{
transform: 'scale(1.5)',
opacity: 0,
},
],
{
duration: 500,
easing: 'linear',
},
);
}
create(event, color) {
const element = event.currentTarget;
element.style.position = 'relative';
element.style.overflow = 'hidden';
const rect = element.getBoundingClientRect();
const radius = this.findFurthestPoint(
event.clientX,
element.offsetWidth,
rect.left,
event.clientY,
element.offsetHeight,
rect.top,
);
const circle = document.createElement('span');
this.appyStyles(circle, color, rect, radius, event);
this.applyAnimation(circle);
element.appendChild(circle);
setTimeout(() => circle.remove(), 500);
}
};