stage setSize() method now converts inputs to integers. added a lot of setSize unit tests

This commit is contained in:
Eric Rowell
2012-06-02 21:27:26 -07:00
parent 91eb4ea371
commit e842cdf8ac
5 changed files with 75 additions and 18 deletions

19
dist/kinetic-core.js vendored
View File

@@ -1574,17 +1574,22 @@ Kinetic.Stage.prototype = {
},
/**
* set stage size
* @param {int} width
* @param {int} height
*/
setSize: function(width, height) {
setSize: function() {
// set stage dimensions
this.attrs.width = width;
this.attrs.height = height;
var size = Kinetic.GlobalObject._getSize(arguments);
this.setAttrs(size);
// convert to integers
this.attrs.width = Math.round(this.attrs.width);
this.attrs.height = Math.round(this.attrs.height);
var width = this.attrs.width;
var height = this.attrs.height;
// set content dimensions
this.content.style.width = this.attrs.width + 'px';
this.content.style.height = this.attrs.height + 'px';
this.content.style.width = width + 'px';
this.content.style.height = height + 'px';
// set buffer layer and path layer sizes
this.bufferLayer.getCanvas().width = width;

File diff suppressed because one or more lines are too long

View File

@@ -91,17 +91,22 @@ Kinetic.Stage.prototype = {
},
/**
* set stage size
* @param {int} width
* @param {int} height
*/
setSize: function(width, height) {
setSize: function() {
// set stage dimensions
this.attrs.width = width;
this.attrs.height = height;
var size = Kinetic.GlobalObject._getSize(arguments);
this.setAttrs(size);
// convert to integers
this.attrs.width = Math.round(this.attrs.width);
this.attrs.height = Math.round(this.attrs.height);
var width = this.attrs.width;
var height = this.attrs.height;
// set content dimensions
this.content.style.width = this.attrs.width + 'px';
this.content.style.height = this.attrs.height + 'px';
this.content.style.width = width + 'px';
this.content.style.height = height + 'px';
// set buffer layer and path layer sizes
this.bufferLayer.getCanvas().width = width;

View File

@@ -734,8 +734,7 @@ Test.prototype.tests = {
});
layer.add(star);
stage.add(layer);
stage.add(layer);
star.saveData();
},
'EVENTS - drag events click': function(containerId) {

View File

@@ -18,6 +18,54 @@ Test.prototype.tests = {
height: 200
});
},
'STAGE - test setSize': function(containerId) {
var stage = new Kinetic.Stage({
container: containerId,
width: 578,
height: 200
});
var layer = new Kinetic.Layer();
var circle = new Kinetic.Circle({
x: stage.getWidth() / 2,
y: stage.getHeight() / 2,
radius: 70,
fill: 'green',
stroke: 'black',
strokeWidth: 4,
name: 'myCircle'
});
test(stage.getSize().width === 578 && stage.getSize().height === 200, 'stage size should be 1 x 2');
stage.setSize(1, 2);
test(stage.getSize().width === 1 && stage.getSize().height === 2, 'stage size should be 1 x 2');
stage.setSize(3);
test(stage.getSize().width === 3 && stage.getSize().height === 3, 'stage size should be 3 x 3');
stage.setSize({
width: 4,
height: 5
});
test(stage.getSize().width === 4 && stage.getSize().height === 5, 'stage size should be 4 x 5');
stage.setSize({
width: 6
});
test(stage.getSize().width === 6 && stage.getSize().height === 5, 'stage size should be 6 x 5');
stage.setSize({
height: 7
});
test(stage.getSize().width === 6 && stage.getSize().height === 7, 'stage size should be 6 x 7');
stage.setSize([8, 9]);
test(stage.getSize().width === 8 && stage.getSize().height === 9, 'stage size should be 8 x 9');
stage.setSize([1, 1, 10, 11]);
test(stage.getSize().width === 10 && stage.getSize().height === 11, 'stage size should be 10 x 11');
// test integer conversion
stage.setSize(300.2, 200.2);
test(stage.getSize().width === 300 && stage.getSize().height === 200, 'stage size should be 300 x 200');
layer.add(circle);
stage.add(layer);
},
'STAGE - add shape then stage then layer': function(containerId) {
var stage = new Kinetic.Stage({
container: containerId,