mirror of
https://github.com/konvajs/konva.git
synced 2025-05-05 22:24:34 +08:00
added pixel ratio optimization to sharpen renderings for devices with a pixel ratio > 1
This commit is contained in:
parent
5e16b3d7d0
commit
f18bf604de
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -28,7 +28,8 @@
|
|||||||
/**
|
/**
|
||||||
* @namespace
|
* @namespace
|
||||||
*/
|
*/
|
||||||
var Kinetic = {}; (function() {
|
var Kinetic = {};
|
||||||
|
(function() {
|
||||||
Kinetic.version = '@version';
|
Kinetic.version = '@version';
|
||||||
/**
|
/**
|
||||||
* @namespace
|
* @namespace
|
||||||
|
@ -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;
|
||||||
|
@ -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);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user