SImgTextItem.vue 6.8 KB

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