Add shader examples for math functions#8802
Conversation
Add comprehensive shader examples for abs, ceil, exp, floor, lerp, log, map, max, min, pow, round, sqrt, and fract functions in p5.strands, demonstrating their usage in WebGL shaders with practical color effects.
nbogie
left a comment
There was a problem hiding this comment.
looks good though i didn't test the examples.
| * let t = millis() * 0.001; | ||
| * let value = 0.5 + 0.5 * sin(t); | ||
| * finalColor.begin(); | ||
| * finalColor.set(lerp([0, 0.8, 0.8, 1], [1, 0.5, 0.3, 1], value)); |
There was a problem hiding this comment.
As elsewhere, i think breaking this down is clearer
let mixFraction = ...
//define the two colours we'll mix between
//both as [r, g, b, a], all from 0.0 to 1.0
let color1 = [0, 0.8, 08, 1]
let color2 = [1, 0.5, 0.3, 1]
let mixedColor = lerp(color1, color2, mixFraction);
finalColor.set(mixedColor);This is also has quite a lot going on in order to demonstrate lerp: (millis and sin, and its normalization), but:
- I think it's ok, if we're assuming the expected reader already understands lerp from a 2d context.
- Otherwise a static form like `
//mix a color 25% cyan, 75% magenta
let mixedColor = lerp(cyan, magenta, 0.25);would be better suited.
However, your example is far more inspiring! I like it!
There was a problem hiding this comment.
perminder-17
left a comment
There was a problem hiding this comment.
On the website when I run your forked branch to check the actual changes, it looks something like this:
You can verify your changes by running the p5.js-website locally against your fork/branch of p5.js.
Here's the step
- Clone the website repository:
git clone https://github.com/processing/p5.js-website.git
cd p5.js-website
- Switch to the 2.0 branch (since you are working on the p5.js 2.x version):
git checkout 2.0
- Install dependencies:
npm install
Run the website against your p5.js branch using the custom:dev script.
npm run custom:dev https://github.com/SOUMITRO-SAHA/p5.js.git#docs-8777-strands-calc-examples
It would run your current changes.
Also, if you need to add new examples, you need to use the @example tag and then write your code instead of ```js . After the changes, you can verify by running the website locally with your changes. Thanks for your work so far :)
|
Thanks for the detailed steps and the screenshot, @perminder-17 ! I really appreciate you taking the time to verify this on the website and showing me how to set it up locally. I wasn't aware the website renders examples differently than how they appear in the web editor. I had been testing all the examples in the p5 web editor and using the ``js example syntax I saw in other parts of the codebase (like I'll:
Thanks for the guidance |
Convert all 13 inline shader examples from markdown code fence (```js example) to JSDoc @example tags so they render correctly on the p5.js website.
WhatsApp.Video.2026-05-20.at.20.08.44.mp4Hi, @perminder-17 , I am currently reviewing other examples that use the following format for examples: **```js example ** I am trying to understand the expected template and check whether this approach will work. From what I understood, we only need to move the example section before the I have also added a dummy video for testing. If this looks fine, then I will raise fixes for all the examples in PR 8001, 8002, and 8003 as well. |
8d1dc3e to
30f6c0e
Compare
|
Thanks @nbogie , @perminder-17 , and @davepagurek for all the feedback! I've tested all the changes locally on the I'll be applying a similar approach to the remaining PRs shortly. |
thanks for the update, I'll take a look soon. |

Resolves #8777 (Calculation batch)
Changes:
Adds inline p5.strands examples to the reference documentation for 13 calculation functions in src/math/calculation.js:
abs()— mirror/fold effect using abs(sin(t))ceil()— stepped color bandsfloor()— posterized banding effectfract()— repeating gradient patternsqrt()— smooth easing curvepow()— gamma curve effectexp()— accelerating brightnesslog()— decelerating color transitionmin()— maximum brightness clampmax()— minimum brightness clampround()— quantized posterized colorslerp()— color blending (maps to GLSLmix)map()— remapped time to colorEach example uses
buildColorShader()withfinalColor.begin()/end()and includes a brief explanation that the function can be used in shaders with p5.strands.Screenshots of the change:
PR Checklist
npm run lintpasses