-
Notifications
You must be signed in to change notification settings - Fork 0
/
rippleButton.js
104 lines (79 loc) · 2.69 KB
/
rippleButton.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
function embed(rootDomNode, label)
{
'use strict';
var rAF = typeof requestAnimationFrame !== 'undefined'
? requestAnimationFrame
: function(cb) { setTimeout(cb, 1000 / 60); };
var geometryDecoder = function (g) {
var rect = g.currentTarget.getBoundingClientRect();
var set = function (x, y) {
return {rect: rect, x: x - rect.left, y: y - rect.top};
};
if ((g.clientX == null) || (g.clientY == null)) {
if(g.touches == null) {
return null
} else {
var touch = g.touches[0]
return set(touch.touchX, touch.touchY);
}
} else {
if ((g.clientX === 0.0) && (g.clientY === 0.0)) {
return {rect: rect, x: rect.width / 2.0, y: rect.height / 2.0};
} else {
return set(g.clientX, g.clientY);
}
}
};
var isVisible = false,
metrics = null;
var button = document.createElement('button');
button.className = "mdl-js-ripple-effect mdl-js-button mdl-button mdl-button--raised"
var ripple = function () {
button.blur();
isVisible = false
rAF(update);
}
button.addEventListener('mouseup', ripple);
button.addEventListener('mouseleave', ripple);
button.addEventListener('ontouchend', ripple);
var buttonHandler = function eventHandler(event)
{
isVisible = true
metrics = geometryDecoder(event)
rAF(update);
};
button.addEventListener('mousedown', buttonHandler);
button.addEventListener('touchstart', buttonHandler);
button.appendChild(document.createTextNode(label));
var span = document.createElement('span');
button.appendChild(span);
rootDomNode.appendChild(button);
var toPx = function (k) {
return Math.round(k) + 'px';
};
function update()
{
if (metrics != null) {
var r = metrics.rect;
var offset = 'translate(' + toPx(metrics.x) + ', ' + toPx(metrics.y) + ')';
var rippleSize = toPx(
(Math.sqrt((r.width * r.width) + (r.height * r.height)) * 2.0) + 2.0);
var scale = isVisible ? 'scale(0.0001, 0.0001)' : '';
var transformString = 'translate(-50%, -50%) ' + offset + scale;
var style = span.style
style.width = rippleSize
style.height = rippleSize
style['-webkit-transform'] = transformString
style['-ms-transform'] = transformString
style.transform = transformString
}
span.className = 'mdl-ripple'
if (isVisible) {
span.className += ' is-visible'
} else {
span.className += ' is-animating'
}
}
update()
return button
}