enlarge.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <template>
  2. <div class="container">
  3. <div>
  4. <canvas id="enlarge" width="400" height="400" tabindex="0"/>
  5. </div>
  6. <div>
  7. <canvas id="enlargeCopy" width="400" height="400" tabindex="0"/>
  8. <div class="mask"></div>
  9. </div>
  10. </div>
  11. </template>
  12. <script lang="ts">
  13. import { SGraphScene, SGraphView, SImageItem } from "@persagy-web/graph";
  14. import { Component, Vue } from "vue-property-decorator";
  15. import {SGraphItem} from "@persagy-web/graph/lib";
  16. import { SPoint } from "@persagy-web/draw/lib";
  17. @Component
  18. export default class GIFCanvas extends Vue {
  19. url: string = require('./items/1.jpg');
  20. viewCopy: SGraphView | null = null;
  21. view: SGraphView | null = null;
  22. /**
  23. * 页面挂载
  24. */
  25. mounted(): void {
  26. // 步骤1
  27. this.view = new SGraphView('enlarge');
  28. this.viewCopy = new SGraphView('enlargeCopy');
  29. // 步骤2
  30. const scene = new SGraphScene();
  31. const item = new SImageItem(null, this.url);
  32. scene.addItem(item);
  33. // 步骤3
  34. item.connect('onMouseMove', this, this.mouseMove);
  35. // 步骤4
  36. this.viewCopy.scene = scene;
  37. this.view.scene = scene;
  38. this.view.fitItemToView([item]);
  39. this.viewCopy.scale = 3 * this.view.scale;
  40. this.view.scalable = false;
  41. }
  42. // 步骤5
  43. mouseMove(item: SGraphItem, ev: MouseEvent[]) {
  44. const p = item.mapToScene(ev[0].x, ev[0].y);
  45. this.locationGraphy(p)
  46. }
  47. // 定位点到放大视图的中心点
  48. locationGraphy(point:any) {
  49. let centerX = (this.viewCopy!!.width / 2) - point.x * this.viewCopy!!.scale;
  50. let centerY = (this.viewCopy!!.height / 2) - point.y * this.viewCopy!!.scale;
  51. this.viewCopy!!.origin = new SPoint(centerX, centerY)
  52. }
  53. }
  54. </script>
  55. <style scoped lang="less">
  56. .container{
  57. height: 500px;
  58. & > div {
  59. float: left;
  60. position: relative;
  61. width: 400px;
  62. height: 400px;
  63. canvas {
  64. position: absolute;
  65. }
  66. .mask {
  67. width: 100%;
  68. height: 100%;
  69. position: absolute;
  70. z-index: 1;
  71. }
  72. }
  73. #enlargeCopy{
  74. margin-left: 10px;
  75. }
  76. }
  77. </style>