private void hookEvents() { HTMLDocument document = canvas.getOwnerDocument(); canvas.addEventListener("mousedown", this, true); document.addEventListener("mousedown", this, true); canvas.addEventListener("mouseup", this, true); document.addEventListener("mouseup", this, true); canvas.addEventListener("mousemove", this, true); document.addEventListener("mousemove", this, true); canvas.addEventListener("mousewheel", this, true); document.addEventListener("keydown", this, false); document.addEventListener("keyup", this, false); document.addEventListener("keypress", this, false); canvas.addEventListener("touchstart", this); canvas.addEventListener("touchmove", this); canvas.addEventListener("touchcancel", this); canvas.addEventListener("touchend", this); }
/** Kindly borrowed from PlayN. **/ protected int getRelativeX(MouseEvent e, HTMLCanvasElement target) { float xScaleRatio = target.getWidth() * 1f / target.getClientWidth(); return Math.round(xScaleRatio * (e.getClientX() - target.getAbsoluteLeft() + target.getScrollLeft() + target.getOwnerDocument() .getScrollLeft())); }
/** Kindly borrowed from PlayN. **/ protected int getRelativeY(MouseEvent e, HTMLCanvasElement target) { float yScaleRatio = target.getHeight() * 1f / target.getClientHeight(); return Math.round(yScaleRatio * (e.getClientY() - target.getAbsoluteTop() + target.getScrollTop() + target.getOwnerDocument() .getScrollTop())); } /*
private void create(int width, int height, @SuppressWarnings("unused") Pixmap.Format format2) { this.width = width; this.height = height; this.format = Format.RGBA8888; canvas = (HTMLCanvasElement) document.createElement("canvas"); canvas.getStyle().setProperty("display", "none"); document.getBody().appendChild(canvas); canvas.setWidth(width); canvas.setHeight(height); context = (CanvasRenderingContext2D) canvas.getContext("2d"); context.setGlobalCompositeOperation("source-over"); id = nextId++; pixmaps.put(id, this); }
public TeaVMGraphics(HTMLCanvasElement element, TeaVMApplicationConfig config) { this.element = element; this.config = config; WebGLContextAttributes attr = WebGLContextAttributes.create(); attr.setAlpha(config.isAlphaEnabled()); attr.setAntialias(config.isAntialiasEnabled()); attr.setStencil(config.isStencilEnabled()); attr.setPremultipliedAlpha(config.isPremultipliedAlpha()); attr.setPreserveDrawingBuffer(config.isDrawingBufferPreserved()); context = (WebGLRenderingContext)element.getContext("webgl"); context.viewport(0, 0, element.getWidth(), element.getHeight()); gl20 = new TeaVMGL20(context); }
@Override public int getHeight() { return element.getHeight(); }
@Override public void dispose() { PixmapEmulator pixmap = pixmaps.remove(id); if (pixmap.canvas != null) { pixmap.canvas.getParentNode().removeChild(pixmap.canvas); } }
private void startGdx() { canvas = config.getCanvas(); graphics = new TeaVMGraphics(canvas, config); files = new TeaVMFiles(); audio = new TeaVMAudio(); input = new TeaVMInput(canvas); Gdx.app = this; Gdx.graphics = graphics; Gdx.gl = graphics.getGL20(); Gdx.gl20 = graphics.getGL20(); Gdx.files = files; Gdx.audio = audio; Gdx.input = input; listener.create(); listener.resize(canvas.getWidth(), canvas.getHeight()); delayedStep(); }