Atlassian uses cookies to improve your browsing experience, perform analytics and research, and conduct advertising. Accept all cookies to indicate that you agree to our use of cookies on your device. Atlassian cookies and tracking notice, (opens new window)
Confluence
For you

Programming
Results will update as you type.
  • Tiling and Repetition
  • Reactors
  • Programming Basics: Parametric and Generative Graphic Design 2016
  • Archive
  • High Scores
  • Artificial Neural Network
  • Alternatives to the Processing IDE
  • p5.js Programming
    • p5.js Introduction
    • p5.js Variables
    • p5.js Random Numbers
    • p5.js SVG + Images
    • p5.js WebGL
    • p5.js Classes and Objects
    • p5.js Events and Functions
    • p5.js Loops
    • p5.js Coordinates
    • P5js Nested Loops
    • p5.js Animation Exercise 1
    • p5.js Animation Exercise 2
    • p5.js Conditionals
    • p5.js Arrays and Lists
    • p5.js Simple Collision Detection
    • p5.js Reactors
    • p5.js Tiling and Repetition
    • p5.js Vectors
    • p5.js Animation Solution with Objects
    • p5.js Easing
    • p5.js Perlin Noise
    • p5.js Particle System
    • p5.js Sound
    • p5j.s Typography
    • P5js Archive
  • Programming in Processing (java)

/
p5.js Tiling and Repetition
Updated Aug 17, 2021

p5.js Tiling and Repetition

Analytics

Loading data...

Creating grids


Arrays in JavaScript have quirks of their own that you may not be expecting if you come from other languages. Two important ones for your use case are:

  • You cannot directly declare multidimensional arrays in JavaScript.

  • There's little efficiency benefit (and no added safety) when you set the size of the array at creation.

In order to create and initialize a multidimensional array, you first need to go through all elements of the one-dimensional array and add a second (third, fourth, etc.) dimension to it.

var x_length = 100 var y_length = 100 var map = [] for (var x = 0; x < x_length; x++) { map[x] = [] for (var y = 0; y < y_length; y++) { map[x][y] = new Element(); } }
let gridW = 10; //amount of shapes in the width let gridH = 10; //amount of shapes in the height let stepX = 60; // horizontal distance between shapes let stepY = 60; // vertical distance between shapes let shapeWidth = 40; let shapeHeight = 40; var GraphicArray = []; function setup() { createCanvas(800, 800); noStroke(); fill(0); background(255); translate((width-(stepX*gridW))/2, (height-(stepY*gridH))/2); // translate graphic to the center of the screen for (var i=0; i < gridW; i++) { GraphicArray[i] = []; //add a second dimension to the array for (var j = 0; j < gridH; j++) { GraphicArray[i][j] = new Graphic(i * stepX, j * stepY, shapeWidth, shapeHeight); GraphicArray[i][j].draw(); } } } class Graphic { constructor(x,y, shapeW, shapeH) { this._pos = createVector(x, y); this._shapeWidth = shapeW; this._shapeHeight = shapeH; } draw() { push(); stroke(0); fill(0); rect(this._pos.x, this._pos.y, this._shapeWidth, this._shapeHeight); pop(); } }

Example: Modifying distribution along width and height

let gridW = 10; let gridH = 10; let border = 100; let stepX = 40; let stepY = 40; let distModifier = 1.05; let graphicSize = 30; function setup() { createCanvas(800,800); background(255); noStroke(); fill(0); let distX = stepX; let distY = stepY; translate(border,border); for(var i=0;i < gridH;i++){ for(var j= 0;j < gridW;j++) { rect(j * distX, i * distY, graphicSize, graphicSize); distX *= distModifier; } distX = stepX; distY *= distModifier; } }

Example: Modifying  distribution with scale

let gridW = 12; let gridH = 12; let border = 60; let stepX = 60; let stepY = 60; let sizeModifier = 1.05; let graphicSize = 30; function setup() { createCanvas(800, 800); noStroke(); fill(0); background(255); rectMode(CENTER); let graphicSizeScaled = graphicSize; translate(border, border); for (var i=0; i < gridH; i++) { for (var j = 0; j < gridW; j++) { rect(j * stepX, i * stepY, graphicSizeScaled, graphicSizeScaled); } graphicSizeScaled *= sizeModifier; } }


Example: Modifying distribution by rotation

 

let gridW = 12; let gridH = 12; let border = 60; let stepX = 60; let stepY = 60; let sizeModifier = 1.05; let graphicSize = 30; function setup() { createCanvas(800, 800); noStroke(); fill(0); background(255); rectMode(CENTER); var rotation = 0; translate(border, border); for (var i=0; i < gridH; i++) { for (var j = 0; j < gridW; j++) { rotation++; push(); translate(j * stepX, i * stepY); rotate(radians(rotation)); rect(0, 0, graphicSize, graphicSize); pop(); } } }
Related content
p5.js Arrays and Lists
p5.js Arrays and Lists
Luke Franzke
p5.js Introduction
p5.js Introduction
Luke Franzke
p5.js Coordinates
p5.js Coordinates
Luke Franzke
p5.js Animation Exercise 1
p5.js Animation Exercise 1
Luke Franzke
p5.js Particle System
p5.js Particle System
paulina.zybinska
p5.js Simple Reactor
p5.js Simple Reactor
Luke Franzke
{"serverDuration": 13, "requestCorrelationId": "72774102480c4144baaa9aff19052816"}