gradient.vue 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <template>
  2. <div>
  3. <canvas :id="id" width="740" height="400" tabindex="0"/>
  4. </div>
  5. </template>
  6. <script lang="ts">
  7. import { SGraphScene, SGraphView } from "@persagy-web/graph/lib";
  8. import { SGradient, SLinearGradient, SRadialGradient } from "@persagy-web/draw/lib";
  9. import { GradRect } from "./GradRect";
  10. import { v1 as uuid } from "uuid";
  11. import { Component, Prop, Vue } from "vue-property-decorator";
  12. /**
  13. * 渐变
  14. *
  15. * @author 郝洁 <haojie@persagy.com>
  16. */
  17. @Component
  18. export default class GradientCanvas extends Vue {
  19. @Prop() private arg!: string;
  20. id: string = uuid();
  21. view: SGraphView | undefined;
  22. /**
  23. * 页面挂载
  24. */
  25. mounted(): void {
  26. this.init();
  27. };
  28. init(): void {
  29. const arr = this.arg.split(',');
  30. this.view = new SGraphView(this.id);
  31. const scene = new SGraphScene();
  32. let grad: SGradient;
  33. try {
  34. if (arr.length > 4) {
  35. // @ts-ignore
  36. grad = new SRadialGradient(...arr);
  37. } else {
  38. // @ts-ignore
  39. grad = new SLinearGradient(...arr);
  40. }
  41. } catch (e) {
  42. grad = new SLinearGradient(0, 0, 0, 1000);
  43. }
  44. const item = new GradRect(null, grad);
  45. scene.addItem(item);
  46. this.view.scene = scene;
  47. this.view.fitSceneToView();
  48. this.view.scalable = false;
  49. }
  50. }
  51. </script>