Commit 7001dc55 authored by David Schnur's avatar David Schnur

Cleaned up the commit from pull request #52.

- Minor code cleanup and comments describing how we use pixel ratios.
parent ec99d31c
...@@ -148,7 +148,6 @@ ...@@ -148,7 +148,6 @@
overlay = null, // canvas for interactive stuff on top of plot overlay = null, // canvas for interactive stuff on top of plot
eventHolder = null, // jQuery object that events should be bound to eventHolder = null, // jQuery object that events should be bound to
ctx = null, octx = null, ctx = null, octx = null,
canvasBackingScale,
xaxes = [], yaxes = [], xaxes = [], yaxes = [],
plotOffset = { left: 0, right: 0, top: 0, bottom: 0}, plotOffset = { left: 0, right: 0, top: 0, bottom: 0},
canvasWidth = 0, canvasHeight = 0, canvasWidth = 0, canvasHeight = 0,
...@@ -716,23 +715,37 @@ ...@@ -716,23 +715,37 @@
}); });
} }
// Retina display support //////////////////////////////////////////////////////////////////////////////////
function backingScale(cctx) { // Returns the display's ratio between physical and device-independent pixels.
if(window.devicePixelRatio > 1 && (cctx.webkitBackingStorePixelRatio === undefined || cctx.webkitBackingStorePixelRatio < 2)) { //
// This is the ratio between the width that the browser advertises and the number
// of pixels actually available 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.
function getPixelRatio(cctx) {
if (window.devicePixelRatio > 1 && (cctx.webkitBackingStorePixelRatio === undefined || cctx.webkitBackingStorePixelRatio < 2)) {
return window.devicePixelRatio; return window.devicePixelRatio;
} }
return 1; return 1;
} }
function makeCanvas(skipPositioning, cls) { function makeCanvas(skipPositioning, cls) {
var c = document.createElement('canvas'); var c = document.createElement('canvas');
c.className = cls; c.className = cls;
var cctx = c.getContext("2d"); var cctx = c.getContext("2d");
canvasBackingScale = backingScale(cctx); // Retina display support
c.width = canvasBackingScale*canvasWidth; // Increase the canvas density based on the display's pixel ratio; basically
c.height = canvasBackingScale*canvasHeight; // 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.
var pixelRatio = getPixelRatio(cctx);
c.width = canvasWidth * pixelRatio;
c.height = canvasHeight * pixelRatio;
c.style.width = canvasWidth + "px"; c.style.width = canvasWidth + "px";
c.style.height = canvasHeight + "px"; c.style.height = canvasHeight + "px";
...@@ -744,11 +757,15 @@ ...@@ -744,11 +757,15 @@
if (!c.getContext) // excanvas hack if (!c.getContext) // excanvas hack
c = window.G_vmlCanvasManager.initElement(c); c = window.G_vmlCanvasManager.initElement(c);
// used for resetting in case we get replotted // Save the context so we can reset in case we get replotted
cctx.save(); cctx.save();
// Retina display support // Scale the coordinate space to match the display density; so even though we
cctx.scale(canvasBackingScale, canvasBackingScale); // 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);
return c; return c;
} }
...@@ -764,17 +781,20 @@ ...@@ -764,17 +781,20 @@
function resizeCanvas(c) { function resizeCanvas(c) {
var cctx = c.getContext("2d"); var cctx = c.getContext("2d");
canvasBackingScale = backingScale(cctx); // Retina display support
// resizing should reset the state (excanvas seems to be // Handle pixel ratios > 1 for retina displays, as explained in makeCanvas
// buggy though)
var pixelRatio = getPixelRatio(cctx);
// Resizing should reset the state (excanvas seems to be buggy though)
if (c.style.width != canvasWidth) { if (c.style.width != canvasWidth) {
c.width = canvasBackingScale*canvasWidth; c.width = canvasWidth * pixelRatio;
c.style.width = canvasWidth + "px"; c.style.width = canvasWidth + "px";
} }
if (c.style.height != canvasHeight) { if (c.style.height != canvasHeight) {
c.height = canvasBackingScale*canvasHeight; c.height = canvasHeight * pixelRatio;
c.style.height = canvasHeight + "px"; c.style.height = canvasHeight + "px";
} }
...@@ -785,8 +805,9 @@ ...@@ -785,8 +805,9 @@
// and save again // and save again
cctx.save(); cctx.save();
// Retina display support // Apply scaling for retina displays, as explained in makeCanvas
cctx.scale(canvasBackingScale, canvasBackingScale);
cctx.scale(pixelRatio, pixelRatio);
} }
function setupCanvases() { function setupCanvases() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment