xxxxxxxxxx
65
/**
* Laura Tessin
* Prompt: the (flash)light at the end of the tunnel
*
*/
let canvas_size;
let p, image_x, image_y;
const delay = 600;
const flashlight = 100;
const image_width = 70;
let frame = 0;
let found_it = false;
function setup() {
// set up canvas
canvas_size = windowHeight;
createCanvas(canvas_size, canvas_size);
background(20);
// initialize icon
p = createP(`<img src="recurse.png" width="${image_width}">`);
p.style('z-index', '-9999');
position_p();
}
function position_p() {
image_x = random(0, canvas_size - image_width);
image_y = random(0, canvas_size - image_width - 30);
p.position(image_x, image_y);
}
function draw() {
background(30);
// add flashlight effect
if (mouseIsPressed) {
erase(220, 10);
ellipse(mouseX, mouseY, flashlight);
noErase();
}
// let icon escape
if (mouseIsPressed &&
(mouseX >= image_x && mouseX <= image_x + image_width) &&
(mouseY >= image_y && mouseY <= image_y + image_width + 30)) {
// add delay when icon was found
let now = millis();
if (!found_it) {
frame = now;
found_it = true;
return;
}
if (frame + delay >= now) return;
// reposition p
position_p();
found_it = false;
}
}