-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
103 lines (86 loc) · 2.35 KB
/
scripts.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
const emoji = [
"🍌",
"🍍",
"🥥",
"🍊",
"🥭",
"🥝",
"🥑",
"🌴",
"🇵🇷",
"🇨🇺",
"🇲🇽",
];
let isPainting = true;
function getRandomEmoji() {
const index = Math.floor(Math.random() * emoji.length);
return emoji[index];
}
function throttle(fn, wait) {
let lastCalled = null;
return function throttledFn() {
const now = Date.now();
if (lastCalled === null || now - lastCalled >= wait) {
fn.apply(null, arguments);
lastCalled = now;
}
};
}
const paintEmoji = throttle(function paintEmoji(x, y) {
const node = document.createElement("span");
node.classList.add("emoji");
node.innerHTML = getRandomEmoji();
node.style.left = x;
node.style.top = y;
document.body.appendChild(node);
// Center node once we know its width and height
node.style.left = Math.ceil(x - node.clientWidth / 2);
node.style.top = Math.ceil(y - node.clientHeight / 2);
setTimeout(() => {
node.style.animationName = "popOut";
setTimeout(() => {
document.body.removeChild(node);
}, 250);
}, 750);
}, 75);
function canPaintInTag(tagName) {
const tagNameBlackList = ["h1", "h2", "h3", "p", "a"];
return tagNameBlackList.indexOf(tagName) < 0;
}
function startPainting() {
isPainting = true;
}
function stopPainting() {
isPainting = false;
}
document.body.addEventListener("touchstart", (event) => {
const touch = event.targetTouches[event.targetTouches.length - 1];
paintEmoji(touch.clientX, touch.clientY);
const tagName = event.target.tagName.toLowerCase();
if (canPaintInTag(tagName)) {
event.preventDefault();
event.stopPropagation();
startPainting();
}
});
document.body.addEventListener("mousemove", (event) => {
const tagName = event.target.tagName.toLowerCase();
if (canPaintInTag(tagName)) {
// Prevent text from being selected while the user is painting
event.preventDefault();
paintEmoji(event.clientX, event.clientY);
} else {
stopPainting();
}
});
document.body.addEventListener("touchmove", (event) => {
if (!isPainting) {
return;
}
// Prevent text from being selected while the user is painting
event.preventDefault();
event.stopPropagation();
const touch = event.targetTouches[event.targetTouches.length - 1];
paintEmoji(touch.clientX, touch.clientY);
});
document.body.addEventListener("touchend", stopPainting);