diff --git a/examples/radial-waves/frag.glsl b/examples/radial-waves/frag.glsl new file mode 100644 index 0000000..5296419 --- /dev/null +++ b/examples/radial-waves/frag.glsl @@ -0,0 +1,16 @@ +precision lowp float; + +varying vec2 vPosition; /* pixel position, X & Y in [-1, +1] */ +uniform vec4 uColor; /* injected from JS */ +uniform float uTime; /* time in seconds since canvas loaded */ + +// Animation code +void main() { + float theta = atan(vPosition.y, vPosition.x); + float rho = length(vPosition.xy); + float v = mod(rho - uTime/10., .2); + float alpha = smoothstep(.1, .2, v); + alpha *= 1. - smoothstep(0., 1., rho); + float fadeIn = smoothstep(0., 1., uTime); + gl_FragColor = fadeIn * alpha * uColor; +} diff --git a/examples/radial-waves/index.ts b/examples/radial-waves/index.ts new file mode 100644 index 0000000..4e2224d --- /dev/null +++ b/examples/radial-waves/index.ts @@ -0,0 +1,12 @@ +import { animate, getComputedStylePropRGBA } from "quad-shader"; +import frag from "./frag.glsl?raw" // PROC: REMOVE +const canvas = document.querySelector("#glcanvas-radial-waves") as HTMLCanvasElement; // PROC: REMOVE +const qs = animate( + canvas, /* the HTMLCanvasElement to draw on */ + frag, /* fragment shader, as a string */ +); + +/* register a callback that updates "uColor" on render */ +/* 'getComputedStylePropRGBA' is a helper that returns the */ +/* given CSS property as a RGBA value */ +qs.uniform4f("uColor", () => getComputedStylePropRGBA(canvas, "accent-color")); // PROC: REPLACE "accent-color" "color" diff --git a/index.html b/index.html index 12ccd7e..7e41ce7 100644 --- a/index.html +++ b/index.html @@ -52,55 +52,34 @@
quad-shaderLet's take the following animation as an example:
++ We'll go through the steps required for displaying the following + animation: +
- First install quad-shader with
- npm install quad-shader.
-
First install quad-shader:
- After having installed the library, all it takes is (literally) three - lines of JavaScript and a fairly short fragment shader, inlined as a - string here for simplicity: -
+npm install quad-shader
- +Then include the following code in your page:
+ +
- Assuming you have a canvas element on the page, you should
- see the animation. Congrats!
+ The canvas should be an HTMLCanvasElement. The
+ frag should be a string with the following content:
You should now see the animation on your page!
+@@ -192,34 +171,32 @@