Jump to content

getContext('2d') canvas question


rovf

Recommended Posts

Sorry, this is really a beginner's question:I stumbled over the following piece of JavaScript code:var ctx=element.getElementsByTagName('canvas')[0].getContext('2d')...var w=ctx.canvas.widthI understand what the code is doing. However, I was surprised to find, that we can extract the canvas out of the context (i.e. ctx.canvas), because when looking at http://www.w3schools.com/tags/ref_canvas.aspI find the following:"This reference will cover the properties and methods of the getContext("2d") object"but looking below, I do not find an attribute "canvas" listed. Did I misunderstand the documentation, i.e. are there maybe even more attributes or methods available for the context object, than are listed on ref_canvas.asp? Or why has the "canvas" attribute been left out?

Link to comment
Share on other sites

The canvas property is a back-reference to the canvas from which the context was created. Check the MDN reference for the canvas:

 

https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement

 

and the 2D context:

 

https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...