0

我正在用画布做一些有价值的事情,我需要获得以下值:

// This one:
window.devicePixelRatio

// and any one of:
Context2d.webkitBackingStorePixelRatio ||
Context2d.mozBackingStorePixelRatio ||
Context2d.msBackingStorePixelRatio ||
Context2d.oBackingStorePixelRatio ||
Context2d.backingStorePixelRatio

这些似乎在 GWT 等效类中不可用(我认为) - 我必须创建一个本地方法来获取它们还是有更好的方法呢?

谢谢

4

1 回答 1

1

你是对的,它们在核心 gwt 类中不可用,所以你必须使用 jsni 来实现它们。

另一种选择是使用gwt-2.5.0 中包含的elemental并具有这些方法,但 elemental 仅针对 webkit 实现。

[编辑]

您可以使用所需的 jsni 方法创建自己的 context2 类,并使用覆盖转换将 gwt context2 转换为您的类:

  public class MyContext2 extends Context2d {
    public final native float webkitBackingStorePixelRatio() /*-{
      return this.webkitBackingStorePixelRatio;
    }-*/;    
  }

  Context2d ctx = ...;

  // call your method through the cast()
  float ratio = ctx.<MyContext2>cast().webkitBackingStorePixelRatio();

  // or convert the context to your class
  MyContext2 mctx = ctx.cast();
  float ratio = mctx.webkitBackingStorePixelRatio();
于 2012-11-24T17:21:44.607 回答