-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
103 lines (85 loc) · 2.99 KB
/
script.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
var intervalTime = 10;
var message = 'Yeah, RIP.';
var logging = false;
function start() {
var canvas;
var text;
var clips = new Clips();
canvas = document.getElementById('canvas');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
canvas.style.display = 'block';
text = new BounceText(canvas.getContext('2d'));
canvas.addEventListener('click', function() {
clips.toggle();
text.stop();
text.start();
});
text.start();
}
function BounceText(ctx) {
var speed = 1;
var intervalId;
ctx.font = (ctx.canvas.width / 10) + 'px Courier';
ctx.fillStyle = 'Goldenrod';
ctx.shadowBlur = 2;
var textSize = ctx.measureText(message);
var textWidth = textSize.width;
var textHeight = textSize.width / 10;
var x = random((textWidth / 10), ctx.canvas.width - (textWidth + (textWidth / 10)));
var y = random(textHeight + (textHeight / 10), ctx.canvas.height - (textHeight / 10));
return {
start: function() {
var vx = random(1, speed) * (random(0, 1) ? 1 : -1);
var vy = random(1, speed) * (random(0, 1) ? 1 : -1);
var shadowMode = random(0, 10);
ctx.shadowOffsetX = -0.2 * vx;
ctx.shadowOffsetY = -0.2 * vy;
ctx.shadowColor = random(0, 1) ? 'White' : 'Black'
intervalId = setInterval(function() {
if (x + textWidth >= ctx.canvas.width || x <= 0) {
vx = -vx;
ctx.shadowOffsetX = -0.2 * vx;
if (shadowMode % 2) ctx.shadowColor = random(0, 1) ? 'White' : 'Black';
}
if (y >= ctx.canvas.height || y - textHeight <= 0) {
vy = -vy;
ctx.shadowOffsetY = -0.2 * vy;
if (shadowMode % 2) ctx.shadowColor = random(0, 1) ? 'White' : 'Black';
}
x += vx;
y += vy;
if (shadowMode === 0) ctx.shadowColor = random(0, 1) ? 'White' : 'Black';
ctx.fillText(message, x, y);
if (logging) console.log(x, y, vx, vy);
}, intervalTime);
},
stop: function() {
clearInterval(intervalId);
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
speed++;
}
}
}
function Clips() {
var clip1 = document.getElementById('clip1');
var clip2 = document.getElementById('clip2');
var playing = false;
clip1.addEventListener('playing', function() { playing = true; });
clip1.addEventListener('pause', function() { playing = false });
clip1.setAttribute('loop', true);
return {
toggle: function() {
if (playing) {
clip1.pause();
clip2.play();
} else {
clip1.play();
}
}
}
}
function random(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
window.onload = start;