xxxxxxxxxx
161
let bhbefore;
let bhafter;
let stormImg;
let reset;
let temp;
let alphaVal = 0;
let isErasing;
//variables for storm
let destroy = null;
let stormX, stormY;
let stormXspeed = 4;
let stormYspeed = 4;
//variables for earth breathing
let breath = 200;
let breathSpeed = 4;
function preload() {
bhbefore = loadImage("/assets/bahamas_before.jpg");
bhafter = loadImage("/assets/bahamas_after.jpg");
stormImg = loadImage("/assets/storm.png");
temp = loadImage("assets/bahamas_before.jpg");
}
function setup() {
createCanvas(windowWidth, windowHeight);
bhbefore.resize(windowWidth, windowHeight);
bhafter.resize(windowWidth, windowHeight);
stormX = random(width / 4);
stormY = random(height / 4);
}
function draw() {
reSet();
bhafter.loadPixels();
bhbefore.loadPixels();
bhbefore.updatePixels();
// tinting 'after' photo
// alpha needs to stay at 255
push();
tint(183, 90, 50, 255);
image(bhafter, 0, 0);
pop();
image(bhbefore, 0, 0);
bhafter.updatePixels();
//control earth breathing with bounce and tint
breath += breathSpeed;
breathSpeed = bounce(breath, 0, 255, breathSpeed);
//console.log(breath);
tint(155, 242, 183, floor(breath));
image(bhbefore, 0, 0);
noTint();
if (millis() > 10000){
//console.log("2 seconds");
movingStorm();
}
}
function movingStorm() {
stormX += stormXspeed * random(-1, 5);
stormY += stormYspeed * random(-2, 3);
// stormX += stormXspeed + sin(frameCount * random(2, 40));
// stormY += stormYspeed + sin(frameCount * random(1, 15));
//bounce pos unless destroy equals true
if (destroy !== true){
stormXspeed = bounce(stormX, 0, width * 0.8, stormXspeed);
stormYspeed = bounce(stormY, 0, height * 0.8, stormYspeed);
} else {
stormXspeed = 10
stormYspeed = 10
}
//image of actual storm
push();
imageMode(CENTER);
image(stormImg, stormX, stormY);
pop();
isErasing = true;
eraseDrawAlpha(alphaVal);
}
function wobble(slightwobble, bigwobble) {
let value = sin(frameCount * slightwobble) * bigwobble;
return value;
}
//function to erase or draw passing in alphaVal
function eraseDrawAlpha(a) {
//time the sketch
let sec = floor(millis() / 1000);
//seconds until the island is destroyed
let timeDestroy = 50;
sizeErase1 = 60;
sizeErase2 = 60;
//erase just under the storm until timeDestroy has elapsed.
for (let r = 1; r < sizeErase1; r++) {
for (let m = 1; m < sizeErase2; m++) {
if (sec > timeDestroy) {
sizeErase1 = width;
sizeErase2 = height;
destroy = true;
bhbefore.set(r, m, [bhbefore.pixels[r], bhbefore.pixels[r + 1], bhbefore.pixels[r + 2], a]);
} else {
bhbefore.set(stormX + r, stormY + m, [bhbefore.pixels[r], bhbefore.pixels[r + 1], bhbefore.pixels[r + 2], a]);
}
}
}
}
//reset if space key is pressed
function reSet() {
if (reset == true) {
bhbefore = temp;
bhbefore.resize(windowWidth, windowHeight);
bhbefore.loadPixels();
reset = false;
temp = loadImage("assets/bahamas_before.jpg");
}
}
function bounce(pos, low, high, speed) {
if (pos < low || pos > high) return speed *= -1;
else return speed
}
function keyPressed() {
if (keyCode === LEFT_ARROW) {
alphaVal = 255;
isErasing = true;
console.log("alphaVal = " + alphaVal);
console.log("is isErasing " + isErasing);
} else if (keyCode === RIGHT_ARROW) {
alphaVal = 0;
isErasing = false;
console.log("alphaVal = " + alphaVal);
console.log("is isErasing " + isErasing);
} else if (keyCode === 32) {
reset = true;
}
}