TextSprite.js 6.7 KB

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