mirror of
https://github.com/konvajs/konva.git
synced 2025-04-05 20:48:28 +08:00
2 lines
3.7 KiB
JavaScript
2 lines
3.7 KiB
JavaScript
Kinetic.Text=function(a){this._initText(a)},Kinetic.Text.prototype={_initText:function(a){this.setDefaultAttrs({fontFamily:"Calibri",text:"",fontSize:12,align:"left",verticalAlign:"top",fontStyle:"normal",padding:0,width:"auto",height:"auto",detectionType:"path",cornerRadius:0,lineHeight:1.2}),this.dummyCanvas=document.createElement("canvas"),this.shapeType="Text",a.drawFunc=this.drawFunc,Kinetic.Shape.call(this,a);var b=["fontFamily","fontSize","fontStyle","padding","align","lineHeight","text","width","height"],c=this;for(var d=0;d<b.length;d++){var e=b[d];this.on(e+"Change.kinetic",c._setTextData)}c._setTextData()},drawFunc:function(a){a.beginPath();var b=this.getWidth(),c=this.getHeight();this.attrs.cornerRadius===0?a.rect(0,0,b,c):(a.moveTo(this.attrs.cornerRadius,0),a.lineTo(b-this.attrs.cornerRadius,0),a.arc(b-this.attrs.cornerRadius,this.attrs.cornerRadius,this.attrs.cornerRadius,Math.PI*3/2,0,!1),a.lineTo(b,c-this.attrs.cornerRadius),a.arc(b-this.attrs.cornerRadius,c-this.attrs.cornerRadius,this.attrs.cornerRadius,0,Math.PI/2,!1),a.lineTo(this.attrs.cornerRadius,c),a.arc(this.attrs.cornerRadius,c-this.attrs.cornerRadius,this.attrs.cornerRadius,Math.PI/2,Math.PI,!1),a.lineTo(0,this.attrs.cornerRadius),a.arc(this.attrs.cornerRadius,this.attrs.cornerRadius,this.attrs.cornerRadius,Math.PI,Math.PI*3/2,!1)),a.closePath(),this.fill(a),this.stroke(a);var d=this.attrs.padding,e=this.attrs.lineHeight*this.getTextHeight(),f=this.textArr;a.font=this.attrs.fontStyle+" "+this.attrs.fontSize+"pt "+this.attrs.fontFamily,a.textBaseline="middle",a.textAlign="left",a.save(),a.translate(d,0),a.translate(0,d+this.getTextHeight()/2);var g=this.appliedShadow;for(var h=0;h<f.length;h++){var i=f[h];this.appliedShadow=g,a.save(),this.attrs.align==="right"?a.translate(this.getWidth()-this._getTextSize(i).width-d*2,0):this.attrs.align==="center"&&a.translate((this.getWidth()-this._getTextSize(i).width-d*2)/2,0),this.fillText(a,i),this.strokeText(a,i),a.restore(),a.translate(0,e)}a.restore()},setText:function(a){var b=Kinetic.Type._isString(a)?a:a.toString();this.setAttr("text",b)},getWidth:function(){return this.attrs.width==="auto"?this.getTextWidth()+this.attrs.padding*2:this.attrs.width},getHeight:function(){return this.attrs.height==="auto"?this.getTextHeight()*this.textArr.length*this.attrs.lineHeight+this.attrs.padding*2:this.attrs.height},getTextWidth:function(){return this.textWidth},getTextHeight:function(){return this.textHeight},_getTextSize:function(a){var b=this.dummyCanvas,c=b.getContext("2d");c.save(),c.font=this.attrs.fontStyle+" "+this.attrs.fontSize+"pt "+this.attrs.fontFamily;var d=c.measureText(a);return c.restore(),{width:d.width,height:parseInt(this.attrs.fontSize,10)}},_setTextData:function(){var a=this.attrs.text.split(""),b=[],c=0,d=!0;this.textWidth=0,this.textHeight=this._getTextSize(this.attrs.text).height;var e=this.attrs.lineHeight*this.textHeight;while(a.length>0&&d&&(this.attrs.height==="auto"||e*(c+1)<this.attrs.height-this.attrs.padding*2)){var f=0,g=undefined;d=!1;while(f<a.length){if(a.indexOf("\n")===f){a.splice(f,1),g=a.splice(0,f).join("");break}var h=a.slice(0,f);if(this.attrs.width!=="auto"&&this._getTextSize(h.join("")).width>this.attrs.width-this.attrs.padding*2){if(f==0)break;var i=h.lastIndexOf(" "),j=h.lastIndexOf("-"),k=Math.max(i,j);if(k>=0){g=a.splice(0,1+k).join("");break}g=a.splice(0,f).join("");break}f++,f===a.length&&(g=a.splice(0,f).join(""))}this.textWidth=Math.max(this.textWidth,this._getTextSize(g).width),g!==undefined&&(b.push(g),d=!0),c++}this.textArr=b}},Kinetic.Global.extend(Kinetic.Text,Kinetic.Shape),Kinetic.Node.addGettersSetters(Kinetic.Text,["fontFamily","fontSize","fontStyle","textFill","textStroke","textStrokeWidth","padding","align","lineHeight"]),Kinetic.Node.addGetters(Kinetic.Text,["text"]);
|