dragome/dragome-sdk

Getting relative X/Y on mouse event

Opened this issue · 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?

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.

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

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.