mirror of
https://github.com/konvajs/konva.git
synced 2025-04-29 07:43:15 +08:00
added support for image de-serialization and cleaned up some of the Shape attr default logic
This commit is contained in:
parent
d3b025254a
commit
3243e5f8ff
43
dist/kinetic-core.js
vendored
43
dist/kinetic-core.js
vendored
@ -1943,16 +1943,6 @@ Kinetic.Shape = function(config) {
|
||||
this.data = [];
|
||||
this.nodeType = 'Shape';
|
||||
|
||||
// defaults
|
||||
if(config.stroke !== undefined || config.strokeWidth !== undefined) {
|
||||
if(config.stroke === undefined) {
|
||||
config.stroke = 'black';
|
||||
}
|
||||
else if(config.strokeWidth === undefined) {
|
||||
config.strokeWidth = 2;
|
||||
}
|
||||
}
|
||||
|
||||
// call super constructor
|
||||
Kinetic.Node.apply(this, [config]);
|
||||
};
|
||||
@ -1987,7 +1977,16 @@ Kinetic.Shape.prototype = {
|
||||
context.fillStyle = this.attrs.fill;
|
||||
context.fill();
|
||||
}
|
||||
if(this.attrs.stroke !== undefined) {
|
||||
|
||||
var stroke, strokeWidth;
|
||||
if(this.attrs.stroke !== undefined || this.attrs.strokeWidth !== undefined) {
|
||||
if(this.attrs.stroke === undefined) {
|
||||
stroke = 'black';
|
||||
}
|
||||
else if(this.attrs.strokeWidth === undefined) {
|
||||
strokeWidth = 2;
|
||||
}
|
||||
|
||||
context.lineWidth = this.attrs.strokeWidth === undefined ? 1 : this.attrs.strokeWidth;
|
||||
context.strokeStyle = this.attrs.stroke;
|
||||
context.stroke();
|
||||
@ -2297,16 +2296,18 @@ Kinetic.Image = function(config) {
|
||||
|
||||
this.shapeType = "Image";
|
||||
config.drawFunc = function() {
|
||||
var width = this.attrs.width !== undefined ? this.attrs.width : this.image.width;
|
||||
var height = this.attrs.height !== undefined ? this.attrs.height : this.image.height;
|
||||
var canvas = this.getCanvas();
|
||||
var context = this.getContext();
|
||||
context.beginPath();
|
||||
this.applyLineJoin();
|
||||
context.rect(0, 0, width, height);
|
||||
context.closePath();
|
||||
this.fillStroke();
|
||||
context.drawImage(this.image, 0, 0, width, height);
|
||||
if(this.image !== undefined) {
|
||||
var width = this.attrs.width !== undefined ? this.attrs.width : this.image.width;
|
||||
var height = this.attrs.height !== undefined ? this.attrs.height : this.image.height;
|
||||
var canvas = this.getCanvas();
|
||||
var context = this.getContext();
|
||||
context.beginPath();
|
||||
this.applyLineJoin();
|
||||
context.rect(0, 0, width, height);
|
||||
context.closePath();
|
||||
this.fillStroke();
|
||||
context.drawImage(this.image, 0, 0, width, height);
|
||||
}
|
||||
};
|
||||
// call super constructor
|
||||
Kinetic.Shape.apply(this, [config]);
|
||||
|
4
dist/kinetic-core.min.js
vendored
4
dist/kinetic-core.min.js
vendored
File diff suppressed because one or more lines are too long
21
src/Shape.js
21
src/Shape.js
@ -32,16 +32,6 @@ Kinetic.Shape = function(config) {
|
||||
this.data = [];
|
||||
this.nodeType = 'Shape';
|
||||
|
||||
// defaults
|
||||
if(config.stroke !== undefined || config.strokeWidth !== undefined) {
|
||||
if(config.stroke === undefined) {
|
||||
config.stroke = 'black';
|
||||
}
|
||||
else if(config.strokeWidth === undefined) {
|
||||
config.strokeWidth = 2;
|
||||
}
|
||||
}
|
||||
|
||||
// call super constructor
|
||||
Kinetic.Node.apply(this, [config]);
|
||||
};
|
||||
@ -76,7 +66,16 @@ Kinetic.Shape.prototype = {
|
||||
context.fillStyle = this.attrs.fill;
|
||||
context.fill();
|
||||
}
|
||||
if(this.attrs.stroke !== undefined) {
|
||||
|
||||
var stroke, strokeWidth;
|
||||
if(this.attrs.stroke !== undefined || this.attrs.strokeWidth !== undefined) {
|
||||
if(this.attrs.stroke === undefined) {
|
||||
stroke = 'black';
|
||||
}
|
||||
else if(this.attrs.strokeWidth === undefined) {
|
||||
strokeWidth = 2;
|
||||
}
|
||||
|
||||
context.lineWidth = this.attrs.strokeWidth === undefined ? 1 : this.attrs.strokeWidth;
|
||||
context.strokeStyle = this.attrs.stroke;
|
||||
context.stroke();
|
||||
|
@ -18,16 +18,18 @@ Kinetic.Image = function(config) {
|
||||
|
||||
this.shapeType = "Image";
|
||||
config.drawFunc = function() {
|
||||
var width = this.attrs.width !== undefined ? this.attrs.width : this.image.width;
|
||||
var height = this.attrs.height !== undefined ? this.attrs.height : this.image.height;
|
||||
var canvas = this.getCanvas();
|
||||
var context = this.getContext();
|
||||
context.beginPath();
|
||||
this.applyLineJoin();
|
||||
context.rect(0, 0, width, height);
|
||||
context.closePath();
|
||||
this.fillStroke();
|
||||
context.drawImage(this.image, 0, 0, width, height);
|
||||
if(this.image !== undefined) {
|
||||
var width = this.attrs.width !== undefined ? this.attrs.width : this.image.width;
|
||||
var height = this.attrs.height !== undefined ? this.attrs.height : this.image.height;
|
||||
var canvas = this.getCanvas();
|
||||
var context = this.getContext();
|
||||
context.beginPath();
|
||||
this.applyLineJoin();
|
||||
context.rect(0, 0, width, height);
|
||||
context.closePath();
|
||||
this.fillStroke();
|
||||
context.drawImage(this.image, 0, 0, width, height);
|
||||
}
|
||||
};
|
||||
// call super constructor
|
||||
Kinetic.Shape.apply(this, [config]);
|
||||
|
@ -693,6 +693,58 @@ Test.prototype.tests = {
|
||||
};
|
||||
imageObj.src = '../darth-vader.jpg';
|
||||
},
|
||||
'STAGE - serialize stage with an image': function(containerId) {
|
||||
var imageObj = new Image();
|
||||
imageObj.onload = function() {
|
||||
var stage = new Kinetic.Stage({
|
||||
container: containerId,
|
||||
width: 578,
|
||||
height: 200
|
||||
});
|
||||
var layer = new Kinetic.Layer();
|
||||
darth = new Kinetic.Image({
|
||||
x: 200,
|
||||
y: 60,
|
||||
image: imageObj,
|
||||
centerOffset: {
|
||||
x: 50,
|
||||
y: imageObj.height / 2
|
||||
}
|
||||
});
|
||||
|
||||
layer.add(darth);
|
||||
stage.add(layer);
|
||||
|
||||
var json = stage.toJSON();
|
||||
|
||||
//console.log(json);
|
||||
|
||||
test(json === '{"attrs":{"width":578,"height":200,"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Stage","children":[{"attrs":{"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Layer","children":[{"attrs":{"detectionType":"path","visible":true,"listening":true,"alpha":1,"x":200,"y":60,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":50,"y":150},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Shape","shapeType":"Image"}]}]}', 'problem serializing stage');
|
||||
};
|
||||
imageObj.src = '../darth-vader.jpg';
|
||||
},
|
||||
'STAGE - load stage with an image': function(containerId) {
|
||||
var imageObj = new Image();
|
||||
imageObj.onload = function() {
|
||||
var stage = new Kinetic.Stage({
|
||||
container: containerId,
|
||||
width: 578,
|
||||
height: 200
|
||||
});
|
||||
|
||||
var json = '{"attrs":{"width":578,"height":200,"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Stage","children":[{"attrs":{"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Layer","children":[{"attrs":{"detectionType":"path","visible":true,"listening":true,"alpha":1,"x":200,"y":60,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":50,"y":150},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Shape","shapeType":"Image"}]}]}';
|
||||
|
||||
stage.load(json);
|
||||
|
||||
var image = stage.getChildren()[0].getChildren()[0];
|
||||
|
||||
image.setImage(imageObj);
|
||||
|
||||
stage.draw();
|
||||
|
||||
};
|
||||
imageObj.src = '../darth-vader.jpg';
|
||||
},
|
||||
'SHAPES - add polygon': function(containerId) {
|
||||
var stage = new Kinetic.Stage({
|
||||
container: containerId,
|
||||
@ -907,7 +959,7 @@ Test.prototype.tests = {
|
||||
layer.add(rect);
|
||||
stage.add(layer);
|
||||
},
|
||||
'SHAPES - use default stroke': function(containerId) {
|
||||
'SHAPES - use default stroke (stroke color should be black)': function(containerId) {
|
||||
var stage = new Kinetic.Stage({
|
||||
container: containerId,
|
||||
width: 578,
|
||||
@ -924,10 +976,8 @@ Test.prototype.tests = {
|
||||
|
||||
layer.add(rect);
|
||||
stage.add(layer);
|
||||
|
||||
test(rect.getStroke() === 'black', 'stroke should be black');
|
||||
},
|
||||
'SHAPES - use default stroke width': function(containerId) {
|
||||
'SHAPES - use default stroke width (stroke width should be 2)': function(containerId) {
|
||||
var stage = new Kinetic.Stage({
|
||||
container: containerId,
|
||||
width: 578,
|
||||
@ -944,8 +994,6 @@ Test.prototype.tests = {
|
||||
|
||||
layer.add(rect);
|
||||
stage.add(layer);
|
||||
|
||||
test(rect.getStrokeWidth() === 2, 'stroke width should be 2');
|
||||
},
|
||||
'SHAPES - set center offset after instantiation': function(containerId) {
|
||||
var stage = new Kinetic.Stage({
|
||||
|
Loading…
Reference in New Issue
Block a user