/
picture-scene.js
62 lines (53 loc) · 2.11 KB
/
picture-scene.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
import { Scene } from './js-demo.js';
export class PictureScene extends Scene {
constructor(url, scale, fromColor, fadeInTime, toColor, fadeOutPos, fadeOutRow, fadeOutTime) {
super();
const pictureName = url.match(/^(.*\/)?(.*?)(\.(png|jpg|jpeg))?$/)[2];
this.name = `Picture (${pictureName})`;
this.url = url;
this.scale = scale;
this.fromColor = fromColor;
this.fadeInTime = fadeInTime;
this.toColor = toColor;
this.fadeOutPos = fadeOutPos;
this.fadeOutRow = fadeOutRow;
this.fadeOutTime = fadeOutTime;
this.fadingOut = false;
this.lastTime = 0.0;
}
async prepare(timeSource, progressCallback) {
progressCallback(0, 1);
timeSource.watch(this.fadeOutPos, this.fadeOutRow);
this.image = await this.loadImage(this.url);
progressCallback(1, 1);
}
handleTime(songPos, row) {
this.fadingOut = true;
this.fadeOutStartTime = this.lastTime;
}
render(canvas, context, time) {
const halfWidth = canvas.width / 2;
const halfHeight = canvas.height / 2;
context.fillStyle = "#000000";
context.fillRect(0, 0, canvas.width, canvas.height);
const iw = this.image.width * this.scale;
const ih = this.image.height * this.scale;
context.drawImage(this.image, halfWidth - (iw >> 1), halfHeight - (ih >> 1), iw, ih);
if (time < this.fadeInTime) {
this.fadingOut = false;
context.globalAlpha = (1 - time / this.fadeInTime);
context.fillStyle = this.fromColor;
context.fillRect(0, 0, canvas.width, canvas.height);
}
else if (this.fadingOut) {
const timeAfterFadeout = time - this.fadeOutStartTime;
const rawFade = timeAfterFadeout / this.fadeOutTime;
const fade = Math.min(1, rawFade);
context.globalAlpha = fade;
context.fillStyle = this.toColor;
context.fillRect(0, 0, canvas.width, canvas.height);
}
context.globalAlpha = 1.0;
this.lastTime = time;
}
}