Group.vue 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <script setup lang="ts">
  2. import { ref, onMounted } from 'vue'
  3. import { Vector2 } from '../lmm/math/Vector2'
  4. import { Group } from '../lmm/objects/Group'
  5. import { Img2D } from '../lmm/objects/Img2D'
  6. import { ImagePromises } from '../lmm/objects/ObjectUtils'
  7. // 获取父级属性
  8. defineProps({
  9. size: { type: Object, default: { width: 1000, height: 500 } },
  10. })
  11. // 对应canvas 画布的Ref对象
  12. const canvasRef = ref<HTMLCanvasElement>()
  13. const images: HTMLImageElement[] = []
  14. for (let i = 1; i < 8; i++) {
  15. const image = new Image()
  16. image.src = `https://yxyy-pandora.oss-cn-beijing.aliyuncs.com/stamp-images/${i}.png`
  17. images.push(image)
  18. }
  19. const group = new Group()
  20. /* 渲染 */
  21. function render(ctx: CanvasRenderingContext2D) {
  22. group.add(
  23. ...images.map((image, i) => {
  24. return new Img2D({
  25. image,
  26. position: new Vector2(200, 80 * i + 50),
  27. size: new Vector2(image.width, image.height).multiplyScalar(0.3),
  28. style: {
  29. shadowColor: 'rgba(0,0,0,0.5)',
  30. shadowBlur: 5,
  31. shadowOffsetY: 20,
  32. },
  33. })
  34. })
  35. )
  36. group.draw(ctx)
  37. }
  38. onMounted(() => {
  39. const canvas = canvasRef.value
  40. const ctx = canvas?.getContext('2d')
  41. if (!ctx) {
  42. return
  43. }
  44. Promise.all(ImagePromises(images)).then(() => {
  45. render(ctx)
  46. })
  47. })
  48. </script>
  49. <template>
  50. <canvas ref="canvasRef" :width="size.width" :height="size.height"></canvas>
  51. </template>
  52. <style scoped></style>