xxxxxxxxxx
63
let bx;
let by;
let boxSize = 75;
let overBox = false;
let locked = false;
let xOffset = 0.0;
let yOffset = 0.0;
function setup() {
createCanvas(720, 400);
bx = width / 2.0;
by = height / 2.0;
rectMode(RADIUS);
strokeWeight(6);
}
function draw() {
background(233, 33, 93);
// background(237, 34, 93);
// Test if the cursor is over the box
if (
mouseX > bx - boxSize &&
mouseX < bx + boxSize &&
mouseY > by - boxSize &&
mouseY < by + boxSize
) {
overBox = true;
if (!locked) {
stroke(255);
fill(244, 122, 158);
}
} else {
stroke(0);
fill(244, 122, 158);
overBox = false;
}
// Draw the box
rect(bx, by, boxSize, boxSize);
}
function mousePressed() {
if (overBox) {
locked = true;
fill(255, 255, 255);
} else {
locked = false;
}
xOffset = mouseX - bx;
yOffset = mouseY - by;
}
function mouseDragged() {
if (locked) {
bx = mouseX - xOffset;
by = mouseY - yOffset;
}
}
function mouseReleased() {
locked = false;
}