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.active) {
|
||||||
if (++(this.frame) % this.refreshInterval === 0) {
|
if (++(this.frame) % this.refreshInterval === 0) {
|
||||||
this.update();
|
this.update();
|
||||||
this.draw();
|
this.draw();
|
||||||
}
|
}
|
||||||
requestAnimationFrame(() => this.loop());
|
requestAnimationFrame(() => this.loop());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public execute(): void {
|
public execute(): void {
|
||||||
|
if (!this.active) {
|
||||||
|
this.active = true;
|
||||||
this.initCanvas();
|
this.initCanvas();
|
||||||
this.initGrain();
|
this.initGrain();
|
||||||
requestAnimationFrame(() => this.loop());
|
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