Skip to content

Commit

Permalink
Fix issue processing#7400 - Issue with mouseX and mouseY when using a…
Browse files Browse the repository at this point in the history
… CSS border
  • Loading branch information
HarshitaKatariya committed Jan 10, 2025
1 parent 4bcbbcd commit 7d923a1
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions test/manual-test-examples/learningprocessing/chp3/example_3_2.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@

// Example 3-2: mouseX and mouseY

let offsetX, offsetY; // Declare variables globally

function setup() {
createCanvas(200, 200);
const rect = canvas.elt.getBoundingClientRect(); // Get canvas position
offsetX = rect.left + window.scrollX;
offsetY = rect.top + window.scrollY;
}

function draw() {
Expand All @@ -19,7 +24,9 @@ function draw() {
fill(175);
rectMode(CENTER);

// mouseX is a keyword that the sketch replaces with the horizontal position of the mouse.
// mouseY is a keyword that the sketch replaces with the vertical position of the mouse.
rect(mouseX, mouseY, 50, 50);
// Corrected mouseX and mouseY for canvas offset
const correctedMouseX = mouseX - offsetX;
const correctedMouseY = mouseY - offsetY;

rect(correctedMouseX, correctedMouseY, 50, 50); // Adjust size as needed
}

0 comments on commit 7d923a1

Please sign in to comment.