-
Notifications
You must be signed in to change notification settings - Fork 2
/
valentines.js
141 lines (127 loc) · 4.31 KB
/
valentines.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
(function (document, Pressure) {
var valentines = document.getElementById('valentines');
var heart = document.getElementById('heart');
var circle = document.getElementById('circle');
var ended = false;
var start = function () {
document.body.classList.add('start');
document.body.classList.remove('end', 'reset');
};
var end = function () {
document.body.classList.add('end');
document.body.classList.remove('start', 'reset');
ended = true;
};
var reset = function () {
document.body.classList.add('reset');
document.body.classList.remove('start', 'end');
};
// Load icon svgs inline so they can be styled and not a font.
var iconsPath = 'lib/Font-Awesome-SVG-PNG/black/svg/';
var loadFontIcon = function (name, element) {
var icon = iconsPath + name + '.svg';
var id = 'fa-' + name;
var xhr = new XMLHttpRequest();
xhr.open('GET', icon, true);
xhr.onload = function (event) {
var icon = xhr.responseXML.documentElement;
icon.id = id;
icon.classList.add(id);
element.appendChild(icon);
};
// Following line is just to be on the safe side;
// not needed if your server delivers SVG with correct MIME type
xhr.overrideMimeType('image/svg+xml');
xhr.send('');
};
loadFontIcon('heart', heart);
loadFontIcon('heart-o', heart);
// Without force touch.
var timeout;
var delay = 2000; // 2 seconds
// Mouse or touch events.
var eventIn = 'mousedown';
var eventOut = 'mouseup';
if ('ontouchstart' in window) {
eventIn = 'touchstart';
eventOut = 'touchend';
}
var heartStart = function () {
start();
timeout = window.setTimeout(function () {
endNoForceTouch();
end();
}, delay);
};
var heartEnd = function () {
window.clearTimeout(timeout);
reset();
};
var startForceTouch = function () {
document.body.classList.add('force-touch');
};
var startNoForceTouch = function () {
document.body.classList.add('no-force-touch');
valentines.addEventListener(eventIn, heartStart, false);
valentines.addEventListener(eventOut, heartEnd, false);
};
var endNoForceTouch = function () {
valentines.removeEventListener(eventIn, heartStart);
valentines.removeEventListener(eventOut, heartEnd);
};
var cancelNoForceTouch = function () {
heartEnd();
endNoForceTouch();
};
// With force touch.
var styleForceValue = function (forceValue) {
// Style the circle and heart like iOS 9+ 3d touch.
// Copied from https://github.com/freinbichler/3d-touch/blob/e8f606284c2e08b039b3a2e595c4d6b1e2e52055/3dtouch.js#L47-L48
heart.style.webkitTransform = 'translateX(-50%) translateY(-50%) scale(' + (1 + forceValue * 1.5) + ')';
circle.style.webkitFilter = 'blur(' + forceValue * 30 + 'px)';
};
// Pressure.js http://yamartino.github.io/pressure/
var isSetup = false;
Pressure.set(valentines, {
start: function (event) {
if (ended) {
return;
}
if (!isSetup) {
startForceTouch();
}
start();
},
end: function (event) {
if (ended) {
return;
}
styleForceValue(0);
reset();
},
change: function (forceValue, event) {
if (ended) {
return;
}
styleForceValue(forceValue);
if (forceValue >= 0.95) {
end();
}
},
unsupported: function () {
// Use non-force-touch functions.
if (!isSetup) {
// Only run once.
heartStart();
startNoForceTouch();
}
isSetup = true;
}
});
// Finish loading. A function in the event queue will only be processed
// once every other bit of JavaScript here has run.
window.setTimeout(function () {
document.getElementById('main').style.display = '';
document.getElementById('loading').style.display = 'none';
}, 1000);
}(window.document, window.Pressure));