Position of Mouse Click Relative to Scene, Not Window

Position of mouse click relative to scene, not window

You need convert your point from view to scene coordinates:

eventPos = view!.convert(event.locationInWindow, to: view!.scene!)

How do I get the coordinates of a mouse click on a canvas element?

If you like simplicity but still want cross-browser functionality I found this solution worked best for me. This is a simplification of @Aldekein´s solution but without jQuery.

function getCursorPosition(canvas, event) {
const rect = canvas.getBoundingClientRect()
const x = event.clientX - rect.left
const y = event.clientY - rect.top
console.log("x: " + x + " y: " + y)
}

const canvas = document.querySelector('canvas')
canvas.addEventListener('mousedown', function(e) {
getCursorPosition(canvas, e)
})

JavaFX MousePosition

I don't understand why you are setting the mouse listener inside the listener for a key frame, but you need to get the coordinates from the mouse event.

MouseEvent defines getX() and getY() to get the coordinates of the mouse event relative to the node itself, getSceneX() and getSceneY() to get the coordinates of the mouse event relative to the whole Scene, and (in Java 8) getScreenX() and getScreenY() to get the coordinates of the mouse event relative to the entrie screen coordinate system.

So, if you're interested in the location of the mouse relative to the window (scene), do

root.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
System.out.println(event.getSceneX());
System.out.println(event.getSceneY());
}
});

JavaFX: how to set tooltip location relative to the mouse cursor?

Although the question is a little bit old,but remains unanswered.

  1. Part 1

What about something that is using Button current position in screen
and set the tooltip position accordingly?

Button button = new Button();
button.setTooltip(new Tooltip("Hey i am a Button"));
button.getTooltip().setOnShowing(s->{

//Get button current bounds on computer screen
Bounds bounds = button.localToScreen(button.getBoundsInLocal());
button.getTooltip().setX(bounds.getMaxX());
button.getTooltip().setY(bounds.getMinY());

});

  1. Part 2

(You can do also with mouse but add somewhere a mouseMoveListener,in
the example it is added on the Button but i would prefer to add it on
the Stage Scene if we are speaking about a lot of Button)

    double currentMouseX;
double currentMouseY;
button.setOnMouseMoved(m->{
currentMouseX=m.getScreenX();
currentMouseY=m.getScreenY();
});

button.getTooltip().setOnShowing(s->{
button.getTooltip().setX(currentMouseX+button.getTooltip().getWidth()+5);
button.getTooltip().setY(currentMouseY);
});

Finnaly:

Actually in the IDE you are using type button.getTooltip().setOn.. and you will see all the available methods that you can use to control tooltip before and after showing or hiding.

How to convert screen coordinates to scene coordinates

See https://github.com/mayognaise/aframe-mouse-cursor-component or https://github.com/mrdoob/three.js/blob/dev/examples/js/controls/DragControls.js or https://www.npmjs.com/package/aframe-click-drag-component for examples

The main chunk of code is like:

    canvas.addEventListener( 'mousemove', function () {

var mouse = new THREE.Vector2();

var rect = canvas.getBoundingClientRect();

mouse.x = ( (event.clientX - rect.left) / rect.width ) * 2 - 1;
mouse.y = - ( (event.clientY - rect.top) / rect.height ) * 2 + 1;

raycaster.setFromCamera( mouse, camera );
}, false);

Find mouse position relative to element

For people using JQuery:

Sometimes, when you have nested elements, one of them with the event attached to it, it can be confusing to understand what your browser sees as the parent. Here, you can specify which parent.

You take the mouse position, and then subtract it from the parent element's offset position.

var x = evt.pageX - $('#element').offset().left;
var y = evt.pageY - $('#element').offset().top;

If you're trying to get the mouse position on a page inside a scrolling pane:

var x = (evt.pageX - $('#element').offset().left) + self.frame.scrollLeft();
var y = (evt.pageY - $('#element').offset().top) + self.frame.scrollTop();

Or the position relative to the page:

var x = (evt.pageX - $('#element').offset().left) + $(window).scrollLeft();
var y = (evt.pageY - $('#element').offset().top) + $(window).scrollTop();

Note the following performance optimisation:

var offset = $('#element').offset();
// Then refer to
var x = evt.pageX - offset.left;

In this way, JQuery does not have to look up #element for each line.

Update

There is a newer, JavaScript-only version in an answer by @anytimecoder -- see also browser support for getBoundingClientRect().

Pyside2 how to get mouse position?

From what I understand you want to get the position on the window if you click anywhere in a widget.

To solve the problem, the logic is as follows:

  • Get the mouse position with respect to the widget
  • Convert that position to a global position, that is, with respect to the screen.
  • Convert that global position to a position relative to the window.

For the first step if mousePressEvent() is used, event.pos() returns the position relative to the widget.

For the second step you must convert that position relative to the widget to global with mapToGlobal().

And for the third step mapFromGlobal() of window is used.

def mousePressEvent(self, event):
p = event.pos() # relative to widget
gp = self.mapToGlobal(p) # relative to screen
rw = self.window().mapFromGlobal(gp) # relative to window
print("position relative to window: ", rw)
super(Widget, self).mousePressEvent(event)

Update:

The QGraphicsScene is not a widget, it is not a visual element, although it is part of the representation of a visual element: the QGraphicsView. For you to understand I will explain you with an analogy, let's say that there is a cameraman recording a scene, in that example the QGraphicsScene is the scene and the QGraphicsView is what the camera records, that is, it shows a piece of the QGraphicsScene, so there could be another cameraman recording the scene from another point, so it would show the same scene from another perspective, so the position of the scene depends on the camera, so if your current question would be equivalent to saying which is the position of the point P respect to the camera i-th, and that from the scene is impossible, you should get it from the camera.

So in conclusion you should not use QGraphicsScene but QGraphicsView, the following solutions implement the same logic using 2 different methods:

1. Creating a custom class of QGraphicsView:

import sys
from PySide2 import QtGui, QtWidgets, QtCore

class GraphicsView(QtWidgets.QGraphicsView):
def mousePressEvent(self, event):
p = event.pos() # relative to widget
gp = self.mapToGlobal(p) # relative to screen
rw = self.window().mapFromGlobal(gp) # relative to window
print("position relative to window: ", rw)
super(GraphicsView, self).mousePressEvent(event)

class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
scene = QtWidgets.QGraphicsScene(self)
view = GraphicsView(scene, self)
self.setCentralWidget(view)

if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main_window = MainWindow()
main_window.resize(500, 500)
main_window.show()
sys.exit(app.exec_())

2. Using eventfilter:

import sys
from PySide2 import QtGui, QtWidgets, QtCore

class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
scene = QtWidgets.QGraphicsScene(self)
self._view = QtWidgets.QGraphicsView(scene, self)
self.setCentralWidget(self._view)
self._view.installEventFilter(self)

def eventFilter(self, obj, event):
if obj is self._view and event.type() == QtCore.QEvent.MouseButtonPress:
p = event.pos() # relative to widget
gp = self.mapToGlobal(p) # relative to screen
rw = self.window().mapFromGlobal(gp) # relative to window
print("position relative to window: ", rw)
return super(MainWindow, self).eventFilter(obj, event)

if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main_window = MainWindow()
main_window.resize(500, 500)
main_window.show()
sys.exit(app.exec_())

On the other hand mapToGlobal() is a method that must be called by an instance, when you use QtWidgets.QWidget.mapToGlobal() there is no instance, my question is, what widget do you have the position? the position you have with respect to self, so you must use self.mapToGlobal() , that works for the objects belonging to a class that inherit from QWidget as QGraphicsView, but not in QGraphicsScene since it does not inherit from QWidget, it is not a widget as indicated in the lines above.



Related Topics



Leave a reply



Submit