added new Line Shape

This commit is contained in:
Eric Rowell 2012-04-28 21:12:01 -07:00
parent ffd9924511
commit 1dbe93a232
7 changed files with 203 additions and 20 deletions

View File

@ -6,7 +6,7 @@ class Build < Thor
"license.js", "src/GlobalObject.js", "src/Node.js", "src/Container.js", "src/Stage.js", "license.js", "src/GlobalObject.js", "src/Node.js", "src/Container.js", "src/Stage.js",
"src/Layer.js", "src/Group.js", "src/Shape.js", "src/shapes/Rect.js", "src/shapes/Circle.js", "src/shapes/Image.js", "src/Layer.js", "src/Group.js", "src/Shape.js", "src/shapes/Rect.js", "src/shapes/Circle.js", "src/shapes/Image.js",
"src/shapes/Polygon.js", "src/shapes/RegularPolygon.js", "src/shapes/Star.js", "src/shapes/Text.js", "src/shapes/Polygon.js", "src/shapes/RegularPolygon.js", "src/shapes/Star.js", "src/shapes/Text.js",
"src/util/Transform.js", "src/util/Transition.js" "src/shapes/Line.js", "src/util/Transform.js", "src/util/Transition.js"
] ]
desc "dev", "Concatenate all the js files into /dist/kinetic-VERSION.js." desc "dev", "Concatenate all the js files into /dist/kinetic-VERSION.js."

92
dist/kinetic-core.js vendored
View File

@ -2397,6 +2397,21 @@ Kinetic.Shape.prototype = {
getCanvas: function() { getCanvas: function() {
return this.tempLayer.getCanvas(); return this.tempLayer.getCanvas();
}, },
/**
* helper method to stroke shape
*/
stroke: function() {
var context = this.getContext();
if(!!this.attrs.stroke || !!this.attrs.strokeWidth) {
var stroke = !!this.attrs.stroke ? this.attrs.stroke : 'black';
var strokeWidth = !!this.attrs.strokeWidth ? this.attrs.strokeWidth : 2;
context.lineWidth = strokeWidth;
context.strokeStyle = stroke;
context.stroke();
}
},
/** /**
* helper method to fill and stroke a shape * helper method to fill and stroke a shape
* based on its fill, stroke, and strokeWidth, properties * based on its fill, stroke, and strokeWidth, properties
@ -2413,14 +2428,7 @@ Kinetic.Shape.prototype = {
context.fill(); context.fill();
} }
if(!!this.attrs.stroke || !!this.attrs.strokeWidth) { this.stroke();
var stroke = !!this.attrs.stroke ? this.attrs.stroke : 'black';
var strokeWidth = !!this.attrs.strokeWidth ? this.attrs.strokeWidth : 2;
context.lineWidth = strokeWidth;
context.strokeStyle = stroke;
context.stroke();
}
}, },
/** /**
* helper method to set the line join of a shape * helper method to set the line join of a shape
@ -3326,6 +3334,74 @@ Kinetic.Text.prototype = {
// extend Shape // extend Shape
Kinetic.GlobalObject.extend(Kinetic.Text, Kinetic.Shape); Kinetic.GlobalObject.extend(Kinetic.Text, Kinetic.Shape);
///////////////////////////////////////////////////////////////////////
// Line
///////////////////////////////////////////////////////////////////////
/**
* Line constructor.&nbsp; Lines are defined by an array of points
* @constructor
* @augments Kinetic.Shape
* @param {Object} config
*/
Kinetic.Line = function(config) {
this.setDefaultAttrs({
points: {},
lineCap: 'butt'
});
this.shapeType = "Line";
config.drawFunc = function() {
var context = this.getContext();
context.beginPath();
this.applyLineJoin();
context.moveTo(this.attrs.points[0].x, this.attrs.points[0].y);
for(var n = 1; n < this.attrs.points.length; n++) {
context.lineTo(this.attrs.points[n].x, this.attrs.points[n].y);
}
if(!!this.attrs.lineCap) {
context.lineCap = this.attrs.lineCap;
}
this.stroke();
};
// call super constructor
Kinetic.Shape.apply(this, [config]);
};
/*
* Line methods
*/
Kinetic.Line.prototype = {
/**
* set points array
* @param {Array} points
*/
setPoints: function(points) {
this.attrs.points = points;
},
/**
* get points array
*/
getPoints: function() {
return this.attrs.points;
},
/**
* set line cap. Can be butt, round, or square
* @param {String} lineCap
*/
setLineCap: function(lineCap) {
this.attrs.lineCap = lineCap;
},
/**
* get line cap
*/
getLineCap: function() {
return this.attrs.lineCap;
}
};
// extend Shape
Kinetic.GlobalObject.extend(Kinetic.Line, Kinetic.Shape);
/* /*
* Last updated November 2011 * Last updated November 2011
* By Simon Sarris * By Simon Sarris

File diff suppressed because one or more lines are too long

View File

@ -50,6 +50,21 @@ Kinetic.Shape.prototype = {
getCanvas: function() { getCanvas: function() {
return this.tempLayer.getCanvas(); return this.tempLayer.getCanvas();
}, },
/**
* helper method to stroke shape
*/
stroke: function() {
var context = this.getContext();
if(!!this.attrs.stroke || !!this.attrs.strokeWidth) {
var stroke = !!this.attrs.stroke ? this.attrs.stroke : 'black';
var strokeWidth = !!this.attrs.strokeWidth ? this.attrs.strokeWidth : 2;
context.lineWidth = strokeWidth;
context.strokeStyle = stroke;
context.stroke();
}
},
/** /**
* helper method to fill and stroke a shape * helper method to fill and stroke a shape
* based on its fill, stroke, and strokeWidth, properties * based on its fill, stroke, and strokeWidth, properties
@ -66,14 +81,7 @@ Kinetic.Shape.prototype = {
context.fill(); context.fill();
} }
if(!!this.attrs.stroke || !!this.attrs.strokeWidth) { this.stroke();
var stroke = !!this.attrs.stroke ? this.attrs.stroke : 'black';
var strokeWidth = !!this.attrs.strokeWidth ? this.attrs.strokeWidth : 2;
context.lineWidth = strokeWidth;
context.strokeStyle = stroke;
context.stroke();
}
}, },
/** /**
* helper method to set the line join of a shape * helper method to set the line join of a shape

67
src/shapes/Line.js Normal file
View File

@ -0,0 +1,67 @@
///////////////////////////////////////////////////////////////////////
// Line
///////////////////////////////////////////////////////////////////////
/**
* Line constructor.&nbsp; Lines are defined by an array of points
* @constructor
* @augments Kinetic.Shape
* @param {Object} config
*/
Kinetic.Line = function(config) {
this.setDefaultAttrs({
points: {},
lineCap: 'butt'
});
this.shapeType = "Line";
config.drawFunc = function() {
var context = this.getContext();
context.beginPath();
this.applyLineJoin();
context.moveTo(this.attrs.points[0].x, this.attrs.points[0].y);
for(var n = 1; n < this.attrs.points.length; n++) {
context.lineTo(this.attrs.points[n].x, this.attrs.points[n].y);
}
if(!!this.attrs.lineCap) {
context.lineCap = this.attrs.lineCap;
}
this.stroke();
};
// call super constructor
Kinetic.Shape.apply(this, [config]);
};
/*
* Line methods
*/
Kinetic.Line.prototype = {
/**
* set points array
* @param {Array} points
*/
setPoints: function(points) {
this.attrs.points = points;
},
/**
* get points array
*/
getPoints: function() {
return this.attrs.points;
},
/**
* set line cap. Can be butt, round, or square
* @param {String} lineCap
*/
setLineCap: function(lineCap) {
this.attrs.lineCap = lineCap;
},
/**
* get line cap
*/
getLineCap: function() {
return this.attrs.lineCap;
}
};
// extend Shape
Kinetic.GlobalObject.extend(Kinetic.Line, Kinetic.Shape);

View File

@ -10,7 +10,7 @@ function log(message) {
* Test constructor * Test constructor
*/ */
function Test() { function Test() {
this.testOnly = 'TRANSITION - all transition types'; this.testOnly = '';
this.counter = 0; this.counter = 0;
} }
/** /**

View File

@ -960,6 +960,38 @@ Test.prototype.tests = {
}); });
//stage.start(); //stage.start();
}, },
'SHAPES - add line': function(containerId) {
var stage = new Kinetic.Stage({
container: containerId,
width: 578,
height: 200
});
var layer = new Kinetic.Layer();
var points = [{
x: 73,
y: 160
}, {
x: 340,
y: 23
}, {
x: 500,
y: 109
}];
var line = new Kinetic.Line({
points: points,
stroke: 'blue',
strokeWidth: 20,
lineCap: 'round',
lineJoin: 'round',
draggable: true
});
layer.add(line);
stage.add(layer);
},
'SHAPES - add regular polygon triangle': function(containerId) { 'SHAPES - add regular polygon triangle': function(containerId) {
var stage = new Kinetic.Stage({ var stage = new Kinetic.Stage({
container: containerId, container: containerId,