dragome / dragome-sdk

Dragome is a tool for creating client side web applications in pure Java (JVM) language.
http://www.dragome.com
Other
81 stars 20 forks source link

Getting relative X/Y on mouse event #98

Open czyzby opened 8 years ago

czyzby commented 8 years ago

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?

fpetrola commented 8 years ago

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 commented 8 years ago

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

czyzby commented 8 years ago

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.