-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
1 lines (1 loc) · 3.53 KB
/
index.js
1
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("three")):"function"==typeof define&&define.amd?define(["three"],e):((t="undefined"!=typeof globalThis?globalThis:t||self).THREE=t.THREE||{},t.THREE.TextTexture=e(t.THREE))}(this,(function(t){"use strict";let e=class extends t.Texture{constructor(){super(document.createElement("canvas"));let e=null,i=()=>{var t;return null!==(t=e)&&void 0!==t?t:e=this.createDrawing()},n=()=>i().width,r=()=>i().height,o=!0,l=1,a=()=>t.MathUtils.ceilPowerOfTwo(n()*l),s=()=>t.MathUtils.ceilPowerOfTwo(r()*l),h=t=>{if(l!==t){let e=a(),i=s();l=t;let n=a(),r=s();n===e&&r===i||(o=!0)}},c=(()=>{let e=new t.Vector3,i=new t.Vector2,o=new t.Vector3,l=new t.Vector3,a=new t.Vector2;return(s,h,c)=>{if(a.set(n(),r()),a.x&&a.y){s.getWorldPosition(o),c.getWorldPosition(e);let n=o.distanceTo(e);if(c.isPerspectiveCamera&&(n*=2*Math.tan(t.MathUtils.degToRad(c.fov)/2)),(c.isPerspectiveCamera||c.isOrthographicCamera)&&(n/=c.zoom),n){var f,d;s.getWorldScale(l);let t=null!==(f=null===(d=h.capabilities)||void 0===d?void 0:d.maxTextureSize)&&void 0!==f?f:1/0;return h.getDrawingBufferSize(i),Math.min(Math.max(l.x/n*(i.x/a.x),l.y/n*(i.y/a.y)),t/a.x,t/a.y)}}return 0}})();Object.defineProperties(this,{width:{get:n},height:{get:r},pixelRatio:{get:()=>l,set:h},needsRedraw:{set(t){t&&(o=!0,e=null)}}}),Object.assign(this,{redraw(){if(o){let t=this.image,e=t.getContext("2d");e.clearRect(0,0,t.width,t.height),t.width=a(),t.height=s(),t.width&&t.height?(e.save(),e.scale(t.width/n(),t.height/r()),((...t)=>{i().draw(...t)})(e),e.restore()):t.width=t.height=1,o=!1,this.needsUpdate=!0}},setOptimalPixelRatio(...t){h(c(...t))}})}};e.prototype.isDynamicTexture=!0;let i=class extends e{constructor({alignment:t="center",backgroundColor:e="rgba(0,0,0,0)",color:i="#fff",fontFamily:n="sans-serif",fontSize:r=16,fontStyle:o="normal",fontVariant:l="normal",fontWeight:a="normal",lineGap:s=1/4,padding:h=.5,strokeColor:c="#fff",strokeWidth:f=0,text:d=""}={}){super(),Object.entries({alignment:t,backgroundColor:e,color:i,fontFamily:n,fontSize:r,fontStyle:o,fontVariant:l,fontWeight:a,lineGap:s,padding:h,strokeColor:c,strokeWidth:f,text:d}).forEach((([t,e])=>{Object.defineProperty(this,t,{get:()=>e,set(t){e!==t&&(e=t,this.needsRedraw=!0)}})}))}get lines(){let{text:t}=this;return t?t.split("\n"):[]}get font(){return function(t,e,i,n,r){let o=document.createElement("span");return o.style.font="1px serif",o.style.fontFamily=t,o.style.fontSize="".concat(e,"px"),o.style.fontStyle=i,o.style.fontVariant=n,o.style.fontWeight=r,o.style.font}(this.fontFamily,this.fontSize,this.fontStyle,this.fontVariant,this.fontWeight)}checkFontFace(){try{let{font:t}=this;return document.fonts.check(t)}catch{}return!0}async loadFontFace(){try{let{font:t}=this;await document.fonts.load(t)}catch{}}createDrawing(){let{alignment:t,backgroundColor:e,color:i,font:n,fontSize:r,lineGap:o,lines:l,padding:a,strokeColor:s,strokeWidth:h}=this;a*=r,o*=r,h*=r;let c=l.length,f=r+o,d=c?(()=>{let t=document.createElement("canvas").getContext("2d");return t.font=n,Math.max(...l.map((e=>t.measureText(e).width)))})():0,g=a+h/2,u=d+2*g;return{width:u,height:(c?r+f*(c-1):0)+2*g,draw(o){let a;o.fillStyle=e,o.fillRect(0,0,o.canvas.width,o.canvas.height);let c=g+r/2;Object.assign(o,{fillStyle:i,font:n,lineWidth:h,miterLimit:1,strokeStyle:s,textAlign:(()=>{switch(t){case"left":return a=g,"left";case"right":return a=u-g,"right"}return a=u/2,"center"})(),textBaseline:"middle"}),l.forEach((t=>{o.fillText(t,a,c),h&&o.strokeText(t,a,c),c+=f}))}}}};return i.prototype.isTextTexture=!0,i}));