TextSprite.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // /**
  2. // * adapted from http://stemkoski.github.io/Three.js/Sprite-Text-Labels.html
  3. // */
  4. import * as THREE from "../../../libs/three.js/build/three.module.js";
  5. import Sprite from './Sprite.js'
  6. import Common from '../utils/Common.js';
  7. //可能还是要用html写,因为要加按钮和图片
  8. export class TextSprite extends THREE.Object3D{
  9. //注:为了分两层控制scale,不直接extend Sprite
  10. constructor( options={}){
  11. super()
  12. let map = new THREE.Texture();
  13. map.minFilter = THREE.LinearFilter;
  14. map.magFilter = THREE.LinearFilter;
  15. this.sprite = new Sprite( Object.assign({
  16. root:this
  17. }
  18. ,options,
  19. {
  20. map,
  21. })
  22. )
  23. this.add(this.sprite)
  24. this.fontWeight = options.fontWeight == void 0 ? 'Bold' : options.fontWeight
  25. this.rectBorderThick = options.rectBorderThick || 0
  26. this.textBorderThick = options.textBorderThick || 0
  27. this.fontface = 'Arial';
  28. this.fontsize = options.fontsize || 16;
  29. this.textBorderColor = options.textBorderColor ? Common.CloneObject(options.textBorderColor):{ r: 0, g: 0, b: 0, a: 0.0 };
  30. this.backgroundColor = options.backgroundColor ? Common.CloneObject(options.backgroundColor):{ r: 255, g: 255, b: 255, a: 1.0 };
  31. this.textColor = options.textColor ? Common.CloneObject(options.textColor):{r: 0, g: 0, b: 0, a: 1.0};
  32. this.borderColor = options.borderColor ? Common.CloneObject(options.borderColor):{ r: 0, g: 0, b: 0, a: 0.0 };
  33. this.borderRadius = options.borderRadius || 6;
  34. this.margin = options.margin
  35. this.setText(options.text)
  36. this.name = options.name
  37. //this.setText(text);
  38. }
  39. setText(text){
  40. if(text == void 0)text = ''
  41. if (this.text !== text) {
  42. if (!(text instanceof Array)) {
  43. this.text = [text + '']
  44. } else this.text = text
  45. this.updateTexture()
  46. this.sprite.waitUpdate() //重新计算各个viewport的matrix
  47. }
  48. }
  49. setTextColor(color){
  50. this.textColor = Common.CloneObject(color);
  51. this.updateTexture();
  52. }
  53. setBorderColor(color){
  54. this.borderColor = Common.CloneObject(color);
  55. this.updateTexture();
  56. }
  57. setBackgroundColor(color){
  58. this.backgroundColor = Common.CloneObject(color);
  59. this.updateTexture();
  60. }
  61. setPos(pos){
  62. this.position.copy(pos)
  63. this.sprite.waitUpdate()
  64. }
  65. update(){
  66. this.sprite.waitUpdate()
  67. }
  68. /* setVisible(v){
  69. Potree.Utils.updateVisible(this, 'setVisible', v)
  70. } */
  71. setUniforms(name,value){
  72. this.sprite.setUniforms(name,value)
  73. }
  74. updateTexture1(){
  75. let canvas = document.createElement('canvas');
  76. let context = canvas.getContext('2d');
  77. const r = window.devicePixelRatio
  78. context.font = this.fontWeight + ' ' + this.fontsize * r + 'px ' + this.fontface;
  79. //context["font-weight"] = 100; //语法与 CSS font 属性相同。
  80. //this.text = '啊啊啊啊啊啊fag'
  81. let metrics = context.measureText(this.text );
  82. let textWidth = metrics.width;
  83. let margin = (this.margin ? new THREE.Vector2().copy(this.margin) : new THREE.Vector2(this.fontsize, Math.max( this.fontsize*0.4, 10) )).clone().multiplyScalar(r);
  84. let spriteWidth = 2 * margin.x + textWidth + 2 * this.rectBorderThick * r ;
  85. let spriteHeight = 2 * margin.y + this.fontsize * r + 2 * this.rectBorderThick * r;
  86. context.canvas.width = spriteWidth;
  87. context.canvas.height = spriteHeight;
  88. context.font = this.fontWeight + ' ' + this.fontsize * r + 'px ' + this.fontface;
  89. /* let diff = 2//针对英文大部分在baseLine之上所以降低一点(metrics.fontBoundingBoxAscent - metrics.fontBoundingBoxDescent) / 2
  90. context.textBaseline = "middle"
  91. */
  92. let expand = Math.max(1, Math.pow(this.fontsize / 16, 1.3)) * r // 针对英文大部分在baseLine之上所以降低一点,或者可以识别当不包含jgqp时才加这个值
  93. //canvas原点在左上角
  94. context.textBaseline = 'alphabetic' // "middle" //设置文字基线。当起点y设置为0时,只有该线以下的部分被绘制出来。middle时文字显示一半(但是对该字体所有字的一半,有的字是不一定显示一半的,尤其汉字),alphabetic时是英文字母的那条基线。
  95. //let actualHeight = metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent; // 当前文本字符串在这个字体下用的实际高度
  96. //文字y向距离从textBaseline向上算
  97. let actualBoundingBoxAscent = metrics.actualBoundingBoxAscent == void 0 ? this.fontsize * r * 0.8 : metrics.actualBoundingBoxAscent //有的流览器没有。只能大概给一个
  98. let y = actualBoundingBoxAscent + margin.y + expand
  99. //console.log(this.text, 'y' , y, 'actualBoundingBoxAscent', metrics.actualBoundingBoxAscent,'expand',expand )
  100. // border color
  101. context.strokeStyle = 'rgba(' + this.borderColor.r + ',' + this.borderColor.g + ',' +
  102. this.borderColor.b + ',' + this.borderColor.a + ')';
  103. let rectBorderThick = this.rectBorderThick * r;
  104. context.lineWidth = rectBorderThick
  105. // background color
  106. context.fillStyle = 'rgba(' + this.backgroundColor.r + ',' + this.backgroundColor.g + ',' +
  107. this.backgroundColor.b + ',' + this.backgroundColor.a + ')';
  108. this.roundRect(context, rectBorderThick / 2 , rectBorderThick / 2,
  109. spriteWidth - rectBorderThick, spriteHeight - rectBorderThick, this.borderRadius * r);
  110. // text color
  111. if(this.textBorderThick){
  112. context.strokeStyle = 'rgba(' + this.textBorderColor.r + ',' + this.textBorderColor.g + ',' +
  113. this.textBorderColor.b + ',' + this.textBorderColor.a + ')';
  114. context.lineWidth = this.textBorderThick * r;
  115. context.strokeText(this.text , rectBorderThick + margin.x, y /* spriteHeight/2 + diff */ );
  116. }
  117. context.fillStyle = 'rgba(' + this.textColor.r + ',' + this.textColor.g + ',' +
  118. this.textColor.b + ',' + this.textColor.a + ')';
  119. context.fillText(this.text , rectBorderThick + margin.x, y/* spriteHeight/2 + diff */ );//x,y
  120. let texture = new THREE.Texture(canvas);
  121. texture.minFilter = THREE.LinearFilter;
  122. texture.magFilter = THREE.LinearFilter;
  123. texture.needsUpdate = true;
  124. //this.material.needsUpdate = true;
  125. if(this.sprite.material.map){
  126. this.sprite.material.map.dispose()
  127. }
  128. this.sprite.material.map = texture;
  129. this.sprite.scale.set(spriteWidth * 0.01 / r, spriteHeight * 0.01 / r, 1.0);
  130. }
  131. updateTexture(){
  132. let canvas = document.createElement('canvas');
  133. let context = canvas.getContext('2d');
  134. const r = window.devicePixelRatio
  135. context.font = this.fontWeight + ' ' + this.fontsize * r + 'px ' + this.fontface;
  136. //context["font-weight"] = 100; //语法与 CSS font 属性相同。
  137. //this.text = '啊啊啊啊啊啊fag'
  138. let textMaxWidth = 0,
  139. infos = []
  140. for (let text of this.text) {
  141. let metrics = context.measureText(text)
  142. let textWidth = metrics.width
  143. infos.push(metrics)
  144. textMaxWidth = Math.max(textMaxWidth, textWidth)
  145. }
  146. let margin = (this.margin ? new THREE.Vector2().copy(this.margin) : new THREE.Vector2(this.fontsize, Math.max( this.fontsize*0.4, 10) )).clone().multiplyScalar(r);
  147. const lineSpace = (this.fontsize + margin.y) * 0.5
  148. let spriteWidth = 2 * margin.x + textMaxWidth + 2 * this.rectBorderThick * r ;
  149. let spriteHeight = 2 * margin.y + this.fontsize * r * this.text.length + 2 * this.rectBorderThick * r + lineSpace * (this.text.length - 1);
  150. context.canvas.width = spriteWidth;
  151. context.canvas.height = spriteHeight;
  152. context.font = this.fontWeight + ' ' + this.fontsize * r + 'px ' + this.fontface;
  153. if(spriteWidth>1000){
  154. console.error('spriteWidth',spriteWidth,'spriteHeight',spriteHeight,this.fontsize,r,this.text,margin)
  155. }
  156. /* let diff = 2//针对英文大部分在baseLine之上所以降低一点(metrics.fontBoundingBoxAscent - metrics.fontBoundingBoxDescent) / 2
  157. context.textBaseline = "middle"
  158. */
  159. let expand = Math.max(1, Math.pow(this.fontsize / 16, 1.3)) * r // 针对英文大部分在baseLine之上所以降低一点,或者可以识别当不包含jgqp时才加这个值
  160. //canvas原点在左上角
  161. context.textBaseline = 'alphabetic' // "middle" //设置文字基线。当起点y设置为0时,只有该线以下的部分被绘制出来。middle时文字显示一半(但是对该字体所有字的一半,有的字是不一定显示一半的,尤其汉字),alphabetic时是英文字母的那条基线。
  162. // border color
  163. context.strokeStyle = 'rgba(' + this.borderColor.r + ',' + this.borderColor.g + ',' + this.borderColor.b + ',' + this.borderColor.a + ')';
  164. let rectBorderThick = this.rectBorderThick * r;
  165. context.lineWidth = rectBorderThick
  166. // background color
  167. context.fillStyle = 'rgba(' + this.backgroundColor.r + ',' + this.backgroundColor.g + ',' + this.backgroundColor.b + ',' + this.backgroundColor.a + ')';
  168. this.roundRect(context, rectBorderThick / 2 , rectBorderThick / 2, spriteWidth - rectBorderThick, spriteHeight - rectBorderThick, this.borderRadius * r);
  169. context.fillStyle = 'rgba(' + this.textColor.r + ',' + this.textColor.g + ',' + this.textColor.b + ',' + this.textColor.a + ')'
  170. let y = margin.y
  171. for (let i = 0; i < this.text.length; i++) {
  172. //let actualHeight = metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent // 当前文本字符串在这个字体下用的实际高度
  173. //文字y向距离从textBaseline向上算
  174. let actualBoundingBoxAscent = infos[i].actualBoundingBoxAscent == void 0 ? this.fontsize * r * 0.8 : infos[i].actualBoundingBoxAscent //有的流览器没有。只能大概给一个
  175. y += actualBoundingBoxAscent + expand
  176. //console.log(actualBoundingBoxAscent)
  177. //console.log(this.text, 'y' , y, 'actualBoundingBoxAscent', metrics.actualBoundingBoxAscent,'expand',expand )
  178. let textLeftSpace = (textMaxWidth - infos[i].width) / 2
  179. let x = this.rectBorderThick + margin.x + textLeftSpace
  180. // text color
  181. if (this.textBorderThick) {
  182. context.strokeStyle = 'rgba(' + this.textBorderColor.r + ',' + this.textBorderColor.g + ',' + this.textBorderColor.b + ',' + this.textBorderColor.a + ')'
  183. context.lineWidth = this.textBorderThick * r
  184. context.strokeText(this.text[i], x, y)
  185. }
  186. if (this.textshadowColor) {
  187. context.shadowOffsetX = 0
  188. context.shadowOffsetY = 0
  189. context.shadowColor = this.textshadowColor
  190. context.shadowBlur = 12 * r
  191. }
  192. context.fillText(this.text[i], x, y)
  193. y += lineSpace
  194. }
  195. let texture = new THREE.Texture(canvas);
  196. texture.minFilter = THREE.LinearFilter;
  197. texture.magFilter = THREE.LinearFilter;
  198. texture.needsUpdate = true;
  199. //this.material.needsUpdate = true;
  200. if(this.sprite.material.map){
  201. this.sprite.material.map.dispose()
  202. }
  203. this.sprite.material.map = texture;
  204. this.sprite.scale.set(spriteWidth * 0.01 / r, spriteHeight * 0.01 / r, 1.0);
  205. }
  206. roundRect(ctx, x, y, w, h, r){
  207. ctx.beginPath();
  208. ctx.moveTo(x + r, y);
  209. ctx.lineTo(x + w - r, y);
  210. ctx.arcTo(x + w, y, x + w, y + r, r );//圆弧。前四个参数同quadraticCurveTo
  211. //ctx.quadraticCurveTo(x + w, y, x + w, y + r); //二次贝塞尔曲线需要两个点。第一个点是用于二次贝塞尔计算中的控制点,第二个点是曲线的结束点。
  212. ctx.lineTo(x + w, y + h - r);
  213. ctx.arcTo(x + w, y + h, x + w - r, y + h, r );
  214. ctx.lineTo(x + r, y + h);
  215. ctx.arcTo(x, y + h, x, y + h - r, r );
  216. ctx.lineTo(x, y + r);
  217. ctx.arcTo(x, y, x + r, y, r );
  218. ctx.closePath();
  219. ctx.fill();
  220. ctx.stroke();
  221. }
  222. dispose(){
  223. this.sprite.material.uniforms.map.value.dispose()
  224. this.parent && this.parent.remove(this)
  225. this.sprite.dispose()
  226. this.removeAllListeners()
  227. this.dispatchEvent('dispose')
  228. }
  229. }