-
Notifications
You must be signed in to change notification settings - Fork 0
/
animation.js
226 lines (191 loc) · 6.23 KB
/
animation.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
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
var canvas = document.querySelector( 'canvas' );
var context = canvas.getContext( '2d' );
let input;
let raf;
var allinputs= [];
function setup() {
resize();
//sets the fill sytle of the Text
addGradient();
context.textAlign="center";
window.addEventListener( 'resize', resize );
window.addEventListener('load', setup, false);
//initialising the input field and setting the value to an empty string
var inp = document.getElementById('input');
inp.value = '';
clear();
}
function addGradient() {
gradient = context.createLinearGradient((width/2), 0, width/2, height);
//lets the text fade into transparency when it reaches the top of the page
gradient.addColorStop(1, 'black');
gradient.addColorStop(0.3, "transparent");
gradient.addColorStop(0, 'transparent');
context.fillStyle = gradient;
}
function resize() {
//ends the current animations for each input still going
allinputs.forEach(inp => {
inp.updatewidth();
inp.endanimation();
});
clear;
//sets the canvas width and height to the new width and height of the window
width = canvas.width = window.innerWidth;
height = canvas.height = window.innerHeight - document.getElementById("start").style.height
- document.getElementById('input').style.height-32;
ypos = height;
//resetting the text size
context.font="bold 2vw arial";
if(width < 960){
context.font="bold 1em arial";
}
//reset gradient
addGradient();
}
function clear() {
context.clearRect(0,0,width,height);
}
setup();
height,
width,
ypos,
window.addEventListener('change', getinput, false);
stars = [];
function getinput(){
//get the input field
var inp = document.getElementById('input');
let inputs = inp.value;
//clear the input field
inp.value = '';
//create a new animation object for each part of the input that is wide enough for the screen
if(context.measureText(inputs).width > width){
lines = getLines(inputs);
for(var i = 0; i <lines.length; i++){
str = lines[i];
input = new inputtext(context, lines[i], height +
(context.measureText(str).actualBoundingBoxAscent
+context.measureText(str).actualBoundingBoxDescent)*i,
width);
allinputs.push(input);
input.animate();
}
} else {
input = new inputtext(context, inputs, height, width);
//add animation obj. to list of all animated objects
allinputs.push(input);
// let it move
input.animate(0)
};
addStar();
}
function getLines(text) {
var words = text.split(" ");
var lines = [];
var currentLine = words[0];
for (var i = 1; i < words.length; i++) {
var word = words[i];
var wdt = context.measureText(currentLine + " " + word).actualBoundingBoxLeft
+context.measureText(currentLine + " " + word).actualBoundingBoxRight;
if (wdt < width) {
currentLine += " " + word;
} else {
lines.push(currentLine);
currentLine = word;
}
}
lines.push(currentLine);
console.log(lines);
return lines;
}
function addStar() {
xpos = Math.floor(Math.random()*width);
ypos = Math.floor(Math.random()*(height/2));
var star = new Star(xpos, ypos, context);
stars.push(star);
star.animate();
}
class Star{
#xpos;
#ypos;
#ctx;
#brighness;
#raf;
#size;
#bright;
constructor(xpos,ypos, ctx){
this.#xpos = xpos;
this.#ypos = ypos;
this.#ctx = ctx;
this.#brighness = 1;
this.#bright= 0.05;
this.#size = 5;
}
draw(){
this.#ctx.strokeStyle = `rgba(255,255,77,${this.#brighness} )`;
this.#ctx.beginPath();
this.#ctx.moveTo(this.#xpos, this.#ypos);
this.#ctx.lineTo(this.#xpos + this.#size, this.#ypos);
this.#ctx.lineTo(this.#xpos - this.#size, this.#ypos);
this.#ctx.lineTo(this.#xpos , this.#ypos);
this.#ctx.lineTo(this.#xpos , this.#ypos+ this.#size);
this.#ctx.lineTo(this.#xpos , this.#ypos- this.#size);
this.#ctx.stroke();
}
animate(){
this.draw();
this.#brighness += this.#bright;
if(this.#brighness <= 0 || this.#brighness >=1){
this.#bright = -this.#bright;
}
this.#raf = requestAnimationFrame(this.animate.bind(this));
}
}
class inputtext{
#ctx;
#text;
#height;
#center;
#raf;
#txtheight;
#speed;
constructor(ctx, input, height, width) {
this.#ctx = ctx;
this.#text = input;
this.#height= height;
this.#center = width/2;
this.#txtheight = ctx.measureText(input).actualBoundingBoxAscent + ctx.measureText(input).actualBoundingBoxDescent;
this.lastime = 0;
this.timer = 0;
this. intervall = 1000/60;
this.h = height;
this.#speed = 3;
}
updatewidth(){
this.#txtheight = this.#ctx.measureText(input).actualBoundingBoxAscent + this.#ctx.measureText(input).actualBoundingBoxDescent;
}
#draw(y) {
this.#ctx.textAlign="center";
this.#ctx.fillText(this.#text, this.#center, y);
}
#clear(){
this.#ctx.clearRect(this.#center-(this.#ctx.measureText(this.#text).actualBoundingBoxLeft +1),
this.#height - this.#ctx.measureText(this.#text).actualBoundingBoxAscent -1,
this.#ctx.measureText(this.#text).actualBoundingBoxRight +this.#ctx.measureText(this.#text).actualBoundingBoxLeft +2,
this.#txtheight + this.#speed*2);
}
animate() {
this.#clear();
this.#draw(this.#height);
this.#height -= this.#speed;
if(this.#height<0){
this.endanimation();
}
this.#raf = requestAnimationFrame(this.animate.bind(this));
}
endanimation(){
cancelAnimationFrame(this.#raf);
this.#clear();
allinputs.splice(allinputs.indexOf(this),1);
}
}