-
Notifications
You must be signed in to change notification settings - Fork 0
/
fallingSentence.js
57 lines (47 loc) · 1.96 KB
/
fallingSentence.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
/*
* REference: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Basic_animations
*/
class fallingSentence extends sentence {
constructor(canvas) {
// Base constructor call
super(canvas);
// Variables specific of this type of sentence
this.speed; // Speed of falling down
this.endingY; // When it starts fading
this.requestSentence(); // Begin
}
draw(deltaT) {
this.fade = this.Y < this.endingY ? true : false;
// Advance sentence fall
this.Y += this.speed * deltaT;
/*
If Y position is under the window size, request a new sentence.
Not if already requested.
If we had some error wait a little bit before retring.
This also manages disconnections.
*/
this.refresh = this.Y - this.size > this.canvasHeight ? true : false;
super.draw(deltaT);
}
// Compute all the parameters from the sentence we just got
createNew() {
// Smaller size as the lenght increases
this.size = Math.floor(400 / (this.sentence.length + 5) + 10);
this.offesetAmount = Math.round(this.size + (this.size / 3));
// Random orizontal position
this.X = Math.floor((Math.random() * (this.canvasWidth - this.canvasWidth / 5)));
// Slower with longer sentences
this.speed = (1 / this.sentence.length) + 0.01;
// Vertical (Y) position
this.Y = Math.floor(Math.random() * this.canvasHeight / 4); // Appear in first quarte of screen
this.endingY = Math.floor(Math.random() * this.canvasHeight / 4 + 3 * this.canvasHeight / 4); // Disappear in last quarter of screen
// Split into lines
super.createNew();
}
// Utility to get text horizontal lenght given the settings
getTextWidth(text, font) {
this.ctx.font = font;
var metrics = this.ctx.measureText(text);
return metrics.width;
}
}