How to draw 3d spiral in p5.js in 2d without webgl or any 3D engine

I intend to draw spirals in the shape of a 3d spring, as in the doodle below. How can I do that through trigonometry without 3d engines like webgl?



Take a look at Khan Academy’s post.

1 Like

See also tutorials trigonometry

I can guide you through when you show more code

1 Like

There’s a couple things I would look into:

  1. You could create a 3d model in blender and load it into p5. This is one simple way to create more complex geometry but it makes it harder to animate the shape. Dan Shiffman made a video about this and there’s a bunch of blender tutorials out there.
  2. As @Chrisir suggest look at using trigonometry. There’s an example of this on the p5 website. I would try drawing a bunch of cylinders to create curves.
  3. This reminded me of an example from three.js using extrude. (Use the drop down in the top right of the screen to select HelixCurve.) I don’t know how well this would translate to p5 but you could look at the code and see if there’s something there.

It’s always nice to break the problem down into smaller parts: let’s look at how we can draw the spiral one-dimension at a time.

Notice the x-axis motion is simply going back and forth with the width decreasing as we go down. Notice the y-axis motion is also going back and forth, but with an added motion going down.

These motion can be represented by two sinusoidal functions with 90 degrees out of phase. Or conveniently, use a sine function for one of the axis, and cosine function for the other function. Note that these two functions alone creates a circle.

Now we just need to multiple the x-axis function with some decaying value, and add the y-axis function with some increasing function to get the spiral you wanted.

Here is a proof of concept:

void setup() {
    size(400, 400, P2D);

void draw() {
    float n = 500;
    for (float t = 0; t < n; t++) {
        // adding width/2 to center the spiral
        float x = cos(t * 0.05) * 100 * map(t, 0, n, 1, 0.5) + width / 2;
        float y = sin(t * 0.05) * 20 + map(t, 0, n, 100, 300);
        ellipse(x, y, 5, 5);




thank you! such intelligent solutions!

That’s not 3D yet

In 3D you want the circle between x and z plane : cos and sin

The going down part is y : y++;

You can now use point (with 3 parameters) or line (with 6 parameters) or sphere with translate (with 3 parameters)

You can use peasyCam

Use lights(); at start of draw()


1 Like

pls read header title again,

so @FSXAC give a very good answer!

1 Like

You might also be interested in applying your spiral math to the Tube from the Shapes3D library.

Edit: that is for Processing (Java), not p5.js

example in 3D as I described above


ArrayList<PVector> listPV = new ArrayList();
float angleForRotation;

void setup() {
  size(800, 800, P3D);

  float x, y, z;      // pos 
  float r = 88; 

  fill(255, 2, 2);
  for (int i=0; i<8*360; i+=4) {
    // x and z are the circle part
    x = r * cos(radians(i));
    // y is the height (which makes the circle to a spiral)
    y = map(i, 
      0, 8*360, 
      -230, 230); 
    z =  r * sin(radians(i));
    // store result 
    listPV.add(new  PVector(x, y, z));
} // func

void draw() {

  translate(width/2, height/2); 

  for (PVector pv : listPV) { 
    translate (pv.x, pv.y, pv.z); 

} // func