clip.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <div>
  3. <canvas :id="id" width="800" height="400" tabindex="0"/>
  4. </div>
  5. </template>
  6. <script lang="ts">
  7. import { Component, Vue } from "vue-property-decorator";
  8. import { v1 as uuid } from "uuid";
  9. import { SCanvasView, SColor, SPainter, SPath } from "@persagy-web/draw/lib";
  10. /**
  11. * 裁剪
  12. *
  13. * @author 郝洁 <haojie@persagy.com>
  14. */
  15. class ClipView extends SCanvasView {
  16. img: CanvasImageSource;
  17. arcX = 100;
  18. arcY = 100;
  19. stepX = 6;
  20. stepY = 8;
  21. timer: any;
  22. _url: string = '';
  23. set url(v: string) {
  24. if (this._url == v) {
  25. return;
  26. }
  27. this._url = v;
  28. // @ts-ignore
  29. this.img.src = v;
  30. } // Set url
  31. get url(): string {
  32. return this._url;
  33. } // Get url
  34. isLoadOver: boolean = false;
  35. /**
  36. * 构造函数
  37. * @param id
  38. */
  39. constructor(id: string) {
  40. super(id);
  41. this.img = new Image();
  42. this.img.onload = (e) => {
  43. this.isLoadOver = true;
  44. this.update();
  45. };
  46. this.img.onerror = (e) => {
  47. this.isLoadOver = false;
  48. this.update();
  49. console.log("加载图片错误!", e);
  50. };
  51. }
  52. /**
  53. * Item 绘制操作
  54. * @param painter 绘制对象
  55. */
  56. onDraw(painter: SPainter): void {
  57. // @ts-ignore
  58. painter.engine._canvas.save();
  59. // painter.save();
  60. //清空画布
  61. painter.clearRect(0, 0, 800, 400);
  62. clearTimeout(this.timer);
  63. //绘制操作
  64. painter.pen.color = SColor.Black;
  65. painter.brush.color = SColor.Black;
  66. painter.drawRect(0, 0, 800, 400);
  67. painter.brush.color = SColor.Transparent;
  68. let path = new SPath();
  69. path.arc(this.arcX, this.arcY, 100, 0, Math.PI * 2, 1);
  70. painter.clip = path;
  71. //绘制路径
  72. painter.drawPath(path);
  73. // console.log('------->');
  74. if (this.isLoadOver) {
  75. //绘制图片
  76. painter.drawImage(this.img, 0, 0, 800, 400);
  77. }
  78. // painter.restore();
  79. // @ts-ignore
  80. painter.engine._canvas.restore();
  81. this.timer = setTimeout(() => {
  82. if (this.arcX + 100 >= 800) {
  83. this.stepX *= -1;
  84. }
  85. if (this.arcX - 100 < 0) {
  86. this.stepX *= -1;
  87. }
  88. if (this.arcY + 100 >= 400) {
  89. this.stepY *= -1;
  90. }
  91. if (this.arcY - 100 < 0) {
  92. this.stepY *= -1;
  93. }
  94. this.arcX += this.stepX;
  95. this.arcY += this.stepY;
  96. this.update()
  97. }, 17);
  98. }
  99. }
  100. @Component
  101. export default class SelectContainerCanvas extends Vue {
  102. id: string = uuid();
  103. view: ClipView | undefined;
  104. img = require('../../public/assets/img/2.jpg');
  105. /**
  106. * 页面挂载
  107. */
  108. mounted(): void {
  109. this.init();
  110. };
  111. init(): void {
  112. this.view = new ClipView(this.id);
  113. this.view.url = this.img;
  114. };
  115. beforeDestroy(): void {
  116. clearTimeout(this.view!!.timer);
  117. }
  118. }
  119. </script>