dragome-sdk icon indicating copy to clipboard operation
dragome-sdk copied to clipboard

Getting relative X/Y on mouse event

Open czyzby opened this issue 8 years ago • 3 comments

GWT LibGDX backend uses these methods to determine relative X and Y from received events:

    protected int getRelativeX (NativeEvent e, CanvasElement target) {
        float xScaleRatio = target.getWidth() * 1f / target.getClientWidth(); // Correct for canvas CSS scaling
        return Math.round(xScaleRatio
            * (e.getClientX() - target.getAbsoluteLeft() + target.getScrollLeft() + target.getOwnerDocument().getScrollLeft()));
    }

    protected int getRelativeY (NativeEvent e, CanvasElement target) {
        float yScaleRatio = target.getHeight() * 1f / target.getClientHeight(); // Correct for canvas CSS scaling
        return Math.round(yScaleRatio
            * (e.getClientY() - target.getAbsoluteTop() + target.getScrollTop() + target.getOwnerDocument().getScrollTop()));
    }

    protected int getRelativeX (Touch touch, CanvasElement target) {
        float xScaleRatio = target.getWidth() * 1f / target.getClientWidth(); // Correct for canvas CSS scaling
        return Math.round(xScaleRatio * touch.getRelativeX(target));
    }

    protected int getRelativeY (Touch touch, CanvasElement target) {
        float yScaleRatio = target.getHeight() * 1f / target.getClientHeight(); // Correct for canvas CSS scaling
        return Math.round(yScaleRatio * touch.getRelativeY(target));
    }

However, Dragome HTMLCanvasElement is missing multiple used methods, like getClientHeight, getAbsoluteLeft or getScrollLeft. Are there any (planned?) canvas extensions that would provide similar API?

czyzby avatar Apr 20 '16 12:04 czyzby

HTMLCanvasElement specification does not contain mentioned methods:

interface HTMLCanvasElement : HTMLElement {
           attribute unsigned long width;
           attribute unsigned long height;

  DOMString toDataURL(in optional DOMString type, in any... args);
  void toBlob(in FileCallback, in optional DOMString type, in any... args);

  object getContext(in DOMString contextId, in any... args);
};

That's why I've already created HTMLCanvasElementExtension interface to extend it with certain methods, may be we can added them to this interface.

fpetrola avatar Apr 20 '16 15:04 fpetrola

Could you add the methods you are requiring to HTMLCanvasElementExtension and make a PR with these changes?

fpetrola avatar Apr 20 '16 18:04 fpetrola

I'd have to inspect GWT code, I'm pretty sure some of these methods are custom and GWT-specific. With that in mind, I think that implementing these with some native code in gdx-dragome might be the sensible way to go, as they don't seem to be part of the standard.

czyzby avatar Apr 21 '16 10:04 czyzby