SEditPolygon.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. <template>
  2. <div>
  3. <el-button @click="show">展示</el-button>
  4. <el-button @click="create">创建</el-button>
  5. <el-button @click="edit">编辑</el-button>
  6. <canvas id="editPolygon" width="800" height="400" tabindex="0"/>
  7. </div>
  8. </template>
  9. <script lang='ts'>
  10. import { SGraphItem, SGraphScene, SGraphView } from "@persagy-web/graph/";
  11. import { SMouseEvent } from "@persagy-web/base/";
  12. import { SColor, SLine, SLineCapStyle, SPainter, SPoint, SRect } from "@persagy-web/draw";
  13. import { SRelationState } from "@persagy-web/big/lib/enums/SRelationState";
  14. import { SMathUtil } from "@persagy-web/big/lib/utils/SMathUtil";
  15. /**
  16. * 可编辑多边形示例
  17. *
  18. * @author 郝洁 <haojie@persagy.com>
  19. */
  20. export default {
  21. data() {
  22. return {
  23. /** 命令所属的场景类 */
  24. scene: null,
  25. /** 实例化 view */
  26. view: null
  27. };
  28. },
  29. /**
  30. * 页面挂载
  31. */
  32. mounted(): void {
  33. this.view = new SGraphView("editPolygon");
  34. },
  35. methods: {
  36. show() {
  37. const scene = new SGraphScene();
  38. this.view.scene = scene;
  39. const spolygonItem = new SPolygonItem(null);
  40. spolygonItem.setStatus = SRelationState.Normal;
  41. const PointList: SPoint[] = [
  42. new SPoint(0, 0),
  43. new SPoint(50, 0),
  44. new SPoint(50, 50),
  45. new SPoint(0, 50)
  46. ];
  47. spolygonItem.setPointList = PointList;
  48. scene.addItem(spolygonItem);
  49. this.view.fitSceneToView();
  50. },
  51. create() {
  52. const scene = new SGraphScene();
  53. this.view.scene = scene;
  54. const spolygonItem = new SPolygonItem(null);
  55. spolygonItem.setStatus = SRelationState.Create;
  56. scene.addItem(spolygonItem);
  57. scene.grabItem = spolygonItem;
  58. this.view.fitSceneToView();
  59. },
  60. edit() {
  61. const scene = new SGraphScene();
  62. this.view.scene = scene;
  63. const spolygonItem = new SPolygonItem(null);
  64. spolygonItem.setStatus = SRelationState.Edit;
  65. const PointList: SPoint[] = [
  66. new SPoint(0, 0),
  67. new SPoint(50, 0),
  68. new SPoint(50, 60),
  69. new SPoint(0, 50)
  70. ];
  71. spolygonItem.setPointList = PointList;
  72. scene.addItem(spolygonItem);
  73. scene.grabItem = spolygonItem;
  74. this.view.fitSceneToView();
  75. }
  76. }
  77. };
  78. /**
  79. * @author hanyaolong
  80. */
  81. class SPolygonItem extends SGraphItem {
  82. /** X坐标最小值 */
  83. private minX = Number.MAX_SAFE_INTEGER;
  84. /** X坐标最大值 */
  85. private maxX = Number.MIN_SAFE_INTEGER;
  86. /** Y坐标最小值 */
  87. private minY = Number.MAX_SAFE_INTEGER;
  88. /** Y坐标最大值 */
  89. private maxY = Number.MIN_SAFE_INTEGER;
  90. /** 轮廓线坐标 */
  91. private pointList: SPoint[] = [];
  92. // 获取当前状态
  93. get getPointList(): SPoint[] {
  94. return this.pointList;
  95. }
  96. // 编辑当前状态
  97. set setPointList(arr: SPoint[]) {
  98. this.pointList = arr;
  99. if (arr) {
  100. this._xyzListToSPointList(arr);
  101. }
  102. this.update();
  103. }
  104. /** 是否闭合 */
  105. closeFlag: boolean = false;
  106. // 当前状态 1:show 2 创建中,3 编辑中
  107. _status: number = SRelationState.Normal;
  108. // 获取当前状态
  109. get getStatus(): number {
  110. return this._status;
  111. }
  112. // 编辑当前状态
  113. set setStatus(value: number) {
  114. this._status = value;
  115. this.update();
  116. }
  117. /** 边框颜色 */
  118. borderColor: SColor = new SColor("#0091FF");
  119. /** 填充颜色 */
  120. brushColor: SColor = new SColor("#1EE887");
  121. /** border宽 只可输入像素宽*/
  122. borderLine: number = 4;
  123. /** 鼠标移动点 */
  124. private lastPoint = new SPoint();
  125. /** 当前鼠标获取顶点对应索引 */
  126. private curIndex: number = -1;
  127. /** 灵敏像素 */
  128. private len: number = 15;
  129. /** 场景像素 */
  130. private scenceLen: number = 15;
  131. /** 场景像素 */
  132. private isAlt: boolean = false;
  133. /**
  134. * 构造函数
  135. *
  136. * @param parent 指向父对象
  137. */
  138. constructor(parent: SGraphItem | null) {
  139. super(parent);
  140. }
  141. ///////////////以下为对pointList 数组的操作方法
  142. /**
  143. * 储存新的多边形顶点
  144. * @param x 点位得x坐标
  145. * @param y 点位得y坐标
  146. * @param i 储存所在索引
  147. * @return SPoint
  148. */
  149. insertPoint(x: number, y: number, i: number | null = null): SPoint {
  150. const point = new SPoint(x, y);
  151. if (i == null) {
  152. this.pointList.push(point);
  153. } else {
  154. this.pointList.splice(i, 0, point);
  155. }
  156. this.update();
  157. return point;
  158. }
  159. /**
  160. * 删除点位
  161. * @param i 删除点所在的索引
  162. * @return SPoint
  163. */
  164. deletePoint(i: number | null = null): SPoint | null | undefined {
  165. let point = null;
  166. if (i != null) {
  167. if (this.pointList.length - 1 >= i) {
  168. point = this.pointList[i];
  169. this.pointList.splice(i, 1);
  170. } else {
  171. point = null;
  172. }
  173. } else {
  174. point = this.pointList.pop();
  175. }
  176. this.update();
  177. return point;
  178. }
  179. /**
  180. * 多边形顶点的移动位置
  181. * @param x 点位得x坐标
  182. * @param y 点位得y坐标
  183. * @param i 点位得i坐标
  184. * @return SPoint
  185. */
  186. movePoint(x: number, y: number, i: number): SPoint | null | undefined {
  187. let point = null;
  188. if (this.pointList.length) {
  189. this.pointList[i].x = x;
  190. this.pointList[i].y = y;
  191. }
  192. point = this.pointList[i];
  193. return point;
  194. }
  195. /**
  196. * 打印出多边形数组
  197. * @return SPoint[]
  198. */
  199. PrintPointList(): SPoint[] {
  200. return this.pointList;
  201. }
  202. ///////////////////////////
  203. /**
  204. * xyz数据转换为SPoint格式函数;获取外接矩阵参数
  205. *
  206. * @param arr 外层传入的数据PointList
  207. */
  208. protected _xyzListToSPointList(arr: SPoint[]): void {
  209. if (arr.length) {
  210. this.pointList = arr.map(it => {
  211. let x = it.x,
  212. y = it.y;
  213. if (x < this.minX) {
  214. this.minX = x;
  215. }
  216. if (y < this.minY) {
  217. this.minY = y;
  218. }
  219. if (x > this.maxX) {
  220. this.maxX = x;
  221. }
  222. if (y > this.maxY) {
  223. this.maxY = y;
  224. }
  225. return new SPoint(x, y);
  226. });
  227. } else {
  228. this.pointList = [];
  229. }
  230. }
  231. ////////////以下为三种状态下的绘制多边形方法
  232. /**
  233. * 展示状态 --绘制多边形数组
  234. * @param painter 绘制类
  235. * @param pointList 绘制多边形数组
  236. *
  237. */
  238. protected drawShowPolygon(painter: SPainter, pointList: SPoint[]): void {
  239. painter.pen.lineCapStyle = SLineCapStyle.Square;
  240. painter.pen.color = this.borderColor;
  241. painter.pen.lineWidth = painter.toPx(this.borderLine);
  242. painter.brush.color = this.brushColor;
  243. painter.drawPolygon([...pointList]);
  244. }
  245. /**
  246. *
  247. * 创建状态 --绘制多边形数组
  248. * @param painter 绘制类
  249. * @param pointList 绘制多边形数组
  250. *
  251. */
  252. protected drawCreatePolygon(painter: SPainter, pointList: SPoint[]): void {
  253. painter.pen.lineCapStyle = SLineCapStyle.Square;
  254. painter.pen.color = this.borderColor;
  255. painter.pen.lineWidth = painter.toPx(4);
  256. painter.drawPolyline(pointList);
  257. if (this.lastPoint) {
  258. painter.drawLine(pointList[pointList.length - 1], this.lastPoint);
  259. }
  260. painter.pen.color = SColor.Transparent;
  261. painter.brush.color = this.brushColor;
  262. painter.pen.lineWidth = painter.toPx(4);
  263. painter.drawPolygon([...pointList, this.lastPoint]);
  264. }
  265. /**
  266. *
  267. * 编辑状态 --绘制多边形数组
  268. *
  269. * @param painter 绘制类
  270. * @param pointList 绘制多边形数组
  271. *
  272. */
  273. protected drawEditPolygon(painter: SPainter, pointList: SPoint[]): void {
  274. // 展示多边形
  275. this.drawShowPolygon(painter, pointList);
  276. // 绘制顶点块
  277. pointList.forEach((item, index) => {
  278. painter.drawCircle(item.x, item.y, painter.toPx(8));
  279. });
  280. }
  281. ////////////////////////////////
  282. /**
  283. * 编辑状态操作多边形数组
  284. *
  285. * @param event 鼠标事件
  286. *
  287. *
  288. */
  289. protected editPolygonPoint(event: SMouseEvent): void {
  290. // 判断是否为删除状态 isAlt = true为删除状态
  291. if (this.isAlt) {
  292. // 1 判断是否点击在多边形顶点
  293. let lenIndex = -1; // 当前点击到的点位索引;
  294. let curenLen = this.scenceLen; // 当前的灵敏度
  295. this.pointList.forEach((item, index) => {
  296. let dis = SMathUtil.pointDistance(event.x, event.y, item.x, item.y);
  297. if (dis < curenLen) {
  298. curenLen = dis;
  299. lenIndex = index;
  300. }
  301. });
  302. // 若点击到,对该索引对应的点做删除
  303. if (lenIndex != -1) {
  304. if (this.pointList.length <= 3) {
  305. return;
  306. }
  307. this.deletePoint(lenIndex);
  308. }
  309. } else {
  310. // 1 判断是否点击在多边形顶点
  311. this.curIndex = -1;
  312. let lenIndex = -1; // 当前点击到的点位索引;
  313. let curenLen = this.scenceLen; // 当前的灵敏度
  314. this.pointList.forEach((item, index) => {
  315. let dis = SMathUtil.pointDistance(event.x, event.y, item.x, item.y);
  316. if (dis < curenLen) {
  317. curenLen = dis;
  318. lenIndex = index;
  319. }
  320. });
  321. this.curIndex = lenIndex;
  322. // 2判断是否点击在多边形得边上
  323. if (-1 == lenIndex) {
  324. let len = SMathUtil.pointToLine(
  325. new SPoint(event.x, event.y),
  326. new SLine(this.pointList[0], this.pointList[1])
  327. ),
  328. index = 0;
  329. if (this.pointList.length > 2) {
  330. for (let i = 1; i < this.pointList.length; i++) {
  331. let dis = SMathUtil.pointToLine(
  332. new SPoint(event.x, event.y),
  333. new SLine(this.pointList[i], this.pointList[i + 1])
  334. );
  335. if (i + 1 == this.pointList.length) {
  336. dis = SMathUtil.pointToLine(
  337. new SPoint(event.x, event.y),
  338. new SLine(this.pointList[i], this.pointList[0])
  339. );
  340. }
  341. if (dis.MinDis < len.MinDis) {
  342. len = dis;
  343. index = i;
  344. }
  345. }
  346. }
  347. // 如果再线上则为新增点
  348. if (len.Point) {
  349. if (len.MinDis <= this.scenceLen) {
  350. this.pointList.splice(index + 1, 0, len.Point);
  351. }
  352. }
  353. }
  354. // 刷新视图
  355. this.update();
  356. }
  357. }
  358. ///////////////////////////////以下为鼠标事件
  359. /**
  360. * 鼠标双击事件
  361. *
  362. * @param event 事件参数
  363. * @return boolean
  364. */
  365. onDoubleClick(event: SMouseEvent): boolean {
  366. // 如果位show状态 双击改对象则需改为编辑状态
  367. if (SRelationState.Normal == this._status) {
  368. this._status = SRelationState.Edit;
  369. } else if (SRelationState.Edit == this._status) {
  370. this._status = SRelationState.Normal;
  371. }
  372. this.update();
  373. return true;
  374. }
  375. /**
  376. * 键盘事件
  377. *
  378. * @param event 事件参数
  379. * @return boolean
  380. */
  381. onKeyDown(event: KeyboardEvent): boolean {
  382. console.log(event);
  383. if (this._status == SRelationState.Normal) {
  384. return false;
  385. } else if (this._status == SRelationState.Create) {
  386. if (event.code == "Enter") {
  387. this._status = SRelationState.Normal;
  388. }
  389. } else if (this._status == SRelationState.Edit) {
  390. if (event.key == "Alt") {
  391. this.isAlt = true;
  392. }
  393. } else {
  394. }
  395. this.update();
  396. return true;
  397. }
  398. /**
  399. * 键盘键抬起事件
  400. *
  401. * @param event 事件参数
  402. * @return boolean
  403. */
  404. onKeyUp(event: KeyboardEvent): boolean {
  405. if (this._status == SRelationState.Edit) {
  406. if (event.key == "Alt") {
  407. this.isAlt = false;
  408. }
  409. }
  410. this.update();
  411. return true;
  412. }
  413. /**
  414. * 鼠标按下事件
  415. *
  416. * @param event 事件参数
  417. * @return boolean
  418. */
  419. onMouseDown(event: SMouseEvent): boolean {
  420. console.log("aaaaaa");
  421. // 如果状态为编辑状态则添加点
  422. if (this._status == SRelationState.Create) {
  423. // 新增顶点
  424. this.insertPoint(event.x, event.y);
  425. } else if (this._status == SRelationState.Edit) {
  426. // 对多边形数组做编辑操作
  427. this.editPolygonPoint(event);
  428. }
  429. return true;
  430. }
  431. /**
  432. * 鼠标移入事件
  433. *
  434. * @param event 事件参数
  435. * @return boolean
  436. */
  437. onMouseEnter(event: SMouseEvent): boolean {
  438. return true;
  439. }
  440. /**
  441. * 鼠标移出事件
  442. *
  443. * @param event 事件参数
  444. * @return boolean
  445. */
  446. onMouseLeave(event: SMouseEvent): boolean {
  447. return true;
  448. }
  449. /**
  450. * 鼠标移动事件
  451. *
  452. * @param event 事件参数
  453. * @return boolean
  454. */
  455. onMouseMove(event: SMouseEvent): boolean {
  456. if (this._status == SRelationState.Create) {
  457. this.lastPoint.x = event.x;
  458. this.lastPoint.y = event.y;
  459. } else if (this._status == SRelationState.Edit) {
  460. if (-1 != this.curIndex) {
  461. this.pointList[this.curIndex].x = event.x;
  462. this.pointList[this.curIndex].y = event.y;
  463. }
  464. }
  465. this.update();
  466. return true;
  467. }
  468. /**
  469. * 鼠标抬起事件
  470. *
  471. * @param event 事件参数
  472. * @return 是否处理
  473. */
  474. onMouseUp(event: SMouseEvent): boolean {
  475. if (this._status == SRelationState.Edit) {
  476. this.curIndex = -1;
  477. }
  478. return true;
  479. }
  480. /**
  481. * 适配事件
  482. *
  483. * @param event 事件参数
  484. * @return boolean
  485. */
  486. onResize(event: SMouseEvent): boolean {
  487. return true;
  488. }
  489. /**
  490. * Item对象边界区域
  491. *
  492. * @return 边界区域
  493. */
  494. boundingRect(): SRect {
  495. return new SRect(
  496. this.minX,
  497. this.minY,
  498. this.maxX - this.minX,
  499. this.maxY - this.minY
  500. );
  501. }
  502. /**
  503. * Item绘制操作
  504. *
  505. * @param painter 绘制对象
  506. */
  507. onDraw(painter: SPainter): void {
  508. this.scenceLen = painter.toPx(this.len);
  509. // 当状态为展示状态
  510. if (this._status == SRelationState.Normal) {
  511. // 闭合多边形
  512. this.drawShowPolygon(painter, this.pointList);
  513. } else if (this._status == SRelationState.Create) {
  514. // 创建状态
  515. this.drawCreatePolygon(painter, this.pointList);
  516. } else {
  517. // 编辑状态
  518. this.drawEditPolygon(painter, this.pointList);
  519. }
  520. }
  521. }
  522. </script>