TextSprite.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. map.minFilter = THREE.LinearFilter;
  13. map.magFilter = THREE.LinearFilter;
  14. this.sprite = new Sprite( Object.assign({
  15. root:this
  16. }
  17. ,options,
  18. {
  19. map,
  20. })
  21. )
  22. this.add(this.sprite)
  23. this.fontWeight = options.fontWeight == void 0 ? 'Bold' : options.fontWeight
  24. this.rectBorderThick = options.rectBorderThick || 0
  25. this.textBorderThick = options.textBorderThick || 0
  26. this.fontface = 'Arial';
  27. this.fontsize = options.fontsize || 16;
  28. this.textBorderColor = options.textBorderColor || { r: 0, g: 0, b: 0, a: 0.0 };
  29. this.backgroundColor = options.backgroundColor || { r: 255, g: 255, b: 255, a: 1.0 };
  30. this.textColor = options.textColor || {r: 0, g: 0, b: 0, a: 1.0};
  31. this.borderColor = options.borderColor || { r: 0, g: 0, b: 0, a: 0.0 };
  32. this.borderRadius = options.borderRadius || 6;
  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 = color;
  46. this.updateTexture();
  47. }
  48. setBorderColor(color){
  49. this.borderColor = color;
  50. this.updateTexture();
  51. }
  52. setBackgroundColor(color){
  53. this.backgroundColor = 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 = '啊啊啊啊啊啊fag'
  76. let metrics = context.measureText(this.text );
  77. let textWidth = metrics.width;
  78. let 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 diff = 2//针对英文大部分在baseLine之上所以降低一点(metrics.fontBoundingBoxAscent - metrics.fontBoundingBoxDescent) / 2
  85. context.textBaseline = "middle"
  86. // border color
  87. context.strokeStyle = 'rgba(' + this.borderColor.r + ',' + this.borderColor.g + ',' +
  88. this.borderColor.b + ',' + this.borderColor.a + ')';
  89. context.lineWidth = this.rectBorderThick;
  90. // background color
  91. context.fillStyle = 'rgba(' + this.backgroundColor.r + ',' + this.backgroundColor.g + ',' +
  92. this.backgroundColor.b + ',' + this.backgroundColor.a + ')';
  93. this.roundRect(context, this.rectBorderThick / 2, this.rectBorderThick / 2,
  94. spriteWidth - this.rectBorderThick, spriteHeight - this.rectBorderThick, this.borderRadius);
  95. // text color
  96. if(this.textBorderThick){
  97. context.strokeStyle = 'rgba(' + this.textBorderColor.r + ',' + this.textBorderColor.g + ',' +
  98. this.textBorderColor.b + ',' + this.textBorderColor.a + ')';
  99. context.lineWidth = this.textBorderThick;
  100. context.strokeText(this.text , this.rectBorderThick + margin.x,spriteHeight/2 + diff );
  101. }
  102. context.fillStyle = 'rgba(' + this.textColor.r + ',' + this.textColor.g + ',' +
  103. this.textColor.b + ',' + this.textColor.a + ')';
  104. context.fillText(this.text , this.rectBorderThick + margin.x, spriteHeight/2 + diff );//x,y
  105. let texture = new THREE.Texture(canvas);
  106. texture.minFilter = THREE.LinearFilter;
  107. texture.magFilter = THREE.LinearFilter;
  108. texture.needsUpdate = true;
  109. //this.material.needsUpdate = true;
  110. if(this.sprite.material.map){
  111. this.sprite.material.map.dispose()
  112. }
  113. this.sprite.material.map = texture;
  114. this.sprite.scale.set(spriteWidth * 0.01, spriteHeight * 0.01, 1.0);
  115. }
  116. roundRect(ctx, x, y, w, h, r){
  117. ctx.beginPath();
  118. ctx.moveTo(x + r, y);
  119. ctx.lineTo(x + w - r, y);
  120. ctx.arcTo(x + w, y, x + w, y + r, r );//圆弧。前四个参数同quadraticCurveTo
  121. //ctx.quadraticCurveTo(x + w, y, x + w, y + r); //二次贝塞尔曲线需要两个点。第一个点是用于二次贝塞尔计算中的控制点,第二个点是曲线的结束点。
  122. ctx.lineTo(x + w, y + h - r);
  123. ctx.arcTo(x + w, y + h, x + w - r, y + h, r );
  124. ctx.lineTo(x + r, y + h);
  125. ctx.arcTo(x, y + h, x, y + h - r, r );
  126. ctx.lineTo(x, y + r);
  127. ctx.arcTo(x, y, x + r, y, r );
  128. ctx.closePath();
  129. ctx.fill();
  130. ctx.stroke();
  131. }
  132. dispose(){
  133. this.sprite.material.uniforms.map.value.dispose()
  134. this.parent && this.parent.remove(this)
  135. this.sprite.dispatchEvent({type:'dispose'})
  136. this.removeAllListeners()
  137. }
  138. }