video.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <div>
  3. <canvas id="videoCanvas" width="600" height="480" tabindex="0"/>
  4. <div id="video"></div>
  5. </div>
  6. </template>
  7. <script lang="ts">
  8. import { SGraphScene, SGraphView, SGraphItem } from "@persagy-web/graph";
  9. import { Component, Vue } from "vue-property-decorator";
  10. import { SPainter, SRect } from "@persagy-web/draw";
  11. class SVideoItem extends SGraphItem {
  12. /** 图片 dom */
  13. VObject: any;
  14. /** 图片加载是否完成 */
  15. isLoadOver: boolean = false;
  16. /** 图片地址 */
  17. private _url: string = "";
  18. get url(): string {
  19. return this._url;
  20. }
  21. set url(v: string) {
  22. this._url = v;
  23. this.VObject = document.createElement('VIDEO');
  24. this.VObject.setAttribute('width', '600');
  25. this.VObject.setAttribute('height', '480');
  26. // 设置自动播放
  27. this.VObject.setAttribute('autoplay', 'autoplay');
  28. // 设置视频路径
  29. this.VObject.setAttribute('src', v);
  30. // 设置循环播放
  31. this.VObject.setAttribute('loop', 'loop');
  32. // 添加到对应的容器中
  33. let VBox = document.getElementById(this.box);
  34. if (VBox) {
  35. // 先清空
  36. VBox.innerHTML = '';
  37. VBox.appendChild(this.VObject);
  38. }
  39. // 隐藏video标签
  40. this.VObject.style.display = 'none';
  41. }
  42. /** item 宽 */
  43. private _width: number = 600;
  44. get width(): number {
  45. return this._width;
  46. }
  47. set width(v: number) {
  48. if (v == this._width) {
  49. return;
  50. }
  51. this._width = v;
  52. this.update();
  53. }
  54. /** item 高 */
  55. private _height: number = 480;
  56. get height(): number {
  57. return this._height;
  58. }
  59. set height(v: number) {
  60. if (v == this._height) {
  61. return;
  62. }
  63. this._height = v;
  64. this.update();
  65. }
  66. /** 视频播放容器 id */
  67. box: string = '';
  68. /**
  69. * 构造函数
  70. *
  71. * @param parent 父类
  72. * @param url 视频路径
  73. * @param box 视频播放容器 id
  74. */
  75. constructor(parent: SGraphItem | null, url: string, box: string) {
  76. super(parent);
  77. if (url && box) {
  78. this.url = url;
  79. this.box = box;
  80. }
  81. }
  82. /**
  83. * Item 对象边界区域
  84. *
  85. * @return 边界区域
  86. */
  87. boundingRect(): SRect {
  88. return new SRect(0, 0, this.width, this.height);
  89. }
  90. /**
  91. * Item 绘制操作
  92. *
  93. * @param painter 绘制对象
  94. */
  95. onDraw(painter: SPainter): void {
  96. if (this.VObject) {
  97. // @ts-ignore
  98. painter.drawImage(this.VObject, 0, 0, this.width, this.height);
  99. }
  100. }
  101. }
  102. @Component
  103. export default class videoCanvas extends Vue {
  104. timer: any = null;
  105. mounted(): void {
  106. const view = new SGraphView('videoCanvas');
  107. const scene = new SGraphScene();
  108. const item = new SVideoItem(null, `https://www.w3cschool.cn/statics/demosource/mov_bbb.mp4`, 'video');
  109. scene.addItem(item);
  110. view.scene = scene;
  111. view.fitSceneToView(1);
  112. setInterval(() => {
  113. view.update();
  114. },60)
  115. view.scalable = false;
  116. }
  117. beforeDestroy(): void {
  118. this.timer && clearInterval(this.timer);
  119. this.timer = null;
  120. }
  121. }
  122. </script>
  123. <style scoped>
  124. </style>