TextSprite.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. //可能还是要用html写,因为要加按钮和图片
  7. export class TextSprite extends THREE.Object3D{
  8. //注:为了分两层控制scale,不直接extend Sprite
  9. constructor( options={}){
  10. super()
  11. let map = new THREE.Texture();
  12. this.sprite = new Sprite( Object.assign({
  13. root:this
  14. }
  15. ,options,
  16. {
  17. map,
  18. })
  19. )
  20. this.add(this.sprite)
  21. this.fontWeight = options.fontWeight == void 0 ? 'Bold' : options.fontWeight
  22. this.rectBorderThick = options.rectBorderThick || 0
  23. this.textBorderThick = options.textBorderThick || 0
  24. this.fontface = 'Arial';
  25. this.fontsize = options.fontsize || 16;
  26. this.textBorderColor = options.textBorderColor || { r: 0, g: 0, b: 0, a: 0.0 };
  27. this.backgroundColor = options.backgroundColor || { r: 255, g: 255, b: 255, a: 1.0 };
  28. this.textColor = options.textColor || {r: 0, g: 0, b: 0, a: 1.0};
  29. this.borderColor = options.borderColor || { r: 0, g: 0, b: 0, a: 0.0 };
  30. this.borderRadius = options.borderRadius || 6;
  31. this.margin = options.margin
  32. if(options.text != void 0)this.setText(options.text)
  33. this.name = options.name
  34. //this.setText(text);
  35. this.addEventListener('dispose', this.dispose.bind(this))
  36. }
  37. setText(text){
  38. if (this.text !== text){
  39. this.text = text + '';
  40. this.updateTexture();
  41. }
  42. }
  43. setTextColor(color){
  44. this.textColor = color;
  45. this.updateTexture();
  46. }
  47. setBorderColor(color){
  48. this.borderColor = color;
  49. this.updateTexture();
  50. }
  51. setBackgroundColor(color){
  52. this.backgroundColor = color;
  53. this.updateTexture();
  54. }
  55. setPos(pos){
  56. this.position.copy(pos)
  57. this.sprite.update()
  58. }
  59. update(){
  60. this.sprite.update()
  61. }
  62. setVisible(v){
  63. Potree.Utils.updateVisible(this, 'setVisible', v)
  64. }
  65. setUniforms(name,value){
  66. this.sprite.setUniforms(name,value)
  67. }
  68. updateTexture(){
  69. let canvas = document.createElement('canvas');
  70. let context = canvas.getContext('2d');
  71. context.font = this.fontWeight + ' ' + this.fontsize + 'px ' + this.fontface;
  72. //context["font-weight"] = 100; //语法与 CSS font 属性相同。
  73. // get size data (height depends only on font size)
  74. //this.text = 'f 啊啊啊 jg'
  75. let metrics = context.measureText(this.text );
  76. let textWidth = metrics.width;
  77. let margin = this.margin || new THREE.Vector2(this.fontsize, Math.max( this.fontsize*0.4, 10) );
  78. let spriteWidth = 2 * margin.x + textWidth + 2 * this.rectBorderThick;
  79. let spriteHeight = 2 * margin.y + this.fontsize + 2 * this.rectBorderThick;
  80. context.canvas.width = spriteWidth;
  81. context.canvas.height = spriteHeight;
  82. context.font = this.fontWeight + ' ' + this.fontsize + 'px ' + this.fontface;
  83. let expand = Math.max(1, Math.pow(this.fontsize / 16, 1.3)) // 针对英文大部分在baseLine之上所以降低一点,或者可以识别当不包含jgqp时才加这个值
  84. //canvas原点在左上角
  85. context.textBaseline = 'alphabetic' // "middle" //设置文字基线。当起点y设置为0时,只有该线以下的部分被绘制出来。middle时文字显示一半(但是对该字体所有字的一半,有的字是不一定显示一半的,尤其汉字),alphabetic时是英文字母的那条基线。
  86. let actualHeight = metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent; // 当前文本字符串在这个字体下用的实际高度
  87. //文字y向距离从textBaseline向上算
  88. let y = metrics.actualBoundingBoxAscent + margin.y + expand
  89. //console.log(this.text, 'y' , y, 'actualBoundingBoxAscent', metrics.actualBoundingBoxAscent,'expand',expand )
  90. // border color
  91. context.strokeStyle = 'rgba(' + this.borderColor.r + ',' + this.borderColor.g + ',' +
  92. this.borderColor.b + ',' + this.borderColor.a + ')';
  93. context.lineWidth = this.rectBorderThick;
  94. // background color
  95. context.fillStyle = 'rgba(' + this.backgroundColor.r + ',' + this.backgroundColor.g + ',' +
  96. this.backgroundColor.b + ',' + this.backgroundColor.a + ')';
  97. this.roundRect(context, this.rectBorderThick / 2, this.rectBorderThick / 2,
  98. spriteWidth - this.rectBorderThick, spriteHeight - this.rectBorderThick, this.borderRadius);
  99. // text color
  100. if(this.textBorderThick){
  101. context.strokeStyle = 'rgba(' + this.textBorderColor.r + ',' + this.textBorderColor.g + ',' +
  102. this.textBorderColor.b + ',' + this.textBorderColor.a + ')';
  103. context.lineWidth = this.textBorderThick;
  104. context.strokeText(this.text , this.rectBorderThick + margin.x, y /* spriteHeight/2 + expand */ );
  105. }
  106. context.fillStyle = 'rgba(' + this.textColor.r + ',' + this.textColor.g + ',' +
  107. this.textColor.b + ',' + this.textColor.a + ')';
  108. context.fillText(this.text , this.rectBorderThick + margin.x, y /* spriteHeight/2 + expand */);//x,y
  109. let texture = new THREE.Texture(canvas);
  110. texture.minFilter = THREE.LinearFilter; //LinearMipMapLinearFilter会缩放到power of 2
  111. texture.magFilter = THREE.LinearFilter;
  112. texture.needsUpdate = true;
  113. if(this.sprite.material.map){
  114. this.sprite.material.map.dispose()
  115. }
  116. this.sprite.material.map = texture;
  117. this.sprite.scale.set(spriteWidth * 0.01, spriteHeight * 0.01, 1.0);
  118. }
  119. roundRect(ctx, x, y, w, h, r){
  120. ctx.beginPath();
  121. ctx.moveTo(x + r, y);
  122. ctx.lineTo(x + w - r, y);
  123. ctx.arcTo(x + w, y, x + w, y + r, r );//圆弧。前四个参数同quadraticCurveTo
  124. //ctx.quadraticCurveTo(x + w, y, x + w, y + r); //二次贝塞尔曲线需要两个点。第一个点是用于二次贝塞尔计算中的控制点,第二个点是曲线的结束点。
  125. ctx.lineTo(x + w, y + h - r);
  126. ctx.arcTo(x + w, y + h, x + w - r, y + h, r );
  127. ctx.lineTo(x + r, y + h);
  128. ctx.arcTo(x, y + h, x, y + h - r, r );
  129. ctx.lineTo(x, y + r);
  130. ctx.arcTo(x, y, x + r, y, r );
  131. ctx.closePath();
  132. ctx.fill();
  133. ctx.stroke();
  134. }
  135. dispose(){
  136. this.sprite.material.uniforms.map.value.dispose()
  137. this.parent && this.parent.remove(this)
  138. this.sprite.dispatchEvent({type:'dispose'})
  139. this.removeAllListeners()
  140. }
  141. }