Vispy: 2D coordinate (x,y) of a point of an image inside a SceneCanvas - vispy

I am working on a program that can display an image on a SceneCanvas and also allow users to click on an image to get an intensity of the point. My question is if there exist a way to get the coordinate X, Y of a point of an image in SceneCanvas. The canvas camera I am using is PanZoomCamera.
Thank you

You can attach an event handler to one or more mouse events like this:
def my_handler(event):
# some stuff
canvas.events.mouse_release.connect(my_handler)
There are other ways to connect a function like this, but we'll stick with this way. Inside this function you'll want to convert your mouse click position in canvas space to visual space:
def my_handler(event):
if event.button == 1:
# left click
transform = your_image_visual.transforms.get_transform(map_to="canvas")
img_x, img_y = transform.imap(event.pos)[:2]
# optionally do the below to tell other handlers not to look at this event:
event.handled = True

Related

Marker Drag and drop event with overlay image calculate pix issue

this is an example. I am creating a custom function GetCoordinates this function calculates x and y positions for the image. but this function is not working for your Mapbox init method. can you help me with what can I do this?. Example: "https://jsfiddle.net/ufnvepms/"
Are you trying to get the coordinates of the marker as it is dragged? Or do you want to get the corresponding x/y position of the source image?
The first can be done by adding a listener on the "dragend" event, to get the lat/lon of the marker, as in this example: https://docs.mapbox.com/mapbox-gl-js/example/drag-a-marker/
If you need the second, it would require some more calculation, but before figuring that out wanted to understand more precisely what you're trying to achieve.

Can I place an object in air - 360 angle (random position) without click instead of find plane in unity Vuforia mid air

I'm trying to show my 3D object in mid air. Currently my object shows on click event, but I don't want click event. I just want the object shown in a random position in midair.
if I break your question into two, then,
Vuforia uses "Anchor input Listener Behavior" to receive the input for the mid-air anchor. I am not sure it will work without a click event.
you have to click to lock the mid-air target, you can gamify it by showing a pop up "click to start".
However, there is always a way out. If you go through PlaneManager.cs class then you fill find a method "PlaceObjectInMidAir", you can carefully build logic and call the following lines in Update method but for one time.
if (TrackingStatusIsTrackedAndNormal)
{
this.contentPositioningBehaviour.AnchorStage = this.midAirAnchor;
this.contentPositioningBehaviour.PositionContentAtMidAirAnchor(midAirTransform);
UtilityHelper.EnableRendererColliderCanvas(this.midAirAugmentation, true);
this.midAirAugmentation.transform.localPosition = Vector3.zero;
UtilityHelper.RotateTowardCamera(this.midAirAugmentation);
}
To spawn the gameobjects randomly in 360 degree, you can use instantiate method in random position with "Random.insideUnitSphere" [see the link]
https://docs.unity3d.com/ScriptReference/Random-insideUnitSphere.html
then make that object child of mid-air Achor "Anchor_MidAir".
Hope my answer would give you a start push.
Good luck!
currently my object show on click event i don't wont click event just object show on random position of midair
If I understand that correctly, your object is shown when the mouse is clicked, and instead of this you want to show it in a random position. Here's what you should do:
Don't listen to that click event. If you don't want something to happen on click, just don't write any code that does that!
To teleport an object to a random position, decide what the minimum and maximum values for your object's position are, and do the following:
yourGameObject.transform.position = new Vector3(
Random.Range(minX, maxX),
Random.Range(minY, maxY),
Random.Range(minZ, maxZ));

How to detect exact location of mouse click on sprite in scratch

I would like to detect the exact location of the mouse click within the 3x3 grid displayed on the screen. How can this be done in MIT scratch? Any suggestions?
There are two ways to do this.
You could create 9 sprites, hide, and use the When this sprite clicked event...
...but it would be a whole lot of unnecessary sprites.
Or you can do the following:
As #PullJosh said, you can use the mouse x and y blocks. Just do some math:
You know that the stage goes from X: -240 to 240, Y: -180 to 180.
Just put that into some code to detect ranges, below is a link to a project that is an example of this:
This project
(Note: This assumes the grid boxes are the same size.)
when greenFlag pressed
forever
if <mouse down?> then
set (lastMouseClickX) to (mouse x)
set (lastMouseClickY) to (mouse y)
end
wait until <not<mouse down?>>
end```
This is pretty simple if you want to involve 2 variables in this:
All you really need to do is to set the mousex position to a variable and the mousey position to a variable after the sprite detects a click on it. Here is an example:

Create a 3d shape (non GUI) with a start and end point

I know the traditional way in Unity to position a shape is to give it a center point, then scale and rotate as needed. However, I'd like for the following:
The player can click once to "start" a shape (basically a line, but I could use a cube or whatever) and then click a second time to "end" the shape, completing it and bringing it into existence in the game space. How could this be done?

Mouse position not consistent with HTML canvas ondrag

I am trying to drag some shapes in HTML canvas but I am encountering a problem with respect to determining the change in mouse coordinates [dx,dy]
First of all, there is no problem in the coordinates themselves, stored in mousePos as the rollover effects work flawlessly. What I am doing is, upon first entering the shape, saving the mouse coordinates.
pos = {x : mousePos[0] , y : mousePos[1]};
Then, onMotion updates the coordinates everytime the mouse moves as well as recording the current position
dx=mousePos[0]-pos.x;
dy=mousePos[1]-pos.y;
pos = {x : mousePos[0] , y : mousePos[1]};
Then I add the dx and dy values to the shapes coordinates (lets take a simple rectangle as an example)
ctx.fillRect(0+this.dx,0+this.dy,100+this.dx,100+this.dy);
as long as the mouse doesn't move too fast, it works relatively well (not perfect though). If I move the mouse very quickly, without going out of the window, the rectangle does not catch up with the mouse. I can understand if there is a delay catching up to the mouse, but how can the delta values be off? Clearly we know where we started, and even if dozens/hundreds of pixels are skipped in the process, eventually the mouse should stop and the correct delta values should be calculated.
Any help would be greatly appreciated as I have hit a conceptual wall here.
You might try to get e.layerX-Y when the onMotion is fired to get the real position instead of the delta. This way it can't be "off".
To use this, place your shape into a div with style="padding:0px;margin=0px;" , because the position is relative to the parent block.