mirror of
https://github.com/konvajs/konva.git
synced 2025-04-05 20:48:28 +08:00
2 lines
5.5 KiB
JavaScript
2 lines
5.5 KiB
JavaScript
Kinetic.Shape=function(a){this._initShape(a)},Kinetic.Shape.prototype={_initShape:function(a){this.nodeType="Shape",this.appliedShadow=!1;var b=Kinetic.Global.shapes,c;for(;;){c=Kinetic.Type._getRandomColorKey();if(c&&!(c in b))break}this.colorKey=c,b[c]=this,Kinetic.Node.call(this,a)},getContext:function(){return this.getLayer().getContext()},getCanvas:function(){return this.getLayer().getCanvas()},stroke:function(a){var b=this.getStrokeWidth(),c=this.getStroke();if(c||b){var d=Kinetic.Global,e=!1;a.save(),this.attrs.shadow&&!this.appliedShadow&&(e=this._applyShadow(a)),a.lineWidth=b||2,a.strokeStyle=c||"black",a.stroke(a),a.restore(),e&&this.stroke(a)}},_getFillType:function(a){return a?Kinetic.Type._isString(a)?"COLOR":a.image?"PATTERN":a.start&&a.end&&!a.start.radius&&!a.end.radius?"LINEAR_GRADIENT":a.start&&a.end&&Kinetic.Type._isNumber(a.start.radius)&&Kinetic.Type._isNumber(a.end.radius)?"RADIAL_GRADIENT":"UNKNOWN":undefined},fill:function(a){var b=!1,c=this.getFill(),d=this._getFillType(c);if(c){a.save(),this.attrs.shadow&&!this.appliedShadow&&(b=this._applyShadow(a));var e=c.start,f=c.end;switch(d){case"COLOR":a.fillStyle=c,a.fill(a);break;case"PATTERN":var g=c.repeat?c.repeat:"repeat";c.scale&&a.scale(c.scale.x,c.scale.y),c.offset&&a.translate(c.offset.x,c.offset.y),a.fillStyle=a.createPattern(c.image,g),a.fill(a);break;case"LINEAR_GRADIENT":var h=a.createLinearGradient(e.x,e.y,f.x,f.y),i=c.colorStops;for(var j=0;j<i.length;j+=2)h.addColorStop(i[j],i[j+1]);a.fillStyle=h,a.fill(a);break;case"RADIAL_GRADIENT":var h=a.createRadialGradient(e.x,e.y,e.radius,f.x,f.y,f.radius),i=c.colorStops;for(var j=0;j<i.length;j+=2)h.addColorStop(i[j],i[j+1]);a.fillStyle=h,a.fill(a);break;default:a.fillStyle="black",a.fill(a)}a.restore()}b&&this.fill(a)},fillText:function(a,b){var c=!1;this.attrs.textFill&&(a.save(),this.attrs.shadow&&!this.appliedShadow&&(c=this._applyShadow(a)),a.fillStyle=this.attrs.textFill,a.fillText(b,0,0),a.restore()),c&&this.fillText(a,b,0,0)},strokeText:function(a,b){var c=!1;if(this.attrs.textStroke||this.attrs.textStrokeWidth){a.save(),this.attrs.shadow&&!this.appliedShadow&&(c=this._applyShadow(a));var d=this.attrs.textStroke?this.attrs.textStroke:"black",e=this.attrs.textStrokeWidth?this.attrs.textStrokeWidth:2;a.lineWidth=e,a.strokeStyle=d,a.strokeText(b,0,0),a.restore()}c&&this.strokeText(a,b,0,0)},drawImage:function(){var a=!1,b=arguments[0];b.save();var c=Array.prototype.slice.call(arguments);if(c.length===6||c.length===10)this.attrs.shadow&&!this.appliedShadow&&(a=this._applyShadow(b)),c.length===6?b.drawImage(c[1],c[2],c[3],c[4],c[5]):b.drawImage(c[1],c[2],c[3],c[4],c[5],c[6],c[7],c[8],c[9]);b.restore(),a&&this.drawImage.apply(this,c)},applyOpacity:function(a){var b=this.getAbsoluteOpacity();b!==1&&(a.globalAlpha=b)},applyLineJoin:function(a){this.attrs.lineJoin&&(a.lineJoin=this.attrs.lineJoin)},applyLineCap:function(a){this.attrs.lineCap&&(a.lineCap=this.attrs.lineCap)},setShadow:function(a){a.offset!==undefined&&(a.offset=Kinetic.Type._getXY(a.offset)),this.setAttr("shadow",Kinetic.Type._merge(a,this.getShadow()))},setFill:function(a){var b=this.getFill(),c=this._getFillType(a),d=this._getFillType(b),e=c==="COLOR"||d==="COLOR",f=c===d||c==="UNKNOWN";a.offset!==undefined&&(a.offset=Kinetic.Type._getXY(a.offset)),!e&&f&&(a=Kinetic.Type._merge(a,b)),this.setAttr("fill",a)},setSize:function(){var a=Kinetic.Type._getSize(Array.prototype.slice.call(arguments));this.setWidth(a.width),this.setHeight(a.height)},getSize:function(){return{width:this.getWidth(),height:this.getHeight()}},_get:function(a){return this.nodeType===a||this.shapeType===a?[this]:[]},_applyShadow:function(a){var b=this.attrs.shadow;if(b){var c=this.getAbsoluteOpacity(),d=b.color?b.color:"black",e=b.blur?b.blur:5,f=b.offset?b.offset:{x:0,y:0};return b.opacity&&(a.globalAlpha=b.opacity*c),a.shadowColor=d,a.shadowBlur=e,a.shadowOffsetX=f.x,a.shadowOffsetY=f.y,this.appliedShadow=!0,!0}return!1},intersects:function(){var a=Kinetic.Type._getXY(Array.prototype.slice.call(arguments)),b=this.getStage(),c=b.bufferCanvas;c.clear(),this.draw(c);var d=c.context.getImageData(Math.round(a.x),Math.round(a.y),1,1).data;return d[3]>0},remove:function(){Kinetic.Node.prototype.remove.call(this),delete Kinetic.Global.shapes[this.colorKey]},draw:function(a){if(this.attrs.drawFunc&&Kinetic.Node.prototype._shouldDraw.call(this,a)){var b=this.getStage(),c=a.getContext(),d=[],e=this.parent;d.unshift(this);while(e)d.unshift(e),e=e.parent;c.save();for(var f=0;f<d.length;f++){var g=d[f],h=g.getTransform(),i=h.getMatrix();c.transform(i[0],i[1],i[2],i[3],i[4],i[5])}this.applyOpacity(c),this.applyLineJoin(c),this.applyLineCap(c),this.appliedShadow=!1;var j=Kinetic.Global.BUFFER_WHITELIST,k=Kinetic.Global.BUFFER_BLACKLIST,l={};if(a.name==="buffer"){for(var f=0;f<j.length;f++){var m=j[f];l[m]=this.attrs[m];if(this.attrs[m]||m==="fill"&&!this.attrs.stroke&&!("image"in this.attrs))this.attrs[m]="#"+this.colorKey}for(var f=0;f<k.length;f++){var m=k[f];l[m]=this.attrs[m],this.attrs[m]=""}"image"in this.attrs&&(l.image=this.attrs.image,this.imageBuffer?this.attrs.image=this.imageBuffer:(this.attrs.image=null,this.attrs.fill="#"+this.colorKey)),c.globalAlpha=1}this.attrs.drawFunc.call(this,a.getContext());if(a.name==="buffer"){var n=j.concat(k);for(var f=0;f<n.length;f++){var m=n[f];this.attrs[m]=l[m]}this.attrs.image=l.image}c.restore()}}},Kinetic.Global.extend(Kinetic.Shape,Kinetic.Node),Kinetic.Node.addGettersSetters(Kinetic.Shape,["stroke","lineJoin","strokeWidth","drawFunc","cornerRadius"]),Kinetic.Node.addGetters(Kinetic.Shape,["shadow","fill"]);
|