Moved canvas creation and size management into a new Canvas class.
This is the first step towards a more object-oriented architecture.
Since we create multiple canvases, and have to maintain several
module-global variables to track their properties, they are the ideal
place to start.
This commit also removes sizing code that was duplicated between
makeCanvas and resizeCanvas.
thrownewError("Canvas is not available. If you're using IE with a fall-back such as Excanvas, then there's either a mistake in your conditional include, or the page has no DOCTYPE and is rendering in Quirks Mode.");
}
}
varcontext=element.getContext("2d");
this.element=element;
this.context=context;
// Determine the screen's ratio of physical to device-independent
// pixels. This is the ratio between the canvas width that the browser
// advertises and the number of pixels actually present in that space.
// The iPhone 4, for example, has a device-independent width of 320px,
// but its screen is actually 640px wide. It therefore has a pixel
// ratio of 2, while most normal devices have a ratio of 1.
// If HTML5 Canvas isn't available, fall back to Excanvas
if(!c.getContext){
if(window.G_vmlCanvasManager){
c=window.G_vmlCanvasManager.initElement(c);
}else{
thrownewError("Canvas is not available. If you're using IE with a fall-back such as Excanvas, then there's either a mistake in your conditional include, or the page has no DOCTYPE and is rendering in Quirks Mode.");
}
}
varcctx=c.getContext("2d");
// Increase the canvas density based on the display's pixel ratio; basically
// giving the canvas more pixels without increasing the size of its element,
// to take advantage of the fact that retina displays have that many more
// pixels than they actually use for page & element widths.
varpixelRatio=getPixelRatio(cctx);
c.width=canvasWidth*pixelRatio;
c.height=canvasHeight*pixelRatio;
c.style.width=canvasWidth+"px";
c.style.height=canvasHeight+"px";
// Save the context so we can reset in case we get replotted
cctx.save();
// Scale the coordinate space to match the display density; so even though we
// may have twice as many pixels, we still want lines and other drawing to
// appear at the same size; the extra pixels will just make them crisper.
cctx.scale(pixelRatio,pixelRatio);
returnc;
}
functiongetCanvasDimensions(){
canvasWidth=placeholder.width();
canvasHeight=placeholder.height();
if(canvasWidth<=0||canvasHeight<=0)
thrownewError("Invalid dimensions for plot, width = "+canvasWidth+", height = "+canvasHeight);
}
functionresizeCanvas(c){
varcctx=c.getContext("2d");
// Handle pixel ratios > 1 for retina displays, as explained in makeCanvas
varpixelRatio=getPixelRatio(cctx);
// Resizing should reset the state (excanvas seems to be buggy though)
if(c.style.width!=canvasWidth){
c.width=canvasWidth*pixelRatio;
c.style.width=canvasWidth+"px";
}
if(c.style.height!=canvasHeight){
c.height=canvasHeight*pixelRatio;
c.style.height=canvasHeight+"px";
}
// so try to get back to the initial state (even if it's
// gone now, this should be safe according to the spec)
cctx.restore();
// and save again
cctx.save();
// Apply scaling for retina displays, as explained in makeCanvas