Project.vue 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <template>
  2. <div>
  3. <mesh name="Project" :position="position">
  4. <geometry type="Box" :args="[size.x, size.y, size.z]" />
  5. <material type="MeshBasic" :color="color" :options="block_opts" />
  6. </mesh>
  7. <mesh name="Label" :position="label_position">
  8. <geometry type="Plane" :args="[label_size.x, label_size.y]" />
  9. <material type="MeshBasic" :options="label_opts">
  10. <texture :options="label_texture_opts" />
  11. </material>
  12. </mesh>
  13. </div>
  14. </template>
  15. <script>
  16. import * as THREE from 'three'
  17. // import { Object3D } from '@'
  18. export default {
  19. name: 'Project',
  20. // mixins: [Object3D],
  21. // props: { size: Object, texture: String, position: Object, color: Number },
  22. props: { data: Object, len: Number, index: Number },
  23. data: () => ({
  24. block_opts: {
  25. side: THREE.DoubleSide,
  26. wireframe: false,
  27. transparent: true,
  28. opacity: 0.6
  29. // renderOrder: 0
  30. },
  31. label_opts: {
  32. side: THREE.DoubleSide,
  33. // wireframe: false,
  34. transparent: true
  35. // opacity: 0.6
  36. // renderOrder: 0
  37. },
  38. size: { x: 10, y: 10, z: 10 },
  39. position: { x: 5, y: 5, z: 0 },
  40. label_position: { x: 5, y: 5, z: 20 },
  41. color: 0xffffff,
  42. label_canvas: null,
  43. label_size: null
  44. }),
  45. computed: {
  46. label_texture_opts () {
  47. return {
  48. canvas: this.label_canvas,
  49. minFilter: THREE.LinearFilter,
  50. wrapS: THREE.ClampToEdgeWrapping,
  51. wrapT: THREE.ClampToEdgeWrapping
  52. }
  53. }
  54. },
  55. created () {
  56. // console.log('this.index', this.index)
  57. this.size.y = 20 + Math.random() * 90
  58. this.position.y = this.label_position.y = -10 + Math.random() * this.size.y / 2
  59. this.position.x = this.label_position.x = -this.len / 2 * 15 + 15 * this.index
  60. this.label_canvas = this.createLabelCanvas()
  61. console.log('this.label_canvas', this.label_canvas)
  62. },
  63. methods: {
  64. createLabelCanvas () {
  65. console.log('createLabelCanvas', this.data.Titre)
  66. const size = 48
  67. const borderSize = 2
  68. const ctx = document.createElement('canvas').getContext('2d')
  69. const font = `${size}px bold noto_sans`
  70. ctx.font = font
  71. // measure how long the name will be
  72. const doubleBorderSize = borderSize * 2
  73. const width = ctx.measureText(this.data.Titre).width + doubleBorderSize
  74. const height = size + doubleBorderSize
  75. this.label_size = { x: width / 10, y: height / 10 }
  76. ctx.canvas.width = width
  77. ctx.canvas.height = height
  78. // need to set font again after resizing canvas
  79. ctx.font = font
  80. ctx.textBaseline = 'top'
  81. // ctx.fillStyle = 'green'
  82. // ctx.fillRect(0, 0, width, height)
  83. ctx.fillStyle = 'white'
  84. ctx.fillText(this.data.Titre, borderSize, borderSize)
  85. // console.log('createLabelCanvas', ctx)
  86. return ctx.canvas
  87. }
  88. }
  89. }
  90. </script>