This repository was archived by the owner on Aug 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
228 lines (198 loc) · 5.53 KB
/
Copy pathscript.js
File metadata and controls
228 lines (198 loc) · 5.53 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
var _createClass = (function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
})();
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
var rid = null;
var frames = 0;
var index = 0;
var fontSize = 40;
var lines = [];
var points = [];
var speed = 15;
var ctx = canvas.getContext("2d");
var btx = buffer.getContext("2d");
var bw = (buffer.width = fontSize);
var bh = (buffer.height = fontSize);
var cw = canvas.width;
var ch = canvas.height;
btx.fillStyle = ctx.fillStyle = "#fff";
btx.font = ctx.font = fontSize + "px Courier New";
var text = [
"Beautiful is better than ugly.",
"Explicit is better than implicit.",
"Simple is better than complex.",
"Complex is better than complicated.",
"Flat is better than nested.",
"Sparse is better than dense.",
"Readability counts.",
"Special cases aren't special enough to break the rules.",
"Although practicality beats purity.",
"Errors should never pass silently.",
"Unless explicitly silenced.",
"In the face of ambiguity, refuse the temptation to guess.",
"There should be one-- and preferably only one --obvious way to do it.",
"Although that way may not be obvious at first unless you're Dutch.",
"Now is better than never.",
"Although never is often better than *right* now.",
"If the implementation is hard to explain, it's a bad idea.",
"If the implementation is easy to explain, it may be a good idea.",
"Namespaces are one honking great idea -- let's do more of those!",
];
text.map(function (t, i) {
text[i] = t.toUpperCase();
});
var Line = (function () {
function Line(text) {
_classCallCheck(this, Line);
this.text = text;
this.width = ctx.measureText(this.text).width;
this.startingPoint = -this.width / 2;
this.letters = [];
this.lettersRy();
}
_createClass(Line, [
{
key: "lettersRy",
value: function lettersRy() {
for (var i = 0; i < this.text.length; i++) {
var letter = this.text[i];
var _start =
this.startingPoint +
ctx.measureText(this.text.substring(0, i)).width;
var pos = { x: _start, y: (ch - fontSize) / 2 };
this.letters.push(new Letter(i, pos, letter));
}
},
},
]);
return Line;
})();
var Letter = (function () {
function Letter(i, pos, letter) {
_classCallCheck(this, Letter);
this.index = index;
this.letter = letter;
this.pos = pos;
this.points = [];
this.addToPointsRy();
}
_createClass(Letter, [
{
key: "addToPointsRy",
value: function addToPointsRy() {
btx.clearRect(0, 0, bw, bh);
btx.fillText(this.letter, 2, bh - 2);
var imgData = btx.getImageData(0, 0, bw, bh);
var pixels = imgData.data;
for (var i = 0; i < pixels.length; i += 4) {
if (pixels[i] == 255) {
var x = (0.25 * i) % bw;
var y = ~~((0.25 * i) / bw);
this.points.push(new Particle(x + this.pos.x, y + this.pos.y));
}
}
},
},
]);
return Letter;
})();
var Particle = (function () {
function Particle(x, y) {
_classCallCheck(this, Particle);
this.x = x;
this.y = y;
this.pos = { x: x, y: y };
this.pn = {
// positive or negative
x: Math.random() > 0.2 ? 1 : -1,
y: Math.random() > 0.2 ? -1 : 1,
};
this.vel = {
x: (this.pn.x * (this.pn.x + Math.random() * 10)) / 90,
y: (this.pn.y * (this.pn.y + Math.random() * 10)) / 90,
alp: (0.1 + Math.random()) / 60,
};
this.alp = 1;
}
_createClass(Particle, [
{
key: "draw",
value: function draw() {
ctx.fillStyle = "rgba(255, 255, 255" + this.alp + ")";
ctx.beginPath();
ctx.fillRect(this.pos.x, this.pos.y, 1, 1);
},
},
{
key: "update",
value: function update() {
this.pos.x += this.vel.x;
this.pos.y += this.vel.y;
this.alp -= this.vel.alp;
},
},
]);
return Particle;
})();
for (var i = 0; i < text.length; i++) {
lines.push(new Line(text[i]));
}
var numLine = 0;
var line = lines[numLine];
ctx.translate(cw / 2, 0);
function Frame() {
rid = window.requestAnimationFrame(Frame);
ctx.clearRect(-cw, -ch, 2 * cw, 2 * ch);
points.map(function (p, i) {
p.update();
p.draw();
if (p.alp <= 0) {
points.splice(i, 1);
}
});
if (frames % speed == 0) {
line.letters[index].points.map(function (p) {
p.pos = { x: p.x, y: p.y };
p.alp = 1;
});
points = points.concat(line.letters[index].points);
index++;
}
if (index == line.letters.length) {
numLine++;
line = lines[numLine % text.length];
index = 0;
frames = 0;
}
frames++;
}
function Init() {
cw = canvas.width = window.innerWidth;
ctx.translate(cw / 2, 0);
if (rid) {
window.cancelAnimationFrame(rid);
rid = null;
}
Frame();
}
setTimeout(function () {
Init();
window.addEventListener("resize", Init, false);
}, 15);