arrow.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <template>
  2. <div>
  3. <el-table :data="tableData" style="width: 100%" class="elementTable">
  4. <el-table-column prop="val" label="值" width="180"></el-table-column>
  5. <el-table-column prop="desc" label="描述"></el-table-column>
  6. </el-table>
  7. <div style="margin: 14px 0;">
  8. <span style="font-size: 14px;">选择起始点样式</span>
  9. <el-select placeholder="请选择" @change="changeStart" size="small" v-model="begin">
  10. <el-option v-for="item in options" :key="item.value" :label="item.label"
  11. :value="item.value"></el-option>
  12. </el-select>
  13. <span style="font-size: 14px;">选择结束点样式</span>
  14. <el-select placeholder="请选择" @change="changeEnd" size="small" v-model="end">
  15. <el-option v-for="item in options" :key="item.value" :label="item.label"
  16. :value="item.value"></el-option>
  17. </el-select>
  18. </div>
  19. <canvas :id="id" width="740" height="400" tabindex="0"/>
  20. </div>
  21. </template>
  22. <script lang="ts">
  23. import { SGraphItem, SGraphScene, SGraphView } from "@persagy-web/graph/lib";
  24. import { SArrowStyleType, SPainter, SPoint, SRect } from "@persagy-web/draw/lib"
  25. import { Component, Vue } from "vue-property-decorator";
  26. import { v1 as uuid } from "uuid";
  27. /**
  28. * 绘制带有末端样式的线段
  29. *
  30. * @author 郝洁 <haojie@persagy.com>
  31. */
  32. class Polyline extends SGraphItem {
  33. /** 起始点样式 */
  34. _begin = SArrowStyleType.Basic;
  35. get begin(): SArrowStyleType {
  36. return this._begin;
  37. }
  38. set begin(v: SArrowStyleType) {
  39. this._begin = v;
  40. this.update();
  41. }
  42. /** 结束点样式 */
  43. _end = SArrowStyleType.Basic;
  44. get end(): SArrowStyleType {
  45. return this._end;
  46. }
  47. set end(v: SArrowStyleType) {
  48. this._end = v;
  49. this.update();
  50. }
  51. pointList: SPoint[] = [
  52. new SPoint(0, 0),
  53. new SPoint(1000, 1000),
  54. new SPoint(1200, 1800),
  55. new SPoint(1800, 1800),
  56. new SPoint(2000, 100),
  57. // new SPoint(0, 0)
  58. ];
  59. boundingRect(): SRect {
  60. return new SRect(0, 0, 2000, 1800);
  61. }
  62. /**
  63. * Item 绘制操作
  64. * @param painter 绘制对象
  65. */
  66. onDraw(painter: SPainter) {
  67. //线宽
  68. painter.pen.lineWidth = painter.toPx(1);
  69. //箭头起点,终点样式
  70. const style = {
  71. begin: this.begin,
  72. end: this.end
  73. };
  74. for (let i = 0; i < this.pointList.length - 1; i++) {
  75. //绘制带箭头的线段
  76. painter.drawArrowLine(this.pointList[i], this.pointList[i + 1], style)
  77. }
  78. }
  79. }
  80. @Component
  81. export default class ShadowCanvas extends Vue {
  82. id: string = uuid();
  83. options = [
  84. {
  85. value: SArrowStyleType.None,
  86. label: 'None'
  87. }, {
  88. value: SArrowStyleType.Basic,
  89. label: 'Basic'
  90. }, {
  91. value: SArrowStyleType.Triangle,
  92. label: 'Triangle'
  93. }, {
  94. value: SArrowStyleType.Diamond,
  95. label: 'Diamond'
  96. }, {
  97. value: SArrowStyleType.Square,
  98. label: 'Square'
  99. }, {
  100. value: SArrowStyleType.Circle,
  101. label: 'Circle'
  102. }
  103. ];
  104. view: SGraphView | undefined;
  105. item: Polyline | undefined;
  106. tableData = [
  107. {
  108. val: 'None',
  109. desc: '无样式'
  110. }, {
  111. val: 'Basic',
  112. desc: '基本箭头'
  113. },
  114. {
  115. val: 'Triangle',
  116. desc: '三角形箭头'
  117. },
  118. {
  119. val: 'Diamond',
  120. desc: '菱形箭头'
  121. },
  122. {
  123. val: 'Square',
  124. desc: '方形箭头'
  125. },
  126. {
  127. val: 'Circle',
  128. desc: '圆形箭头'
  129. },
  130. ];
  131. begin: SArrowStyleType = SArrowStyleType.Basic;
  132. end: SArrowStyleType = SArrowStyleType.Basic;
  133. /**
  134. * 页面挂载
  135. */
  136. mounted(): void {
  137. this.init()
  138. };
  139. init() {
  140. this.view = new SGraphView(this.id);
  141. const scene = new SGraphScene();
  142. this.item = new Polyline(null);
  143. scene.addItem(this.item);
  144. this.view.scene = scene;
  145. this.view.fitSceneToView();
  146. this.view.scalable = false;
  147. };
  148. changeStart(val: SArrowStyleType): void {
  149. this.item!!.begin = val
  150. };
  151. changeEnd(val: SArrowStyleType): void {
  152. this.item!!.end = val
  153. }
  154. }
  155. </script>