gifItem2.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <template>
  2. <div>
  3. <canvas id="gifItem2" width="400" height="400" tabindex="0"/>
  4. <p>图片源(注意:这是一张图片)</p>
  5. <img src="./2.png">
  6. </div>
  7. </template>
  8. <script lang="ts">
  9. import { SGraphScene, SGraphView, SImageItem } from "@persagy-web/graph";
  10. import { Component, Vue } from "vue-property-decorator";
  11. import { SPainter } from "@persagy-web/draw";
  12. // 前置条件
  13. class SGGifItem extends SImageItem {
  14. /** 当前帧数 */
  15. _curFrame: number = 0;
  16. /** 裁剪的起始点 x 坐标 */
  17. sx: number = 0;
  18. /** 裁剪的起始点 y 坐标 */
  19. sy: number = 0;
  20. onDraw(painter: SPainter) {
  21. painter.translate(-this.origin.x, -this.origin.y);
  22. if (this.isLoadOver) {
  23. // @ts-ignore
  24. painter.drawImage(this.img, 0, 0, this.imgWidth, this.imgHeight, this.sx + this.width * this._curFrame, this.sy, this.width, this.height);
  25. this._curFrame++;
  26. if (this._curFrame > 15) {
  27. this._curFrame = 0;
  28. }
  29. }
  30. }
  31. }
  32. @Component
  33. export default class GIFCanvas2 extends Vue {
  34. imgSrc: string = require('./2.png');
  35. /**
  36. * 页面挂载
  37. */
  38. mounted(): void {
  39. // 步骤1
  40. const view = new SGraphView('gifItem2');
  41. const scene = new SGraphScene();
  42. // 步骤2
  43. const item = new SGGifItem(null, this.imgSrc);
  44. // 步骤3
  45. item.width = 500;
  46. item.height = 500;
  47. // 步骤4
  48. scene.addItem(item);
  49. view.scene = scene;
  50. view.fitItemToView([item]);
  51. // 步骤5
  52. setInterval(() => {
  53. view.update();
  54. }, 80)
  55. }
  56. }
  57. </script>
  58. <style scoped>
  59. </style>