Img2D.vue 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <script setup lang="ts">
  2. import { ref, onMounted } from 'vue'
  3. import { Vector2 } from '../lmm/math/Vector2'
  4. import { Img2D } from '../lmm/objects/Img2D'
  5. // 获取父级属性
  6. const props = defineProps({
  7. size: { type: Object, default: { width: 0, height: 0 } },
  8. })
  9. // 对应canvas 画布的Ref对象
  10. const canvasRef = ref<HTMLCanvasElement>()
  11. /* 图案 */
  12. const image = new Image()
  13. image.src =
  14. 'https://yxyy-pandora.oss-cn-beijing.aliyuncs.com/stamp-images/1.png'
  15. const pattern = new Img2D({ image })
  16. /* 渲染 */
  17. function render(ctx: CanvasRenderingContext2D) {
  18. const imgSize = new Vector2(image.width, image.height).multiplyScalar(0.6)
  19. pattern.setOption({
  20. /* 模型矩阵 */
  21. rotate: 0.4,
  22. position: new Vector2(0, -50),
  23. scale: new Vector2(0.5),
  24. /* Img属性 */
  25. size: imgSize.clone(),
  26. offset: imgSize.clone().multiplyScalar(-0.5),
  27. view: {
  28. x: 0,
  29. y: 0,
  30. width: image.width / 2,
  31. height: image.height / 2,
  32. },
  33. /* 样式 */
  34. style: {
  35. globalAlpha: 0.8,
  36. shadowColor: 'rgba(0,0,0,0.5)',
  37. shadowBlur: 5,
  38. shadowOffsetY: 20,
  39. },
  40. })
  41. /* 把canvas坐标系的原点移动到画布中心,以此为裁剪空间 */
  42. ctx.translate(props.size.width / 2, props.size.height / 2)
  43. /* 绘图 */
  44. pattern.draw(ctx)
  45. /* 绘制图案边界 */
  46. ctx.save()
  47. pattern.crtPath(ctx)
  48. ctx.stroke()
  49. ctx.restore()
  50. }
  51. onMounted(() => {
  52. const canvas = canvasRef.value
  53. const ctx = canvas?.getContext('2d')
  54. if (!ctx) {
  55. return
  56. }
  57. image.onload = function () {
  58. render(ctx)
  59. }
  60. })
  61. </script>
  62. <template>
  63. <canvas ref="canvasRef" :width="size.width" :height="size.height"></canvas>
  64. </template>
  65. <style scoped></style>