Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Finding mouse position on canvas. Is there a better way?

Participant ,
Aug 08, 2018 Aug 08, 2018

I have a canvas 2000 px wide, which fits the width of the client window.

If I use a mouse event with event.clientX I must calculate the relative position with respect to the window width.

If I use a mouse event with event.clientY I must calculate the relative position with respect to the window width plus offset due to the scrolling of the window.

$(canvas).mousedown(grdown.bind(this));

function grdown(e){

var rect = canvas.getBoundingClientRect();

console.log("clientX   " + (e.clientX * (2000/canvas.width)) + "    clientY   " + ((e.clientY * (2000/canvas.width))-rect.top * (2000/canvas.width)    )    );

}            //          this eventListener is done with jQuery

Is there a simpler way to do this?

3.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Aug 08, 2018 Aug 08, 2018

Why on Earth are you using jQuery? Canvas mode's API has a full-featured mouse event system built right in.

EaselJS v1.0.0 API Documentation : MouseEvent

EaselJS Tutorial: Mouse Interaction

Example code for reporting mouse click location:

stage.addEventListener("stagemousedown", mouseClicked);

function mouseClicked(evt) {

    console.log(evt.stageX, evt.stageY);

}

If the canvas can scale, just divide the coordinates by stage.scaleX and stage.scaleY to get normalized values.

Translate
Aug 08, 2018 Aug 08, 2018

Hi PATurmel12,

I am just a beginner with Js and AS coding. However tried to find out some relevant information for you.

Please check out the below links and see if they can trigger some idea in your mind with respect to the codes.

Hope this helps.

Thanks!

Ankush

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 08, 2018 Aug 08, 2018

Why on Earth are you using jQuery? Canvas mode's API has a full-featured mouse event system built right in.

EaselJS v1.0.0 API Documentation : MouseEvent

EaselJS Tutorial: Mouse Interaction

Example code for reporting mouse click location:

stage.addEventListener("stagemousedown", mouseClicked);

function mouseClicked(evt) {

    console.log(evt.stageX, evt.stageY);

}

If the canvas can scale, just divide the coordinates by stage.scaleX and stage.scaleY to get normalized values.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 08, 2018 Aug 08, 2018

Thanks for you response,

JQuery gives a much more responsive web page.

Your suggestion of using stage.scaleX does not allow for the offset due to scrolling.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 08, 2018 Aug 08, 2018
LATEST

You said you wanted to find the mouse position on the canvas. That's what the code I posted does.

> Your suggestion of using stage.scaleX does not allow for the offset due to scrolling.

I have no idea what this is supposed to mean. Scaling and scrolling are completely different things.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines