Skip to content
This repository was archived by the owner on Mar 15, 2024. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@ const diff_enum = {
};

// Utility Functions
const mv_x = (what, x) => what.style.left = x,
mv_y = (what, y) => what.style.top = y,
rand = (limit) => Math.random() * limit,
inRange = (lower, upper, what) => what >= lower && what <= upper;
const mv_x = function mv_x(what, x) {
return what.style.left = x;
},
mv_y = function mv_y(what, y) {
return what.style.top = y;
},
rand = function rand(limit) {
return Math.random() * limit;
},
inRange = function inRange(lower, upper, what) {
return what >= lower && what <= upper;
};

// Selectors. Duh!
const catch_me = document.getElementById("catch_me"),
Expand All @@ -22,12 +30,22 @@ const catch_height = 50,
catch_width = 100,
difficulty = diff_enum.HARD; // CHANGE THIS FOR MORE FUN

const incr_counter = () => counter.innerText = +counter.innerText + 1;
const incr_counter = function incr_counter() {
return counter.innerText = +counter.innerText + 1;
};
// Next positions should be within the visible portion of the window.
const rand_x = () => rand(window.innerWidth - catch_width),
rand_y = () => rand(window.innerHeight - catch_height),
mv_catch_x = () => mv_x(catch_me, rand_x()),
mv_catch_y = () => mv_y(catch_me, rand_y());
const rand_x = function rand_x() {
return rand(window.innerWidth - catch_width);
},
rand_y = function rand_y() {
return rand(window.innerHeight - catch_height);
},
mv_catch_x = function mv_catch_x() {
return mv_x(catch_me, rand_x());
},
mv_catch_y = function mv_catch_y() {
return mv_y(catch_me, rand_y());
};

// Initial State
catch_me.style.position = "absolute";
Expand All @@ -39,14 +57,14 @@ mv_catch_y();

// If you can get your mouse inside here you get a point.
// But the question is, can you? ;)
catch_me.addEventListener("mouseenter", () => {
catch_me.addEventListener("mouseenter", function () {
mv_catch_x();
mv_catch_y();
incr_counter();
});

// Track mouse movent inside body.
body.addEventListener("mousemove", e => {
body.addEventListener("mousemove", function (e) {
const delta_x = e.offsetX - catch_me.offsetLeft,
delta_y = e.offsetY - catch_me.offsetTop,
xInRange = inRange(-difficulty, difficulty + catch_width, delta_x),
Expand Down