Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WinMouseX and WinMouseY not set properly when clicking on an option in select element #3066

Closed
3 of 15 tasks
dekmm opened this issue Jul 7, 2018 · 5 comments
Closed
3 of 15 tasks

Comments

@dekmm
Copy link
Contributor

dekmm commented Jul 7, 2018

Nature of issue?

  • Found a bug

Most appropriate sub-area of p5.js?

  • Color
  • Core/Environment/Rendering
  • Data
  • Events
  • Image
  • IO
  • Math
  • Typography
  • Utilities
  • WebGL
  • Other (specify if possible)

Which platform were you using when you encountered this?

  • Mobile/Tablet (touch devices)
  • Desktop/Laptop
  • Others (specify if possible)

Details about the bug:

  • p5.js version: 0.6.1
  • Web browser and version: Firefox 62.0b6 (64-bit)
  • Operating System: Windows 10
  • Steps to reproduce this:

The bug is regarding the winMouseX and winMouseY variables used in the mouseReleased() event. The documentation for the variables says that they store the current horizontal/vertical position of the mouse point relative to the origin point of the window. Now, I'm assuming window in this case means the browser window. If so, I will carry. If not, the variables are probably working as they should.

I was recently experimenting with some pathfinding algorithms and so I needed a playground to run tests on. The playground consisted of a simple grid which was filling up the entire canvas (the width and height of the canvas are always equal to either the width or the height of the browser window depending if the window was in portrait or landscape mode) and right next to the canvas is a select element with some options to set walkable and unwalkable tiles on to the grid and things of that nature.

Before adding the select element, I implemented a simple mouseReleased() event which would just add an unwalkable node to the grid. Nothing special

function mouseReleased() {
  let tileX = Math.floor(map(mouseX, 0, canvasSize, 0, grid.tilesX))
  let tileY = Math.floor(map(mouseY, 0, canvasSize, 0, grid.tilesY))
  if (tileX >= grid.tilesX || tileY >= grid.tilesY) return false
  grid.set(new UnwalkableNode(tileX, tileY)) // Not related to the issue, just creates a black rectangle in the grid
}

But after adding the select element next to the canvas and selecting any one of the options, the tile at position (0, 0) would get changed to an unwalkable node which is indicated by a black rectangle on the canvas. I figured, since I created the select element through p5, it probably has a mouseReleased() event as well and manages the mouseX and mouseY. So I added a simple check to see if the mouse pointer is within the canvas by using winMouseX and winMouseY

function mouseReleased() {
  let canvas = select("#canvas")
  let canvasStyle = canvas.elt.style
  let minX = Pixels.toInt(canvasStyle.left) //Pixels.toInt() convers a string of format 0px to an integer, not significant in the issue
  let minY = Pixels.toInt(canvasStyle.top)
  let maxX = Pixels.toInt(canvasStyle.width)   minX
  let maxY = Pixels.toInt(canvasStyle.height)   minY

  console.log(`Mouse in canvas: ${(winMouseX >= minX && winMouseX <= maxX) && (winMouseY >= minY && winMouseY <= maxY)}`)
  console.log(`WinMouseX: ${winMouseX}, WinMouseY: ${winMouseY}`)
  console.log(`Canvas MinX: ${minX}, Canvas MinY: ${minY}, Canvas MaxX: ${maxX}, Canvas MaxY: ${maxY}`)

  if ((winMouseX >= minX && winMouseX <= maxX) && (winMouseY >= minY && winMouseY <= maxY)) {
    let tileX = Math.floor(map(mouseX, 0, canvasSize, 0, grid.tilesX))
    let tileY = Math.floor(map(mouseY, 0, canvasSize, 0, grid.tilesY))
    if (tileX >= grid.tilesX || tileY >= grid.tilesY) return false
    grid.set(new UnwalkableNode(tileX, tileY))
  }
}

Since I can't paste the entire project here, I will paste the outputs of the 3 console.logs for some of the tested situations.

Clicked inside the canvas on a tile:

Mouse in canvas: true 
WinMouseX: 102, WinMouseY: 94
Canvas MinX: 0, Canvas MinY: 0, Canvas MaxX: 966, Canvas MaxY: 966

Clicked outside the canvas, not on the select element:

Mouse in canvas: false 
WinMouseX: 1234, WinMouseY: 333 
Canvas MinX: 0, Canvas MinY: 0, Canvas MaxX: 966, Canvas MaxY: 966

Clicked on the select element, not on an option:

Mouse in canvas: false 
WinMouseX: 1058, WinMouseY: 19 
Canvas MinX: 0, Canvas MinY: 0, Canvas MaxX: 966, Canvas MaxY: 966

Clicked on an option inside the select element:

Mouse in canvas: true 
WinMouseX: 0, WinMouseY: 0 
Canvas MinX: 0, Canvas MinY: 0, Canvas MaxX: 966, Canvas MaxY: 966

Now as you can see, whenever I click on a option in the select element, the winMouseX and winMouseY variables just reset to 0 even though the pointer is towards the middle of the window.

@Nishant-Ingle
Copy link

I tried to recreate it, everything is working fine.
Download and run it: https://drive.google.com/open?id=1C0N6aAMAcKIw9PFSI5Wod144xjWx_mCN

Try select any option from select and you will see that winMouseX and winMouseY are not resetted to 0. If this doesn't solve the problem then I would request you to share the code just as I did.

@dekmm
Copy link
Contributor Author

dekmm commented Jan 8, 2020

Hi there,

considering I opened this issue a year and a half ago, I can't really provide the code I used back then.

I would just like to know which version of p5.js you used when testing this? It's certainly possible that this was resolved given the time frame.

@Nishant-Ingle
Copy link

Hi there,

considering I opened this issue a year and a half ago, I can't really provide the code I used back then.

I would just like to know which version of p5.js you used when testing this? It's certainly possible that this was resolved given the time frame.

p5.js v0.10.2 October 14, 2019

@dekmm
Copy link
Contributor Author

dekmm commented Jan 8, 2020

That might explain why you're getting a different result. I've experienced this issue on version 0.6.1, which is quite old by now

@montoyamoraga
Copy link
Member

since this might have been an old bug,
that was resolved with newer versions,
and also we can't replicate this issue,
i am closing it :)

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

No branches or pull requests

3 participants