Sunday, May 12, 2024
154
rated 0 times [  161] [ 7]  / answers: 1 / hits: 43932  / 13 Years ago, tue, march 15, 2011, 12:00:00

Is there any way to obtain the canvas that a context is used for?



Why I'm asking is because I'm creating a prototype function for CanvasRenderingContext2D in which I need the width/height of the canvas element.



E.g.:



var cv = document.getElementById('canvas');
var ctx = cv.getContext('2d');
// Using only 'ctx', how to get 'cv'?

More From » html5-canvas

 Answers
68

ctx.canvas should return the canvas DOM node, from which you can get height and width.



I tried it with https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Basic_usage



Firefox was able to return ctx.canvas, as well as ctx.canvas.width and ctx.canvas.height. Also confirmed in Chrome.


[#93274] Monday, March 14, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
emiliano

Total Points: 381
Total Questions: 109
Total Answers: 93

Location: Jersey
Member since Fri, Oct 1, 2021
3 Years ago
emiliano questions
;