forked from SeregPie/THREE.TextTexture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
THREE.TextTexture.min.js
1 lines (1 loc) · 3.72 KB
/
THREE.TextTexture.min.js
1
(function(a){let b=function(){let c=document.createElement('canvas').getContext('2d');return function(d,e){return c.font=d,c.measureText(e)}}();a.TextTexture=class extends a.Texture{constructor({autoRedraw:i=!0,text:j='',fontStyle:k='normal',fontVariant:l='normal',fontWeight:m='normal',fontSize:n=16,fontFamily:o='sans-serif',textAlign:p='center',lineHeight:q=1,padding:r=1/4,magFilter:s=a.LinearFilter,minFilter:t=a.LinearFilter,mapping:c,wrapS:d,wrapT:e,format:f,type:g,anisotropy:h}={}){super(document.createElement('canvas'),c,d,e,s,t,f,g,h),this.autoRedraw=i,this._text=j,this._fontStyle=k,this._fontVariant=l,this._fontWeight=m,this._fontSize=n,this._fontFamily=o,this._textAlign=p,this._lineHeight=q,this._padding=r,this.redraw()}redraw(){let c=this.image.getContext('2d');if(c.clearRect(0,0,c.canvas.width,c.canvas.height),this.textWidth&&this.textHeight){c.canvas.width=this.textWidth+2*this.paddingInPixels,c.canvas.height=this.textHeight+2*this.paddingInPixels,c.font=this.font,c.textAlign=this.textAlign,c.textBaseline='middle',c.fillStyle='white';let d=this.paddingInPixels+(()=>{switch(c.textAlign.toLowerCase()){case'left':return 0;case'right':return this.textWidth;case'center':return this.textWidth/2;}})(),e=this.paddingInPixels+this.fontSize/2;for(let f of this.lines)c.fillText(f,d,e),e+=this.lineHeightInPixels}else c.canvas.width=c.canvas.height=1;this.needsUpdate=!0}_redrawIfAuto(){this.autoRedraw&&this.redraw()}get text(){return this._text}set text(c){this._text!==c&&(this._text=c,this._lines=void 0,this._textWidth=void 0,this._textHeight=void 0,this._redrawIfAuto())}_computeLines(){return this.text?this.text.split('\n'):[]}get lines(){return void 0===this._lines&&(this._lines=this._computeLines()),this._lines}get linesCount(){return this.lines.length}get fontStyle(){return this._fontStyle}set fontStyle(c){this._fontStyle!==c&&(this._fontStyle=c,this._font=void 0,this._textWidth=void 0,this._redrawIfAuto())}get fontVariant(){return this._fontVariant}set fontVariant(c){this._fontVariant!==c&&(this._fontVariant=c,this._font=void 0,this._textWidth=void 0,this._redrawIfAuto())}get fontWeight(){return this._fontWeight}set fontWeight(c){this._fontWeight!==c&&(this._fontWeight=c,this._font=void 0,this._textWidth=void 0,this._redrawIfAuto())}get fontSize(){return this._fontSize}set fontSize(c){this._fontSize!==c&&(this._fontSize=c,this._font=void 0,this._textWidth=void 0,this._textHeight=void 0,this._redrawIfAuto())}get fontFamily(){return this._fontFamily}set fontFamily(c){this._fontFamily!==c&&(this._fontFamily=c,this._font=void 0,this._textWidth=void 0,this._redrawIfAuto())}_computeFont(){return[this.fontStyle,this.fontVariant,this.fontWeight,`${this.fontSize}px`,this.fontFamily].join(' ')}get font(){return void 0===this._font&&(this._font=this._computeFont()),this._font}get textAlign(){return this._textAlign}set textAlign(c){this._textAlign!==c&&(this._textAlign=c,this._redrawIfAuto())}get lineHeight(){return this._lineHeight}set lineHeight(c){this._lineHeight!==c&&(this._lineHeight=c,this._textHeight=void 0,this._redrawIfAuto())}get lineHeightInPixels(){return this.fontSize*this.lineHeight}_computeTextWidth(){let c=0;for(let d of this.lines)c=Math.max(b(this.font,d).width,c);return c}get textWidth(){return void 0===this._textWidth&&(this._textWidth=this._computeTextWidth()),this._textWidth}_computeTextHeight(){return this.fontSize*(this.lineHeight*(this.linesCount-1)+1)}get textHeight(){return void 0===this._textHeight&&(this._textHeight=this._computeTextHeight()),this._textHeight}get padding(){return this._padding}set padding(c){this._padding!==c&&(this._padding=c,this._redrawIfAuto())}get paddingInPixels(){return this.fontSize*this.padding}get aspect(){return this.image.width&&this.image.height?this.image.width/this.image.height:1}}})(THREE);