Line inside for() wont go behind image

Hello everyone,can someone help me with this codepen? I can’t get the lines to go behind the big central circle, even putting the center image before the for().

var canvas;
var width = window.innerWidth;
var height = window.innerHeight;
let angle = 0;
var unitSize = [125,125,125,125,125,125,125,125,125];
var xoff = [0,1,2,3,4,5,6,7,8];
var x = [0,1,2,3,4,5,6,7,8];
var circles = [0,1,2,3,4,5,6,7,8];
var images = [0,1,2,3,4,5,6,7,8,9];

  function preload(){

  	images[0] = loadImage(""); 
  	images[1] = loadImage("");
  	images[2] = loadImage("");
  	 images[3] = loadImage("");
  	 images[4] = loadImage("");
  	 images[5] = loadImage("");
  	 images[6] = loadImage("");
  	 images[7] =  loadImage("");
  	 images[8] = loadImage("");
  	 images[9] = loadImage("");

  function setup() {
    canvas = createCanvas(window.innerWidth, window.innerHeight);       //use WEBGL when using texture()

  function windowResized() {
    resizeCanvas(windowWidth, windowHeight);
  //if you don't want to spin it when scroll delete this
  function mouseWheel(event) {
    angle += (*0.001;

  function draw() {
    //delete this line when using WEBGL
    background( 255 );
    //big central image
    //rotate the black ellipses
    //rotation speed
    angle = angle + 0.0007;
    //using noise() to get smooth random x values and using xoff to     get different random values for each black ellipse
    for (var i = x.length - 1; i >= 0; i--) {
      x[i] = map(noise(xoff[i]), 0 , 1 , 200 , 230);
      xoff[i] += 0.01;
    //draw the black ellipses
    for( var i = 0; i < circles.length; i++) {
    rotate( TWO_PI/9.0 );
    line( 0, 0,x[i],0);
    image(images[i], x[i],0, 125,125);       
    // fill(0);
    //draw each ellipse with different x value
    //ellipse( x[i], 0, unitSize[i], unitSize[i] ); 


1 Like

If you want the lines to go behind the image, then draw the image after the lines.

In other words, move this line:


So it’s the last line in the draw() function.

Think about it this way: drawing in P5.js is a bit like painting on a canvas. If you want to draw lines behind a circle, you’ll draw the lines first, and then draw the circle on top of the lines.


When i try to put it in the last line it rotates with the other little circles, and it cant rotate

Sounds like you’re going to have to rearrange your code to fix the rotation. Maybe take a look at the push() and pop() functions in the reference.


Wow that was easier than i thought it would be, i put a push and pop around the rotate() and the for() and it worked! Thanks Kevin

1 Like

You’re welcome, glad I could help! Thanks for posting the CodePen, that makes our job much easier!

1 Like