Project.vue 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <template>
  2. <div>
  3. <mesh name="Project" :position="position">
  4. <geometry type="Box" :args="[size.x, size.y, size.z]" />
  5. <material type="MeshLambert" :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: 35, y: 10, z: 10 },
  39. position: { x: 5, y: 5, z: 0 },
  40. label_position: { x: 5, y: 5, z: 6 },
  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 = 30 + Math.random() * 90
  58. this.position.y = -10 + Math.random() * this.size.y / 2
  59. // this.label_position.y = this.position.y + this.size.y / 2 - 5
  60. this.label_position.y = 5
  61. this.position.x = this.label_position.x = (-this.len + 1) / 2 * (this.size.x + 5) + (this.size.x + 5) * this.index
  62. this.label_canvas = this.createLabelCanvas()
  63. console.log('this.label_canvas', this.label_canvas)
  64. },
  65. methods: {
  66. createLabelCanvas () {
  67. console.log('createLabelCanvas', this.data.Titre)
  68. const size = 48
  69. const borderSize = 2
  70. const ctx = document.createElement('canvas').getContext('2d')
  71. const font = `${size}px bold noto_sans`
  72. ctx.font = font
  73. // measure how long the name will be
  74. const doubleBorderSize = borderSize * 2
  75. const width = ctx.measureText(this.data.Titre).width + doubleBorderSize
  76. const height = size + doubleBorderSize
  77. this.label_size = { x: width / 21, y: height / 21 }
  78. ctx.canvas.width = width
  79. ctx.canvas.height = height
  80. // need to set font again after resizing canvas
  81. ctx.font = font
  82. ctx.textBaseline = 'top'
  83. // ctx.fillStyle = 'green'
  84. // ctx.fillRect(0, 0, width, height)
  85. ctx.fillStyle = 'black'
  86. ctx.fillText(this.data.Titre, borderSize, borderSize)
  87. // console.log('createLabelCanvas', ctx)
  88. return ctx.canvas
  89. }
  90. }
  91. }
  92. </script>