Skip to content Skip to sidebar Skip to footer

Get A Reference To A Canvas Element From Its Context?

In one part of my code, I call getContext('2d') on a canvas element to produce a CanvasRenderingContext2D object. That object goes on to get passed around a fair bit from function

Solution 1:

So you've got your context:

var ctx = myCanvas.getContext('2d'); // the canvas' 2d context

later on you can always do:

ctx.canvas // the context's canvas, which in this case is the same as myCanvas

From the Canvas spec:

interface CanvasRenderingContext2D {

  // back-reference to the canvas
  readonly attribute HTMLCanvasElement canvas;

Post a Comment for "Get A Reference To A Canvas Element From Its Context?"