added getter and setter methods for pixel ratio. Also added unit tests

This commit is contained in:
Eric Rowell
2013-08-27 21:51:54 -07:00
parent 6663ec0652
commit c8936a4bc0
2 changed files with 38 additions and 2 deletions

View File

@@ -7,7 +7,8 @@
|| context.mozBackingStorePixelRatio
|| context.msBackingStorePixelRatio
|| context.oBackingStorePixelRatio
|| context.backingStorePixelRatio || 1,
|| context.backingStorePixelRatio
|| 1,
_pixelRatio = devicePixelRatio / backingStoreRatio;
/**
@@ -46,11 +47,31 @@
this.context = this.element.getContext(contextType);
this.setSize(width, height);
},
/**
* get pixel ratio
* @method
* @memberof Kinetic.Canvas.prototype
* @returns {Number} pixel ratio
*/
getPixelRatio: function() {
return this.pixelRatio;
},
/**
* get pixel ratio
* @method
* @memberof Kinetic.Canvas.prototype
*/
setPixelRatio: function(pixelRatio) {
this.pixelRatio = pixelRatio;
this.setSize(this.getWidth(), this.getHeight());
},
/**
* reset canvas context transform
* @method
* @memberof Kinetic.Canvas.prototype
*/
reset: function() {
this.getContext().setTransform(1 * _pixelRatio, 0, 0, 1 * _pixelRatio, 0, 0);
this.getContext().setTransform(1 * this.pixelRatio, 0, 0, 1 * this.pixelRatio, 0, 0);
},
/**
* get canvas element

View File

@@ -26,6 +26,21 @@ Test.Modules.CANVAS = {
stage.draw();
layer.getCanvas().setPixelRatio(1);
test(layer.getCanvas().getPixelRatio() === 1, 'pixel ratio should be 1');
test(layer.getCanvas().width === 289, 'canvas width should be 289');
test(layer.getCanvas().height === 100, 'canvas height should be 100');
layer.getCanvas().setPixelRatio(2);
test(layer.getCanvas().getPixelRatio() === 2, 'pixel ratio should be 2');
test(layer.getCanvas().width === 578, 'canvas width should be 578');
test(layer.getCanvas().height === 200, 'canvas height should be 200');
layer.draw();
}