added pixel ratio optimization to sharpen renderings for devices with a pixel ratio > 1

This commit is contained in:
Eric Rowell 2012-12-13 21:53:39 -08:00
parent 5e16b3d7d0
commit f18bf604de
4 changed files with 26 additions and 9 deletions

View File

@ -6,13 +6,15 @@
* @param {Number} height * @param {Number} height
*/ */
Kinetic.Canvas = function(width, height) { Kinetic.Canvas = function(width, height) {
this.width = width;
this.height = height;
this.element = document.createElement('canvas'); this.element = document.createElement('canvas');
this.context = this.element.getContext('2d'); this.context = this.element.getContext('2d');
this.setSize(width || 0, height || 0);
// set dimensions
this.element.width = width || 0;
this.element.height = height || 0;
}; };
// calculate pixel ratio
var canvas = document.createElement('canvas'), context = canvas.getContext('2d'), devicePixelRatio = window.devicePixelRatio || 1, backingStoreRatio = context.webkitBackingStorePixelRatio || context.mozBackingStorePixelRatio || context.msBackingStorePixelRatio || context.oBackingStorePixelRatio || context.backingStorePixelRatio || 1;
Kinetic.Canvas.pixelRatio = devicePixelRatio / backingStoreRatio;
Kinetic.Canvas.prototype = { Kinetic.Canvas.prototype = {
/** /**
@ -47,7 +49,10 @@
* @methodOf Kinetic.Canvas.prototype * @methodOf Kinetic.Canvas.prototype
*/ */
setWidth: function(width) { setWidth: function(width) {
this.element.width = width; this.width = width;
// take into account pixel ratio
this.element.width = width * Kinetic.Canvas.pixelRatio;
this.element.style.width = width + 'px';
}, },
/** /**
* set height * set height
@ -55,7 +60,10 @@
* @methodOf Kinetic.Canvas.prototype * @methodOf Kinetic.Canvas.prototype
*/ */
setHeight: function(height) { setHeight: function(height) {
this.element.height = height; this.height = height;
// take into account pixel ratio
this.element.height = height * Kinetic.Canvas.pixelRatio;
this.element.style.height = height + 'px';
}, },
/** /**
* get width * get width
@ -63,7 +71,7 @@
* @methodOf Kinetic.Canvas.prototype * @methodOf Kinetic.Canvas.prototype
*/ */
getWidth: function() { getWidth: function() {
return this.element.width; return this.width;
}, },
/** /**
* get height * get height
@ -71,7 +79,7 @@
* @methodOf Kinetic.Canvas.prototype * @methodOf Kinetic.Canvas.prototype
*/ */
getHeight: function() { getHeight: function() {
return this.element.height; return this.height;
}, },
/** /**
* set size * set size
@ -163,6 +171,12 @@
if(lineJoin) { if(lineJoin) {
this.context.lineJoin = lineJoin; this.context.lineJoin = lineJoin;
} }
},
_handlePixelRatio: function() {
var pixelRatio = Kinetic.Canvas.pixelRatio;
if(pixelRatio !== 1) {
this.getContext().scale(pixelRatio, pixelRatio);
}
} }
}; };

View File

@ -28,7 +28,8 @@
/** /**
* @namespace * @namespace
*/ */
var Kinetic = {}; (function() { var Kinetic = {};
(function() {
Kinetic.version = '@version'; Kinetic.version = '@version';
/** /**
* @namespace * @namespace

View File

@ -230,6 +230,7 @@
} }
context.save(); context.save();
canvas._handlePixelRatio();
canvas._applyOpacity(this); canvas._applyOpacity(this);
canvas._applyLineJoin(this); canvas._applyLineJoin(this);
var len = family.length; var len = family.length;

View File

@ -44,6 +44,7 @@
}, },
drawFunc: function(canvas) { drawFunc: function(canvas) {
var context = canvas.getContext(); var context = canvas.getContext();
// draw rect // draw rect
Kinetic.Rect.prototype.drawFunc.call(this, canvas); Kinetic.Rect.prototype.drawFunc.call(this, canvas);