Align.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <template>
  2. <div>
  3. <el-button @click="addCircle">Circle</el-button>
  4. <el-button @click="addRect">Rect</el-button>
  5. <el-select placeholder="请选择" @change="changeAlign" v-model="value">
  6. <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
  7. </el-select>
  8. <canvas id="align" width="740" height="400" tabindex="0"></canvas>
  9. </div>
  10. </template>
  11. <script>
  12. import { SGraphItem, SGraphLayoutType, SGraphScene, SGraphView } from "@persagy-web/graph/lib";
  13. import { SColor, SRect } from "@persagy-web/draw/lib";
  14. /**
  15. * 对其
  16. *
  17. * @author 郝洁 <haojie@persagy.com>
  18. */
  19. class RectItem extends SGraphItem {
  20. width = 200;
  21. height = 100;
  22. text = '';
  23. constructor(parent) {
  24. super(parent);
  25. this.moveable = true;
  26. this.selectable = true;
  27. this.text = new Date().getMilliseconds().toString()
  28. }
  29. /**
  30. * 矩形数据类型绘制
  31. */
  32. boundingRect() {
  33. return new SRect(0, 0, this.width, this.height);
  34. }
  35. /**
  36. * Item 绘制操作
  37. * @param canvas 绘制对象
  38. */
  39. onDraw(canvas) {
  40. canvas.pen.color = SColor.Transparent;
  41. canvas.pen.lineWidth = canvas.toPx(1);
  42. canvas.brush.color = this.selected ? SColor.Red : new SColor("#00fcee");
  43. canvas.drawRect(0, 0, this.width, this.height);
  44. canvas.pen.lineDash = [10, 10];
  45. canvas.pen.color = SColor.Yellow;
  46. canvas.brush.color = SColor.Transparent;
  47. canvas.drawRect(this.boundingRect());
  48. canvas.brush.color = SColor.Black;
  49. canvas.drawText(`${this.x},${this.y}`, 0, 0);
  50. }
  51. }
  52. class CircleItem extends SGraphItem {
  53. r = 75;
  54. text = '';
  55. constructor(parent) {
  56. super(parent);
  57. this.moveable = true;
  58. this.selectable = true;
  59. this.text = new Date().getMilliseconds().toString()
  60. }
  61. /**
  62. * 矩形数据类型绘制
  63. */
  64. boundingRect() {
  65. return new SRect(0, 0, this.r * 2, this.r * 2);
  66. }
  67. onDraw(canvas) {
  68. canvas.pen.color = SColor.Transparent;
  69. canvas.pen.lineWidth = canvas.toPx(1);
  70. canvas.brush.color = this.selected ? SColor.Red : new SColor("#00fcee");
  71. canvas.drawCircle(this.r, this.r, this.r);
  72. canvas.pen.color = SColor.Yellow;
  73. canvas.brush.color = SColor.Transparent;
  74. canvas.pen.lineDash = [10, 10];
  75. canvas.drawRect(this.boundingRect());
  76. canvas.brush.color = SColor.Black;
  77. canvas.drawText(`${this.x},${this.y}`, 0, 0);
  78. }
  79. }
  80. class SScene extends SGraphScene {
  81. /** 定义命令 */
  82. cmd = 0;
  83. /**
  84. * 构造函数
  85. */
  86. constructor() {
  87. super();
  88. }
  89. onMouseUp(event) {
  90. switch (this.cmd) {
  91. case 1:
  92. this.addCircle(event.x, event.y);
  93. break;
  94. case 2:
  95. this.addRect(event.x, event.y);
  96. break;
  97. default:
  98. super.onMouseUp(event);
  99. }
  100. this.cmd = 0;
  101. return false
  102. }
  103. addCircle(x, y) {
  104. let item = new CircleItem(null);
  105. item.moveTo(x - 50, y - 50);
  106. this.addItem(item);
  107. }
  108. addRect(x, y) {
  109. let item = new RectItem(null);
  110. item.moveTo(x - 50, y - 50);
  111. this.addItem(item);
  112. }
  113. }
  114. class TestView extends SGraphView {
  115. /**
  116. * 构造函数
  117. */
  118. constructor() {
  119. super("align");
  120. }
  121. }
  122. export default {
  123. data() {
  124. return {
  125. scene: new SScene(),
  126. value: -1,
  127. options: [
  128. {
  129. value: SGraphLayoutType.Left,
  130. label: '左对齐'
  131. },
  132. {
  133. value: SGraphLayoutType.Right,
  134. label: '右对齐'
  135. },
  136. {
  137. value: SGraphLayoutType.Top,
  138. label: '顶对齐'
  139. },
  140. {
  141. value: SGraphLayoutType.Bottom,
  142. label: '底对齐'
  143. },
  144. {
  145. value: SGraphLayoutType.Center,
  146. label: '水平居中对齐'
  147. },
  148. {
  149. value: SGraphLayoutType.Middle,
  150. label: '垂直居中对齐'
  151. },
  152. {
  153. value: SGraphLayoutType.Vertical,
  154. label: '垂直分布'
  155. },
  156. {
  157. value: SGraphLayoutType.Horizontal,
  158. label: '水平分布'
  159. }
  160. ]
  161. }
  162. },
  163. mounted() {
  164. let view = new TestView();
  165. view.scene = this.scene;//new SScene(); //this.data.scene;
  166. },
  167. methods: {
  168. addCircle() {
  169. this.scene.cmd = 1;
  170. },
  171. addRect() {
  172. this.scene.cmd = 2;
  173. },
  174. changeAlign(v) {
  175. this.scene.selectContainer.layout(v)
  176. // console.log(this.scene.selectContainer.layout(SGraphLayoutType.Left))
  177. }
  178. }
  179. }
  180. </script>
  181. <style scoped>
  182. </style>