TextSprite.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. export class TextSprite extends THREE.Object3D {
  2. constructor(options = {}) {
  3. super()
  4. let map = new THREE.Texture()
  5. map.minFilter = THREE.LinearFilter
  6. map.magFilter = THREE.LinearFilter
  7. this.sprite = new THREE.Sprite(
  8. new THREE.SpriteMaterial({
  9. map,
  10. color: 0xffffff,
  11. transparent: true,
  12. depthTest: false,
  13. depthWrite: false,
  14. })
  15. )
  16. this.add(this.sprite)
  17. this.sprite.renderOrder = options.renderOrder != void 0 ? options.renderOrder : 2
  18. this.rectBorderThick = options.rectBorderThick || 0
  19. this.textBorderThick = options.textBorderThick || 0
  20. this.fontface = 'Arial'
  21. this.fontsize = options.fontsize || 16
  22. this.textBorderColor = options.textBorderColor || { r: 0, g: 0, b: 0, a: 0.0 }
  23. this.backgroundColor = options.backgroundColor || { r: 255, g: 255, b: 255, a: 1.0 }
  24. this.textColor = options.textColor || { r: 0, g: 0, b: 0, a: 1.0 }
  25. this.borderColor = options.borderColor || { r: 0, g: 0, b: 0, a: 0.0 }
  26. this.borderRadius = options.borderRadius || 6
  27. if (options.text != void 0) this.setText(options.text)
  28. this.name = options.name
  29. //this.setText(text);
  30. this.addEventListener('dispose', this.dispose.bind(this))
  31. }
  32. setText(text) {
  33. if (this.text !== text) {
  34. this.text = text + ''
  35. this.updateTexture()
  36. }
  37. }
  38. setTextColor(color) {
  39. this.textColor = color
  40. this.updateTexture()
  41. }
  42. setBorderColor(color) {
  43. this.borderColor = color
  44. this.updateTexture()
  45. }
  46. setBackgroundColor(color) {
  47. this.backgroundColor = color
  48. this.updateTexture()
  49. }
  50. setPos(pos) {
  51. this.position.copy(pos)
  52. this.sprite.update()
  53. }
  54. update() {
  55. this.sprite.update()
  56. }
  57. setVisible(v) {
  58. this.visible = v
  59. }
  60. setUniforms(name, value) {
  61. this.sprite.setUniforms(name, value)
  62. }
  63. updateTexture() {
  64. let canvas = document.createElement('canvas')
  65. let context = canvas.getContext('2d')
  66. context.font = 'Bold ' + this.fontsize + 'px ' + this.fontface
  67. context['font-weight'] = 100 //语法与 CSS font 属性相同。
  68. // get size data (height depends only on font size)
  69. //this.text = '啊啊啊啊啊啊fag'
  70. let metrics = context.measureText(this.text)
  71. let textWidth = metrics.width
  72. let margin = new THREE.Vector2(this.fontsize, this.fontsize * 0.4)
  73. let spriteWidth = 2 * margin.x + textWidth + 2 * this.rectBorderThick
  74. let spriteHeight = 2 * margin.y + this.fontsize + 2 * this.rectBorderThick
  75. context.canvas.width = spriteWidth
  76. context.canvas.height = spriteHeight
  77. context.font = 'Bold ' + this.fontsize + 'px ' + this.fontface
  78. let diff = 2 //针对英文大部分在baseLine之上所以降低一点(metrics.fontBoundingBoxAscent - metrics.fontBoundingBoxDescent) / 2
  79. context.textBaseline = 'middle'
  80. // border color
  81. context.strokeStyle = 'rgba(' + this.borderColor.r + ',' + this.borderColor.g + ',' + this.borderColor.b + ',' + this.borderColor.a + ')'
  82. context.lineWidth = this.rectBorderThick
  83. // background color
  84. context.fillStyle = 'rgba(' + this.backgroundColor.r + ',' + this.backgroundColor.g + ',' + this.backgroundColor.b + ',' + this.backgroundColor.a + ')'
  85. this.roundRect(context, this.rectBorderThick / 2, this.rectBorderThick / 2, spriteWidth - this.rectBorderThick, spriteHeight - this.rectBorderThick, this.borderRadius)
  86. // text color
  87. if (this.textBorderThick) {
  88. context.strokeStyle = 'rgba(' + this.textBorderColor.r + ',' + this.textBorderColor.g + ',' + this.textBorderColor.b + ',' + this.textBorderColor.a + ')'
  89. context.lineWidth = this.textBorderThick
  90. context.strokeText(this.text, this.rectBorderThick + margin.x, spriteHeight / 2 + diff)
  91. }
  92. context.fillStyle = 'rgba(' + this.textColor.r + ',' + this.textColor.g + ',' + this.textColor.b + ',' + this.textColor.a + ')'
  93. context.fillText(this.text, this.rectBorderThick + margin.x, spriteHeight / 2 + diff) //x,y
  94. let texture = new THREE.Texture(canvas)
  95. texture.minFilter = THREE.LinearFilter
  96. texture.magFilter = THREE.LinearFilter
  97. texture.needsUpdate = true
  98. //this.material.needsUpdate = true;
  99. if (this.sprite.material.map) {
  100. this.sprite.material.map.dispose()
  101. }
  102. this.sprite.material.map = texture
  103. this.sprite.scale.set(spriteWidth * 0.01, spriteHeight * 0.01, 1.0)
  104. }
  105. roundRect(ctx, x, y, w, h, r) {
  106. ctx.beginPath()
  107. ctx.moveTo(x + r, y)
  108. ctx.lineTo(x + w - r, y)
  109. ctx.arcTo(x + w, y, x + w, y + r, r) //圆弧。前四个参数同quadraticCurveTo
  110. //ctx.quadraticCurveTo(x + w, y, x + w, y + r); //二次贝塞尔曲线需要两个点。第一个点是用于二次贝塞尔计算中的控制点,第二个点是曲线的结束点。
  111. ctx.lineTo(x + w, y + h - r)
  112. ctx.arcTo(x + w, y + h, x + w - r, y + h, r)
  113. ctx.lineTo(x + r, y + h)
  114. ctx.arcTo(x, y + h, x, y + h - r, r)
  115. ctx.lineTo(x, y + r)
  116. ctx.arcTo(x, y, x + r, y, r)
  117. ctx.closePath()
  118. ctx.fill()
  119. ctx.stroke()
  120. }
  121. dispose() {
  122. this.sprite.material.uniforms.map.value.dispose()
  123. this.parent && this.parent.remove(this)
  124. this.sprite.dispatchEvent({ type: 'dispose' })
  125. this.removeAllListeners()
  126. }
  127. }