我建议遵循 KineticJS 源代码中的方法。 This blog post 解释了如何,但有点过时,所以我将包含一个最新示例,该示例还展示了如何向自定义形状添加属性。
下面的代码给出了一个创建一个新的Shape.Arc 对象的例子。此示例显示如何添加新函数和属性。
var Shape = {};
(function () {
Shape.Arc = function (config) {
this.initArc(config);
};
Shape.Arc.prototype = {
initArc: function (config) {
Kinetic.Shape.call(this, config);
this._setDrawFuncs();
this.shapeType = 'Arc;'
drc.ui.utils.setupShape(this);
},
drawFunc: function (context) {
context.beginPath();
context.arc(0,0, this.getRadius(), this.getStartAngle(),
this.getEndAngle(), true);
context.fillStrokeShape(this);
}
};
Kinetic.Util.extend(Shape.Arc, Kinetic.Shape);
//Add properties to shape.
//The code below adds the getRadius(), getStartAngle() functions above.
Kinetic.Factory.addGetterSetter(Shape.Arc, 'radius', 0);
Kinetic.Factory.addGetterSetter(Shape.Arc, 'startAngle', 0);
Kinetic.Factory.addGetterSetter(Shape.Arc, 'endAngle', 180);
})();
重要的是它被包装在一个匿名函数中,这样可以创建多个实例。
创建弧线:
var arc = new Shape.Arc({
radius: radius,
x: centreX,
y: centreY,
startAngle: 0,
endAngle: Math.PI * 2
});