xxxxxxxxxx
/*
Instructions:
- Press 'a' to enable automatic mode
- Press 'm' to switch to manual mode
- Press 'p' to change posters
- Press 'b' to change brushes
- Press 'x' to apply blur
- Press 'z' to reset the canvas
- Use 0-9 keys to control brush speed
- Press 's' to save the canvas
*/
var posters = []; // posters array
var brushes = []; // brushes array
var currentPosterIndex = 0; // current poster index
var currentBrushIndex = 0; // current brush index
var isManual = false; // control mode
var brushSpeed = 1; // brush speed on start
var posX, posY; // coordinates
var isBlurred = false; // blur effect status
function preload() {
// load posters
for (let i = 1; i <= 8; i++) {
posters.push(loadImage("poster" + i + ".jpg"));
}
// load brushes
for (let i = 1; i <= 8; i++) {
brushes.push(loadImage("brush" + i + ".png"));
}
}
function setup() {
createCanvas(1280, windowHeight); // canvas size
background(0); // background
image(posters[currentPosterIndex], 0, 0, width, height); // draw first poster
frameRate(60); // framerate
setBrushSpeed(1); // brush speed on startup
enableRandomMode(); // start automatically on startup
}
function draw() {
// set the coordinates
if (isManual) {
posX = mouseX;
posY = mouseY;
if (mouseIsPressed) {
applyBrush(posX, posY);
}
} else {
posX = int(random(0, width));
posY = int(random(0, height));
applyBrush(posX, posY);
}
// blur effect
if (isBlurred) {
filter(BLUR, 3);
}
}
function applyBrush(x, y) {
noErase();
image(brushes[currentBrushIndex], x - 50, y - 50, 100, 100);
}
// key shortcuts
function keyPressed() {
if (key === "m") {
enableUserMode();
} else if (key === "a") {
enableRandomMode();
} else if (key === "p") {
changePoster();
} else if (key === "b") {
changeBrush();
} else if (key === "s") {
saveCanvas('Vanishing.Sunshine.jpg');
} else if (key === "x") {
enableBlur();
} else if (key === "z") {
disableBlur();
} else if (keyCode >= 48 && keyCode <= 57 && !isManual) {
setBrushSpeed(key - "0");
}
}
function enableBlur() {
isBlurred = true;
}
function disableBlur() {
isBlurred = false;
// reset canvas
background(0);
image(posters[currentPosterIndex], 0, 0, width, height);
}
function enableUserMode() {
isManual = true;
frameRate(60); // manual mode framerate
}
function enableRandomMode() {
isManual = false;
frameRate(brushSpeed); // brush speed on automatic mode
}
// change posters by using loop
function changePoster() {
currentPosterIndex = (currentPosterIndex + 1) % posters.length; // move to next poster
background(0); // background
image(posters[currentPosterIndex], 0, 0, width, height); // draw new poster
}
// change brushes by using loop
function changeBrush() {
currentBrushIndex = (currentBrushIndex + 1) % brushes.length; // move to next brush
}
function setBrushSpeed(speed) {
switch (speed) {
case 0:
brushSpeed = 0; // stop
break;
case 1:
brushSpeed = 1; // slowest
break;
case 5:
brushSpeed = 5; // medium
break;
case 9:
brushSpeed = 10; // fastest
break;
default:
brushSpeed = 5; // default
break;
}
if (!isManual) {
frameRate(brushSpeed); // set brush speed automatically
}
}