xxxxxxxxxx
78
function setup() {
createCanvas(800, 800);
// remember to set the angle mode that rotating is more natural for us
angleMode(DEGREES);
// we'll stop animation, otherwise our toast will be all jittery
noLoop()
}
// some useful functions
function randomInteger (min, max) {
return Math.floor(min + ((max-min) * Math.random()))
}
function pick (inputArray) {
return inputArray[randomInteger(0,inputArray.length)]
}
function buildArray (n, fillFunction) {
let outputArray = [];
for (let i = 0; i < n; i++) {
outputArray.push(fillFunction(i))
}
return outputArray
}
class Point {
constructor(x,y) {
this.x = x;
this.y = y;
}
move(xDistance, yDistance) {
return new Point(this.x + xDistance, this.y + yDistance)
}
}
// defining our toast
class Toast {
constructor(size, tColor, point) {
this.toastSize = size;
this.toastColor = tColor;
this.xPosition = point.x;
this.yPosition = point.y;
}
draw() {
fill(color(this.toastColor));
// let's randomly rotate the toast so that it's cute
// remember to translate before you rotate or the toast is going to be in a crazy place
translate(this.xPosition, this.yPosition)
// we'll use pick here so that we can rotate either right or left randomly
rotate(15 * pick([1,-1]) * Math.random())
rect(0,0, this.toastSize, this.toastSize);
resetMatrix()
}
}
// data for toast
// we can use the remainder operator to make a grid
let somePoints = buildArray (100, i => new Point(100 + ((i%10) * 50), 100 + (50 * Math.floor(i/10))))
let toastColors = ['peru',"blanchedalmond","burlywood","tan","saddlebrown"];
// actually making the toast
let someToast = buildArray (100, i=> new Toast (20, pick(toastColors), somePoints[i]))
let filteredToast = someToast.filter (t => t.toastColor == 'peru' || t.toastColor == 'saddlebrown')
// drawing the toast
function draw() {
background(220);
filteredToast.forEach(t => t.draw())
}