Search code examples
javagwtjava-canvas

How to extend the Context2d of a canvas?


I want to extend Context2d in order to create a specific method that can be invoked by context.myMethod();

Using this class:

class MyContext extends Context2d {
    public myMethod();
}

BUT I'm creating the canvas by Canvas canvas = Canvas.createIfSupported(); and thus I would get the Context2d by canvas.getContext2d();

How can I now force the latest method to return MyContext class which extends the Context2d, as there is no setContext2d() on a canvas element...


Solution

  • Context2d is a JavaScriptObject so all you need is to cast it to MyContext and/or use the cast() method:

    MyContext ctx = canvas.getContext2d().cast();