mirror of
https://github.com/konvajs/konva.git
synced 2025-12-05 03:24:23 +08:00
added new Line Shape
This commit is contained in:
24
src/Shape.js
24
src/Shape.js
@@ -50,6 +50,21 @@ Kinetic.Shape.prototype = {
|
||||
getCanvas: function() {
|
||||
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
|
||||
* based on its fill, stroke, and strokeWidth, properties
|
||||
@@ -66,14 +81,7 @@ Kinetic.Shape.prototype = {
|
||||
context.fill();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
this.stroke();
|
||||
},
|
||||
/**
|
||||
* helper method to set the line join of a shape
|
||||
|
||||
67
src/shapes/Line.js
Normal file
67
src/shapes/Line.js
Normal file
@@ -0,0 +1,67 @@
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
// Line
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Line constructor. 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);
|
||||
Reference in New Issue
Block a user