ability to animate film grain alpha
This commit is contained in:
parent
6de6aa0acc
commit
d100f81f0c
|
@ -16,6 +16,8 @@ export class FilmGrain {
|
||||||
private grainScaleY: number;
|
private grainScaleY: number;
|
||||||
private refreshInterval: number;
|
private refreshInterval: number;
|
||||||
private alpha: number;
|
private alpha: number;
|
||||||
|
private targetAlpha: number;
|
||||||
|
private targetStep: number = 0;
|
||||||
private canvasId?: string;
|
private canvasId?: string;
|
||||||
private patternPixelDataLength: number;
|
private patternPixelDataLength: number;
|
||||||
|
|
||||||
|
@ -29,13 +31,14 @@ export class FilmGrain {
|
||||||
private patternData?: ImageData;
|
private patternData?: ImageData;
|
||||||
|
|
||||||
private frame: number = 0;
|
private frame: number = 0;
|
||||||
|
private active: boolean = false;
|
||||||
|
|
||||||
constructor(options?: FilmGrainOptions) {
|
constructor(options?: FilmGrainOptions) {
|
||||||
this.patternSize = options?.patternSize || 64;
|
this.patternSize = options?.patternSize || 64;
|
||||||
this.grainScaleX = options?.grainScale?.x || 3;
|
this.grainScaleX = options?.grainScale?.x || 3;
|
||||||
this.grainScaleY = options?.grainScale?.y || 1;
|
this.grainScaleY = options?.grainScale?.y || 1;
|
||||||
this.refreshInterval = options?.refreshInterval || 4;
|
this.refreshInterval = options?.refreshInterval || 4;
|
||||||
this.alpha = options?.alpha || 25;
|
this.alpha = this.targetAlpha = options?.alpha == null ? 25 : options.alpha;
|
||||||
this.canvasId = options?.canvasId;
|
this.canvasId = options?.canvasId;
|
||||||
this.patternPixelDataLength = this.patternSize * this.patternSize * 4;
|
this.patternPixelDataLength = this.patternSize * this.patternSize * 4;
|
||||||
}
|
}
|
||||||
|
@ -79,6 +82,16 @@ export class FilmGrain {
|
||||||
}
|
}
|
||||||
|
|
||||||
private update(): void {
|
private update(): void {
|
||||||
|
// handle alpha animations
|
||||||
|
if (Math.abs(this.targetAlpha - this.alpha) > this.targetStep) {
|
||||||
|
if (this.targetAlpha < this.alpha) {
|
||||||
|
this.alpha -= this.targetStep;
|
||||||
|
} else {
|
||||||
|
this.alpha += this.targetStep;
|
||||||
|
}
|
||||||
|
} else if (this.alpha !== this.targetAlpha) {
|
||||||
|
this.alpha = this.targetAlpha;
|
||||||
|
}
|
||||||
// put a random shade of gray into every pixel of the pattern
|
// put a random shade of gray into every pixel of the pattern
|
||||||
if (this.patternData && this.patternContext) {
|
if (this.patternData && this.patternContext) {
|
||||||
for (let i = 0; i < this.patternPixelDataLength; i += 4) {
|
for (let i = 0; i < this.patternPixelDataLength; i += 4) {
|
||||||
|
@ -101,16 +114,39 @@ export class FilmGrain {
|
||||||
}
|
}
|
||||||
|
|
||||||
private loop(): void {
|
private loop(): void {
|
||||||
if (++(this.frame) % this.refreshInterval === 0) {
|
if (this.active) {
|
||||||
this.update();
|
if (++(this.frame) % this.refreshInterval === 0) {
|
||||||
this.draw();
|
this.update();
|
||||||
|
this.draw();
|
||||||
|
}
|
||||||
|
requestAnimationFrame(() => this.loop());
|
||||||
}
|
}
|
||||||
requestAnimationFrame(() => this.loop());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public execute(): void {
|
public execute(): void {
|
||||||
this.initCanvas();
|
if (!this.active) {
|
||||||
this.initGrain();
|
this.active = true;
|
||||||
requestAnimationFrame(() => this.loop());
|
this.initCanvas();
|
||||||
|
this.initGrain();
|
||||||
|
requestAnimationFrame(() => this.loop());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public setAlpha(alpha: number, step: number = 5): void {
|
||||||
|
this.targetAlpha = alpha;
|
||||||
|
this.targetStep = step;
|
||||||
|
}
|
||||||
|
|
||||||
|
public stop(): void {
|
||||||
|
this.active = false;
|
||||||
|
if (!this.canvasId && this.canvas) {
|
||||||
|
// if we created the original canvas, just remove it
|
||||||
|
this.canvas.remove();
|
||||||
|
} else {
|
||||||
|
if (this.context) {
|
||||||
|
// otherwise clear it out
|
||||||
|
this.context.clearRect(0, 0, this.viewWidth, this.viewHeight);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue