Access mouse position on top of canvas

It could be really useful to be able to monitor the position of the mouse on top of canvas in Photoshop.
As a minimal proposal, it should be possible to get the value of the mouse on top of canvas in percents.
I.e. something like:

getMousePos() returns [0.5, 0.5] when the mouse is in the center of the image.

It could be also useful to get an RGB value of the pixel, where the mouse is currently on. It could be a bit tricky, RGB value could be calculated for all values or for values of the current layer or below (or even values of the layer below current).

@DavideBarranca do you remember that color picker action/descriptor to pick at cursor position?

No, did it get the x,y coord too?

No, it solved just half of the problem. Reading color at some coordinates :smiley: But maybe count tool could be misused as it generates a lot of events as you drag it :smiley:

@Jarda Disclaimer could be issued in docs. We already have something similar with “Don’t subscribe to all possible events, please”. And I am pretty sure nobody would use a laggy panel, so panel developers could implement their code based on best practices.

If we use subscription on events - it could only make issues when subscribed.

And still it is kinda does not make issues, because my prototype works with legacy extensions and a mouse listener though a C++ library. But now we are a sandbox in UXP, so not an option to implement.

That is why I was thinking on polling it - much easier to implement, not soo resource intensive, if you poll it 5 times a second (and you only poll it if a panel is open anyway).

Such functionality is used for example in the Curves panel, it shows a vertical line for a hovered pixel. Which is super useful for stuff like custom spectrograms/histograms/custom color pickers.

There could be some performance issues to be added by this into core base of Photoshop, but I think the code is already there, just not exposed to UXP (i.e. native panels use it all around).

In the perfect world, would be nice to have a mechanism to program hand adjustment tool from the curve panel (i.e. you can handle custom tools). But that would never happen, because it is a lot of functionality :grinning:

Oh, and tracking mouse outside of a panel, especially for dragging events is useful too (at least it is how all panels in all software handle drag, they just don’t stop receiving drag events, when mouse is not under the panel).

Hi! I’m also interested in retrieving the mouse coordinates in the canvas.
Is there any way to get them?

I tried to get the coords with this code, but it only prints the coordinates when the mouse is over the plugin’s panel:

function onMouseMove(e)
{
  console.error(`Mouse ${e.pageX}, ${e.pageY}`);
}

document.addEventListener('mousemove', onMouseMove, false);

Maybe it could be good to clarify whether this is about <canvas> tag or Photoshop canvas. I was in impression that it is about Photoshop canvas.

Yeah, an original topic was about accessing mouse position on top of Photoshop image area. Some API for color picking without pressing any button.

sob., 22 lip 2023, 09:50 użytkownik Jaroslav Bereza via Adobe Creative Cloud Developer Forums <notifications@xdplugins.discoursemail.com> napisał:

Accessing mouse position outside of a panel is useful for people who are making dragging elements work the same as for the native panels (I.e. keep dragging when not over the panel). But it is more of an usability feature.

sob., 22 lip 2023, 12:07 użytkownik Michael Uvarov <freeakk@gmail.com> napisał:

Hi. Yes, I was referring as well to the Photoshop canvas.
The code I posted was just a quick test to check if, by some miracle, it would work.

Sorry to interfere,
I may have mist something but it looks like no solution was found.
I get color & position with the “ColorSamplerTool” and the following code

//##################################################

var listener = (e, d) => {
  myEvent = e
  myData = d

	if (myEvent == "make") {
		var target = myData._target[0]._ref
		if(target == "colorSampler")
		{
		executeAsModal( async () => {
			await getData()
			await printPoint()
		})
		}
	}
}

//##################################################

require("photoshop").action.addNotificationListener(
  [
		{  event: "make"  },//this should work
  ],
  listener
)

//##################################################

const getData = async () => {
	await executeAsModal(() => {
		const cs = app.activeDocument.colorSamplers[0];  // 0 for first, 1 for second
		const { color, position } = cs; // destructuring assignment
		
		doc = app.activeDocument
		w = doc.width 
		h = doc.height

		positionX = Number(position.x)/w*100; 
		positionY = Number(position.y)/h*100; 
		
		myRed = Math.round(color.rgb.red)
		myGreen = Math.round(color.rgb.green)
		myBlue = Math.round(color.rgb.blue)
	
		console.log("Red",myRed,"Green",myGreen,"Blue",myBlue)
		console.log("X",position.x,"Y", position.y);
		console.log("Point X in percent",positionX)
		console.log("Point Y in percent",positionY)
		app.activeDocument.colorSamplers.removeAll();
	})
}

Color sample tool is cool and useful, but it is not for continious monitoring sadly. I.e. no way to subscribe to the hover events.

Probably more useful way is to use rectangular selection tool and sample colours using it :slight_smile: i.e. it is more user friendly

pon., 31 lip 2023, 20:44 użytkownik Pierre Dore via Adobe Creative Cloud Developer Forums <notifications@xdplugins.discoursemail.com> napisał:

Thanks for your opinion. I agree but it was what I needed for my project.
I posted it knowing that the trigger could be changed to get the results required

Hi!
I’ve realized that Photoshop has an Info panel that, among other things, shows the mouse coordinates.

If we could somehow access this information, it would be perfect.
I don’t see anything related on the public API, but perhaps it’s possible via batchPlay calls?

I’m afraid that, to the best of my knowledge, x,y coords in the Info panel aren’t exposed to the scripting layer.

Yeah, internally PS knows it and built-in panels use it (i.e. info and curves panels).
Probably we would have to wait till Adobe team would start using the info for their javascript panels.