Skip to content

Conversation

@mattdesl
Copy link
Contributor

Hello!

This changes the way mouse and touch values are stored to address the following two tickets:
#1176
#1269

Currently, mouseX and mouseY values are always lagging behind the real system values when the mouse is moving.

The new approach works like this:

  • On mouse events, set the mouseX and mouseY values so that they represent the actual mouse value at that event
  • After we draw() a frame, we set the pmouseX and pmouseY to the value of mouseX and mouseY, the next time the draw is called it will have the previous mouse position
  • The first time we change mouseX and mouseY, we also set the pmouseX/pmouseY to the same value. This way they don't start at (0, 0) which is a bit arbitrary.
  • Same logic applied for touch events and pwinMouseX / pwinMouseY

Some tests, I'm using the p5js editor.

Example 1

function setup() {
  createCanvas(windowWidth, windowHeight);
}

function draw () {
  line(mouseX, mouseY, pmouseX, pmouseY);
}

Before PR:
screen shot 2016-06-16 at 12 03 32 pm

After PR:
screen shot 2016-06-16 at 12 00 34 pm

Example 2

function setup() {
  createCanvas(windowWidth, windowHeight);
}

function mouseMoved() {
  ellipse(mouseX, mouseY, 50, 50);
}

Before PR:
screen shot 2016-06-16 at 12 03 09 pm

After PR:
screen shot 2016-06-16 at 12 00 51 pm

Let me know what you think! 😄

@lmccart
Copy link
Member

lmccart commented Jun 17, 2016

this is great and makes a lot of sense, thank you!

islemaster added a commit to code-dot-org/code-dot-org that referenced this pull request Nov 9, 2016
The collision update brings with it a p5 version update - we're now on p5.js 0.5.4.  It turns out in [0.5.2](https://github.com/processing/p5.js/releases/tag/0.5.2) a [change to how `mouseX`/`mouseY` are tracked and updated](processing/p5.js#1471) was introduced.  That's code we'd monkeypatched over to scale mouse coordinates with our resizable playspace.

This updates the monkeypatches (for both mouse and touch coordinates) to match the code in p5.js 0.5.4, but keeps our scaling modification intact.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants