localizing mouse click coordinates

Having great fun with the webSDK today but have hit a snag. I need to be able to tell where I’ve clicked or dragged relative to the control. Since we use absolute positioning I can’t just use the offsetLeft properties of the object, they are always 0. I have tried half a dozen different ways to do this including walking up the parent tree and adding all the leftOffsets together as well as actually passing the left value that is setup in XTension and using that to calculate the offset.

They all appear to be off by the sum of the margins and borders in all the CSS up to that point. So with just one thing on the screen with all default I’m off by 5 or 8 pixels or so and it gets worse the further you embed something. Or at least I think that is the case. How does Xojo figure out where a click has happened? Is there a framework call i can use to localize a mouse event?

ack, light of a new day reveals the failed assumptions of the day before :wink: I was actually drawing the indication wider than I thought I was which was causing me to think the click coordinates were off, but they aren’t. Never mind! In the process of trying to figure it out though I came across literally dozens of ways to localize mouse coordinates. At the moment I’m doing this, where “theCanvas” is the control I’m interested in getting the X offset of:

var bodyRect = document.body.getBoundingClientRect(),
	elemRect = theCanvas.getBoundingClientRect();

var localX = e.pageX - elemRect.left - bodyRect.left;

is there any “best practice” of doing so within a web control? Does anyone have a favorite method that they use for a specific reason I don’t know about yet? I haven’t tested that to see if it takes into account scrolling yet either.

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.