@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,20 @@
|
|
|
1
|
+
/** a uniform's value: a `float`, or the components of a `vec2` to `vec4` */
|
|
2
|
+
export type ShaderUniformValue = number | readonly [number] | readonly [number, number] | readonly [number, number, number] | readonly [number, number, number, number];
|
|
3
|
+
/** values of the uniforms a fragment declares itself, by their GLSL name */
|
|
4
|
+
export type ShaderUniforms = Readonly<Record<string, ShaderUniformValue>>;
|
|
5
|
+
/**
|
|
6
|
+
* A fragment shader for the loading wave's WebGL renderer.
|
|
7
|
+
*
|
|
8
|
+
* `fragment` is GLSL ES 3.00 that defines `vec4 wsShade(vec2 point)`: the
|
|
9
|
+
* straight (not premultiplied) colour and opacity at `point`, in CSS pixels
|
|
10
|
+
* from the host's top left corner. The lib puts the `ws_*` uniforms and
|
|
11
|
+
* helpers before it and a `main` after it (see the README), so the fragment
|
|
12
|
+
* holds neither `#version` nor `main`. At `ws_wave == 0` it draws its resting
|
|
13
|
+
* state, without `ws_time`: that is what `afterLoading: 'keep'` leaves and
|
|
14
|
+
* what `prefers-reduced-motion` shows.
|
|
15
|
+
*/
|
|
16
|
+
export interface LoadingWaveShader {
|
|
17
|
+
readonly fragment: string;
|
|
18
|
+
/** values of uniforms the fragment declares itself; never `ws_*` ones */
|
|
19
|
+
readonly uniforms?: ShaderUniforms;
|
|
20
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { LoadingWaveShader } from './loading-wave-shader';
|
|
2
|
+
/** the shaders did not compile or link on this GPU */
|
|
3
|
+
export declare class WaveShaderError extends Error {
|
|
4
|
+
readonly name = "WaveShaderError";
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* The uniforms the lib sets for every shader. `ws_bufferSize` and `ws_dots`
|
|
8
|
+
* are for `main` only.
|
|
9
|
+
*/
|
|
10
|
+
export declare const STANDARD_UNIFORMS: readonly ["ws_size", "ws_bufferSize", "ws_pixel", "ws_spacing", "ws_dotSize", "ws_color", "ws_accent", "ws_center", "ws_frontRadius", "ws_echoRadius", "ws_travel", "ws_falloff", "ws_rise", "ws_tail", "ws_maxPush", "ws_phase", "ws_time", "ws_previousSweep", "ws_wave", "ws_dots"];
|
|
11
|
+
export type StandardUniform = (typeof STANDARD_UNIFORMS)[number];
|
|
12
|
+
/** one triangle that covers the whole canvas, no vertex buffer needed */
|
|
13
|
+
export declare 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";
|
|
14
|
+
/**
|
|
15
|
+
* What every fragment shader starts with: the uniforms and helpers of the
|
|
16
|
+
* contract (`LoadingWaveShader`). The `WS_*` constants mirror the
|
|
17
|
+
* TypeScript ones (`renderer.ts`, `wave-model.ts`, `echo.ts`);
|
|
18
|
+
* `shader-constants.spec.ts` keeps them equal. Kept a plain literal, so a
|
|
19
|
+
* bundler can tell it has no side effects.
|
|
20
|
+
*/
|
|
21
|
+
export declare const WAVE_SHADER_HEADER = "#version 300 es\nprecision highp float;\n\nuniform vec2 ws_size;\nuniform vec2 ws_bufferSize;\nuniform float ws_pixel;\nuniform float ws_spacing;\nuniform float ws_dotSize;\nuniform vec4 ws_color;\nuniform vec4 ws_accent;\nuniform vec2 ws_center;\nuniform float ws_frontRadius;\nuniform float ws_echoRadius;\nuniform float ws_travel;\nuniform float ws_falloff;\nuniform float ws_rise;\nuniform float ws_tail;\nuniform float ws_maxPush;\nuniform float ws_phase;\nuniform float ws_time;\nuniform float ws_previousSweep;\nuniform float ws_wave;\nuniform float ws_dots;\n\nconst float WS_REST_ALPHA = 0.22;\nconst float WS_LIT_ALPHA = 0.9;\nconst float WS_LIT_GROWTH = 0.8;\nconst float WS_FRONT_LINE_ALPHA = 0.14;\nconst float WS_AFTERGLOW = 0.45;\nconst float WS_AFTERGLOW_FADE_START = 0.45;\nconst float WS_ECHO_STRENGTH = 0.4;\n\n/** how far p lies behind the front: negative ahead of it */\nfloat ws_behindFront(vec2 p) {\n return ws_frontRadius - length(p - ws_center);\n}\n\n/** how far p lies behind the echo; far ahead of it while there is none */\nfloat ws_behindEcho(vec2 p) {\n return ws_echoRadius < 0.0 ? -1.0e6 : ws_echoRadius - length(p - ws_center);\n}\n\n/** the bright part right behind a front: 1 there, dimming behind, 0 ahead */\nfloat ws_frontGlow(float behind) {\n return smoothstep(-ws_rise, 0.0, behind) * exp(-max(behind, 0.0) / ws_falloff);\n}\n\n/** what stays lit once the front passed, fading until the next one comes */\nfloat ws_afterglow(float behind) {\n if (behind < 0.0 && ws_previousSweep < 0.5) {\n return 0.0;\n }\n float sincePassed = (behind >= 0.0 ? behind : behind + ws_travel) / ws_travel;\n return WS_AFTERGLOW * (1.0 - smoothstep(WS_AFTERGLOW_FADE_START, 1.0, sincePassed));\n}\n\n/** the wave's light at p, 0 to 1, already scaled by ws_wave */\nfloat ws_waveIntensity(vec2 p) {\n float behind = ws_behindFront(p);\n return ws_wave * max(ws_frontGlow(behind), ws_afterglow(behind));\n}\n\n/** the echo's front glow at p, scaled by its strength and ws_wave */\nfloat ws_echoGlow(vec2 p) {\n return ws_wave * WS_ECHO_STRENGTH * ws_frontGlow(ws_behindEcho(p));\n}\n\n/** a stable pseudo-random number in [0, 1) per p, say per grid cell */\nfloat ws_hash(vec2 p) {\n vec3 p3 = fract(vec3(p.xyx) * 0.1031);\n p3 += dot(p3, p3.yzx + 33.33);\n return fract((p3.x + p3.y) * p3.z);\n}\n\n/** two stable pseudo-random numbers in [0, 1) per p */\nvec2 ws_hash2(vec2 p) {\n vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));\n p3 += dot(p3, p3.yzx + 33.33);\n return fract((p3.xx + p3.yz) * p3.zy);\n}\n\n/** the grid cell p lies in */\nvec2 ws_cell(vec2 p) {\n return floor(p / ws_spacing);\n}\n\n/** the centre of a grid cell, where its dot sits */\nvec2 ws_cellCenter(vec2 cell) {\n return (cell + 0.5) * ws_spacing;\n}\n\n/** anti-aliased coverage of a disk at p, 0 to 1 */\nfloat ws_dotDisk(vec2 p, vec2 center, float radius) {\n float edge = 0.5 * ws_pixel;\n return 1.0 - smoothstep(radius - edge, radius + edge, length(p - center));\n}\n\n/** anti-aliased coverage of a line of the given width along a segment */\nfloat ws_segment(vec2 p, vec2 a, vec2 b, float width) {\n vec2 ab = b - a;\n float along = clamp(dot(p - a, ab) / max(dot(ab, ab), 1.0e-6), 0.0, 1.0);\n float away = length(p - a - ab * along);\n float edge = 0.5 * ws_pixel;\n return 1.0 - smoothstep(0.5 * width - edge, 0.5 * width + edge, away);\n}\n\n/** the dots' colour mixed towards the accent by lit, 0 to 1 */\nvec4 ws_ink(float lit) {\n return mix(ws_color, ws_accent, clamp(lit, 0.0, 1.0));\n}\n";
|
|
22
|
+
/**
|
|
23
|
+
* What every fragment shader ends with: maps the drawing buffer onto the
|
|
24
|
+
* host in CSS pixels (y down), calls `wsShade` and fades it by `ws_dots`.
|
|
25
|
+
*/
|
|
26
|
+
export declare const WAVE_SHADER_MAIN = "\nout vec4 ws_outColor;\n\nvoid main() {\n // the drawing buffer may be smaller than asked for: map it onto the host\n vec2 cssPerPixel = ws_size / ws_bufferSize;\n vec2 point = vec2(gl_FragCoord.x, ws_bufferSize.y - gl_FragCoord.y) * cssPerPixel;\n vec4 shade = clamp(wsShade(point), 0.0, 1.0);\n float alpha = shade.a * ws_dots;\n ws_outColor = vec4(shade.rgb * alpha, alpha);\n}\n";
|
|
27
|
+
/** the whole fragment shader for a `LoadingWaveShader` */
|
|
28
|
+
export declare function assembleFragment(shader: LoadingWaveShader): string;
|
|
29
|
+
/**
|
|
30
|
+
* The program of the full-canvas triangle and the shader's fragment.
|
|
31
|
+
* @throws WaveShaderError when it does not compile or link on this GPU
|
|
32
|
+
*/
|
|
33
|
+
export declare function linkWaveProgram(gl: WebGL2RenderingContext, shader: LoadingWaveShader): WebGLProgram;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { LoadingWaveShader } from '../loading-wave-shader';
|
|
2
|
+
/**
|
|
3
|
+
* The default look on the GPU: the wave as a ripple running through water
|
|
4
|
+
* over the dot grid. The crests (front and echo) are a height field; the
|
|
5
|
+
* dots are seen through its slope (refraction), shaded as small beads lit
|
|
6
|
+
* from the crest ahead of them, and bloom in the accent colour; the water
|
|
7
|
+
* itself shows as a faint body with a glossy sheen on the crests' slopes.
|
|
8
|
+
* Drawn over the host's background, so it reads in light and dark themes.
|
|
9
|
+
*/
|
|
10
|
+
export declare const rippleShader: LoadingWaveShader;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { WaveFrame, WaveLook, WaveRenderer } from '../renderer';
|
|
2
|
+
import { WaveGeometry } from '../wave-model';
|
|
3
|
+
import type { LoadingWaveShader, ShaderUniforms } from './loading-wave-shader';
|
|
4
|
+
import { WaveShaderError } from './shader-program';
|
|
5
|
+
export { WaveShaderError };
|
|
6
|
+
export interface WebGlWaveRendererHooks {
|
|
7
|
+
/** the browser took the GPU context away; the renderer draws no more */
|
|
8
|
+
readonly onContextLost: () => void;
|
|
9
|
+
/** called once by dispose(), after the context was freed */
|
|
10
|
+
readonly onDispose?: () => void;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Draws the wave with a WebGL2 fragment shader (`LoadingWaveShader`) over a
|
|
14
|
+
* full-canvas triangle, setting the contract's `ws_*` uniforms and the
|
|
15
|
+
* shader's own each frame.
|
|
16
|
+
*/
|
|
17
|
+
export declare class WebGlWaveRenderer implements WaveRenderer {
|
|
18
|
+
readonly canvas: HTMLCanvasElement;
|
|
19
|
+
private readonly gl;
|
|
20
|
+
private look;
|
|
21
|
+
private readonly hooks;
|
|
22
|
+
readonly kind = "webgl";
|
|
23
|
+
private readonly program;
|
|
24
|
+
private readonly uniforms;
|
|
25
|
+
private readonly ownLocations;
|
|
26
|
+
private ownUniforms;
|
|
27
|
+
/** uniforms `setShaderUniforms` left out, set to 0 on the next draw */
|
|
28
|
+
private pendingResets;
|
|
29
|
+
private geometry?;
|
|
30
|
+
private lost;
|
|
31
|
+
private disposed;
|
|
32
|
+
private readonly handleContextLost;
|
|
33
|
+
/** @throws WaveShaderError when the shaders fail on this GPU */
|
|
34
|
+
constructor(canvas: HTMLCanvasElement, gl: WebGL2RenderingContext, look: WaveLook, shader: LoadingWaveShader, hooks: WebGlWaveRendererHooks);
|
|
35
|
+
/**
|
|
36
|
+
* A WebGL2 context, or `null` where the browser offers none. With
|
|
37
|
+
* `hardwareOnly` also `null` where it would render on the CPU (no GPU, a
|
|
38
|
+
* blocked driver, a software renderer).
|
|
39
|
+
*/
|
|
40
|
+
static createContext(canvas: HTMLCanvasElement, hardwareOnly?: boolean): WebGL2RenderingContext | null;
|
|
41
|
+
resize(geometry: WaveGeometry, pixelRatio: number): void;
|
|
42
|
+
draw(frame: WaveFrame): void;
|
|
43
|
+
setLook(look: WaveLook): void;
|
|
44
|
+
/**
|
|
45
|
+
* a uniform the new values leave out goes back to 0, the value GLSL gives
|
|
46
|
+
* every uniform before it is set
|
|
47
|
+
*/
|
|
48
|
+
setShaderUniforms(uniforms: ShaderUniforms | undefined): void;
|
|
49
|
+
dispose(): void;
|
|
50
|
+
private setUniforms;
|
|
51
|
+
/** where the shader's own uniform lives, looked up once per name */
|
|
52
|
+
private ownLocation;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The echo's radius, trailing the front by `ECHO_LAG` of a sweep; negative,
|
|
56
|
+
* so no echo shows, until the front has run that far.
|
|
57
|
+
*/
|
|
58
|
+
export declare function echoRadius(geometry: WaveGeometry, elapsedMs: number): number;
|
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wolsok/loading-wave",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "An Angular directive that lays a calm loading wave of lit dots behind any element, on the GPU with a 2D canvas fallback",
|
|
5
|
+
"bugs": {
|
|
6
|
+
"url": "https://github.com/WolfSoko/wol-sok-mono/issues"
|
|
7
|
+
},
|
|
8
|
+
"homepage": "https://angularexamples.wolsok.de/loadingWavePlayground",
|
|
9
|
+
"repository": "https://github.com/WolfSoko/wol-sok-mono/tree/main/libs/public/ws-loading-wave",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"keywords": [
|
|
12
|
+
"wolf soko",
|
|
13
|
+
"wolsok",
|
|
14
|
+
"angular",
|
|
15
|
+
"angular-lib",
|
|
16
|
+
"directive",
|
|
17
|
+
"loading",
|
|
18
|
+
"loading-indicator",
|
|
19
|
+
"animation",
|
|
20
|
+
"webgl",
|
|
21
|
+
"shader",
|
|
22
|
+
"dots",
|
|
23
|
+
"wave"
|
|
24
|
+
],
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"@angular/core": "^22.0.0"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"tslib": "^2.3.0"
|
|
30
|
+
},
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
},
|
|
34
|
+
"module": "esm2022/wolsok-loading-wave.js",
|
|
35
|
+
"typings": "wolsok-loading-wave.d.ts",
|
|
36
|
+
"exports": {
|
|
37
|
+
"./package.json": {
|
|
38
|
+
"default": "./package.json"
|
|
39
|
+
},
|
|
40
|
+
".": {
|
|
41
|
+
"types": "./wolsok-loading-wave.d.ts",
|
|
42
|
+
"default": "./esm2022/wolsok-loading-wave.js"
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"sideEffects": false,
|
|
46
|
+
"type": "module"
|
|
6
47
|
}
|