Img.vue 1.7 KB

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