Skip to content

Add shader examples to trigonometric functions#8801

Open
SOUMITRO-SAHA wants to merge 2 commits into
processing:dev-2.0from
SOUMITRO-SAHA:docs-8777-strands-trig-examples
Open

Add shader examples to trigonometric functions#8801
SOUMITRO-SAHA wants to merge 2 commits into
processing:dev-2.0from
SOUMITRO-SAHA:docs-8777-strands-trig-examples

Conversation

@SOUMITRO-SAHA
Copy link
Copy Markdown

Resolves #8777

Changes:

Adds inline p5.strands examples to the reference documentation for 8 trigonometry functions in src/math/trigonometry.js:

  • sin() — sine-driven color oscillation
  • cos() — cosine-driven color oscillation
  • tan() — rapid tangent-based color transitions
  • asin() — smooth color transition via asin(sin(t))
  • acos() — pulsing color transition via acos(cos(t))
  • atan() — sawtooth color effect via atan(tan(t))
  • degrees() — convert radians to degrees in shader
  • radians() — convert degrees to radians in shader

Each example uses buildColorShader() with finalColor.begin()/end() and includes a brief explanation that the function can be used in shaders with p5.strands.

Screenshots of the change:

Documentation-only change

PR Checklist

Add shader examples using p5.strands for acos, asin, atan, cos, sin,
tan, degrees, and radians functions to demonstrate their usage in shader
contexts.
@welcome
Copy link
Copy Markdown

welcome Bot commented May 16, 2026

🎉 Thanks for opening this pull request! For guidance on contributing, check out our contributor guidelines and other resources for contributors!
🤔 Please ensure that your PR links to an issue, which has been approved for work by a maintainer; otherwise, there might already be someone working on it, or still ongoing discussion about implementation. You are welcome to join the discussion in an Issue if you're not sure!
🌸 Once your PR is merged, be sure to add yourself to the list of contributors on the readme page !

Thank You!

Copy link
Copy Markdown
Contributor

@nbogie nbogie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great stuff!

Comment thread src/math/trigonometry.js Outdated
* let t = millis() * 0.001;
* let value = asin(sin(t)) / (PI / 2);
* finalColor.begin();
* finalColor.set(mix([0, 1, 0.5, 1], [0.5, 0, 1, 1], 0.5 + 0.5 * value));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe pull out the colour arguments into their own colours to make this line less complex.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I'll extract the colors into named variables for clarity. Something like:

function shaderCallback() {
  let t = millis() * 0.001;
  let value = (asin(sin(t)) / (PI / 2) + 1) * 0.5; 
  let green = [0, 1, 0.5, 1];
  let purple = [0.5, 0, 1, 1];
  finalColor.begin();
  finalColor.set(mix(green, purple, value));
  finalColor.end();
}

This also normalizes value to 0-1 earlier, which addresses your second point about consistency. I'll push this update!

Comment thread src/math/trigonometry.js Outdated
*
* function shaderCallback() {
* let t = millis() * 0.001;
* let value = atan(tan(t)) / (PI / 2);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be great if a demo of atan didn't also involve atan of tan!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've simplified it to just atan(t) with a time-based input, which is much clearer for demonstrating the function. The updated example normalizes the result to 0-1 and uses named color variables for readability:

function shaderCallback() {
  let t = millis() * 0.001;
  let value = (atan(t) + PI / 2) / PI;
  let pink = [1, 0, 0.5, 1];
  let lime = [0.5, 1, 0, 1];
  finalColor.begin();
  finalColor.set(mix(pink, lime, value));
  finalColor.end();
}

This shows atan() converting a linear time value into an angle-based color transition.

Comment thread src/math/trigonometry.js Outdated
* let t = millis() * 0.001;
* let value = asin(sin(t)) / (PI / 2);
* finalColor.begin();
* finalColor.set(mix([0, 1, 0.5, 1], [0.5, 0, 1, 1], 0.5 + 0.5 * value));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in most of these you've set value to 0 to 1 earlier. that would also simplify this line and be more consistent.

@SOUMITRO-SAHA
Copy link
Copy Markdown
Author

Thanks @nbogie for the review! I've updated all examples to:

  • Extract colors into named variables
  • Normalize value to 0-1 upfront
  • Simplify atan() to just atan(t)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants