@wolsok/loading-wave 0.0.0-stage → 0.0.1

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.
Files changed (45) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +95 -2
  3. package/esm2022/index.js +6 -0
  4. package/esm2022/index.js.map +1 -0
  5. package/esm2022/lib/canvas-wave-renderer.js +103 -0
  6. package/esm2022/lib/canvas-wave-renderer.js.map +1 -0
  7. package/esm2022/lib/choose-wave-renderer.js +61 -0
  8. package/esm2022/lib/choose-wave-renderer.js.map +1 -0
  9. package/esm2022/lib/dot-color.js +63 -0
  10. package/esm2022/lib/dot-color.js.map +1 -0
  11. package/esm2022/lib/loading-wave-options.js +34 -0
  12. package/esm2022/lib/loading-wave-options.js.map +1 -0
  13. package/esm2022/lib/loading-wave.directive.js +74 -0
  14. package/esm2022/lib/loading-wave.directive.js.map +1 -0
  15. package/esm2022/lib/loading-wave.js +230 -0
  16. package/esm2022/lib/loading-wave.js.map +1 -0
  17. package/esm2022/lib/renderer.js +18 -0
  18. package/esm2022/lib/renderer.js.map +1 -0
  19. package/esm2022/lib/wave-environment.js +59 -0
  20. package/esm2022/lib/wave-environment.js.map +1 -0
  21. package/esm2022/lib/wave-model.js +147 -0
  22. package/esm2022/lib/wave-model.js.map +1 -0
  23. package/esm2022/lib/wave-timeline.js +61 -0
  24. package/esm2022/lib/wave-timeline.js.map +1 -0
  25. package/esm2022/lib/webgl/wave-shaders.js +114 -0
  26. package/esm2022/lib/webgl/wave-shaders.js.map +1 -0
  27. package/esm2022/lib/webgl/webgl-wave-renderer.js +172 -0
  28. package/esm2022/lib/webgl/webgl-wave-renderer.js.map +1 -0
  29. package/esm2022/wolsok-loading-wave.js +5 -0
  30. package/esm2022/wolsok-loading-wave.js.map +1 -0
  31. package/index.d.ts +4 -0
  32. package/lib/canvas-wave-renderer.d.ts +27 -0
  33. package/lib/choose-wave-renderer.d.ts +28 -0
  34. package/lib/dot-color.d.ts +15 -0
  35. package/lib/loading-wave-options.d.ts +35 -0
  36. package/lib/loading-wave.d.ts +55 -0
  37. package/lib/loading-wave.directive.d.ts +29 -0
  38. package/lib/renderer.d.ts +49 -0
  39. package/lib/wave-environment.d.ts +23 -0
  40. package/lib/wave-model.d.ts +74 -0
  41. package/lib/wave-timeline.d.ts +33 -0
  42. package/lib/webgl/wave-shaders.d.ts +14 -0
  43. package/lib/webgl/webgl-wave-renderer.d.ts +47 -0
  44. package/package.json +44 -3
  45. package/wolsok-loading-wave.d.ts +5 -0
@@ -0,0 +1,114 @@
1
+ import { FRONT_LINE_ALPHA, LIT_ALPHA, LIT_GROWTH, REST_ALPHA, } from '../renderer';
2
+ import { AFTERGLOW, AFTERGLOW_FADE_START } from '../wave-model';
3
+ /** strength of the echo ripple trailing the front, times the main front's */
4
+ export const ECHO_STRENGTH = 0.4;
5
+ /** the echo trails the front by this share of a sweep */
6
+ export const ECHO_LAG = 1 / 3;
7
+ /** opacity of the glow around a dot right behind the front */
8
+ const GLOW_ALPHA = 0.18;
9
+ /** width of the glow, in dot spacings */
10
+ const GLOW_SPACINGS = 0.18;
11
+ const glsl = (value) => value.toFixed(5);
12
+ /** one triangle that covers the whole canvas, no vertex buffer needed */
13
+ export const WAVE_VERTEX_SHADER = `#version 300 es
14
+ void main() {
15
+ vec2 corner = vec2(float((gl_VertexID << 1) & 2), float(gl_VertexID & 2));
16
+ gl_Position = vec4(corner * 2.0 - 1.0, 0.0, 1.0);
17
+ }
18
+ `;
19
+ /**
20
+ * The wave model of `wave-model.ts` (front glow, afterglow, push) per pixel, for every dot of the 3 × 3
21
+ * cells around it: brightness, growth and push behind the front, plus what
22
+ * the 2D canvas cannot afford: a glow around lit dots and an echo ripple
23
+ * trailing the front. Lengths are in CSS pixels, y grows downwards as in CSS.
24
+ * A negative `u_echoRadius` shows no echo, as before the echo's first sweep.
25
+ */
26
+ export const WAVE_FRAGMENT_SHADER = `#version 300 es
27
+ precision highp float;
28
+
29
+ uniform vec2 u_size;
30
+ uniform vec2 u_bufferSize;
31
+ uniform float u_spacing;
32
+ uniform float u_dotSize;
33
+ uniform vec3 u_color;
34
+ uniform vec2 u_center;
35
+ uniform float u_frontRadius;
36
+ uniform float u_echoRadius;
37
+ uniform float u_travel;
38
+ uniform float u_previousSweep;
39
+ uniform float u_falloff;
40
+ uniform float u_rise;
41
+ uniform float u_maxPush;
42
+ uniform float u_wave;
43
+ uniform float u_dots;
44
+
45
+ out vec4 outColor;
46
+
47
+ const float REST_ALPHA = ${glsl(REST_ALPHA)};
48
+ const float LIT_ALPHA = ${glsl(LIT_ALPHA)};
49
+ const float LIT_GROWTH = ${glsl(LIT_GROWTH)};
50
+ const float FRONT_LINE_ALPHA = ${glsl(FRONT_LINE_ALPHA)};
51
+ const float ECHO_STRENGTH = ${glsl(ECHO_STRENGTH)};
52
+ const float GLOW_ALPHA = ${glsl(GLOW_ALPHA)};
53
+ const float GLOW_SPACINGS = ${glsl(GLOW_SPACINGS)};
54
+ const float AFTERGLOW = ${glsl(AFTERGLOW)};
55
+ const float AFTERGLOW_FADE_START = ${glsl(AFTERGLOW_FADE_START)};
56
+
57
+ float frontGlow(float behind) {
58
+ return smoothstep(-u_rise, 0.0, behind) * exp(-max(behind, 0.0) / u_falloff);
59
+ }
60
+
61
+ float afterglow(float behind) {
62
+ if (behind < 0.0 && u_previousSweep < 0.5) {
63
+ return 0.0;
64
+ }
65
+ float sincePassed = (behind >= 0.0 ? behind : behind + u_travel) / u_travel;
66
+ return AFTERGLOW * (1.0 - smoothstep(AFTERGLOW_FADE_START, 1.0, sincePassed));
67
+ }
68
+
69
+ float pushAt(float behind) {
70
+ if (behind <= 0.0) {
71
+ return 0.0;
72
+ }
73
+ float scaled = behind / u_falloff;
74
+ return u_maxPush * scaled * exp(1.0 - scaled);
75
+ }
76
+
77
+ void main() {
78
+ // the drawing buffer may be smaller than asked for: map it onto the host
79
+ vec2 cssPerPixel = u_size / u_bufferSize;
80
+ vec2 point = vec2(gl_FragCoord.x, u_bufferSize.y - gl_FragCoord.y) * cssPerPixel;
81
+ vec2 cell = floor(point / u_spacing);
82
+ float antialias = 0.5 * cssPerPixel.x;
83
+ float glowWidth = GLOW_SPACINGS * u_spacing;
84
+ float alpha = 0.0;
85
+ for (int row = -1; row <= 1; row++) {
86
+ for (int column = -1; column <= 1; column++) {
87
+ vec2 center = (cell + vec2(float(column), float(row)) + 0.5) * u_spacing;
88
+ if (center.x < 0.0 || center.y < 0.0 || center.x >= u_size.x || center.y >= u_size.y) {
89
+ continue;
90
+ }
91
+ vec2 fromWaveCenter = center - u_center;
92
+ float waveDistance = length(fromWaveCenter);
93
+ float behindFront = u_frontRadius - waveDistance;
94
+ float behindEcho = u_echoRadius - waveDistance;
95
+ float fronts = frontGlow(behindFront) + ECHO_STRENGTH * frontGlow(behindEcho);
96
+ float lit = u_wave * clamp(max(fronts, afterglow(behindFront)), 0.0, 1.0);
97
+ float push = u_wave * (pushAt(behindFront) + ECHO_STRENGTH * pushAt(behindEcho));
98
+ vec2 dotCenter = center + push * fromWaveCenter / waveDistance;
99
+ float radius = 0.5 * u_dotSize * (1.0 + LIT_GROWTH * lit);
100
+ float fromDot = length(point - dotCenter);
101
+ float core = 1.0 - smoothstep(radius - antialias, radius + antialias, fromDot);
102
+ // only the fronts glow, the afterglow stays calm
103
+ float glow = GLOW_ALPHA * u_wave * clamp(fronts, 0.0, 1.0)
104
+ * exp(-fromDot * fromDot / (2.0 * glowWidth * glowWidth));
105
+ alpha += core * (REST_ALPHA + (LIT_ALPHA - REST_ALPHA) * lit) + glow;
106
+ }
107
+ }
108
+ float fromFront = abs(length(point - u_center) - u_frontRadius);
109
+ alpha += FRONT_LINE_ALPHA * u_wave * (1.0 - smoothstep(0.0, cssPerPixel.x, fromFront));
110
+ alpha = clamp(alpha, 0.0, 1.0) * u_dots;
111
+ outColor = vec4(u_color * alpha, alpha);
112
+ }
113
+ `;
114
+ //# sourceMappingURL=wave-shaders.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wave-shaders.js","sourceRoot":"","sources":["../../../../../../../libs/public/ws-loading-wave/src/lib/webgl/wave-shaders.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAChB,SAAS,EACT,UAAU,EACV,UAAU,GACX,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,SAAS,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AAEhE,6EAA6E;AAC7E,MAAM,CAAC,MAAM,aAAa,GAAG,GAAG,CAAC;AACjC,yDAAyD;AACzD,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,GAAG,CAAC,CAAC;AAC9B,8DAA8D;AAC9D,MAAM,UAAU,GAAG,IAAI,CAAC;AACxB,yCAAyC;AACzC,MAAM,aAAa,GAAG,IAAI,CAAC;AAE3B,MAAM,IAAI,GAAG,CAAC,KAAa,EAAU,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;AAEzD,yEAAyE;AACzE,MAAM,CAAC,MAAM,kBAAkB,GAAG;;;;;CAKjC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG;;;;;;;;;;;;;;;;;;;;;2BAqBT,IAAI,CAAC,UAAU,CAAC;0BACjB,IAAI,CAAC,SAAS,CAAC;2BACd,IAAI,CAAC,UAAU,CAAC;iCACV,IAAI,CAAC,gBAAgB,CAAC;8BACzB,IAAI,CAAC,aAAa,CAAC;2BACtB,IAAI,CAAC,UAAU,CAAC;8BACb,IAAI,CAAC,aAAa,CAAC;0BACvB,IAAI,CAAC,SAAS,CAAC;qCACJ,IAAI,CAAC,oBAAoB,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA0D9D,CAAC","sourcesContent":["import {\n FRONT_LINE_ALPHA,\n LIT_ALPHA,\n LIT_GROWTH,\n REST_ALPHA,\n} from '../renderer';\nimport { AFTERGLOW, AFTERGLOW_FADE_START } from '../wave-model';\n\n/** strength of the echo ripple trailing the front, times the main front's */\nexport const ECHO_STRENGTH = 0.4;\n/** the echo trails the front by this share of a sweep */\nexport const ECHO_LAG = 1 / 3;\n/** opacity of the glow around a dot right behind the front */\nconst GLOW_ALPHA = 0.18;\n/** width of the glow, in dot spacings */\nconst GLOW_SPACINGS = 0.18;\n\nconst glsl = (value: number): string => value.toFixed(5);\n\n/** one triangle that covers the whole canvas, no vertex buffer needed */\nexport const WAVE_VERTEX_SHADER = `#version 300 es\nvoid main() {\n vec2 corner = vec2(float((gl_VertexID << 1) & 2), float(gl_VertexID & 2));\n gl_Position = vec4(corner * 2.0 - 1.0, 0.0, 1.0);\n}\n`;\n\n/**\n * The wave model of `wave-model.ts` (front glow, afterglow, push) per pixel, for every dot of the 3 × 3\n * cells around it: brightness, growth and push behind the front, plus what\n * the 2D canvas cannot afford: a glow around lit dots and an echo ripple\n * trailing the front. Lengths are in CSS pixels, y grows downwards as in CSS.\n * A negative `u_echoRadius` shows no echo, as before the echo's first sweep.\n */\nexport const WAVE_FRAGMENT_SHADER = `#version 300 es\nprecision highp float;\n\nuniform vec2 u_size;\nuniform vec2 u_bufferSize;\nuniform float u_spacing;\nuniform float u_dotSize;\nuniform vec3 u_color;\nuniform vec2 u_center;\nuniform float u_frontRadius;\nuniform float u_echoRadius;\nuniform float u_travel;\nuniform float u_previousSweep;\nuniform float u_falloff;\nuniform float u_rise;\nuniform float u_maxPush;\nuniform float u_wave;\nuniform float u_dots;\n\nout vec4 outColor;\n\nconst float REST_ALPHA = ${glsl(REST_ALPHA)};\nconst float LIT_ALPHA = ${glsl(LIT_ALPHA)};\nconst float LIT_GROWTH = ${glsl(LIT_GROWTH)};\nconst float FRONT_LINE_ALPHA = ${glsl(FRONT_LINE_ALPHA)};\nconst float ECHO_STRENGTH = ${glsl(ECHO_STRENGTH)};\nconst float GLOW_ALPHA = ${glsl(GLOW_ALPHA)};\nconst float GLOW_SPACINGS = ${glsl(GLOW_SPACINGS)};\nconst float AFTERGLOW = ${glsl(AFTERGLOW)};\nconst float AFTERGLOW_FADE_START = ${glsl(AFTERGLOW_FADE_START)};\n\nfloat frontGlow(float behind) {\n return smoothstep(-u_rise, 0.0, behind) * exp(-max(behind, 0.0) / u_falloff);\n}\n\nfloat afterglow(float behind) {\n if (behind < 0.0 && u_previousSweep < 0.5) {\n return 0.0;\n }\n float sincePassed = (behind >= 0.0 ? behind : behind + u_travel) / u_travel;\n return AFTERGLOW * (1.0 - smoothstep(AFTERGLOW_FADE_START, 1.0, sincePassed));\n}\n\nfloat pushAt(float behind) {\n if (behind <= 0.0) {\n return 0.0;\n }\n float scaled = behind / u_falloff;\n return u_maxPush * scaled * exp(1.0 - scaled);\n}\n\nvoid main() {\n // the drawing buffer may be smaller than asked for: map it onto the host\n vec2 cssPerPixel = u_size / u_bufferSize;\n vec2 point = vec2(gl_FragCoord.x, u_bufferSize.y - gl_FragCoord.y) * cssPerPixel;\n vec2 cell = floor(point / u_spacing);\n float antialias = 0.5 * cssPerPixel.x;\n float glowWidth = GLOW_SPACINGS * u_spacing;\n float alpha = 0.0;\n for (int row = -1; row <= 1; row++) {\n for (int column = -1; column <= 1; column++) {\n vec2 center = (cell + vec2(float(column), float(row)) + 0.5) * u_spacing;\n if (center.x < 0.0 || center.y < 0.0 || center.x >= u_size.x || center.y >= u_size.y) {\n continue;\n }\n vec2 fromWaveCenter = center - u_center;\n float waveDistance = length(fromWaveCenter);\n float behindFront = u_frontRadius - waveDistance;\n float behindEcho = u_echoRadius - waveDistance;\n float fronts = frontGlow(behindFront) + ECHO_STRENGTH * frontGlow(behindEcho);\n float lit = u_wave * clamp(max(fronts, afterglow(behindFront)), 0.0, 1.0);\n float push = u_wave * (pushAt(behindFront) + ECHO_STRENGTH * pushAt(behindEcho));\n vec2 dotCenter = center + push * fromWaveCenter / waveDistance;\n float radius = 0.5 * u_dotSize * (1.0 + LIT_GROWTH * lit);\n float fromDot = length(point - dotCenter);\n float core = 1.0 - smoothstep(radius - antialias, radius + antialias, fromDot);\n // only the fronts glow, the afterglow stays calm\n float glow = GLOW_ALPHA * u_wave * clamp(fronts, 0.0, 1.0)\n * exp(-fromDot * fromDot / (2.0 * glowWidth * glowWidth));\n alpha += core * (REST_ALPHA + (LIT_ALPHA - REST_ALPHA) * lit) + glow;\n }\n }\n float fromFront = abs(length(point - u_center) - u_frontRadius);\n alpha += FRONT_LINE_ALPHA * u_wave * (1.0 - smoothstep(0.0, cssPerPixel.x, fromFront));\n alpha = clamp(alpha, 0.0, 1.0) * u_dots;\n outColor = vec4(u_color * alpha, alpha);\n}\n`;\n"]}
@@ -0,0 +1,172 @@
1
+ import { dotsOpacity, sizeCanvas, } from '../renderer';
2
+ import { frontRadius, hasPreviousSweep } from '../wave-model';
3
+ import { ECHO_LAG, WAVE_FRAGMENT_SHADER, WAVE_VERTEX_SHADER, } from './wave-shaders';
4
+ const UNIFORMS = [
5
+ 'u_size',
6
+ 'u_bufferSize',
7
+ 'u_spacing',
8
+ 'u_dotSize',
9
+ 'u_color',
10
+ 'u_center',
11
+ 'u_frontRadius',
12
+ 'u_echoRadius',
13
+ 'u_travel',
14
+ 'u_previousSweep',
15
+ 'u_falloff',
16
+ 'u_rise',
17
+ 'u_maxPush',
18
+ 'u_wave',
19
+ 'u_dots',
20
+ ];
21
+ /** the shaders did not compile or link on this GPU */
22
+ export class WaveShaderError extends Error {
23
+ constructor() {
24
+ super(...arguments);
25
+ this.name = 'WaveShaderError';
26
+ }
27
+ }
28
+ /**
29
+ * Draws the wave with a WebGL2 fragment shader: the 2D canvas's look plus a
30
+ * glow around lit dots and an echo ripple trailing the front.
31
+ */
32
+ export class WebGlWaveRenderer {
33
+ /** @throws WaveShaderError when the shaders fail on this GPU */
34
+ constructor(canvas, gl, look, hooks) {
35
+ this.canvas = canvas;
36
+ this.gl = gl;
37
+ this.look = look;
38
+ this.hooks = hooks;
39
+ this.kind = 'webgl';
40
+ this.lost = false;
41
+ this.disposed = false;
42
+ this.handleContextLost = (event) => {
43
+ event.preventDefault();
44
+ this.lost = true;
45
+ this.hooks.onContextLost();
46
+ };
47
+ this.program = linkProgram(gl);
48
+ this.uniforms = Object.fromEntries(UNIFORMS.map((name) => [name, gl.getUniformLocation(this.program, name)]));
49
+ canvas.addEventListener('webglcontextlost', this.handleContextLost);
50
+ }
51
+ /**
52
+ * A WebGL2 context, or `null` where the browser offers none. With
53
+ * `hardwareOnly` also `null` where it would render on the CPU (no GPU, a
54
+ * blocked driver, a software renderer).
55
+ */
56
+ static createContext(canvas, hardwareOnly = true) {
57
+ return canvas.getContext('webgl2', {
58
+ alpha: true,
59
+ premultipliedAlpha: true,
60
+ antialias: false,
61
+ depth: false,
62
+ stencil: false,
63
+ powerPreference: 'low-power',
64
+ failIfMajorPerformanceCaveat: hardwareOnly,
65
+ });
66
+ }
67
+ resize(geometry, pixelRatio) {
68
+ this.geometry = geometry;
69
+ sizeCanvas(this.canvas, geometry, pixelRatio);
70
+ // a GPU may give a smaller drawing buffer than the canvas asks for
71
+ this.gl.viewport(0, 0, this.gl.drawingBufferWidth, this.gl.drawingBufferHeight);
72
+ }
73
+ draw(frame) {
74
+ const { gl, geometry } = this;
75
+ if (this.lost || this.disposed || geometry == null) {
76
+ return;
77
+ }
78
+ gl.clearColor(0, 0, 0, 0);
79
+ gl.clear(gl.COLOR_BUFFER_BIT);
80
+ if (dotsOpacity(frame, this.look.color) <= 0) {
81
+ return;
82
+ }
83
+ gl.useProgram(this.program);
84
+ this.setUniforms(geometry, frame);
85
+ gl.drawArrays(gl.TRIANGLES, 0, 3);
86
+ }
87
+ setLook(look) {
88
+ this.look = look;
89
+ }
90
+ dispose() {
91
+ if (this.disposed) {
92
+ return;
93
+ }
94
+ this.disposed = true;
95
+ this.canvas.removeEventListener('webglcontextlost', this.handleContextLost);
96
+ if (!this.gl.isContextLost()) {
97
+ this.gl.deleteProgram(this.program);
98
+ this.gl.getExtension('WEBGL_lose_context')?.loseContext();
99
+ }
100
+ this.hooks.onDispose?.();
101
+ }
102
+ setUniforms(geometry, frame) {
103
+ const { gl, uniforms, look } = this;
104
+ gl.uniform2f(uniforms.u_size, geometry.width, geometry.height);
105
+ gl.uniform2f(uniforms.u_bufferSize, gl.drawingBufferWidth, gl.drawingBufferHeight);
106
+ gl.uniform1f(uniforms.u_spacing, geometry.dotSpacing);
107
+ gl.uniform1f(uniforms.u_dotSize, look.dotSize);
108
+ gl.uniform3f(uniforms.u_color, look.color.r / 255, look.color.g / 255, look.color.b / 255);
109
+ gl.uniform2f(uniforms.u_center, geometry.centerX, geometry.centerY);
110
+ gl.uniform1f(uniforms.u_frontRadius, frontRadius(geometry, frame.elapsedMs));
111
+ gl.uniform1f(uniforms.u_echoRadius, echoRadius(geometry, frame.elapsedMs));
112
+ gl.uniform1f(uniforms.u_travel, geometry.travel);
113
+ gl.uniform1f(uniforms.u_previousSweep, hasPreviousSweep(geometry, frame.elapsedMs) ? 1 : 0);
114
+ gl.uniform1f(uniforms.u_falloff, geometry.falloff);
115
+ gl.uniform1f(uniforms.u_rise, geometry.rise);
116
+ gl.uniform1f(uniforms.u_maxPush, geometry.maxPush);
117
+ gl.uniform1f(uniforms.u_wave, frame.wave);
118
+ gl.uniform1f(uniforms.u_dots, dotsOpacity(frame, look.color));
119
+ }
120
+ }
121
+ /**
122
+ * The echo's radius, trailing the front by `ECHO_LAG` of a sweep; negative,
123
+ * so no echo shows, until the front has run that far.
124
+ */
125
+ export function echoRadius(geometry, elapsedMs) {
126
+ const echoElapsed = elapsedMs - ECHO_LAG * geometry.sweepMs;
127
+ return echoElapsed < 0 ? -1 : frontRadius(geometry, echoElapsed);
128
+ }
129
+ function compileShader(gl, type, source) {
130
+ const shader = gl.createShader(type);
131
+ if (shader == null) {
132
+ throw new WaveShaderError('could not create a shader');
133
+ }
134
+ gl.shaderSource(shader, source);
135
+ gl.compileShader(shader);
136
+ if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
137
+ const log = gl.getShaderInfoLog(shader);
138
+ gl.deleteShader(shader);
139
+ throw new WaveShaderError(`shader did not compile: ${log}`);
140
+ }
141
+ return shader;
142
+ }
143
+ function linkProgram(gl) {
144
+ const vertex = compileShader(gl, gl.VERTEX_SHADER, WAVE_VERTEX_SHADER);
145
+ let fragment;
146
+ try {
147
+ fragment = compileShader(gl, gl.FRAGMENT_SHADER, WAVE_FRAGMENT_SHADER);
148
+ }
149
+ catch (error) {
150
+ gl.deleteShader(vertex);
151
+ throw error;
152
+ }
153
+ const program = gl.createProgram();
154
+ if (program == null) {
155
+ gl.deleteShader(vertex);
156
+ gl.deleteShader(fragment);
157
+ throw new WaveShaderError('could not create a program');
158
+ }
159
+ gl.attachShader(program, vertex);
160
+ gl.attachShader(program, fragment);
161
+ gl.linkProgram(program);
162
+ // the program keeps what it needs
163
+ gl.deleteShader(vertex);
164
+ gl.deleteShader(fragment);
165
+ if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
166
+ const log = gl.getProgramInfoLog(program);
167
+ gl.deleteProgram(program);
168
+ throw new WaveShaderError(`shaders did not link: ${log}`);
169
+ }
170
+ return program;
171
+ }
172
+ //# 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,WAAW,EACX,UAAU,GAIX,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAgB,MAAM,eAAe,CAAC;AAC5E,OAAO,EACL,QAAQ,EACR,oBAAoB,EACpB,kBAAkB,GACnB,MAAM,gBAAgB,CAAC;AAExB,MAAM,QAAQ,GAAG;IACf,QAAQ;IACR,cAAc;IACd,WAAW;IACX,WAAW;IACX,SAAS;IACT,UAAU;IACV,eAAe;IACf,cAAc;IACd,UAAU;IACV,iBAAiB;IACjB,WAAW;IACX,QAAQ;IACR,WAAW;IACX,QAAQ;IACR,QAAQ;CACA,CAA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Y,CAAC,MAAM,CAAC,CAAC;QACxB,EAAE,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC;QAC1B,MAAM,IAAI,eAAe,CAAC,4BAA4B,CAAC,CAAC;IAC1D,CAAC;IACD,EAAE,CAAC,YAAY,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IACjC,EAAE,CAAC,YAAY,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;IACnC,EAAE,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;IACxB,kCAAkC;IAClC,EAAE,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC;IACxB,EAAE,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC;IAC1B,IAAI,CAAC,EAAE,CAAC,mBAAmB,CAAC,OAAO,EAAE,EAAE,CAAC,WAAW,CAAC,EAAE,CAAC;QACrD,MAAM,GAAG,GAAG,EAAE,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC;QAC1C,EAAE,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;QAC1B,MAAM,IAAI,eAAe,CAAC,yBAAyB,GAAG,EAAE,CAAC,CAAC;IAC5D,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["import {\n dotsOpacity,\n sizeCanvas,\n WaveFrame,\n WaveLook,\n WaveRenderer,\n} from '../renderer';\nimport { frontRadius, hasPreviousSweep, WaveGeometry } from '../wave-model';\nimport {\n ECHO_LAG,\n WAVE_FRAGMENT_SHADER,\n WAVE_VERTEX_SHADER,\n} from './wave-shaders';\n\nconst UNIFORMS = [\n 'u_size',\n 'u_bufferSize',\n 'u_spacing',\n 'u_dotSize',\n 'u_color',\n 'u_center',\n 'u_frontRadius',\n 'u_echoRadius',\n 'u_travel',\n 'u_previousSweep',\n 'u_falloff',\n 'u_rise',\n 'u_maxPush',\n 'u_wave',\n 'u_dots',\n] as const;\n\ntype UniformName = (typeof UNIFORMS)[number];\n\n/** the shaders did not compile or link on this GPU */\nexport class WaveShaderError extends Error {\n public override readonly name = 'WaveShaderError';\n}\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: the 2D canvas's look plus a\n * glow around lit dots and an echo ripple trailing the front.\n */\nexport class WebGlWaveRenderer implements WaveRenderer {\n public readonly kind = 'webgl';\n private readonly program: WebGLProgram;\n private readonly uniforms: Record<UniformName, WebGLUniformLocation | null>;\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 private readonly hooks: WebGlWaveRendererHooks\n ) {\n this.program = linkProgram(gl);\n this.uniforms = Object.fromEntries(\n UNIFORMS.map((name) => [name, gl.getUniformLocation(this.program, name)])\n ) as Record<UniformName, WebGLUniformLocation | null>;\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 (dotsOpacity(frame, this.look.color) <= 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 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, look } = this;\n gl.uniform2f(uniforms.u_size, geometry.width, geometry.height);\n gl.uniform2f(\n uniforms.u_bufferSize,\n gl.drawingBufferWidth,\n gl.drawingBufferHeight\n );\n gl.uniform1f(uniforms.u_spacing, geometry.dotSpacing);\n gl.uniform1f(uniforms.u_dotSize, look.dotSize);\n gl.uniform3f(\n uniforms.u_color,\n look.color.r / 255,\n look.color.g / 255,\n look.color.b / 255\n );\n gl.uniform2f(uniforms.u_center, geometry.centerX, geometry.centerY);\n gl.uniform1f(\n uniforms.u_frontRadius,\n frontRadius(geometry, frame.elapsedMs)\n );\n gl.uniform1f(uniforms.u_echoRadius, echoRadius(geometry, frame.elapsedMs));\n gl.uniform1f(uniforms.u_travel, geometry.travel);\n gl.uniform1f(\n uniforms.u_previousSweep,\n hasPreviousSweep(geometry, frame.elapsedMs) ? 1 : 0\n );\n gl.uniform1f(uniforms.u_falloff, geometry.falloff);\n gl.uniform1f(uniforms.u_rise, geometry.rise);\n gl.uniform1f(uniforms.u_maxPush, geometry.maxPush);\n gl.uniform1f(uniforms.u_wave, frame.wave);\n gl.uniform1f(uniforms.u_dots, dotsOpacity(frame, look.color));\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\nfunction compileShader(\n gl: WebGL2RenderingContext,\n type: number,\n source: string\n): WebGLShader {\n const shader = gl.createShader(type);\n if (shader == null) {\n throw new WaveShaderError('could not create a shader');\n }\n gl.shaderSource(shader, source);\n gl.compileShader(shader);\n if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {\n const log = gl.getShaderInfoLog(shader);\n gl.deleteShader(shader);\n throw new WaveShaderError(`shader did not compile: ${log}`);\n }\n return shader;\n}\n\nfunction linkProgram(gl: WebGL2RenderingContext): WebGLProgram {\n const vertex = compileShader(gl, gl.VERTEX_SHADER, WAVE_VERTEX_SHADER);\n let fragment: WebGLShader;\n try {\n fragment = compileShader(gl, gl.FRAGMENT_SHADER, WAVE_FRAGMENT_SHADER);\n } catch (error) {\n gl.deleteShader(vertex);\n throw error;\n }\n const program = gl.createProgram();\n if (program == null) {\n gl.deleteShader(vertex);\n gl.deleteShader(fragment);\n throw new WaveShaderError('could not create a program');\n }\n gl.attachShader(program, vertex);\n gl.attachShader(program, fragment);\n gl.linkProgram(program);\n // the program keeps what it needs\n gl.deleteShader(vertex);\n gl.deleteShader(fragment);\n if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {\n const log = gl.getProgramInfoLog(program);\n gl.deleteProgram(program);\n throw new WaveShaderError(`shaders did not link: ${log}`);\n }\n return program;\n}\n"]}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Generated bundle index. Do not edit.
3
+ */
4
+ export * from './index';
5
+ //# sourceMappingURL=wolsok-loading-wave.js.map
@@ -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,4 @@
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, WaveRendererPreference, } from './lib/loading-wave-options';
4
+ export type { WaveRendererKind } from './lib/renderer';
@@ -0,0 +1,27 @@
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
+ dispose(): void;
23
+ /** draws every dot at rest in one path, returns the lit ones */
24
+ private drawRestingDots;
25
+ private drawLitDots;
26
+ private drawFrontLine;
27
+ }
@@ -0,0 +1,28 @@
1
+ import type { WaveRendererPreference } from './loading-wave-options';
2
+ import type { WaveLook, WaveRenderer } from './renderer';
3
+ /** counts the waves that hold a WebGL context */
4
+ export declare class GpuContextBudget {
5
+ private readonly max;
6
+ private live;
7
+ constructor(max?: number);
8
+ tryAcquire(): boolean;
9
+ release(): void;
10
+ }
11
+ /** the budget all waves of the page share */
12
+ export declare const PAGE_GPU_BUDGET: GpuContextBudget;
13
+ export interface ChooseWaveRendererParams {
14
+ readonly preference: WaveRendererPreference;
15
+ readonly look: WaveLook;
16
+ /** a new, detached canvas for each renderer tried */
17
+ readonly createCanvas: () => HTMLCanvasElement;
18
+ /** the WebGL renderer lost its context; swap it for a 2D one */
19
+ readonly onContextLost: () => void;
20
+ readonly gpuBudget?: GpuContextBudget;
21
+ }
22
+ /**
23
+ * The WebGL renderer where WebGL2 runs (for `'auto'` only on a GPU, for
24
+ * `'webgl'` also in software), the budget allows one more and the shaders
25
+ * compile; the 2D canvas renderer otherwise and always for `'canvas'`.
26
+ * `null` only where not even a 2D context exists.
27
+ */
28
+ 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,35 @@
1
+ import { InjectionToken, Provider } from '@angular/core';
2
+ /** what the dots do once loading ended */
3
+ export type AfterLoading = 'hide' | 'keep';
4
+ /** which renderer draws the wave */
5
+ export type WaveRendererPreference = 'auto' | 'webgl' | 'canvas';
6
+ export interface LoadingWaveOptions {
7
+ /**
8
+ * `'hide'`: dots and wave fade out and the effect is removed.
9
+ * `'keep'`: the wave fades out, the dots stay as a quiet background.
10
+ */
11
+ readonly afterLoading: AfterLoading;
12
+ /** distance between two dots, in CSS pixels */
13
+ readonly dotSpacing: number;
14
+ /** diameter of a dot at rest, in CSS pixels */
15
+ readonly dotSize: number;
16
+ /** any CSS colour; unset, the dots take the host's `currentColor` */
17
+ readonly color?: string;
18
+ /** how long one sweep of the wave across the element takes, in ms */
19
+ readonly sweepMs: number;
20
+ /**
21
+ * `'auto'` draws with a WebGL2 shader on a GPU, `'webgl'` also where the
22
+ * browser renders WebGL in software; both fall back to a 2D canvas where
23
+ * that fails. `'canvas'` never uses WebGL.
24
+ */
25
+ readonly renderer: WaveRendererPreference;
26
+ }
27
+ export declare const DEFAULT_LOADING_WAVE_OPTIONS: LoadingWaveOptions;
28
+ export declare const LOADING_WAVE_OPTIONS: InjectionToken<LoadingWaveOptions>;
29
+ /** sets app-wide defaults; the directive's own options still win */
30
+ export declare function provideLoadingWave(options: Partial<LoadingWaveOptions>): Provider;
31
+ /**
32
+ * Merges `overrides` into `defaults`, ignoring `undefined` values and
33
+ * replacing sizes and durations that are no positive finite number.
34
+ */
35
+ export declare function resolveLoadingWaveOptions(defaults: LoadingWaveOptions, overrides?: Partial<LoadingWaveOptions>): LoadingWaveOptions;
@@ -0,0 +1,55 @@
1
+ import type { LoadingWaveOptions } 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: LoadingWaveOptions, 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: LoadingWaveOptions): 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,49 @@
1
+ import type { WaveGeometry } from './wave-model';
2
+ export type WaveRendererKind = 'webgl' | 'canvas';
3
+ /** a colour channel by channel, each 0 to 255 */
4
+ export interface Rgb {
5
+ readonly r: number;
6
+ readonly g: number;
7
+ readonly b: number;
8
+ /** the colour's own opacity, 0 to 1; 1 when unset */
9
+ readonly alpha?: number;
10
+ }
11
+ /** the frame's grid opacity times the colour's own */
12
+ export declare function dotsOpacity(frame: WaveFrame, color: Rgb): number;
13
+ /** how the dots look, the same in both renderers */
14
+ export interface WaveLook {
15
+ /** diameter of a dot at rest, in CSS pixels */
16
+ readonly dotSize: number;
17
+ readonly color: Rgb;
18
+ }
19
+ /** what one frame shows */
20
+ export interface WaveFrame {
21
+ /** time since the wave started, drives where the front is */
22
+ readonly elapsedMs: number;
23
+ /** strength of the wave, 0 (no wave) to 1, for fading it in and out */
24
+ readonly wave: number;
25
+ /** opacity of the whole dot grid, 0 to 1, for fading it in and out */
26
+ readonly dots: number;
27
+ }
28
+ /** opacity of a dot at rest, times the colour's */
29
+ export declare const REST_ALPHA = 0.22;
30
+ /** opacity of a dot right behind the front */
31
+ export declare const LIT_ALPHA = 0.9;
32
+ /** how much a dot right behind the front grows, 0.8 = 80% larger */
33
+ export declare const LIT_GROWTH = 0.8;
34
+ /** opacity of the hairline along the front */
35
+ export declare const FRONT_LINE_ALPHA = 0.14;
36
+ /** draws the dot grid and the wave into its own canvas */
37
+ export interface WaveRenderer {
38
+ readonly kind: WaveRendererKind;
39
+ readonly canvas: HTMLCanvasElement;
40
+ /** sizes the canvas to the geometry at the given device pixel ratio */
41
+ resize(geometry: WaveGeometry, pixelRatio: number): void;
42
+ draw(frame: WaveFrame): void;
43
+ /** changes the dots' look, say after the theme changed the host's colour */
44
+ setLook(look: WaveLook): void;
45
+ /** frees what the renderer holds; the canvas stays where it is */
46
+ dispose(): void;
47
+ }
48
+ /** sets the canvas's backing store to the CSS size times the pixel ratio */
49
+ 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;