Skip to content

Instantly share code, notes, and snippets.

@branflake2267
Created February 24, 2017 15:58
Show Gist options
  • Save branflake2267/8e4b9d4f2dc594fe21a125155516ec97 to your computer and use it in GitHub Desktop.
Save branflake2267/8e4b9d4f2dc594fe21a125155516ec97 to your computer and use it in GitHub Desktop.
GWT Elemental 2 Canvas and 2d Context use. (casting from NativeObject).
import elemental2.CanvasRenderingContext2D;
import elemental2.Document;
import elemental2.Global;
import elemental2.HTMLBodyElement;
import elemental2.HTMLCanvasElement;
/**
* <inherits name="elemental2"/>
*/
public class Elemental2CanvasCircleExample implements EntryPoint {
@Override
public void onModuleLoad() {
// JSNI: $doc
Document document = Global.document;
// CanvasElement
HTMLCanvasElement canvas = (HTMLCanvasElement) document.createElement("canvas");
canvas.width = 200;
canvas.height = 200;
canvas.style.margin = "10px";
canvas.style.border = "1px dashed coral";
double centerX = canvas.width / 2;
double centerY = canvas.height / 2;
double radius = 40;
// RootPanel.get().add(canvasElement);
HTMLBodyElement bodyElement = Global.document.body;
bodyElement.appendChild(canvas);
// Double cast for now
CanvasRenderingContext2D context = (CanvasRenderingContext2D) (Object) canvas.getContext("2d");
context.beginPath();
context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
context.fillStyle = "coral";
context.fill();
context.lineWidth = 5;
context.strokeStyle = "#003300";
context.stroke();
}
}
@branflake2267
Copy link
Author

screen shot 2017-02-24 at 7 56 56 am

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment