let colors = ["#7344AC", "#E87CBE", "#F5E993", "#7396A6", "#69D3C1"];
createCanvas(width, height);
squares.push(new Square(0, 0, width, height));
for (var i = 0; i < width; i += step) {
splitSquaresWith({ y: i });
splitSquaresWith({ x: i });
for (let i = 0; i < squares.length; i++) {
squares[i].displayRadial();
constructor(x, y, width, height) {
drawingContext.strokeStyle = "#000";
drawingContext.lineWidth = 6;
let shuffledColors = shuffle(colors);
let gradient = shuffledColors[0];
let x2 = this.x+this.width;
if (abs(this.width - this.height) < 0.1) {
if (this.width > this.height) {
gradient = drawingContext.createLinearGradient(x1, y1, x2, y2);
gradient.addColorStop(0, color(shuffledColors[0]));
gradient.addColorStop(0.5, color(shuffledColors[1]));
gradient.addColorStop(1, color(shuffledColors[2]));
drawingContext.fillStyle = gradient;
rect(this.x, this.y, this.width, this.height);
drawingContext.strokeStyle = "#000";
drawingContext.lineWidth = 5;
let shuffledColors = shuffle(colors);
let gradient = shuffledColors[0];
if (abs(this.width - this.height) < 0.1) {
let xr = this.x+this.width/2;
let yr = this.y+this.height/2;
gradient = drawingContext.createRadialGradient(xr, yr, 0, xr, yr, this.width/2);
gradient.addColorStop(0, color(shuffledColors[0]));
gradient.addColorStop(0.5, color(shuffledColors[1]));
gradient.addColorStop(1, color(shuffledColors[2]));
let x2 = this.x+this.width;
if (this.width > this.height) {
gradient = drawingContext.createLinearGradient(x1, y1, x2, y2);
gradient.addColorStop(0, color(shuffledColors[0]));
gradient.addColorStop(0.5, color(shuffledColors[1]));
gradient.addColorStop(1, color(shuffledColors[2]));
drawingContext.fillStyle = gradient;
rect(this.x, this.y, this.width, this.height);
function splitSquaresWith(coordinates) {
const { x, y } = coordinates;
for (let i = squares.length - 1; i >= 0; i--) {
const square = squares[i];
if (x && x > square.x && x < square.x + square.width) {
if (Math.random() > 0.5) {
if (y && y > square.y && y < square.y + square.height) {
if (Math.random() > 0.5) {
function splitOnX(square, splitAt) {
var squareA = new Square(square.x, square.y, square.width - (square.width - splitAt + square.x), square.height);
var squareB = new Square(splitAt, square.y, square.width - splitAt + square.x, square.height);
function splitOnY(square, splitAt) {
var squareA = new Square(square.x, square.y, square.width, square.height - (square.height - splitAt + square.y));
var squareB = new Square(square.x, splitAt, square.width, square.height - splitAt + square.y);