P5js. Sliding matrix of circles, can't generate or fill new row of circles in 2D array












1















Hi am creating a 2D array of circles, which slides down every second.



Right now if the height of the circles is bigger than the sketch height, it resets the height of the circles to the top.



enter image description here



So, i generate once a grid and i just slide it, cyclically.



Now, i want it not be bee just a grid repeating itself. I want it that it generates a new row at the beggining everytime, so that it never repeats itself...



But i cant get it to work , i have tried many things but none work..



Any ideas of how can i get this behaviour?



The code works this way:



There is a circle factory function, a line factory functionm and i grid factory function.



The grid factory calls the line factory and this one calls the circle one.



Then on setup i have a setInterval function that, every seconds calls the slide method on the circle object. and if its height is bigger than the height it resets the height to the top.



Here is the codepen:
https://codepen.io/giorgiomartini/pen/MzGmwW



 let canvasHeight = 500
let row
let grid
let amtOfHorizontalCircles = 15
let linesAmt = 50
let ySpacing = 10
let lineSpacing = canvasHeight/linesAmt

const createCircle = (fillColor, x, y, maxRadius) => {
let xpos = x
let ypos = y
return {
xpos,
ypos,
display() {
noStroke()
fill(fillColor)
ellipse(xpos, ypos, maxRadius, maxRadius)
},
slide(amt) {
if( ypos > linesAmt * lineSpacing ) {
ypos = lineSpacing
}
ypos += amt
},
}
}

const createLine = (arr, amt,x, y) => {
if( arr.length < amt) {
arr.push(createCircle(`rgba(205, 64, 24, ${Math.random().toFixed(1)})`, x, y, Math.random()*9))
createLine(arr, amt, x+=width/amtOfHorizontalCircles, y)
}
return arr
}


const createGrid = (arr, linesAmt, y) => {
if( arr.length < linesAmt) {
arr.push(createLine(, amtOfHorizontalCircles, 0, y))
createGrid(arr, linesAmt, y += lineSpacing)}
return arr
}

const slideRow = () => {
// shits heights, and moves y's to begginig of they are higher tha Height
grid.forEach( (line) => {
line.forEach( x => {
x.slide(ySpacing)
})
})
//grid[grid.length -1] = createLine(, amtOfHorizontalCircles, 0, 0)
}


function setup () {
createCanvas(300, canvasHeight)
background("#140c28")
grid = createGrid(, linesAmt, 10)
setInterval(slideRow, 1000)
}

function draw () {
background("#140c28")
grid.forEach( row => {
row.forEach( x => {
x.display()
})
})
}









share|improve this question





























    1















    Hi am creating a 2D array of circles, which slides down every second.



    Right now if the height of the circles is bigger than the sketch height, it resets the height of the circles to the top.



    enter image description here



    So, i generate once a grid and i just slide it, cyclically.



    Now, i want it not be bee just a grid repeating itself. I want it that it generates a new row at the beggining everytime, so that it never repeats itself...



    But i cant get it to work , i have tried many things but none work..



    Any ideas of how can i get this behaviour?



    The code works this way:



    There is a circle factory function, a line factory functionm and i grid factory function.



    The grid factory calls the line factory and this one calls the circle one.



    Then on setup i have a setInterval function that, every seconds calls the slide method on the circle object. and if its height is bigger than the height it resets the height to the top.



    Here is the codepen:
    https://codepen.io/giorgiomartini/pen/MzGmwW



     let canvasHeight = 500
    let row
    let grid
    let amtOfHorizontalCircles = 15
    let linesAmt = 50
    let ySpacing = 10
    let lineSpacing = canvasHeight/linesAmt

    const createCircle = (fillColor, x, y, maxRadius) => {
    let xpos = x
    let ypos = y
    return {
    xpos,
    ypos,
    display() {
    noStroke()
    fill(fillColor)
    ellipse(xpos, ypos, maxRadius, maxRadius)
    },
    slide(amt) {
    if( ypos > linesAmt * lineSpacing ) {
    ypos = lineSpacing
    }
    ypos += amt
    },
    }
    }

    const createLine = (arr, amt,x, y) => {
    if( arr.length < amt) {
    arr.push(createCircle(`rgba(205, 64, 24, ${Math.random().toFixed(1)})`, x, y, Math.random()*9))
    createLine(arr, amt, x+=width/amtOfHorizontalCircles, y)
    }
    return arr
    }


    const createGrid = (arr, linesAmt, y) => {
    if( arr.length < linesAmt) {
    arr.push(createLine(, amtOfHorizontalCircles, 0, y))
    createGrid(arr, linesAmt, y += lineSpacing)}
    return arr
    }

    const slideRow = () => {
    // shits heights, and moves y's to begginig of they are higher tha Height
    grid.forEach( (line) => {
    line.forEach( x => {
    x.slide(ySpacing)
    })
    })
    //grid[grid.length -1] = createLine(, amtOfHorizontalCircles, 0, 0)
    }


    function setup () {
    createCanvas(300, canvasHeight)
    background("#140c28")
    grid = createGrid(, linesAmt, 10)
    setInterval(slideRow, 1000)
    }

    function draw () {
    background("#140c28")
    grid.forEach( row => {
    row.forEach( x => {
    x.display()
    })
    })
    }









    share|improve this question



























      1












      1








      1








      Hi am creating a 2D array of circles, which slides down every second.



      Right now if the height of the circles is bigger than the sketch height, it resets the height of the circles to the top.



      enter image description here



      So, i generate once a grid and i just slide it, cyclically.



      Now, i want it not be bee just a grid repeating itself. I want it that it generates a new row at the beggining everytime, so that it never repeats itself...



      But i cant get it to work , i have tried many things but none work..



      Any ideas of how can i get this behaviour?



      The code works this way:



      There is a circle factory function, a line factory functionm and i grid factory function.



      The grid factory calls the line factory and this one calls the circle one.



      Then on setup i have a setInterval function that, every seconds calls the slide method on the circle object. and if its height is bigger than the height it resets the height to the top.



      Here is the codepen:
      https://codepen.io/giorgiomartini/pen/MzGmwW



       let canvasHeight = 500
      let row
      let grid
      let amtOfHorizontalCircles = 15
      let linesAmt = 50
      let ySpacing = 10
      let lineSpacing = canvasHeight/linesAmt

      const createCircle = (fillColor, x, y, maxRadius) => {
      let xpos = x
      let ypos = y
      return {
      xpos,
      ypos,
      display() {
      noStroke()
      fill(fillColor)
      ellipse(xpos, ypos, maxRadius, maxRadius)
      },
      slide(amt) {
      if( ypos > linesAmt * lineSpacing ) {
      ypos = lineSpacing
      }
      ypos += amt
      },
      }
      }

      const createLine = (arr, amt,x, y) => {
      if( arr.length < amt) {
      arr.push(createCircle(`rgba(205, 64, 24, ${Math.random().toFixed(1)})`, x, y, Math.random()*9))
      createLine(arr, amt, x+=width/amtOfHorizontalCircles, y)
      }
      return arr
      }


      const createGrid = (arr, linesAmt, y) => {
      if( arr.length < linesAmt) {
      arr.push(createLine(, amtOfHorizontalCircles, 0, y))
      createGrid(arr, linesAmt, y += lineSpacing)}
      return arr
      }

      const slideRow = () => {
      // shits heights, and moves y's to begginig of they are higher tha Height
      grid.forEach( (line) => {
      line.forEach( x => {
      x.slide(ySpacing)
      })
      })
      //grid[grid.length -1] = createLine(, amtOfHorizontalCircles, 0, 0)
      }


      function setup () {
      createCanvas(300, canvasHeight)
      background("#140c28")
      grid = createGrid(, linesAmt, 10)
      setInterval(slideRow, 1000)
      }

      function draw () {
      background("#140c28")
      grid.forEach( row => {
      row.forEach( x => {
      x.display()
      })
      })
      }









      share|improve this question
















      Hi am creating a 2D array of circles, which slides down every second.



      Right now if the height of the circles is bigger than the sketch height, it resets the height of the circles to the top.



      enter image description here



      So, i generate once a grid and i just slide it, cyclically.



      Now, i want it not be bee just a grid repeating itself. I want it that it generates a new row at the beggining everytime, so that it never repeats itself...



      But i cant get it to work , i have tried many things but none work..



      Any ideas of how can i get this behaviour?



      The code works this way:



      There is a circle factory function, a line factory functionm and i grid factory function.



      The grid factory calls the line factory and this one calls the circle one.



      Then on setup i have a setInterval function that, every seconds calls the slide method on the circle object. and if its height is bigger than the height it resets the height to the top.



      Here is the codepen:
      https://codepen.io/giorgiomartini/pen/MzGmwW



       let canvasHeight = 500
      let row
      let grid
      let amtOfHorizontalCircles = 15
      let linesAmt = 50
      let ySpacing = 10
      let lineSpacing = canvasHeight/linesAmt

      const createCircle = (fillColor, x, y, maxRadius) => {
      let xpos = x
      let ypos = y
      return {
      xpos,
      ypos,
      display() {
      noStroke()
      fill(fillColor)
      ellipse(xpos, ypos, maxRadius, maxRadius)
      },
      slide(amt) {
      if( ypos > linesAmt * lineSpacing ) {
      ypos = lineSpacing
      }
      ypos += amt
      },
      }
      }

      const createLine = (arr, amt,x, y) => {
      if( arr.length < amt) {
      arr.push(createCircle(`rgba(205, 64, 24, ${Math.random().toFixed(1)})`, x, y, Math.random()*9))
      createLine(arr, amt, x+=width/amtOfHorizontalCircles, y)
      }
      return arr
      }


      const createGrid = (arr, linesAmt, y) => {
      if( arr.length < linesAmt) {
      arr.push(createLine(, amtOfHorizontalCircles, 0, y))
      createGrid(arr, linesAmt, y += lineSpacing)}
      return arr
      }

      const slideRow = () => {
      // shits heights, and moves y's to begginig of they are higher tha Height
      grid.forEach( (line) => {
      line.forEach( x => {
      x.slide(ySpacing)
      })
      })
      //grid[grid.length -1] = createLine(, amtOfHorizontalCircles, 0, 0)
      }


      function setup () {
      createCanvas(300, canvasHeight)
      background("#140c28")
      grid = createGrid(, linesAmt, 10)
      setInterval(slideRow, 1000)
      }

      function draw () {
      background("#140c28")
      grid.forEach( row => {
      row.forEach( x => {
      x.display()
      })
      })
      }






      javascript processing p5.js






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 '18 at 16:35







      Giorgio Martini

















      asked Nov 25 '18 at 16:30









      Giorgio MartiniGiorgio Martini

      16218




      16218
























          1 Answer
          1






          active

          oldest

          votes


















          2















          I want it that it generates a new row at the beginning every time, so that it never repeats itself...




          Generate a new random radius for every circle, which is "flipped" from the bottom to the top:



          slide(amt) {
          if( ypos > linesAmt * lineSpacing ) {
          ypos = lineSpacing
          maxRadius = Math.random()*9 // <-- new random radius for the circle
          }
          ypos += amt
          }





          share|improve this answer
























          • Awesome! thanks... i had tried something very similar but didnt work somehow... thanks anyway! :)

            – Giorgio Martini
            Nov 25 '18 at 17:11











          • @GiorgioMartini You're welcome.

            – Rabbid76
            Nov 25 '18 at 17:12











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53469544%2fp5js-sliding-matrix-of-circles-cant-generate-or-fill-new-row-of-circles-in-2d%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2















          I want it that it generates a new row at the beginning every time, so that it never repeats itself...




          Generate a new random radius for every circle, which is "flipped" from the bottom to the top:



          slide(amt) {
          if( ypos > linesAmt * lineSpacing ) {
          ypos = lineSpacing
          maxRadius = Math.random()*9 // <-- new random radius for the circle
          }
          ypos += amt
          }





          share|improve this answer
























          • Awesome! thanks... i had tried something very similar but didnt work somehow... thanks anyway! :)

            – Giorgio Martini
            Nov 25 '18 at 17:11











          • @GiorgioMartini You're welcome.

            – Rabbid76
            Nov 25 '18 at 17:12
















          2















          I want it that it generates a new row at the beginning every time, so that it never repeats itself...




          Generate a new random radius for every circle, which is "flipped" from the bottom to the top:



          slide(amt) {
          if( ypos > linesAmt * lineSpacing ) {
          ypos = lineSpacing
          maxRadius = Math.random()*9 // <-- new random radius for the circle
          }
          ypos += amt
          }





          share|improve this answer
























          • Awesome! thanks... i had tried something very similar but didnt work somehow... thanks anyway! :)

            – Giorgio Martini
            Nov 25 '18 at 17:11











          • @GiorgioMartini You're welcome.

            – Rabbid76
            Nov 25 '18 at 17:12














          2












          2








          2








          I want it that it generates a new row at the beginning every time, so that it never repeats itself...




          Generate a new random radius for every circle, which is "flipped" from the bottom to the top:



          slide(amt) {
          if( ypos > linesAmt * lineSpacing ) {
          ypos = lineSpacing
          maxRadius = Math.random()*9 // <-- new random radius for the circle
          }
          ypos += amt
          }





          share|improve this answer














          I want it that it generates a new row at the beginning every time, so that it never repeats itself...




          Generate a new random radius for every circle, which is "flipped" from the bottom to the top:



          slide(amt) {
          if( ypos > linesAmt * lineSpacing ) {
          ypos = lineSpacing
          maxRadius = Math.random()*9 // <-- new random radius for the circle
          }
          ypos += amt
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '18 at 17:05









          Rabbid76Rabbid76

          36.5k113247




          36.5k113247













          • Awesome! thanks... i had tried something very similar but didnt work somehow... thanks anyway! :)

            – Giorgio Martini
            Nov 25 '18 at 17:11











          • @GiorgioMartini You're welcome.

            – Rabbid76
            Nov 25 '18 at 17:12



















          • Awesome! thanks... i had tried something very similar but didnt work somehow... thanks anyway! :)

            – Giorgio Martini
            Nov 25 '18 at 17:11











          • @GiorgioMartini You're welcome.

            – Rabbid76
            Nov 25 '18 at 17:12

















          Awesome! thanks... i had tried something very similar but didnt work somehow... thanks anyway! :)

          – Giorgio Martini
          Nov 25 '18 at 17:11





          Awesome! thanks... i had tried something very similar but didnt work somehow... thanks anyway! :)

          – Giorgio Martini
          Nov 25 '18 at 17:11













          @GiorgioMartini You're welcome.

          – Rabbid76
          Nov 25 '18 at 17:12





          @GiorgioMartini You're welcome.

          – Rabbid76
          Nov 25 '18 at 17:12


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53469544%2fp5js-sliding-matrix-of-circles-cant-generate-or-fill-new-row-of-circles-in-2d%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)