Skip to content

Commit dc10b0a

Browse files
JUNAID NAWAZJUNAID NAWAZ
authored andcommitted
docs(p5.strands): improve smoothstep examples
1 parent db08f6d commit dc10b0a

1 file changed

Lines changed: 59 additions & 6 deletions

File tree

src/strands/p5.strands.js

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,65 @@ if (typeof p5 !== "undefined") {
303303
* @example
304304
* <div modernizr='webgl'>
305305
* <code>
306-
* function material() {
307-
* let t = uniformFloat();
308-
* combineColors.begin();
309-
* let fade = smoothstep(0.2, 0.8, sin(t * 0.001));
310-
* combineColors.opacity *= fade;
311-
* combineColors.end();
306+
* // Example 1: Smooth color fade across the canvas (no uniforms)
307+
*
308+
* let fadeShader;
309+
*
310+
* function fadeCallback() {
311+
* getFinalColor((color) => {
312+
* // Normalize x position from 0 → 1 across the canvas
313+
* let x = pixelInputs.position.x / canvasSize.x;
314+
*
315+
* // Smooth transition from black to red
316+
* let t = smoothstep(0.2, 0.8, x);
317+
*
318+
* return [t, 0, 0, 1];
319+
* });
320+
* }
321+
*
322+
* function setup() {
323+
* createCanvas(300, 200, WEBGL);
324+
* fadeShader = baseColorShader().modify(fadeCallback);
325+
* }
326+
*
327+
* function draw() {
328+
* background(0);
329+
* shader(fadeShader);
330+
* rect(-width / 2, -height / 2, width, height);
331+
* }
332+
* </code>
333+
* </div>
334+
*
335+
* @example
336+
* <div modernizr='webgl'>
337+
* <code>
338+
* // Example 2: Animate the smooth transition over time (uses a uniform)
339+
*
340+
* let animatedShader;
341+
*
342+
* function animatedFadeCallback() {
343+
* const time = uniformFloat(() => millis() * 0.001);
344+
*
345+
* getFinalColor((color) => {
346+
* let x = pixelInputs.position.x / canvasSize.x;
347+
*
348+
* // Move the smoothstep window over time
349+
* let center = 0.5 + 0.3 * sin(time);
350+
* let t = smoothstep(center - 0.1, center + 0.1, x);
351+
*
352+
* return [t, 0, 0, 1];
353+
* });
354+
* }
355+
*
356+
* function setup() {
357+
* createCanvas(300, 200, WEBGL);
358+
* animatedShader = baseColorShader().modify(animatedFadeCallback);
359+
* }
360+
*
361+
* function draw() {
362+
* background(0);
363+
* shader(animatedShader);
364+
* rect(-width / 2, -height / 2, width, height);
312365
* }
313366
* </code>
314367
* </div>

0 commit comments

Comments
 (0)