SImgTextItem.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <template>
  2. <div>
  3. <el-button @click="changemaodian">切换锚点显示状态</el-button>
  4. <el-button @click="changetext">切换文本显示状态</el-button>
  5. <canvas id="editPolygon" width="740" height="400" tabindex="0"></canvas>
  6. </div>
  7. </template>
  8. <script lang="ts">
  9. import {
  10. SAnchorItem,
  11. SGraphItem,
  12. SGraphScene,
  13. SGraphView,
  14. SImageItem,
  15. SObjectItem,
  16. STextItem
  17. } from "@persagy-web/graph/lib";
  18. import { SItemStatus } from "@persagy-web/big/lib";
  19. import { SColor, SPainter, SRect, SSize } from "@persagy-web/draw/lib";
  20. import { SMouseEvent } from "@persagy-web/base/lib";
  21. /**
  22. * 图例item icon
  23. *
  24. * @author 郝洁 <haojie@persagy.com>
  25. */
  26. class SImgTextItem extends SObjectItem {
  27. /** item状态 */
  28. _status: SItemStatus = SItemStatus.Normal;
  29. get status(): SItemStatus {
  30. return this._status;
  31. }
  32. set status(v: SItemStatus) {
  33. this._status = v;
  34. this.update();
  35. }
  36. /** 是否显示文字 */
  37. _showText: boolean = true;
  38. get showText(): boolean {
  39. return this._showText;
  40. }
  41. set showText(v: boolean) {
  42. if (v === this._showText) {
  43. return
  44. }
  45. this._showText = v;
  46. this.textItem.visible = v;
  47. }
  48. /** 是否显示锚点 */
  49. _showAnchor: boolean = false;
  50. get showAnchor(): boolean {
  51. return this._showAnchor;
  52. }
  53. set showAnchor(v: boolean) {
  54. this._showAnchor = v;
  55. this.anchorList.forEach(t => {
  56. t.visible = v;
  57. })
  58. }
  59. /** X轴坐标 */
  60. get x(): number {
  61. return this.pos.x;
  62. } // Get x
  63. set x(v: number) {
  64. this.pos.x = v;
  65. this.$emit("changePos");
  66. this.update();
  67. } // Set x
  68. /** Y轴坐标 */
  69. get y(): number {
  70. return this.pos.y;
  71. } // Get y
  72. set y(v: number) {
  73. this.pos.y = v;
  74. this.$emit("changePos");
  75. this.update();
  76. } // Set y
  77. /** img Item */
  78. img: SImageItem = new SImageItem(this);
  79. /** text item */
  80. textItem: STextItem = new STextItem(this);
  81. /**
  82. * 构造体
  83. *
  84. */
  85. constructor(parent: SGraphItem | null) {
  86. super(parent);
  87. this.img.url = `http://adm.sagacloud.cn:8080/doc/assets/img/logo.png`;
  88. this.img.width = 32;
  89. this.img.height = 32;
  90. let anchorPoint = [{x: 0, y: this.img.height / 2}, {x: 0, y: -this.img.height / 2}, {
  91. x: -this.img.width / 2,
  92. y: 0
  93. }, {x: this.img.width / 2, y: 0}];
  94. this.anchorList = anchorPoint.map(t => {
  95. let item = new SAnchorItem(this);
  96. item.moveTo(t.x, t.y);
  97. return item;
  98. });
  99. this.update();
  100. this.textItem.text = "x2";
  101. this.textItem.moveTo(18, -6);
  102. this.moveable = true;
  103. this.selectable = true;
  104. this.textItem.enabled = false;
  105. this.img.enabled = false;
  106. }
  107. onMouseEnter(event: SMouseEvent): boolean {
  108. this.showAnchor = true;
  109. return true;
  110. }
  111. onMouseLeave(event: SMouseEvent): boolean {
  112. this.showAnchor = false;
  113. return true;
  114. }
  115. onMouseMove(event: SMouseEvent): boolean {
  116. return super.onMouseMove(event);
  117. }
  118. /**
  119. * 鼠标按下事件
  120. *
  121. */
  122. onMouseDown(event: SMouseEvent): boolean {
  123. console.log(this.textItem)
  124. if (this.status == SItemStatus.Normal) {
  125. return super.onMouseDown(event);
  126. } else if (this.status == SItemStatus.Edit) {
  127. return super.onMouseDown(event);
  128. }
  129. return true;
  130. } // Function onMouseDown()
  131. /**
  132. * 宽高发发生变化
  133. *
  134. * @param oldSize 改之前的大小
  135. * @param newSize 改之后大小
  136. */
  137. onResize(oldSize: SSize, newSize: SSize) {
  138. console.log(arguments);
  139. } // Function onResize()
  140. /**
  141. * 鼠标双击事件
  142. *
  143. * @param event 鼠标事件
  144. * @return 是否处理事件
  145. */
  146. onDoubleClick(event: SMouseEvent): boolean {
  147. this.status = SItemStatus.Edit;
  148. return true;
  149. } // Function onDoubleClick()
  150. /**
  151. * 宽高发发生变化
  152. *
  153. * @return SRect 所有子对象的并集
  154. */
  155. boundingRect(): SRect {
  156. let rect = this.img.boundingRect().adjusted(this.img.pos.x, this.img.pos.y, 0, 0);
  157. if (this.showText) {
  158. rect = rect.unioned(this.textItem.boundingRect().adjusted(this.textItem.pos.x, this.textItem.pos.y, 0, 0))
  159. }
  160. return rect;
  161. } // Function boundingRect()
  162. /**
  163. * Item绘制操作
  164. *
  165. * @param painter painter对象
  166. */
  167. onDraw(painter: SPainter): void {
  168. painter.pen.lineWidth = painter.toPx(1);
  169. painter.pen.color = new SColor("#00FF00");
  170. painter.brush.color = SColor.Transparent;
  171. if (this.showAnchor) {
  172. painter.brush.color = SColor.Gray
  173. }
  174. painter.drawRect(this.boundingRect());
  175. } // Function onDraw()
  176. }
  177. export default {
  178. name: "ImgTextItem",
  179. data() {
  180. return {
  181. scene: null,
  182. view: null,
  183. input: '',
  184. };
  185. },
  186. mounted() {
  187. console.log(22222222222222222)
  188. // @ts-ignore
  189. this.view = new SGraphView("editPolygon");
  190. // @ts-ignore
  191. this.scene = new SGraphScene();
  192. // @ts-ignore
  193. this.view.scene = this.scene;
  194. // @ts-ignore
  195. this.init()
  196. },
  197. methods: {
  198. init() {
  199. // @ts-ignore
  200. this.item = new SImgTextItem(null);
  201. // @ts-ignore
  202. this.item.moveable = true;
  203. // @ts-ignore
  204. this.scene.addItem(this.item);
  205. // this.view.fitSceneToView();
  206. },
  207. changemaodian() {
  208. // @ts-ignore
  209. this.item.showAnchor = !this.item.showAnchor;
  210. },
  211. changetext() {
  212. // @ts-ignore
  213. this.item.showText = !this.item.showText;
  214. }
  215. }
  216. }
  217. </script>
  218. <style scoped>
  219. canvas {
  220. border: 1px solid #ccc;
  221. }
  222. canvas:focus {
  223. outline: none;
  224. }
  225. </style>