Skip to content

Commit 5dd9b2d

Browse files
JUNAID NAWAZJUNAID NAWAZ
authored andcommitted
docs(p5.strands): fix smoothstep examples and add animated example
1 parent dc10b0a commit 5dd9b2d

1 file changed

Lines changed: 42 additions & 30 deletions

File tree

src/strands/p5.strands.js

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ function strands(p5, fn) {
3030
//////////////////////////////////////////////
3131
// Global Runtime
3232
//////////////////////////////////////////////
33-
function initStrandsContext(ctx, backend, { active = false, renderer = null, baseShader = null } = {}) {
33+
function initStrandsContext(
34+
ctx,
35+
backend,
36+
{ active = false, renderer = null, baseShader = null } = {},
37+
) {
3438
ctx.dag = createDirectedAcyclicGraph();
3539
ctx.cfg = createControlFlowGraph();
3640
ctx.uniforms = [];
@@ -78,11 +82,8 @@ function strands(p5, fn) {
7882

7983
const prev = {};
8084
for (const key of Object.getOwnPropertyNames(fn)) {
81-
const descriptor = Object.getOwnPropertyDescriptor(
82-
fn,
83-
key
84-
);
85-
if (descriptor && !descriptor.get && typeof fn[key] === 'function') {
85+
const descriptor = Object.getOwnPropertyDescriptor(fn, key);
86+
if (descriptor && !descriptor.get && typeof fn[key] === "function") {
8687
prev[key] = window[key];
8788
window[key] = fn[key].bind(pInst);
8889
}
@@ -104,7 +105,10 @@ function strands(p5, fn) {
104105

105106
p5.Shader.prototype.modify = function (shaderModifier, scope = {}) {
106107
try {
107-
if (shaderModifier instanceof Function || typeof shaderModifier === 'string') {
108+
if (
109+
shaderModifier instanceof Function ||
110+
typeof shaderModifier === "string"
111+
) {
108112
// Reset the context object every time modify is called;
109113
// const backend = glslBackend;
110114
initStrandsContext(strandsContext, this._renderer.strandsBackend, {
@@ -121,9 +125,10 @@ function strands(p5, fn) {
121125
if (options.parser) {
122126
// #7955 Wrap function declaration code in brackets so anonymous functions are not top level statements, which causes an error in acorn when parsing
123127
// https://github.com/acornjs/acorn/issues/1385
124-
const sourceString = typeof shaderModifier === 'string'
125-
? `(${shaderModifier})`
126-
: `(${shaderModifier.toString()})`;
128+
const sourceString =
129+
typeof shaderModifier === "string"
130+
? `(${shaderModifier})`
131+
: `(${shaderModifier.toString()})`;
127132
strandsCallback = transpileStrandsToJS(
128133
p5,
129134
sourceString,
@@ -303,65 +308,72 @@ if (typeof p5 !== "undefined") {
303308
* @example
304309
* <div modernizr='webgl'>
305310
* <code>
306-
* // Example 1: Smooth color fade across the canvas (no uniforms)
311+
* // Example 1: Smooth fade across the canvas using coordinates (no uniforms)
307312
*
308313
* let fadeShader;
309314
*
310315
* function fadeCallback() {
311-
* getFinalColor((color) => {
312-
* // Normalize x position from 0 → 1 across the canvas
313-
* let x = pixelInputs.position.x / canvasSize.x;
316+
* getColor((inputs, canvasContent) => {
317+
* // Normalized x coordinate (0 → 1 across the canvas)
318+
* let x = inputs.texCoord.x;
314319
*
315-
* // Smooth transition from black to red
316-
* let t = smoothstep(0.2, 0.8, x);
320+
* // Narrow smooth transition band
321+
* let t = smoothstep(0.4, 0.6, x);
317322
*
318-
* return [t, 0, 0, 1];
323+
* // Sample the original color
324+
* let col = getTexture(canvasContent, inputs.texCoord);
325+
*
326+
* // Fade from black to the original image
327+
* return [col.rgb * t, col.a];
319328
* });
320329
* }
321330
*
322331
* function setup() {
323332
* createCanvas(300, 200, WEBGL);
324-
* fadeShader = baseColorShader().modify(fadeCallback);
333+
* fadeShader = baseFilterShader().modify(fadeCallback);
325334
* }
326335
*
327336
* function draw() {
328337
* background(0);
329-
* shader(fadeShader);
330-
* rect(-width / 2, -height / 2, width, height);
338+
* fill(255);
339+
* rect(-100, -50, 200, 100);
340+
* filter(fadeShader);
331341
* }
332342
* </code>
333343
* </div>
334344
*
335345
* @example
336346
* <div modernizr='webgl'>
337347
* <code>
338-
* // Example 2: Animate the smooth transition over time (uses a uniform)
348+
* // Example 2: Animate the smooth transition using a uniform
339349
*
340350
* let animatedShader;
341351
*
342352
* function animatedFadeCallback() {
343353
* const time = uniformFloat(() => millis() * 0.001);
344354
*
345-
* getFinalColor((color) => {
346-
* let x = pixelInputs.position.x / canvasSize.x;
355+
* getColor((inputs, canvasContent) => {
356+
* let x = inputs.texCoord.x;
347357
*
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);
358+
* // Move the smoothstep band back and forth
359+
* let center = 0.5 + 0.25 * sin(time);
360+
* let t = smoothstep(center - 0.05, center + 0.05, x);
351361
*
352-
* return [t, 0, 0, 1];
362+
* let col = getTexture(canvasContent, inputs.texCoord);
363+
* return [col.rgb * t, col.a];
353364
* });
354365
* }
355366
*
356367
* function setup() {
357368
* createCanvas(300, 200, WEBGL);
358-
* animatedShader = baseColorShader().modify(animatedFadeCallback);
369+
* animatedShader = baseFilterShader().modify(animatedFadeCallback);
359370
* }
360371
*
361372
* function draw() {
362373
* background(0);
363-
* shader(animatedShader);
364-
* rect(-width / 2, -height / 2, width, height);
374+
* fill(255);
375+
* rect(-100, -50, 200, 100);
376+
* filter(animatedShader);
365377
* }
366378
* </code>
367379
* </div>

0 commit comments

Comments
 (0)