@wolsok/loading-wave 0.0.0-stage → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +196 -2
- package/esm2022/index.js +7 -0
- package/esm2022/index.js.map +1 -0
- package/esm2022/lib/canvas-wave-renderer.js +125 -0
- package/esm2022/lib/canvas-wave-renderer.js.map +1 -0
- package/esm2022/lib/choose-wave-renderer.js +66 -0
- package/esm2022/lib/choose-wave-renderer.js.map +1 -0
- package/esm2022/lib/dot-color.js +63 -0
- package/esm2022/lib/dot-color.js.map +1 -0
- package/esm2022/lib/loading-wave-options.js +48 -0
- package/esm2022/lib/loading-wave-options.js.map +1 -0
- package/esm2022/lib/loading-wave.directive.js +74 -0
- package/esm2022/lib/loading-wave.directive.js.map +1 -0
- package/esm2022/lib/loading-wave.js +238 -0
- package/esm2022/lib/loading-wave.js.map +1 -0
- package/esm2022/lib/renderer.js +18 -0
- package/esm2022/lib/renderer.js.map +1 -0
- package/esm2022/lib/wave-environment.js +59 -0
- package/esm2022/lib/wave-environment.js.map +1 -0
- package/esm2022/lib/wave-model.js +171 -0
- package/esm2022/lib/wave-model.js.map +1 -0
- package/esm2022/lib/wave-timeline.js +61 -0
- package/esm2022/lib/wave-timeline.js.map +1 -0
- package/esm2022/lib/webgl/echo.js +5 -0
- package/esm2022/lib/webgl/echo.js.map +1 -0
- package/esm2022/lib/webgl/loading-wave-shader.js +1 -0
- package/esm2022/lib/webgl/loading-wave-shader.js.map +1 -0
- package/esm2022/lib/webgl/shader-program.js +227 -0
- package/esm2022/lib/webgl/shader-program.js.map +1 -0
- package/esm2022/lib/webgl/shaders/ripple.shader.js +110 -0
- package/esm2022/lib/webgl/shaders/ripple.shader.js.map +1 -0
- package/esm2022/lib/webgl/webgl-wave-renderer.js +182 -0
- package/esm2022/lib/webgl/webgl-wave-renderer.js.map +1 -0
- package/esm2022/wolsok-loading-wave.js +5 -0
- package/esm2022/wolsok-loading-wave.js.map +1 -0
- package/index.d.ts +6 -0
- package/lib/canvas-wave-renderer.d.ts +29 -0
- package/lib/choose-wave-renderer.d.ts +31 -0
- package/lib/dot-color.d.ts +15 -0
- package/lib/loading-wave-options.d.ts +55 -0
- package/lib/loading-wave.d.ts +55 -0
- package/lib/loading-wave.directive.d.ts +29 -0
- package/lib/renderer.d.ts +54 -0
- package/lib/wave-environment.d.ts +23 -0
- package/lib/wave-model.d.ts +85 -0
- package/lib/wave-timeline.d.ts +33 -0
- package/lib/webgl/echo.d.ts +4 -0
- package/lib/webgl/loading-wave-shader.d.ts +20 -0
- package/lib/webgl/shader-program.d.ts +33 -0
- package/lib/webgl/shaders/ripple.shader.d.ts +10 -0
- package/lib/webgl/webgl-wave-renderer.d.ts +58 -0
- package/package.json +44 -3
- package/wolsok-loading-wave.d.ts +5 -0
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { sizeCanvas, } from '../renderer';
|
|
2
|
+
import { frontRadius, hasPreviousSweep, sweepPhase, } from '../wave-model';
|
|
3
|
+
import { linkWaveProgram, STANDARD_UNIFORMS, WaveShaderError, } from './shader-program';
|
|
4
|
+
import { ECHO_LAG } from './echo';
|
|
5
|
+
export { WaveShaderError };
|
|
6
|
+
/**
|
|
7
|
+
* Draws the wave with a WebGL2 fragment shader (`LoadingWaveShader`) over a
|
|
8
|
+
* full-canvas triangle, setting the contract's `ws_*` uniforms and the
|
|
9
|
+
* shader's own each frame.
|
|
10
|
+
*/
|
|
11
|
+
export class WebGlWaveRenderer {
|
|
12
|
+
/** @throws WaveShaderError when the shaders fail on this GPU */
|
|
13
|
+
constructor(canvas, gl, look, shader, hooks) {
|
|
14
|
+
this.canvas = canvas;
|
|
15
|
+
this.gl = gl;
|
|
16
|
+
this.look = look;
|
|
17
|
+
this.hooks = hooks;
|
|
18
|
+
this.kind = 'webgl';
|
|
19
|
+
this.ownLocations = new Map();
|
|
20
|
+
/** uniforms `setShaderUniforms` left out, set to 0 on the next draw */
|
|
21
|
+
this.pendingResets = {};
|
|
22
|
+
this.lost = false;
|
|
23
|
+
this.disposed = false;
|
|
24
|
+
this.handleContextLost = (event) => {
|
|
25
|
+
event.preventDefault();
|
|
26
|
+
this.lost = true;
|
|
27
|
+
this.hooks.onContextLost();
|
|
28
|
+
};
|
|
29
|
+
this.program = linkWaveProgram(gl, shader);
|
|
30
|
+
this.uniforms = Object.fromEntries(STANDARD_UNIFORMS.map((name) => [
|
|
31
|
+
name,
|
|
32
|
+
gl.getUniformLocation(this.program, name),
|
|
33
|
+
]));
|
|
34
|
+
this.ownUniforms = shader.uniforms ?? {};
|
|
35
|
+
canvas.addEventListener('webglcontextlost', this.handleContextLost);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* A WebGL2 context, or `null` where the browser offers none. With
|
|
39
|
+
* `hardwareOnly` also `null` where it would render on the CPU (no GPU, a
|
|
40
|
+
* blocked driver, a software renderer).
|
|
41
|
+
*/
|
|
42
|
+
static createContext(canvas, hardwareOnly = true) {
|
|
43
|
+
return canvas.getContext('webgl2', {
|
|
44
|
+
alpha: true,
|
|
45
|
+
premultipliedAlpha: true,
|
|
46
|
+
antialias: false,
|
|
47
|
+
depth: false,
|
|
48
|
+
stencil: false,
|
|
49
|
+
powerPreference: 'low-power',
|
|
50
|
+
failIfMajorPerformanceCaveat: hardwareOnly,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
resize(geometry, pixelRatio) {
|
|
54
|
+
this.geometry = geometry;
|
|
55
|
+
sizeCanvas(this.canvas, geometry, pixelRatio);
|
|
56
|
+
// a GPU may give a smaller drawing buffer than the canvas asks for
|
|
57
|
+
this.gl.viewport(0, 0, this.gl.drawingBufferWidth, this.gl.drawingBufferHeight);
|
|
58
|
+
}
|
|
59
|
+
draw(frame) {
|
|
60
|
+
const { gl, geometry } = this;
|
|
61
|
+
if (this.lost || this.disposed || geometry == null) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
gl.clearColor(0, 0, 0, 0);
|
|
65
|
+
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
66
|
+
if (frame.dots <= 0) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
gl.useProgram(this.program);
|
|
70
|
+
this.setUniforms(geometry, frame);
|
|
71
|
+
gl.drawArrays(gl.TRIANGLES, 0, 3);
|
|
72
|
+
}
|
|
73
|
+
setLook(look) {
|
|
74
|
+
this.look = look;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* a uniform the new values leave out goes back to 0, the value GLSL gives
|
|
78
|
+
* every uniform before it is set
|
|
79
|
+
*/
|
|
80
|
+
setShaderUniforms(uniforms) {
|
|
81
|
+
const next = uniforms ?? {};
|
|
82
|
+
for (const [name, value] of Object.entries(this.ownUniforms)) {
|
|
83
|
+
if (!(name in next)) {
|
|
84
|
+
this.pendingResets[name] = unsetValue(value);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
this.ownUniforms = next;
|
|
88
|
+
}
|
|
89
|
+
dispose() {
|
|
90
|
+
if (this.disposed) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
this.disposed = true;
|
|
94
|
+
this.canvas.removeEventListener('webglcontextlost', this.handleContextLost);
|
|
95
|
+
if (!this.gl.isContextLost()) {
|
|
96
|
+
this.gl.deleteProgram(this.program);
|
|
97
|
+
this.gl.getExtension('WEBGL_lose_context')?.loseContext();
|
|
98
|
+
}
|
|
99
|
+
this.hooks.onDispose?.();
|
|
100
|
+
}
|
|
101
|
+
setUniforms(geometry, frame) {
|
|
102
|
+
const { gl, uniforms: locations, look } = this;
|
|
103
|
+
const bufferWidth = gl.drawingBufferWidth;
|
|
104
|
+
const bufferHeight = gl.drawingBufferHeight;
|
|
105
|
+
gl.uniform2f(locations.ws_size, geometry.width, geometry.height);
|
|
106
|
+
gl.uniform2f(locations.ws_bufferSize, bufferWidth, bufferHeight);
|
|
107
|
+
gl.uniform1f(locations.ws_pixel, geometry.width / Math.max(1, bufferWidth));
|
|
108
|
+
gl.uniform1f(locations.ws_spacing, geometry.dotSpacing);
|
|
109
|
+
gl.uniform1f(locations.ws_dotSize, look.dotSize);
|
|
110
|
+
setColor(gl, locations.ws_color, look.color);
|
|
111
|
+
setColor(gl, locations.ws_accent, look.accent ?? look.color);
|
|
112
|
+
gl.uniform2f(locations.ws_center, geometry.centerX, geometry.centerY);
|
|
113
|
+
gl.uniform1f(locations.ws_frontRadius, frontRadius(geometry, frame.elapsedMs));
|
|
114
|
+
gl.uniform1f(locations.ws_echoRadius, echoRadius(geometry, frame.elapsedMs));
|
|
115
|
+
gl.uniform1f(locations.ws_travel, geometry.travel);
|
|
116
|
+
gl.uniform1f(locations.ws_falloff, geometry.falloff);
|
|
117
|
+
gl.uniform1f(locations.ws_rise, geometry.rise);
|
|
118
|
+
gl.uniform1f(locations.ws_tail, geometry.tail);
|
|
119
|
+
gl.uniform1f(locations.ws_maxPush, geometry.maxPush);
|
|
120
|
+
gl.uniform1f(locations.ws_phase, sweepPhase(geometry, frame.elapsedMs));
|
|
121
|
+
gl.uniform1f(locations.ws_time, frame.elapsedMs / 1000);
|
|
122
|
+
gl.uniform1f(locations.ws_previousSweep, hasPreviousSweep(geometry, frame.elapsedMs) ? 1 : 0);
|
|
123
|
+
gl.uniform1f(locations.ws_wave, frame.wave);
|
|
124
|
+
gl.uniform1f(locations.ws_dots, frame.dots);
|
|
125
|
+
// a dropped uniform is reset once, then left alone
|
|
126
|
+
const own = { ...this.pendingResets, ...this.ownUniforms };
|
|
127
|
+
this.pendingResets = {};
|
|
128
|
+
for (const [name, value] of Object.entries(own)) {
|
|
129
|
+
// the ws_ uniforms are the lib's: a shader's own never override them
|
|
130
|
+
if (!name.startsWith('ws_')) {
|
|
131
|
+
setOwnUniform(gl, this.ownLocation(name), value);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/** where the shader's own uniform lives, looked up once per name */
|
|
136
|
+
ownLocation(name) {
|
|
137
|
+
if (!this.ownLocations.has(name)) {
|
|
138
|
+
this.ownLocations.set(name, this.gl.getUniformLocation(this.program, name));
|
|
139
|
+
}
|
|
140
|
+
return this.ownLocations.get(name) ?? null;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
/** zeros of the same size as `value` */
|
|
144
|
+
const ZEROS = {
|
|
145
|
+
1: [0],
|
|
146
|
+
2: [0, 0],
|
|
147
|
+
3: [0, 0, 0],
|
|
148
|
+
4: [0, 0, 0, 0],
|
|
149
|
+
};
|
|
150
|
+
function unsetValue(value) {
|
|
151
|
+
return typeof value === 'number' ? 0 : ZEROS[value.length];
|
|
152
|
+
}
|
|
153
|
+
/** a colour as a straight RGBA `vec4`, each 0 to 1 */
|
|
154
|
+
function setColor(gl, location, { r, g, b, alpha = 1 }) {
|
|
155
|
+
gl.uniform4f(location, r / 255, g / 255, b / 255, alpha);
|
|
156
|
+
}
|
|
157
|
+
/** sets a `float` to `vec4` uniform by how many components the value has */
|
|
158
|
+
function setOwnUniform(gl, location, value) {
|
|
159
|
+
const [x, y = 0, z = 0, w = 0] = typeof value === 'number' ? [value] : value;
|
|
160
|
+
const size = typeof value === 'number' ? 1 : value.length;
|
|
161
|
+
if (size === 1) {
|
|
162
|
+
gl.uniform1f(location, x);
|
|
163
|
+
}
|
|
164
|
+
else if (size === 2) {
|
|
165
|
+
gl.uniform2f(location, x, y);
|
|
166
|
+
}
|
|
167
|
+
else if (size === 3) {
|
|
168
|
+
gl.uniform3f(location, x, y, z);
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
gl.uniform4f(location, x, y, z, w);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* The echo's radius, trailing the front by `ECHO_LAG` of a sweep; negative,
|
|
176
|
+
* so no echo shows, until the front has run that far.
|
|
177
|
+
*/
|
|
178
|
+
export function echoRadius(geometry, elapsedMs) {
|
|
179
|
+
const echoElapsed = elapsedMs - ECHO_LAG * geometry.sweepMs;
|
|
180
|
+
return echoElapsed < 0 ? -1 : frontRadius(geometry, echoElapsed);
|
|
181
|
+
}
|
|
182
|
+
//# sourceMappingURL=webgl-wave-renderer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webgl-wave-renderer.js","sourceRoot":"","sources":["../../../../../../../libs/public/ws-loading-wave/src/lib/webgl/webgl-wave-renderer.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,GAKX,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,WAAW,EACX,gBAAgB,EAChB,UAAU,GAEX,MAAM,eAAe,CAAC;AAMvB,OAAO,EACL,eAAe,EACf,iBAAiB,EAEjB,eAAe,GAChB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAElC,OAAO,EAAE,eAAe,EAAE,CAAC;AAS3B;;;;GAIG;AACH,MAAM,OAAO,iBAAiB;IAwB5B,gEAAgE;IAChE,YACkB,MAAyB,EACxB,EAA0B,EACnC,IAAc,EACtB,MAAyB,EACR,KAA6B;QAJ9B,WAAM,GAAN,MAAM,CAAmB;QACxB,OAAE,GAAF,EAAE,CAAwB;QACnC,SAAI,GAAJ,IAAI,CAAU;QAEL,UAAK,GAAL,KAAK,CAAwB;QA7BhC,SAAI,GAAG,OAAO,CAAC;QAMd,iBAAY,GAAG,IAAI,GAAG,EAGpC,CAAC;QAEJ,uEAAuE;QAC/D,kBAAa,GAAuC,EAAE,CAAC;QAEvD,SAAI,GAAG,KAAK,CAAC;QACb,aAAQ,GAAG,KAAK,CAAC;QAER,sBAAiB,GAAG,CAAC,KAAY,EAAQ,EAAE;YAC1D,KAAK,CAAC,cAAc,EAAE,CAAC;YACvB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;YACjB,IAAI,CAAC,KAAK,CAAC,aAAa,EAAE,CAAC;QAC7B,CAAC,CAAC;QAUA,IAAI,CAAC,OAAO,GAAG,eAAe,CAAC,EAAE,EAAE,MA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CAAC;IACrC,CAAC;AACH,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,UAAU,CAAC,QAAsB,EAAE,SAAiB;IAClE,MAAM,WAAW,GAAG,SAAS,GAAG,QAAQ,GAAG,QAAQ,CAAC,OAAO,CAAC;IAC5D,OAAO,WAAW,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,QAAQ,EAAE,WAAW,CAAC,CAAC;AACnE,CAAC","sourcesContent":["import {\n sizeCanvas,\n WaveFrame,\n WaveLook,\n WaveRenderer,\n type Rgb,\n} from '../renderer';\nimport {\n frontRadius,\n hasPreviousSweep,\n sweepPhase,\n WaveGeometry,\n} from '../wave-model';\nimport type {\n LoadingWaveShader,\n ShaderUniforms,\n ShaderUniformValue,\n} from './loading-wave-shader';\nimport {\n linkWaveProgram,\n STANDARD_UNIFORMS,\n StandardUniform,\n WaveShaderError,\n} from './shader-program';\nimport { ECHO_LAG } from './echo';\n\nexport { WaveShaderError };\n\nexport interface WebGlWaveRendererHooks {\n /** the browser took the GPU context away; the renderer draws no more */\n readonly onContextLost: () => void;\n /** called once by dispose(), after the context was freed */\n readonly onDispose?: () => void;\n}\n\n/**\n * Draws the wave with a WebGL2 fragment shader (`LoadingWaveShader`) over a\n * full-canvas triangle, setting the contract's `ws_*` uniforms and the\n * shader's own each frame.\n */\nexport class WebGlWaveRenderer implements WaveRenderer {\n public readonly kind = 'webgl';\n private readonly program: WebGLProgram;\n private readonly uniforms: Record<\n StandardUniform,\n WebGLUniformLocation | null\n >;\n private readonly ownLocations = new Map<\n string,\n WebGLUniformLocation | null\n >();\n private ownUniforms: ShaderUniforms;\n /** uniforms `setShaderUniforms` left out, set to 0 on the next draw */\n private pendingResets: Record<string, ShaderUniformValue> = {};\n private geometry?: WaveGeometry;\n private lost = false;\n private disposed = false;\n\n private readonly handleContextLost = (event: Event): void => {\n event.preventDefault();\n this.lost = true;\n this.hooks.onContextLost();\n };\n\n /** @throws WaveShaderError when the shaders fail on this GPU */\n public constructor(\n public readonly canvas: HTMLCanvasElement,\n private readonly gl: WebGL2RenderingContext,\n private look: WaveLook,\n shader: LoadingWaveShader,\n private readonly hooks: WebGlWaveRendererHooks\n ) {\n this.program = linkWaveProgram(gl, shader);\n this.uniforms = Object.fromEntries(\n STANDARD_UNIFORMS.map((name) => [\n name,\n gl.getUniformLocation(this.program, name),\n ])\n ) as Record<StandardUniform, WebGLUniformLocation | null>;\n this.ownUniforms = shader.uniforms ?? {};\n canvas.addEventListener('webglcontextlost', this.handleContextLost);\n }\n\n /**\n * A WebGL2 context, or `null` where the browser offers none. With\n * `hardwareOnly` also `null` where it would render on the CPU (no GPU, a\n * blocked driver, a software renderer).\n */\n public static createContext(\n canvas: HTMLCanvasElement,\n hardwareOnly = true\n ): WebGL2RenderingContext | null {\n return canvas.getContext('webgl2', {\n alpha: true,\n premultipliedAlpha: true,\n antialias: false,\n depth: false,\n stencil: false,\n powerPreference: 'low-power',\n failIfMajorPerformanceCaveat: hardwareOnly,\n });\n }\n\n public resize(geometry: WaveGeometry, pixelRatio: number): void {\n this.geometry = geometry;\n sizeCanvas(this.canvas, geometry, pixelRatio);\n // a GPU may give a smaller drawing buffer than the canvas asks for\n this.gl.viewport(\n 0,\n 0,\n this.gl.drawingBufferWidth,\n this.gl.drawingBufferHeight\n );\n }\n\n public draw(frame: WaveFrame): void {\n const { gl, geometry } = this;\n if (this.lost || this.disposed || geometry == null) {\n return;\n }\n gl.clearColor(0, 0, 0, 0);\n gl.clear(gl.COLOR_BUFFER_BIT);\n if (frame.dots <= 0) {\n return;\n }\n gl.useProgram(this.program);\n this.setUniforms(geometry, frame);\n gl.drawArrays(gl.TRIANGLES, 0, 3);\n }\n\n public setLook(look: WaveLook): void {\n this.look = look;\n }\n\n /**\n * a uniform the new values leave out goes back to 0, the value GLSL gives\n * every uniform before it is set\n */\n public setShaderUniforms(uniforms: ShaderUniforms | undefined): void {\n const next = uniforms ?? {};\n for (const [name, value] of Object.entries(this.ownUniforms)) {\n if (!(name in next)) {\n this.pendingResets[name] = unsetValue(value);\n }\n }\n this.ownUniforms = next;\n }\n\n public dispose(): void {\n if (this.disposed) {\n return;\n }\n this.disposed = true;\n this.canvas.removeEventListener('webglcontextlost', this.handleContextLost);\n if (!this.gl.isContextLost()) {\n this.gl.deleteProgram(this.program);\n this.gl.getExtension('WEBGL_lose_context')?.loseContext();\n }\n this.hooks.onDispose?.();\n }\n\n private setUniforms(geometry: WaveGeometry, frame: WaveFrame): void {\n const { gl, uniforms: locations, look } = this;\n const bufferWidth = gl.drawingBufferWidth;\n const bufferHeight = gl.drawingBufferHeight;\n gl.uniform2f(locations.ws_size, geometry.width, geometry.height);\n gl.uniform2f(locations.ws_bufferSize, bufferWidth, bufferHeight);\n gl.uniform1f(locations.ws_pixel, geometry.width / Math.max(1, bufferWidth));\n gl.uniform1f(locations.ws_spacing, geometry.dotSpacing);\n gl.uniform1f(locations.ws_dotSize, look.dotSize);\n setColor(gl, locations.ws_color, look.color);\n setColor(gl, locations.ws_accent, look.accent ?? look.color);\n gl.uniform2f(locations.ws_center, geometry.centerX, geometry.centerY);\n gl.uniform1f(\n locations.ws_frontRadius,\n frontRadius(geometry, frame.elapsedMs)\n );\n gl.uniform1f(\n locations.ws_echoRadius,\n echoRadius(geometry, frame.elapsedMs)\n );\n gl.uniform1f(locations.ws_travel, geometry.travel);\n gl.uniform1f(locations.ws_falloff, geometry.falloff);\n gl.uniform1f(locations.ws_rise, geometry.rise);\n gl.uniform1f(locations.ws_tail, geometry.tail);\n gl.uniform1f(locations.ws_maxPush, geometry.maxPush);\n gl.uniform1f(locations.ws_phase, sweepPhase(geometry, frame.elapsedMs));\n gl.uniform1f(locations.ws_time, frame.elapsedMs / 1000);\n gl.uniform1f(\n locations.ws_previousSweep,\n hasPreviousSweep(geometry, frame.elapsedMs) ? 1 : 0\n );\n gl.uniform1f(locations.ws_wave, frame.wave);\n gl.uniform1f(locations.ws_dots, frame.dots);\n // a dropped uniform is reset once, then left alone\n const own = { ...this.pendingResets, ...this.ownUniforms };\n this.pendingResets = {};\n for (const [name, value] of Object.entries(own)) {\n // the ws_ uniforms are the lib's: a shader's own never override them\n if (!name.startsWith('ws_')) {\n setOwnUniform(gl, this.ownLocation(name), value);\n }\n }\n }\n\n /** where the shader's own uniform lives, looked up once per name */\n private ownLocation(name: string): WebGLUniformLocation | null {\n if (!this.ownLocations.has(name)) {\n this.ownLocations.set(\n name,\n this.gl.getUniformLocation(this.program, name)\n );\n }\n return this.ownLocations.get(name) ?? null;\n }\n}\n\n/** zeros of the same size as `value` */\nconst ZEROS: Record<1 | 2 | 3 | 4, ShaderUniformValue> = {\n 1: [0],\n 2: [0, 0],\n 3: [0, 0, 0],\n 4: [0, 0, 0, 0],\n};\n\nfunction unsetValue(value: ShaderUniformValue): ShaderUniformValue {\n return typeof value === 'number' ? 0 : ZEROS[value.length];\n}\n\n/** a colour as a straight RGBA `vec4`, each 0 to 1 */\nfunction setColor(\n gl: WebGL2RenderingContext,\n location: WebGLUniformLocation | null,\n { r, g, b, alpha = 1 }: Rgb\n): void {\n gl.uniform4f(location, r / 255, g / 255, b / 255, alpha);\n}\n\n/** sets a `float` to `vec4` uniform by how many components the value has */\nfunction setOwnUniform(\n gl: WebGL2RenderingContext,\n location: WebGLUniformLocation | null,\n value: ShaderUniformValue\n): void {\n const [x, y = 0, z = 0, w = 0] = typeof value === 'number' ? [value] : value;\n const size = typeof value === 'number' ? 1 : value.length;\n if (size === 1) {\n gl.uniform1f(location, x);\n } else if (size === 2) {\n gl.uniform2f(location, x, y);\n } else if (size === 3) {\n gl.uniform3f(location, x, y, z);\n } else {\n gl.uniform4f(location, x, y, z, w);\n }\n}\n\n/**\n * The echo's radius, trailing the front by `ECHO_LAG` of a sweep; negative,\n * so no echo shows, until the front has run that far.\n */\nexport function echoRadius(geometry: WaveGeometry, elapsedMs: number): number {\n const echoElapsed = elapsedMs - ECHO_LAG * geometry.sweepMs;\n return echoElapsed < 0 ? -1 : frontRadius(geometry, echoElapsed);\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wolsok-loading-wave.js","sourceRoot":"","sources":["../../../../../libs/public/ws-loading-wave/src/wolsok-loading-wave.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,SAAS,CAAC","sourcesContent":["/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"]}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { LoadingWaveDirective } from './lib/loading-wave.directive';
|
|
2
|
+
export { DEFAULT_LOADING_WAVE_OPTIONS, LOADING_WAVE_OPTIONS, provideLoadingWave, } from './lib/loading-wave-options';
|
|
3
|
+
export type { AfterLoading, LoadingWaveOptions, ResolvedLoadingWaveOptions, WaveRendererPreference, } from './lib/loading-wave-options';
|
|
4
|
+
export type { WaveRendererKind } from './lib/renderer';
|
|
5
|
+
export type { LoadingWaveShader, ShaderUniforms, ShaderUniformValue, } from './lib/webgl/loading-wave-shader';
|
|
6
|
+
export { rippleShader } from './lib/webgl/shaders/ripple.shader';
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { WaveFrame, WaveLook, WaveRenderer } from './renderer';
|
|
2
|
+
import { WaveGeometry } from './wave-model';
|
|
3
|
+
/**
|
|
4
|
+
* Draws the wave of the video with the 2D canvas API: dots that brighten,
|
|
5
|
+
* grow and are pushed a little behind the front, and the front's hairline.
|
|
6
|
+
* Runs everywhere, with or without a GPU.
|
|
7
|
+
*/
|
|
8
|
+
export declare class CanvasWaveRenderer implements WaveRenderer {
|
|
9
|
+
readonly canvas: HTMLCanvasElement;
|
|
10
|
+
private readonly context;
|
|
11
|
+
private look;
|
|
12
|
+
readonly kind = "canvas";
|
|
13
|
+
private geometry?;
|
|
14
|
+
private centers;
|
|
15
|
+
private pixelRatio;
|
|
16
|
+
private constructor();
|
|
17
|
+
/** `null` where the canvas has no 2D context */
|
|
18
|
+
static create(canvas: HTMLCanvasElement, look: WaveLook): CanvasWaveRenderer | null;
|
|
19
|
+
resize(geometry: WaveGeometry, pixelRatio: number): void;
|
|
20
|
+
draw(frame: WaveFrame): void;
|
|
21
|
+
setLook(look: WaveLook): void;
|
|
22
|
+
/** a 2D canvas runs no shader */
|
|
23
|
+
setShaderUniforms(): void;
|
|
24
|
+
dispose(): void;
|
|
25
|
+
/** draws every dot at rest in one path, returns the lit ones */
|
|
26
|
+
private drawRestingDots;
|
|
27
|
+
private drawLitDots;
|
|
28
|
+
private drawFrontLine;
|
|
29
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { WaveRendererPreference } from './loading-wave-options';
|
|
2
|
+
import type { WaveLook, WaveRenderer } from './renderer';
|
|
3
|
+
import type { LoadingWaveShader } from './webgl/loading-wave-shader';
|
|
4
|
+
/** counts the waves that hold a WebGL context */
|
|
5
|
+
export declare class GpuContextBudget {
|
|
6
|
+
private readonly max;
|
|
7
|
+
private live;
|
|
8
|
+
constructor(max?: number);
|
|
9
|
+
tryAcquire(): boolean;
|
|
10
|
+
release(): void;
|
|
11
|
+
}
|
|
12
|
+
/** the budget all waves of the page share */
|
|
13
|
+
export declare const PAGE_GPU_BUDGET: GpuContextBudget;
|
|
14
|
+
export interface ChooseWaveRendererParams {
|
|
15
|
+
readonly preference: WaveRendererPreference;
|
|
16
|
+
readonly look: WaveLook;
|
|
17
|
+
/** what the WebGL renderer draws with */
|
|
18
|
+
readonly shader: LoadingWaveShader;
|
|
19
|
+
/** a new, detached canvas for each renderer tried */
|
|
20
|
+
readonly createCanvas: () => HTMLCanvasElement;
|
|
21
|
+
/** the WebGL renderer lost its context; swap it for a 2D one */
|
|
22
|
+
readonly onContextLost: () => void;
|
|
23
|
+
readonly gpuBudget?: GpuContextBudget;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The WebGL renderer where WebGL2 runs (for `'auto'` only on a GPU, for
|
|
27
|
+
* `'webgl'` also in software), the budget allows one more and the shaders
|
|
28
|
+
* compile; the 2D canvas renderer otherwise and always for `'canvas'`.
|
|
29
|
+
* `null` only where not even a 2D context exists.
|
|
30
|
+
*/
|
|
31
|
+
export declare function chooseWaveRenderer(params: ChooseWaveRendererParams): WaveRenderer | null;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Rgb } from './renderer';
|
|
2
|
+
/** the colour the dots take when the host's colour cannot be read */
|
|
3
|
+
export declare const FALLBACK_DOT_COLOR: Rgb;
|
|
4
|
+
/** reads `rgb(…)`/`rgba(…)`, as `getComputedStyle` gives most colours */
|
|
5
|
+
export declare function parseRgbFunction(cssColor: string): Rgb | null;
|
|
6
|
+
/**
|
|
7
|
+
* Turns any computed CSS colour into channels: `rgb()` directly, others
|
|
8
|
+
* (`oklch()`, `color()`, …) by painting one pixel with a 2D canvas.
|
|
9
|
+
*/
|
|
10
|
+
export declare function cssColorToRgb(cssColor: string, createCanvas: () => HTMLCanvasElement): Rgb;
|
|
11
|
+
/**
|
|
12
|
+
* The colour `color` (any CSS colour, variables included) has inside `host`;
|
|
13
|
+
* the host's own `currentColor` without one.
|
|
14
|
+
*/
|
|
15
|
+
export declare function resolveDotColor(host: HTMLElement, color: string | undefined, createCanvas: () => HTMLCanvasElement): Rgb;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { InjectionToken, Provider } from '@angular/core';
|
|
2
|
+
import type { LoadingWaveShader } from './webgl/loading-wave-shader';
|
|
3
|
+
/** what the dots do once loading ended */
|
|
4
|
+
export type AfterLoading = 'hide' | 'keep';
|
|
5
|
+
/** which renderer draws the wave */
|
|
6
|
+
export type WaveRendererPreference = 'auto' | 'webgl' | 'canvas';
|
|
7
|
+
export interface LoadingWaveOptions {
|
|
8
|
+
/**
|
|
9
|
+
* `'hide'`: dots and wave fade out and the effect is removed.
|
|
10
|
+
* `'keep'`: the wave fades out, the dots stay as a quiet background.
|
|
11
|
+
*/
|
|
12
|
+
readonly afterLoading: AfterLoading;
|
|
13
|
+
/** distance between two dots, in CSS pixels */
|
|
14
|
+
readonly dotSpacing: number;
|
|
15
|
+
/** diameter of a dot at rest, in CSS pixels */
|
|
16
|
+
readonly dotSize: number;
|
|
17
|
+
/** any CSS colour; unset, the dots take the host's `currentColor` */
|
|
18
|
+
readonly color?: string;
|
|
19
|
+
/**
|
|
20
|
+
* any CSS colour for the lit dots and the wave itself; unset, they take
|
|
21
|
+
* the dots' colour
|
|
22
|
+
*/
|
|
23
|
+
readonly accentColor?: string;
|
|
24
|
+
/** how long one sweep of the wave across the element takes, in ms */
|
|
25
|
+
readonly sweepMs: number;
|
|
26
|
+
/**
|
|
27
|
+
* `'auto'` draws with a WebGL2 shader on a GPU, `'webgl'` also where the
|
|
28
|
+
* browser renders WebGL in software; both fall back to a 2D canvas where
|
|
29
|
+
* that fails. `'canvas'` never uses WebGL.
|
|
30
|
+
*/
|
|
31
|
+
readonly renderer: WaveRendererPreference;
|
|
32
|
+
/**
|
|
33
|
+
* 0 (a straight front) to 1 (a half circle): how far the wave front's ends
|
|
34
|
+
* lag its middle, as a share of `min(height / 2, width)`; 0.5 when unset
|
|
35
|
+
*/
|
|
36
|
+
readonly frontRoundness?: number;
|
|
37
|
+
/**
|
|
38
|
+
* the fragment shader the WebGL renderer draws with: a built-in one
|
|
39
|
+
* (`rippleShader`) or one's own; unset, `rippleShader`. The 2D canvas
|
|
40
|
+
* always draws the dot wave.
|
|
41
|
+
*/
|
|
42
|
+
readonly shader?: LoadingWaveShader;
|
|
43
|
+
}
|
|
44
|
+
/** options with every default filled in, as the wave runs with them */
|
|
45
|
+
export type ResolvedLoadingWaveOptions = LoadingWaveOptions & Required<Pick<LoadingWaveOptions, 'frontRoundness' | 'shader'>>;
|
|
46
|
+
export declare const DEFAULT_LOADING_WAVE_OPTIONS: ResolvedLoadingWaveOptions;
|
|
47
|
+
export declare const LOADING_WAVE_OPTIONS: InjectionToken<LoadingWaveOptions>;
|
|
48
|
+
/** sets app-wide defaults; the directive's own options still win */
|
|
49
|
+
export declare function provideLoadingWave(options: Partial<LoadingWaveOptions>): Provider;
|
|
50
|
+
/**
|
|
51
|
+
* Merges `overrides` into `defaults`, ignoring `undefined` values,
|
|
52
|
+
* replacing sizes and durations that are no positive finite number and
|
|
53
|
+
* clamping the front's roundness into 0 to 1.
|
|
54
|
+
*/
|
|
55
|
+
export declare function resolveLoadingWaveOptions(defaults: LoadingWaveOptions, overrides?: Partial<LoadingWaveOptions>): ResolvedLoadingWaveOptions;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { ResolvedLoadingWaveOptions } from './loading-wave-options';
|
|
2
|
+
import type { WaveRendererKind } from './renderer';
|
|
3
|
+
import type { WaveEnvironment } from './wave-environment';
|
|
4
|
+
/**
|
|
5
|
+
* The most device pixels a wave's canvas holds: a tall host (a whole page)
|
|
6
|
+
* gets a lower pixel ratio rather than a canvas of hundreds of megabytes.
|
|
7
|
+
*/
|
|
8
|
+
export declare const MAX_CANVAS_PIXELS = 4000000;
|
|
9
|
+
/** the device pixel ratio, lowered where the canvas would grow too large */
|
|
10
|
+
export declare function fitPixelRatio(width: number, height: number, pixelRatio: number): number;
|
|
11
|
+
/**
|
|
12
|
+
* The loading wave of one host element, without Angular: puts a canvas
|
|
13
|
+
* behind the host's content while loading, runs the animation and removes
|
|
14
|
+
* it again (`'hide'`) or leaves the resting dots (`'keep'`).
|
|
15
|
+
*/
|
|
16
|
+
export declare class LoadingWave {
|
|
17
|
+
private readonly host;
|
|
18
|
+
private options;
|
|
19
|
+
private readonly env;
|
|
20
|
+
private readonly onRendererChange;
|
|
21
|
+
private renderer?;
|
|
22
|
+
private timeline?;
|
|
23
|
+
private frameId?;
|
|
24
|
+
private visible;
|
|
25
|
+
private loading;
|
|
26
|
+
private disconnects;
|
|
27
|
+
private hostStyle?;
|
|
28
|
+
constructor(host: HTMLElement, options: ResolvedLoadingWaveOptions, env: WaveEnvironment, onRendererChange?: (kind: WaveRendererKind | null) => void);
|
|
29
|
+
setLoading(loading: boolean): void;
|
|
30
|
+
/**
|
|
31
|
+
* applies new options: look, spacing and speed in place; a new renderer or
|
|
32
|
+
* `afterLoading` starts a running effect over, and resting dots stay where
|
|
33
|
+
* the new options keep them
|
|
34
|
+
*/
|
|
35
|
+
setOptions(options: ResolvedLoadingWaveOptions): void;
|
|
36
|
+
dispose(): void;
|
|
37
|
+
private mount;
|
|
38
|
+
private unmount;
|
|
39
|
+
private createRenderer;
|
|
40
|
+
/** the GPU took the WebGL context away: go on with a 2D canvas */
|
|
41
|
+
private fallBackToCanvas;
|
|
42
|
+
private look;
|
|
43
|
+
private resize;
|
|
44
|
+
private recolor;
|
|
45
|
+
private setVisible;
|
|
46
|
+
private scheduleFrame;
|
|
47
|
+
private cancelFrame;
|
|
48
|
+
private drawFrame;
|
|
49
|
+
/**
|
|
50
|
+
* `isolation` keeps the canvas's `z-index: -1` above the host's own
|
|
51
|
+
* background; a static host becomes `relative` to hold the canvas.
|
|
52
|
+
*/
|
|
53
|
+
private styleHost;
|
|
54
|
+
private restoreHost;
|
|
55
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { InjectionToken } from '@angular/core';
|
|
2
|
+
import { LoadingWaveOptions } from './loading-wave-options';
|
|
3
|
+
import type { WaveRendererKind } from './renderer';
|
|
4
|
+
import { WaveEnvironment } from './wave-environment';
|
|
5
|
+
import * as i0 from "@angular/core";
|
|
6
|
+
/** the browser APIs the wave runs on; specs replace them */
|
|
7
|
+
export declare const LOADING_WAVE_ENVIRONMENT: InjectionToken<WaveEnvironment>;
|
|
8
|
+
/**
|
|
9
|
+
* Lays a calm loading wave behind the host's content: a grid of dots that a
|
|
10
|
+
* curved wave front lights again and again while `wsLoadingWave` is true.
|
|
11
|
+
*
|
|
12
|
+
* ```html
|
|
13
|
+
* <section [wsLoadingWave]="loading()" [wsLoadingWaveOptions]="{ afterLoading: 'keep' }">
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* Runs only in the browser, never on the server.
|
|
17
|
+
*/
|
|
18
|
+
export declare class LoadingWaveDirective {
|
|
19
|
+
/** whether the host is loading */
|
|
20
|
+
readonly wsLoadingWave: import("@angular/core").InputSignalWithTransform<boolean, unknown>;
|
|
21
|
+
/** overrides the app-wide options (`provideLoadingWave`) for this host */
|
|
22
|
+
readonly wsLoadingWaveOptions: import("@angular/core").InputSignal<Partial<LoadingWaveOptions> | undefined>;
|
|
23
|
+
private readonly activeRendererState;
|
|
24
|
+
/** what draws the effect right now, `null` while nothing shows */
|
|
25
|
+
readonly activeRenderer: import("@angular/core").Signal<WaveRendererKind | null>;
|
|
26
|
+
constructor();
|
|
27
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<LoadingWaveDirective, never>;
|
|
28
|
+
static ɵdir: i0.ɵɵDirectiveDeclaration<LoadingWaveDirective, "[wsLoadingWave]", ["wsLoadingWave"], { "wsLoadingWave": { "alias": "wsLoadingWave"; "required": false; "isSignal": true; }; "wsLoadingWaveOptions": { "alias": "wsLoadingWaveOptions"; "required": false; "isSignal": true; }; }, {}, never, never, true, never>;
|
|
29
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { ShaderUniforms } from './webgl/loading-wave-shader';
|
|
2
|
+
import type { WaveGeometry } from './wave-model';
|
|
3
|
+
export type WaveRendererKind = 'webgl' | 'canvas';
|
|
4
|
+
/** a colour channel by channel, each 0 to 255 */
|
|
5
|
+
export interface Rgb {
|
|
6
|
+
readonly r: number;
|
|
7
|
+
readonly g: number;
|
|
8
|
+
readonly b: number;
|
|
9
|
+
/** the colour's own opacity, 0 to 1; 1 when unset */
|
|
10
|
+
readonly alpha?: number;
|
|
11
|
+
}
|
|
12
|
+
/** the frame's grid opacity times the colour's own */
|
|
13
|
+
export declare function dotsOpacity(frame: WaveFrame, color: Rgb): number;
|
|
14
|
+
/** how the dots look, the same in both renderers */
|
|
15
|
+
export interface WaveLook {
|
|
16
|
+
/** diameter of a dot at rest, in CSS pixels */
|
|
17
|
+
readonly dotSize: number;
|
|
18
|
+
readonly color: Rgb;
|
|
19
|
+
/** the colour lit dots and the wave take; the dots' colour when unset */
|
|
20
|
+
readonly accent?: Rgb;
|
|
21
|
+
}
|
|
22
|
+
/** what one frame shows */
|
|
23
|
+
export interface WaveFrame {
|
|
24
|
+
/** time since the wave started, drives where the front is */
|
|
25
|
+
readonly elapsedMs: number;
|
|
26
|
+
/** strength of the wave, 0 (no wave) to 1, for fading it in and out */
|
|
27
|
+
readonly wave: number;
|
|
28
|
+
/** opacity of the whole dot grid, 0 to 1, for fading it in and out */
|
|
29
|
+
readonly dots: number;
|
|
30
|
+
}
|
|
31
|
+
/** opacity of a dot at rest, times the colour's */
|
|
32
|
+
export declare const REST_ALPHA = 0.22;
|
|
33
|
+
/** opacity of a dot right behind the front */
|
|
34
|
+
export declare const LIT_ALPHA = 0.9;
|
|
35
|
+
/** how much a dot right behind the front grows, 0.8 = 80% larger */
|
|
36
|
+
export declare const LIT_GROWTH = 0.8;
|
|
37
|
+
/** opacity of the hairline along the front */
|
|
38
|
+
export declare const FRONT_LINE_ALPHA = 0.14;
|
|
39
|
+
/** draws the dot grid and the wave into its own canvas */
|
|
40
|
+
export interface WaveRenderer {
|
|
41
|
+
readonly kind: WaveRendererKind;
|
|
42
|
+
readonly canvas: HTMLCanvasElement;
|
|
43
|
+
/** sizes the canvas to the geometry at the given device pixel ratio */
|
|
44
|
+
resize(geometry: WaveGeometry, pixelRatio: number): void;
|
|
45
|
+
draw(frame: WaveFrame): void;
|
|
46
|
+
/** changes the dots' look, say after the theme changed the host's colour */
|
|
47
|
+
setLook(look: WaveLook): void;
|
|
48
|
+
/** changes the values of a shader's own uniforms; only WebGL has any */
|
|
49
|
+
setShaderUniforms(uniforms: ShaderUniforms | undefined): void;
|
|
50
|
+
/** frees what the renderer holds; the canvas stays where it is */
|
|
51
|
+
dispose(): void;
|
|
52
|
+
}
|
|
53
|
+
/** sets the canvas's backing store to the CSS size times the pixel ratio */
|
|
54
|
+
export declare function sizeCanvas(canvas: HTMLCanvasElement, geometry: WaveGeometry, pixelRatio: number): void;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ChooseWaveRendererParams } from './choose-wave-renderer';
|
|
2
|
+
import type { Rgb, WaveRenderer } from './renderer';
|
|
3
|
+
/** stops an observation */
|
|
4
|
+
export type Disconnect = () => void;
|
|
5
|
+
/** what the loading wave takes from the browser, replaceable in specs */
|
|
6
|
+
export interface WaveEnvironment {
|
|
7
|
+
now(): number;
|
|
8
|
+
requestFrame(callback: () => void): number;
|
|
9
|
+
cancelFrame(id: number): void;
|
|
10
|
+
pixelRatio(): number;
|
|
11
|
+
prefersReducedMotion(): boolean;
|
|
12
|
+
createCanvas(): HTMLCanvasElement;
|
|
13
|
+
chooseRenderer(params: ChooseWaveRendererParams): WaveRenderer | null;
|
|
14
|
+
resolveColor(host: HTMLElement, color: string | undefined): Rgb;
|
|
15
|
+
/** calls back when the element's size changed */
|
|
16
|
+
observeSize(element: HTMLElement, onResize: () => void): Disconnect;
|
|
17
|
+
/** calls back when the element enters or leaves the viewport */
|
|
18
|
+
observeVisibility(element: HTMLElement, onChange: (visible: boolean) => void): Disconnect;
|
|
19
|
+
/** calls back when the page's theme may have changed the colours */
|
|
20
|
+
observeTheme(onChange: () => void): Disconnect;
|
|
21
|
+
}
|
|
22
|
+
/** the real browser, tolerant of the APIs a browser (or jsdom) lacks */
|
|
23
|
+
export declare function browserWaveEnvironment(document: Document): WaveEnvironment;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The loading wave as pure math, shared by the 2D canvas renderer and
|
|
3
|
+
* mirrored by the WebGL shaders' header (`webgl/shader-program.ts`).
|
|
4
|
+
*
|
|
5
|
+
* The wave front is an arc of a circle whose centre lies left of the
|
|
6
|
+
* element, at mid-height: a ripple from off-screen. How round it is
|
|
7
|
+
* (`frontRoundness`) sets the circle's radius. Its radius grows at an even
|
|
8
|
+
* speed, so the front sweeps from the left edge until the lit tail behind
|
|
9
|
+
* it has left the element at every row, then starts again.
|
|
10
|
+
* Every length is in CSS pixels, every time in milliseconds.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* How round the front is by default: its ends lag its middle by half of
|
|
14
|
+
* `min(height / 2, width)`.
|
|
15
|
+
*/
|
|
16
|
+
export declare const DEFAULT_FRONT_ROUNDNESS = 0.5;
|
|
17
|
+
/** how lit a dot stays once the front passed it, as in the video */
|
|
18
|
+
export declare const AFTERGLOW = 0.45;
|
|
19
|
+
/** share of a sweep after which a passed dot's afterglow starts to fade */
|
|
20
|
+
export declare const AFTERGLOW_FADE_START = 0.45;
|
|
21
|
+
/**
|
|
22
|
+
* The most dots a grid holds: a tiny spacing on a large host is widened
|
|
23
|
+
* rather than creating millions of dots each frame.
|
|
24
|
+
*/
|
|
25
|
+
export declare const MAX_DOTS = 20000;
|
|
26
|
+
export interface WaveGeometry {
|
|
27
|
+
readonly width: number;
|
|
28
|
+
readonly height: number;
|
|
29
|
+
readonly dotSpacing: number;
|
|
30
|
+
readonly sweepMs: number;
|
|
31
|
+
readonly centerX: number;
|
|
32
|
+
readonly centerY: number;
|
|
33
|
+
/** the front's radius at the start of a sweep: it touches the left edge */
|
|
34
|
+
readonly startRadius: number;
|
|
35
|
+
/** the front's radius at the end of a sweep: the tail left every row */
|
|
36
|
+
readonly endRadius: number;
|
|
37
|
+
/** how far the front's radius grows in one sweep */
|
|
38
|
+
readonly travel: number;
|
|
39
|
+
/** distance behind the front over which a dot dims to 1/e */
|
|
40
|
+
readonly falloff: number;
|
|
41
|
+
/** distance ahead of the front over which a dot lights up */
|
|
42
|
+
readonly rise: number;
|
|
43
|
+
/** length of the lit tail behind the front */
|
|
44
|
+
readonly tail: number;
|
|
45
|
+
/** the farthest a lit dot is pushed along the wave */
|
|
46
|
+
readonly maxPush: number;
|
|
47
|
+
}
|
|
48
|
+
export interface WaveGeometryOptions {
|
|
49
|
+
readonly dotSpacing: number;
|
|
50
|
+
readonly sweepMs: number;
|
|
51
|
+
/**
|
|
52
|
+
* 0 (a straight front) to 1: how far the front's ends lag its middle, as a
|
|
53
|
+
* share of `min(height / 2, width)`; `DEFAULT_FRONT_ROUNDNESS` when unset
|
|
54
|
+
*/
|
|
55
|
+
readonly frontRoundness?: number;
|
|
56
|
+
}
|
|
57
|
+
export interface Point {
|
|
58
|
+
readonly x: number;
|
|
59
|
+
readonly y: number;
|
|
60
|
+
}
|
|
61
|
+
export interface Displacement {
|
|
62
|
+
readonly dx: number;
|
|
63
|
+
readonly dy: number;
|
|
64
|
+
}
|
|
65
|
+
export declare function waveGeometry(width: number, height: number, { dotSpacing: askedSpacing, sweepMs, frontRoundness, }: WaveGeometryOptions): WaveGeometry;
|
|
66
|
+
/** the share of the current sweep that has passed, 0 up to (not including) 1 */
|
|
67
|
+
export declare function sweepPhase(geometry: WaveGeometry, elapsedMs: number): number;
|
|
68
|
+
/** the front's radius at the time `elapsedMs` */
|
|
69
|
+
export declare function frontRadius(geometry: WaveGeometry, elapsedMs: number): number;
|
|
70
|
+
/** whether a sweep went before this one, whose afterglow and echo show */
|
|
71
|
+
export declare function hasPreviousSweep(geometry: WaveGeometry, elapsedMs: number): boolean;
|
|
72
|
+
/**
|
|
73
|
+
* How much the wave lights the dot at `(x, y)`: 1 right behind the front,
|
|
74
|
+
* dimming over a few spacings to the afterglow of the dots it passed, which
|
|
75
|
+
* fades out again until the next front comes. On the first sweep nothing
|
|
76
|
+
* ahead of the front is lit.
|
|
77
|
+
*/
|
|
78
|
+
export declare function waveIntensity(x: number, y: number, elapsedMs: number, geometry: WaveGeometry): number;
|
|
79
|
+
/**
|
|
80
|
+
* How far the wave pushes the dot at `(x, y)` away from the circle's centre:
|
|
81
|
+
* nothing at rest, most at one falloff behind the front.
|
|
82
|
+
*/
|
|
83
|
+
export declare function dotDisplacement(x: number, y: number, elapsedMs: number, geometry: WaveGeometry): Displacement;
|
|
84
|
+
/** the centres of the grid's dots, row by row, each centred in its cell */
|
|
85
|
+
export declare function dotCenters({ width, height, dotSpacing, }: Pick<WaveGeometry, 'width' | 'height' | 'dotSpacing'>): Point[];
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { AfterLoading } from './loading-wave-options';
|
|
2
|
+
import type { WaveFrame } from './renderer';
|
|
3
|
+
/** how long the wave and the dots take to fade in or out */
|
|
4
|
+
export declare const FADE_MS = 400;
|
|
5
|
+
export interface TimelineFrame {
|
|
6
|
+
readonly frame: WaveFrame;
|
|
7
|
+
/** frames stay the same from now on until loading changes again */
|
|
8
|
+
readonly settled: boolean;
|
|
9
|
+
/** `'hide'` faded out completely: the effect can go */
|
|
10
|
+
readonly finished: boolean;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* What the effect shows over time, as loading starts and stops: the wave
|
|
14
|
+
* sweeps from the left edge each time loading starts; wave and dots fade in
|
|
15
|
+
* and out over `FADE_MS`, starting from where they are. Without motion
|
|
16
|
+
* (`prefers-reduced-motion`) the wave never shows.
|
|
17
|
+
*/
|
|
18
|
+
export declare class WaveTimeline {
|
|
19
|
+
private readonly afterLoading;
|
|
20
|
+
private readonly motion;
|
|
21
|
+
private loading;
|
|
22
|
+
private sweepStartedAt;
|
|
23
|
+
private wave;
|
|
24
|
+
private dots;
|
|
25
|
+
/**
|
|
26
|
+
* @param restingDots opacity the dots start with before loading: 1 to show
|
|
27
|
+
* the dots `'keep'` left, say after the options changed
|
|
28
|
+
*/
|
|
29
|
+
constructor(afterLoading: AfterLoading, motion: boolean, restingDots?: number);
|
|
30
|
+
start(now: number): void;
|
|
31
|
+
stop(now: number): void;
|
|
32
|
+
frameAt(now: number): TimelineFrame;
|
|
33
|
+
}
|