Draw.js 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  1. import { dataService } from "../Service/DataService.js";
  2. import { stateService } from "../Service/StateService.js";
  3. import { measureService } from "../Service/MeasureService";
  4. import { coordinate } from "../Coordinate.js";
  5. import Style from "../Style.js";
  6. import VectorType from "../enum/VectorType.js";
  7. import SelectState from "../enum/SelectState.js";
  8. import { mathUtil } from "../Util/MathUtil.js";
  9. import ElementEvents from "../enum/ElementEvents.js";
  10. import Constant from "../Constant.js";
  11. const help = {
  12. getVectorStyle(vector) {
  13. const itemsEntry = [
  14. [stateService.getSelectItem(), "Select"],
  15. [stateService.getDraggingItem(), "Dragging"],
  16. [stateService.getFocusItem(), "Focus"],
  17. ];
  18. return itemsEntry.reduce((prev, [item, attr]) => {
  19. if (
  20. item &&
  21. item.type === VectorType[vector.geoType] &&
  22. vector.vectorId === item.vectorId
  23. ) {
  24. return Style[attr][vector.geoType];
  25. } else {
  26. return prev
  27. }
  28. }, Style[vector.geoType]);
  29. },
  30. setVectorStyle(ctx, vector) {
  31. const styles = help.getVectorStyle(vector);
  32. for (const style in styles) {
  33. ctx[style] = styles[style];
  34. }
  35. }
  36. }
  37. export default class Draw {
  38. constructor() {
  39. this.context = null;
  40. }
  41. initContext(canvas) {
  42. if (canvas) {
  43. this.context = canvas.getContext("2d");
  44. } else {
  45. this.context = null;
  46. }
  47. }
  48. clear() {
  49. this.context.clearRect(
  50. 0,
  51. 0,
  52. this.context.canvas.width,
  53. this.context.canvas.height
  54. );
  55. }
  56. drawBackGroundImg(vector) {
  57. this.context.save();
  58. this.context.restore();
  59. }
  60. drawRoad(vector, isTemp) {
  61. this.context.save();
  62. this.context.beginPath();
  63. help.setVectorStyle(this.context, vector)
  64. const startReal = isTemp ? vector.start : dataService.getPoint(vector.startId)
  65. const endReal = isTemp ? vector.end : dataService.getPoint(vector.endId)
  66. const startScreen = coordinate.getScreenXY(startReal);
  67. const endScreen = coordinate.getScreenXY(endReal);
  68. if (!isTemp) {
  69. this.drawText(
  70. { x: (startReal.x + endReal.x) / 2, y: (startReal.y + endReal.y) / 2 },
  71. vector.vectorId
  72. );
  73. } else {
  74. this.context.globalAlpha = 0.3
  75. }
  76. this.drawEdge(vector, isTemp);
  77. this.context.beginPath();
  78. this.context.moveTo(startScreen.x, startScreen.y);
  79. this.context.lineTo(endScreen.x, endScreen.y);
  80. this.context.stroke();
  81. this.context.restore();
  82. }
  83. drawCurveRoad(vector, isTemp) {
  84. const getStyleLines = () => {
  85. const leftPoints = vector.leftLanes
  86. .map(line => line.map(coordinate.getScreenXY.bind(coordinate)))
  87. .map(mathUtil.getCurvesByPoints)
  88. const rightPoints = vector.rightLanes
  89. .map(line => line.map(coordinate.getScreenXY.bind(coordinate)))
  90. .map(mathUtil.getCurvesByPoints)
  91. console.log(dataService.getCurveEdge(vector.rightEdgeId))
  92. // dataService.getCurveEdge(vector.rightEdgeId)
  93. // dataService.getCurveEdge(vector.leftEdgeId)
  94. const styleLines = {
  95. dotted: [
  96. // ...leftPoints,
  97. // ...rightPoints,
  98. ],
  99. solid: [
  100. vector.curves.map(line =>
  101. Object.entries(line).reduce((t, [key, val]) => {
  102. t[key] = coordinate.getScreenXY(val)
  103. return t;
  104. }, {})
  105. )
  106. ],
  107. };
  108. return styleLines
  109. // return Object.entries(styleLines).reduce((t, [key, lines]) => {
  110. // t[key] = lines.map((line) =>
  111. // line.map((point) => coordinate.getScreenXY(point))
  112. // );
  113. // return t;
  114. // }, {});
  115. };
  116. const draw = (curveLines, drawPoints = false) => {
  117. if (drawPoints) {
  118. const radius = Style.Point.radius * coordinate.ratio;
  119. for (const point of curveLines) {
  120. ctx.beginPath();
  121. ctx.arc(point.x, point.y, radius, 0, 2 * Math.PI);
  122. ctx.fill();
  123. }
  124. }
  125. // ctx.lineCap = "round"; //线段端点的样式
  126. let i = 0;
  127. for (const curve of curveLines) {
  128. ctx.beginPath();
  129. ctx.bezierCurveTo(
  130. curve.start.x,
  131. curve.start.y,
  132. curve.end.x,
  133. curve.end.y,
  134. curve.control.x,
  135. curve.control.y
  136. );
  137. ctx.strokeStyle = ['red', 'blue'][i++ % curveLines.length]
  138. console.log(['red', 'blue'][i++ % curveLines.length], i++ % curveLines.length)
  139. ctx.stroke();
  140. }
  141. };
  142. const ctx = this.context;
  143. ctx.save();
  144. help.setVectorStyle(ctx, vector)
  145. const styleLines = getStyleLines();
  146. for (const style in styleLines) {
  147. const isSolid = style === "solid";
  148. ctx.setLineDash(isSolid ? [] : [15, 15]);
  149. const lines = styleLines[style];
  150. for (const points of lines) {
  151. draw(points, true);
  152. }
  153. }
  154. ctx.restore();
  155. }
  156. drawCurveEdge(vector, isTemp) {
  157. //判断是否与road方向一致。角度足够小,路足够宽,有可能向量方向不一致
  158. let start = dataService.getCurvePoint(vector.startId);
  159. let end = dataService.getCurvePoint(vector.endId);
  160. let leftEdge = dataService.getCurveEdge(vector.leftEdgeId);
  161. let rightEdge = dataService.getCurveEdge(vector.rightEdgeId);
  162. if (isTemp) {
  163. start = vector.start;
  164. end = vector.end;
  165. leftEdge = vector.leftEdge;
  166. rightEdge = vector.rightEdge;
  167. }
  168. const leftFlag = mathUtil.isSameDirForVector(
  169. start,
  170. end,
  171. leftEdge.start,
  172. leftEdge.end
  173. );
  174. const rightFlag = mathUtil.isSameDirForVector(
  175. start,
  176. end,
  177. rightEdge.start,
  178. rightEdge.end
  179. );
  180. this.context.save();
  181. this.context.lineCap = "round"; //线段端点的样式
  182. this.context.strokeStyle = Style.Road.strokeStyle;
  183. const selectItem = stateService.getSelectItem();
  184. const draggingItem = stateService.getDraggingItem();
  185. const focusItem = stateService.getFocusItem();
  186. if (selectItem && selectItem.type == VectorType.CurveRoad) {
  187. if (vector.vectorId == selectItem.vectorId) {
  188. this.context.strokeStyle = Style.Select.Road.strokeStyle;
  189. }
  190. } else if (draggingItem && draggingItem.type == VectorType.CurveRoad) {
  191. if (vector.vectorId == draggingItem.vectorId) {
  192. this.context.strokeStyle = Style.Select.Road.strokeStyle;
  193. }
  194. } else if (focusItem && focusItem.type == VectorType.CurveRoad) {
  195. if (vector.vectorId == focusItem.vectorId) {
  196. this.context.strokeStyle = Style.Focus.Road.strokeStyle;
  197. }
  198. }
  199. let point1, point2;
  200. this.context.globalAlpha = 0.3;
  201. this.context.lineWidth = 1;
  202. if (leftFlag > 0) {
  203. this.context.beginPath();
  204. point1 = coordinate.getScreenXY(leftEdge.start);
  205. point2 = coordinate.getScreenXY(leftEdge.end);
  206. this.context.moveTo(point1.x, point1.y);
  207. this.context.lineTo(point2.x, point2.y);
  208. this.context.stroke();
  209. }
  210. if (rightFlag > 0) {
  211. point1 = coordinate.getScreenXY(rightEdge.start);
  212. point2 = coordinate.getScreenXY(rightEdge.end);
  213. this.context.moveTo(point1.x, point1.y);
  214. this.context.lineTo(point2.x, point2.y);
  215. this.context.stroke();
  216. }
  217. this.context.restore();
  218. this.drawText(
  219. {
  220. x: (leftEdge.start.x + leftEdge.end.x) / 2,
  221. y: (leftEdge.start.y + leftEdge.end.y) / 2,
  222. },
  223. vector.leftEdgeId
  224. );
  225. this.drawText(
  226. {
  227. x: (rightEdge.start.x + rightEdge.end.x) / 2,
  228. y: (rightEdge.start.y + rightEdge.end.y) / 2,
  229. },
  230. vector.rightEdgeId
  231. );
  232. }
  233. drawCurvePoint(vector) {
  234. const pt = coordinate.getScreenXY({ x: vector.x, y: vector.y });
  235. const selectItem = stateService.getSelectItem();
  236. const draggingItem = stateService.getDraggingItem();
  237. const focusItem = stateService.getFocusItem();
  238. let radius = Style.Point.radius;
  239. if (
  240. (draggingItem &&
  241. draggingItem.type == VectorType.CurvePoint &&
  242. vector.vectorId == draggingItem.vectorId) ||
  243. (selectItem &&
  244. selectItem.type == VectorType.CurvePoint &&
  245. vector.vectorId == selectItem.vectorId)
  246. ) {
  247. this.context.save();
  248. this.context.lineWidth = Style.Select.Point.lineWidth * coordinate.ratio;
  249. this.context.strokeStyle = Style.Select.Point.strokeStyle;
  250. this.context.fillStyle = Style.Select.Point.fillStyle;
  251. radius = Style.Select.Point.radius;
  252. } else if (
  253. focusItem &&
  254. focusItem.type == VectorType.CurvePoint &&
  255. vector.vectorId == focusItem.vectorId
  256. ) {
  257. this.context.save();
  258. this.context.lineWidth = Style.Focus.Point.lineWidth * coordinate.ratio;
  259. this.context.strokeStyle = Style.Focus.Point.strokeStyle;
  260. this.context.fillStyle = Style.Focus.Point.fillStyle;
  261. radius = Style.Focus.Point.radius;
  262. }
  263. // else {
  264. // return;
  265. // }
  266. this.context.beginPath();
  267. this.context.arc(
  268. pt.x,
  269. pt.y,
  270. radius * coordinate.ratio,
  271. 0,
  272. Math.PI * 2,
  273. true
  274. );
  275. this.context.stroke();
  276. this.context.fill();
  277. this.context.restore();
  278. this.drawText(vector, vector.vectorId);
  279. }
  280. drawEdge(vector, isTemp) {
  281. //判断是否与road方向一致。角度足够小,路足够宽,有可能向量方向不一致
  282. let start = dataService.getPoint(vector.startId);
  283. let end = dataService.getPoint(vector.endId);
  284. let leftEdge = dataService.getEdge(vector.leftEdgeId);
  285. let rightEdge = dataService.getEdge(vector.rightEdgeId);
  286. if (isTemp) {
  287. start = vector.start;
  288. end = vector.end;
  289. leftEdge = vector.leftEdge;
  290. rightEdge = vector.rightEdge;
  291. }
  292. const leftFlag = mathUtil.isSameDirForVector(
  293. start,
  294. end,
  295. leftEdge.start,
  296. leftEdge.end
  297. );
  298. const rightFlag = mathUtil.isSameDirForVector(
  299. start,
  300. end,
  301. rightEdge.start,
  302. rightEdge.end
  303. );
  304. this.context.save();
  305. this.context.lineCap = "round"; //线段端点的样式
  306. this.context.strokeStyle = Style.Road.strokeStyle;
  307. const selectItem = stateService.getSelectItem();
  308. const draggingItem = stateService.getDraggingItem();
  309. const focusItem = stateService.getFocusItem();
  310. if (selectItem && selectItem.type == VectorType.Road) {
  311. if (vector.vectorId == selectItem.vectorId) {
  312. this.context.strokeStyle = Style.Select.Road.strokeStyle;
  313. }
  314. } else if (draggingItem && draggingItem.type == VectorType.Road) {
  315. if (vector.vectorId == draggingItem.vectorId) {
  316. this.context.strokeStyle = Style.Select.Road.strokeStyle;
  317. }
  318. } else if (focusItem && focusItem.type == VectorType.Road) {
  319. if (vector.vectorId == focusItem.vectorId) {
  320. this.context.strokeStyle = Style.Focus.Road.strokeStyle;
  321. }
  322. }
  323. let point1, point2;
  324. this.context.globalAlpha = 0.3;
  325. this.context.lineWidth = 1;
  326. if (leftFlag > 0) {
  327. this.context.beginPath();
  328. point1 = coordinate.getScreenXY(leftEdge.start);
  329. point2 = coordinate.getScreenXY(leftEdge.end);
  330. this.context.moveTo(point1.x, point1.y);
  331. this.context.lineTo(point2.x, point2.y);
  332. this.context.stroke();
  333. }
  334. if (rightFlag > 0) {
  335. point1 = coordinate.getScreenXY(rightEdge.start);
  336. point2 = coordinate.getScreenXY(rightEdge.end);
  337. this.context.moveTo(point1.x, point1.y);
  338. this.context.lineTo(point2.x, point2.y);
  339. this.context.stroke();
  340. }
  341. this.context.restore();
  342. this.drawText(
  343. {
  344. x: (leftEdge.start.x + leftEdge.end.x) / 2,
  345. y: (leftEdge.start.y + leftEdge.end.y) / 2,
  346. },
  347. vector.leftEdgeId
  348. );
  349. this.drawText(
  350. {
  351. x: (rightEdge.start.x + rightEdge.end.x) / 2,
  352. y: (rightEdge.start.y + rightEdge.end.y) / 2,
  353. },
  354. vector.rightEdgeId
  355. );
  356. }
  357. drawPoint(vector) {
  358. const pt = coordinate.getScreenXY({ x: vector.x, y: vector.y });
  359. const selectItem = stateService.getSelectItem();
  360. const draggingItem = stateService.getDraggingItem();
  361. const focusItem = stateService.getFocusItem();
  362. let radius = Style.Point.radius;
  363. if (
  364. (draggingItem &&
  365. draggingItem.type == VectorType.Point &&
  366. vector.vectorId == draggingItem.vectorId) ||
  367. (selectItem &&
  368. selectItem.type == VectorType.Point &&
  369. vector.vectorId == selectItem.vectorId)
  370. ) {
  371. this.context.save();
  372. this.context.lineWidth = Style.Select.Point.lineWidth * coordinate.ratio;
  373. this.context.strokeStyle = Style.Select.Point.strokeStyle;
  374. this.context.fillStyle = Style.Select.Point.fillStyle;
  375. radius = Style.Select.Point.radius;
  376. } else if (
  377. focusItem &&
  378. focusItem.type == VectorType.Point &&
  379. vector.vectorId == focusItem.vectorId
  380. ) {
  381. this.context.save();
  382. this.context.lineWidth = Style.Focus.Point.lineWidth * coordinate.ratio;
  383. this.context.strokeStyle = Style.Focus.Point.strokeStyle;
  384. this.context.fillStyle = Style.Focus.Point.fillStyle;
  385. radius = Style.Focus.Point.radius;
  386. }
  387. // else {
  388. // return;
  389. // }
  390. this.context.beginPath();
  391. this.context.arc(
  392. pt.x,
  393. pt.y,
  394. radius * coordinate.ratio,
  395. 0,
  396. Math.PI * 2,
  397. true
  398. );
  399. this.context.stroke();
  400. this.context.fill();
  401. this.context.restore();
  402. this.drawText(vector, vector.vectorId);
  403. }
  404. drawControlPoint(vector) {
  405. // const pt = coordinate.getScreenXY({ x: vector.x, y: vector.y });
  406. // const color = this.rgb();
  407. // this.context.strokeStyle = color;
  408. // this.context.fillStyle = color;
  409. // let radius = Style.Point.radius;
  410. // this.context.beginPath();
  411. // this.context.arc(
  412. // pt.x,
  413. // pt.y,
  414. // radius * coordinate.ratio,
  415. // 0,
  416. // Math.PI * 2,
  417. // true
  418. // );
  419. // this.context.stroke();
  420. // this.context.fill();
  421. // let start = dataService
  422. // .getEdge(vector.edgeInfo1.id)
  423. // .getPosition(vector.edgeInfo1.dir);
  424. // start = coordinate.getScreenXY(start);
  425. // let end = dataService
  426. // .getEdge(vector.edgeInfo2.id)
  427. // .getPosition(vector.edgeInfo2.dir);
  428. // end = coordinate.getScreenXY(end);
  429. // this.context.beginPath();
  430. // this.context.moveTo(start.x, start.y);
  431. // this.context.quadraticCurveTo(pt.x, pt.y, end.x, end.y);
  432. // this.context.stroke();
  433. // this.context.restore();
  434. // this.drawText(vector, vector.vectorId);
  435. // const pt2 = this.twoOrderBezier(0.5, start, pt, end);
  436. // this.context.save();
  437. // this.context.strokeStyle = color;
  438. // this.context.fillStyle = color;
  439. // this.context.beginPath();
  440. // this.context.arc(
  441. // pt2.x,
  442. // pt2.y,
  443. // radius * coordinate.ratio * 3,
  444. // 0,
  445. // Math.PI * 2,
  446. // true
  447. // );
  448. // this.context.stroke();
  449. // this.context.fill();
  450. // this.context.restore();
  451. let pt = coordinate.getScreenXY({ x: vector.x, y: vector.y });
  452. const color = this.rgb();
  453. this.context.strokeStyle = color;
  454. this.context.fillStyle = color;
  455. let radius = Style.Point.radius;
  456. let start = dataService
  457. .getEdge(vector.edgeInfo1.id)
  458. .getPosition(vector.edgeInfo1.dir);
  459. start = coordinate.getScreenXY(start);
  460. let end = dataService
  461. .getEdge(vector.edgeInfo2.id)
  462. .getPosition(vector.edgeInfo2.dir);
  463. end = coordinate.getScreenXY(end);
  464. const pt2 = this.twoOrderBezier(0.5, start, pt, end);
  465. pt = this.twoOrderBezier2(0.5, start, pt2, end);
  466. this.context.save();
  467. //曲线
  468. this.context.moveTo(start.x, start.y);
  469. this.context.quadraticCurveTo(pt.x, pt.y, end.x, end.y);
  470. this.context.stroke();
  471. this.context.restore();
  472. this.context.save();
  473. this.context.beginPath();
  474. this.context.arc(
  475. pt.x,
  476. pt.y,
  477. radius * coordinate.ratio * 3,
  478. 0,
  479. Math.PI * 2,
  480. true
  481. );
  482. this.context.stroke();
  483. this.context.fill();
  484. this.context.restore();
  485. }
  486. twoOrderBezier(t, p1, cp, p2) {
  487. //参数分别是t,起始点,控制点和终点
  488. var x1 = p1.x;
  489. var y1 = p1.y;
  490. var cx = cp.x;
  491. var cy = cp.y;
  492. var x2 = p2.x;
  493. var y2 = p2.y;
  494. // var [x1, y1] = p1,
  495. // [cx, cy] = cp,
  496. // [x2, y2] = p2;
  497. var x = (1 - t) * (1 - t) * x1 + 2 * t * (1 - t) * cx + t * t * x2,
  498. y = (1 - t) * (1 - t) * y1 + 2 * t * (1 - t) * cy + t * t * y2;
  499. return {
  500. x: x,
  501. y: y,
  502. };
  503. }
  504. //t是0.5,求cp。p是曲线上的点
  505. twoOrderBezier2(t, p1, p, p2) {
  506. var x1 = p1.x;
  507. var y1 = p1.y;
  508. var x2 = p2.x;
  509. var y2 = p2.y;
  510. let cx = (p.x - t * t * x2 - (1 - t) * (1 - t) * x1) / (2 * t * (1 - t));
  511. let cy = (p.y - t * t * y2 - (1 - t) * (1 - t) * y1) / (2 * t * (1 - t));
  512. return {
  513. x: cx,
  514. y: cy,
  515. };
  516. }
  517. // 文字
  518. drawText(position, txt, angle) {
  519. this.context.save();
  520. this.setCanvasStyle(Style.Font);
  521. if (coordinate.ratio == Constant.ratio) {
  522. this.context.font = "12px Microsoft YaHei";
  523. } else {
  524. this.context.font = "12px Microsoft YaHei";
  525. }
  526. let pt = coordinate.getScreenXY(position);
  527. if (angle) {
  528. this.context.translate(pt.x, pt.y);
  529. this.context.rotate(angle);
  530. //this.context.strokeText(txt, 0, 0)
  531. this.context.fillText(txt, 0, 0);
  532. } else {
  533. //this.context.strokeText(txt, pt.x, pt.y)
  534. this.context.fillText(txt, pt.x, pt.y);
  535. }
  536. this.context.restore();
  537. }
  538. drawTag(geometry, styleType, hide) {
  539. this.context.save();
  540. this.context.lineWidth = Style.Tag.lineWidth * coordinate.ratio;
  541. this.context.strokeStyle = Style.Tag.strokeStyle;
  542. this.context.fillStyle = Style.Tag.fillStyle;
  543. if (styleType) {
  544. if (styleType == "style-1") {
  545. this.context.lineWidth =
  546. Style.DownLoad.style1.Tag.lineWidth * coordinate.ratio;
  547. this.context.strokeStyle = Style.DownLoad.style1.Tag.strokeStyle;
  548. this.context.fillStyle = Style.DownLoad.style1.Tag.fillStyle;
  549. } else if (styleType == "style-2") {
  550. this.context.lineWidth =
  551. Style.DownLoad.style2.Tag.lineWidth * coordinate.ratio;
  552. this.context.strokeStyle = Style.DownLoad.style2.Tag.strokeStyle;
  553. this.context.fillStyle = Style.DownLoad.style2.Tag.fillStyle;
  554. } else if (styleType == "style-3") {
  555. this.context.lineWidth =
  556. Style.DownLoad.style3.Tag.lineWidth * coordinate.ratio;
  557. this.context.strokeStyle = Style.DownLoad.style3.Tag.strokeStyle;
  558. this.context.fillStyle = Style.DownLoad.style3.Tag.fillStyle;
  559. } else if (styleType == "style-4") {
  560. this.context.lineWidth =
  561. Style.DownLoad.style4.Tag.lineWidth * coordinate.ratio;
  562. this.context.strokeStyle = Style.DownLoad.style4.Tag.strokeStyle;
  563. this.context.fillStyle = Style.DownLoad.style4.Tag.fillStyle;
  564. }
  565. } else {
  566. const selectItem = stateService.getSelectItem();
  567. const draggingItem = stateService.getDraggingItem();
  568. const focusItem = stateService.getFocusItem();
  569. if (selectItem && selectItem.type == VectorType.Tag) {
  570. if (geometry.vectorId == selectItem.vectorId) {
  571. this.context.strokeStyle = Style.Select.Tag.strokeStyle;
  572. this.context.fillStyle = Style.Select.Tag.fillStyle;
  573. }
  574. } else if (draggingItem && draggingItem.type == VectorType.Tag) {
  575. if (geometry.vectorId == draggingItem.vectorId) {
  576. this.context.strokeStyle = Style.Select.Tag.strokeStyle;
  577. this.context.fillStyle = Style.Select.Tag.fillStyle;
  578. }
  579. }
  580. if (focusItem && focusItem.type == VectorType.Tag) {
  581. if (geometry.vectorId == focusItem.vectorId) {
  582. this.context.strokeStyle = Style.Focus.Tag.strokeStyle;
  583. this.context.fillStyle = Style.Focus.Tag.fillStyle;
  584. }
  585. }
  586. }
  587. //正在添加
  588. if (geometry.adding) {
  589. let points2d = geometry.points2d;
  590. let points = [];
  591. for (let i = 0; i < points2d.length; ++i) {
  592. points[i] = coordinate.getScreenXY({
  593. x: points2d[i].x,
  594. y: points2d[i].y,
  595. });
  596. }
  597. this.context.strokeStyle = Style.Tag.strokeStyle_adding;
  598. this.context.fillStyle = Style.Tag.fillStyle_adding;
  599. this.context.beginPath();
  600. this.context.moveTo(points[0].x, points[0].y);
  601. this.context.lineTo(points[1].x, points[1].y);
  602. this.context.lineTo(points[2].x, points[2].y);
  603. this.context.lineTo(points[3].x, points[3].y);
  604. this.context.closePath();
  605. this.context.stroke();
  606. for (let i = 4; i < points.length - 1; i += 2) {
  607. this.context.moveTo(points[i].x, points[i].y);
  608. this.context.lineTo(points[i + 1].x, points[i + 1].y);
  609. }
  610. this.context.stroke();
  611. } else {
  612. const fontSize = coordinate.ratio == Constant.ratio ? 36 : 12;
  613. this.context.font = `400 ${fontSize}px Microsoft YaHei`;
  614. //根据文字的长度,更新标注范围
  615. let title = geometry.title;
  616. if (!hide && (title == null || title.trim() == "")) {
  617. console.log(dataService.$app.config);
  618. // title = '请输入名称'
  619. title = dataService.$app.config.i18n("cad.input");
  620. }
  621. geometry.des += "";
  622. if (geometry.des != "") {
  623. geometry.sideWidth = Math.max(
  624. this.context.measureText(title).width,
  625. this.context.measureText(
  626. parseFloat(geometry.des.replace(",", "")).toFixed(2)
  627. ).width
  628. );
  629. geometry.setPoints2d();
  630. }
  631. let points2d = geometry.points2d;
  632. let points = [];
  633. for (let i = 0; i < points2d.length; ++i) {
  634. points[i] = coordinate.getScreenXY({
  635. x: points2d[i].x,
  636. y: points2d[i].y,
  637. });
  638. }
  639. let pt = { x: geometry.center.x, y: geometry.center.y };
  640. pt = coordinate.getScreenXY({
  641. x: geometry.center.x,
  642. y: geometry.center.y,
  643. });
  644. const fontWidth1 = this.context.measureText(title).width;
  645. const line1 = mathUtil.createLine1(
  646. {
  647. x: (points[0].x + points[3].x) / 2,
  648. y: (points[0].y + points[3].y) / 2,
  649. },
  650. {
  651. x: (points[2].x + points[1].x) / 2,
  652. y: (points[2].y + points[1].y) / 2,
  653. }
  654. );
  655. let fontWidth2 = this.context.measureText(geometry.des + "m²").width;
  656. if (geometry.des != "" && geometry.unit == "ft") {
  657. fontWidth2 = this.context.measureText(
  658. parseFloat(geometry.des.replace(",", "")).toFixed(2) + "ft²"
  659. ).width;
  660. }
  661. const line2 = mathUtil.createLine1(points[2], points[3]);
  662. const fontStart1 = mathUtil.getDisPointsLine(
  663. line1,
  664. pt,
  665. fontWidth1 / 2,
  666. fontWidth1 / 2
  667. );
  668. const fontStart2 = mathUtil.getDisPointsLine(
  669. line2,
  670. {
  671. x: (points[2].x + points[3].x) / 2,
  672. y: (points[2].y + points[3].y) / 2,
  673. },
  674. fontWidth2 / 2,
  675. fontWidth2 / 2
  676. );
  677. if (fontStart1.newpoint1.x < fontStart1.newpoint2.x) {
  678. this.context.fillText(
  679. title,
  680. fontStart1.newpoint1.x,
  681. fontStart1.newpoint1.y
  682. );
  683. if (geometry.des) {
  684. if (measureService.unit == "ft" && geometry.unit == "m") {
  685. let area = uoMService.convert(
  686. geometry.des,
  687. "area",
  688. void 0,
  689. "imperial",
  690. 0.01,
  691. false
  692. );
  693. this.context.fillText(
  694. parseFloat(area.replace(",", "")).toFixed(2),
  695. fontStart2.newpoint1.x + fontSize / 1.5,
  696. fontStart2.newpoint1.y
  697. );
  698. } else if (measureService.unit == "m" && geometry.unit == "ft") {
  699. let area = uoMService.convertBack(
  700. geometry.des,
  701. "area",
  702. 7,
  703. "imperial",
  704. 0.01,
  705. false
  706. );
  707. this.context.fillText(
  708. parseFloat(area.replace(",", "")).toFixed(2),
  709. fontStart2.newpoint1.x + fontSize / 1.5,
  710. fontStart2.newpoint1.y
  711. );
  712. } else if (geometry.unit == "m") {
  713. this.context.fillText(
  714. parseFloat(geometry.des).toFixed(2) + "m²",
  715. fontStart2.newpoint1.x,
  716. fontStart2.newpoint1.y
  717. );
  718. } else if (geometry.unit == "ft") {
  719. this.context.fillText(
  720. parseFloat(geometry.des.replace(",", "")).toFixed(2) + "ft²",
  721. fontStart2.newpoint1.x,
  722. fontStart2.newpoint1.y
  723. );
  724. }
  725. }
  726. } else {
  727. this.context.fillText(
  728. title,
  729. fontStart1.newpoint2.x,
  730. fontStart1.newpoint2.y
  731. );
  732. if (geometry.des) {
  733. if (measureService.unit == "ft" && geometry.unit == "m") {
  734. let area = uoMService.convert(
  735. geometry.des,
  736. "area",
  737. void 0,
  738. "imperial",
  739. 0.01,
  740. false
  741. );
  742. this.context.fillText(
  743. parseFloat(area.replace(",", "")).toFixed(2),
  744. fontStart2.newpoint2.x + fontSize / 1.5,
  745. fontStart2.newpoint2.y
  746. );
  747. } else if (measureService.unit == "m" && geometry.unit == "ft") {
  748. let area = uoMService.convertBack(
  749. geometry.des,
  750. "area",
  751. 7,
  752. "imperial",
  753. 0.01,
  754. false
  755. );
  756. this.context.fillText(
  757. parseFloat(area.replace(",", "")).toFixed(2),
  758. fontStart2.newpoint2.x + fontSize / 1.5,
  759. fontStart2.newpoint2.y
  760. );
  761. } else if (geometry.unit == "m") {
  762. this.context.fillText(
  763. parseFloat(geometry.des).toFixed(2) + "m²",
  764. fontStart2.newpoint2.x,
  765. fontStart2.newpoint2.y
  766. );
  767. } else if (geometry.unit == "ft") {
  768. this.context.fillText(
  769. parseFloat(geometry.des.replace(",", "")).toFixed(2) + "ft²",
  770. fontStart2.newpoint2.x,
  771. fontStart2.newpoint2.y
  772. );
  773. }
  774. }
  775. }
  776. }
  777. this.context.restore();
  778. }
  779. drawCircle(element) {
  780. let radius = null;
  781. const twoPi = Math.PI * 2;
  782. const pt = coordinate.getScreenXY(element);
  783. this.context.save();
  784. if (element.name == ElementEvents.AddingPoint) {
  785. radius = Style.Element.AddingPoint.radius * coordinate.ratio;
  786. this.context.strokeStyle = Style.Element.AddingPoint.strokeStyle;
  787. this.context.fillStyle = Style.Element.AddingPoint.fillStyle;
  788. } else if (element.name == ElementEvents.StartSymbolPoints) {
  789. radius = Style.Element.StartSymbolPoints.radius * coordinate.ratio;
  790. this.context.strokeStyle = Style.Element.StartSymbolPoints.strokeStyle;
  791. this.context.fillStyle = Style.Element.StartSymbolPoints.fillStyle;
  792. } else if (element.name == ElementEvents.EndSymbolPoints) {
  793. radius = Style.Element.EndSymbolPoints.radius * coordinate.ratio;
  794. this.context.strokeStyle = Style.Element.EndSymbolPoints.strokeStyle;
  795. this.context.fillStyle = Style.Element.EndSymbolPoints.fillStyle;
  796. } else if (element.name == "pano") {
  797. radius = Style.Pano.radius * coordinate.ratio;
  798. this.context.strokeStyle = Style.Pano.strokeStyle;
  799. this.context.fillStyle = Style.Pano.fillStyle;
  800. this.context.lineWidth = Style.Pano.lineWidth;
  801. }
  802. this.context.beginPath();
  803. this.context.arc(pt.x, pt.y, radius, 0, twoPi, true);
  804. this.context.stroke();
  805. this.context.fill();
  806. this.context.restore();
  807. }
  808. drawLine(element) {
  809. let start = coordinate.getScreenXY(element.start);
  810. let end = coordinate.getScreenXY(element.end);
  811. this.context.save();
  812. if (element.name == ElementEvents.VCheckLinesX) {
  813. this.context.lineWidth =
  814. Style.Element.VCheckLinesX.lineWidth * coordinate.ratio;
  815. this.context.strokeStyle = Style.Element.VCheckLinesX.strokeStyle;
  816. this.context.setLineDash([3, 2, 2]);
  817. start.y = 0;
  818. end.y = this.context.canvas.clientHeight;
  819. } else if (element.name == ElementEvents.VCheckLinesY) {
  820. this.context.lineWidth =
  821. Style.Element.VCheckLinesY.lineWidth * coordinate.ratio;
  822. this.context.strokeStyle = Style.Element.VCheckLinesY.strokeStyle;
  823. this.context.setLineDash([3, 2, 2]);
  824. start.x = 0;
  825. end.x = this.context.canvas.clientWidth;
  826. } else if (element.name == ElementEvents.NewRoad) {
  827. this.context.lineWidth =
  828. Style.Element.NewRoad.lineWidth * coordinate.ratio;
  829. this.context.strokeStyle = Style.Element.NewRoad.strokeStyle;
  830. if (element.state == "error") {
  831. this.context.strokeStyle = Style.Element.NewRoad.errorStrokeStyle;
  832. }
  833. } else if (element.name == ElementEvents.CheckLinesX) {
  834. this.context.lineWidth =
  835. Style.Element.CheckLinesX.lineWidth * coordinate.ratio;
  836. this.context.strokeStyle = Style.Element.CheckLinesX.strokeStyle;
  837. this.context.setLineDash([3, 2, 2]);
  838. } else if (element.name == ElementEvents.CheckLinesY) {
  839. this.context.lineWidth =
  840. Style.Element.CheckLinesY.lineWidth * coordinate.ratio;
  841. this.context.strokeStyle = Style.Element.CheckLinesY.strokeStyle;
  842. this.context.setLineDash([3, 2, 2]);
  843. } else if (element.name == ElementEvents.SignLine1) {
  844. this.context.lineWidth =
  845. Style.Element.SignLine1.lineWidth * coordinate.ratio;
  846. this.context.strokeStyle = Style.Element.SignLine1.strokeStyle;
  847. this.context.setLineDash([3, 2, 2]);
  848. } else if (element.name == ElementEvents.SignLine2) {
  849. this.context.lineWidth =
  850. Style.Element.SignLine2.lineWidth * coordinate.ratio;
  851. this.context.strokeStyle = Style.Element.SignLine2.strokeStyle;
  852. this.context.setLineDash([3, 2, 2]);
  853. }
  854. this.context.beginPath();
  855. this.context.moveTo(start.x, start.y);
  856. this.context.lineTo(end.x, end.y);
  857. this.context.stroke();
  858. this.context.restore();
  859. // if (element.name == ElementEvents.NewRoad) {
  860. // //添加测量值
  861. // this.drawMeasureTxt(element.start, element.end);
  862. // }
  863. // this.context.save();
  864. // this.context.lineWidth = Style.Point.lineWidth * coordinate.ratio;
  865. // this.context.strokeStyle = Style.Point.strokeStyle;
  866. // this.context.fillStyle = Style.Point.fillStyle;
  867. // let radius = Style.Point.radius;
  868. // this.context.beginPath();
  869. // this.context.arc(
  870. // start.x,
  871. // start.y,
  872. // radius * coordinate.ratio,
  873. // 0,
  874. // Math.PI * 2,
  875. // true
  876. // );
  877. // this.context.stroke();
  878. // this.context.fill();
  879. // this.context.restore();
  880. }
  881. drawTestLine(start, end, hit) {
  882. start = coordinate.getScreenXY(start);
  883. end = coordinate.getScreenXY(end);
  884. this.context.save();
  885. this.context.strokeStyle = "blue";
  886. this.context.beginPath();
  887. this.context.moveTo(start.x, start.y);
  888. this.context.lineTo(end.x, end.y);
  889. this.context.stroke();
  890. // this.context.beginPath();
  891. // this.context.arc(start.x, start.y, Constant.minAdsorbPix, 0, 2 * Math.PI);
  892. // if (!hit) {
  893. // this.context.fillStyle = "black";
  894. // } else {
  895. // this.context.fillStyle = "red";
  896. // }
  897. // this.context.fill();
  898. this.context.restore();
  899. }
  900. //由多个点构成,里面的坐标都已经是屏幕坐标
  901. drawMeasure(points, dir, styleType) {
  902. this.context.save();
  903. this.context.strokeStyle = Style.Measure.strokeStyle;
  904. this.context.lineWidth = Style.Measure.lineWidth * coordinate.ratio;
  905. if (styleType) {
  906. if (styleType == "style-1") {
  907. this.context.lineWidth =
  908. Style.DownLoad.style1.Measure.lineWidth * coordinate.ratio;
  909. this.context.strokeStyle = Style.DownLoad.style1.Measure.strokeStyle;
  910. } else if (styleType == "style-2") {
  911. this.context.lineWidth =
  912. Style.DownLoad.style1.Measure.lineWidth * coordinate.ratio;
  913. this.context.strokeStyle = Style.DownLoad.style1.Measure.strokeStyle;
  914. } else if (styleType == "style-3") {
  915. this.context.lineWidth =
  916. Style.DownLoad.style1.Measure.lineWidth * coordinate.ratio;
  917. this.context.strokeStyle = Style.DownLoad.style1.Measure.strokeStyle;
  918. } else if (styleType == "style-4") {
  919. this.context.lineWidth =
  920. Style.DownLoad.style1.Measure.lineWidth * coordinate.ratio;
  921. this.context.strokeStyle = Style.DownLoad.style1.Measure.strokeStyle;
  922. }
  923. }
  924. for (let i = 0; i < points.length - 1; ++i) {
  925. let start = coordinate.getScreenXY(points[i]);
  926. let end = coordinate.getScreenXY(points[i + 1]);
  927. let angle = 0;
  928. if (dir == "top") {
  929. start.y = measureService.region.top * coordinate.ratio;
  930. end.y = measureService.region.top * coordinate.ratio;
  931. } else if (dir == "bottom") {
  932. start.y = measureService.region.bottom * coordinate.ratio;
  933. end.y = measureService.region.bottom * coordinate.ratio;
  934. } else if (dir == "left") {
  935. start.x = measureService.region.left * coordinate.ratio;
  936. end.x = measureService.region.left * coordinate.ratio;
  937. angle = (-90 / 180) * Math.PI;
  938. } else if (dir == "right") {
  939. start.x = measureService.region.right * coordinate.ratio;
  940. end.x = measureService.region.right * coordinate.ratio;
  941. angle = (90 / 180) * Math.PI;
  942. }
  943. let line = mathUtil.createLine1(start, end);
  944. if (line == null) {
  945. continue;
  946. }
  947. let lines = mathUtil.getParallelLineForDistance(
  948. line,
  949. 6 * coordinate.ratio
  950. );
  951. let start1 = mathUtil.getJoinLinePoint(start, lines.line1);
  952. let end1 = mathUtil.getJoinLinePoint(end, lines.line1);
  953. let start2 = mathUtil.getJoinLinePoint(start, lines.line2);
  954. let end2 = mathUtil.getJoinLinePoint(end, lines.line2);
  955. this.context.beginPath();
  956. this.context.moveTo(start1.x, start1.y);
  957. this.context.lineTo(start2.x, start2.y);
  958. this.context.stroke();
  959. this.context.beginPath();
  960. this.context.moveTo(end1.x, end1.y);
  961. this.context.lineTo(end2.x, end2.y);
  962. this.context.stroke();
  963. let mid = {
  964. x: (start.x + end.x) / 2,
  965. y: (start.y + end.y) / 2,
  966. };
  967. let vLine = mathUtil.getVerticalLine(line, mid);
  968. lines = mathUtil.getParallelLineForDistance(vLine, 22 * coordinate.ratio);
  969. let join1 = mathUtil.getIntersectionPoint(line, lines.line1);
  970. let join2 = mathUtil.getIntersectionPoint(line, lines.line2);
  971. if (
  972. mathUtil.getDistance(start, join1) < mathUtil.getDistance(start, mid)
  973. ) {
  974. let measureValue = mathUtil.getDistance(points[i], points[i + 1]);
  975. if (measureService.unit == "ft") {
  976. measureValue =
  977. " " +
  978. uoMService.convert(
  979. measureValue,
  980. "distance",
  981. void 0,
  982. "imperial",
  983. 0.01,
  984. true
  985. ) +
  986. " ";
  987. //measureValue = mathUtil.getFixed(measureValue / measureService.ftUnit, 2) + 'ft'
  988. } else {
  989. measureValue = mathUtil.getFixed(measureValue, 2) + "m";
  990. }
  991. if (
  992. mathUtil.getDistance(start, end) >
  993. this.context.measureText(measureValue).width
  994. ) {
  995. this.context.beginPath();
  996. this.context.moveTo(start.x, start.y);
  997. this.context.lineTo(join1.x, join1.y);
  998. this.context.stroke();
  999. this.context.beginPath();
  1000. this.context.moveTo(join2.x, join2.y);
  1001. this.context.lineTo(end.x, end.y);
  1002. this.context.stroke();
  1003. this.context.save();
  1004. if (coordinate.ratio == Constant.ratio) {
  1005. this.context.font = "36px Microsoft YaHei";
  1006. } else {
  1007. this.context.font = "12px Microsoft YaHei";
  1008. }
  1009. if (styleType == "style-1" || styleType == "style-3") {
  1010. this.context.fillStyle = "#000000";
  1011. this.context.strokeStyle = "#000000";
  1012. } else {
  1013. this.context.fillStyle = "#FFFFFF";
  1014. this.context.strokeStyle = "#FFFFFF";
  1015. }
  1016. this.context.textAlign = "center";
  1017. this.context.textBaseline = "middle";
  1018. this.context.miterLimit = 10;
  1019. this.context.direction = "ltr";
  1020. if (angle) {
  1021. this.context.translate(mid.x, mid.y);
  1022. this.context.rotate(angle);
  1023. this.context.fillText(measureValue, 0, 0);
  1024. } else {
  1025. this.context.fillText(measureValue, mid.x, mid.y);
  1026. }
  1027. this.context.restore();
  1028. } else {
  1029. this.context.beginPath();
  1030. this.context.moveTo(start.x, start.y);
  1031. this.context.lineTo(end.x, end.y);
  1032. this.context.stroke();
  1033. }
  1034. } else {
  1035. let measureValue = mathUtil.getDistance(points[i], points[i + 1]);
  1036. if (measureService.unit == "ft") {
  1037. //measureValue = mathUtil.getFixed(measureValue / measureService.ftUnit, 2) + 'ft'
  1038. measureValue =
  1039. " " +
  1040. uoMService.convert(
  1041. measureValue,
  1042. "distance",
  1043. void 0,
  1044. "imperial",
  1045. 0.01,
  1046. true
  1047. ) +
  1048. " ";
  1049. } else {
  1050. measureValue = mathUtil.getFixed(measureValue, 2) + "m";
  1051. }
  1052. if (
  1053. mathUtil.getDistance(start, end) >
  1054. this.context.measureText(measureValue).width
  1055. ) {
  1056. this.context.beginPath();
  1057. this.context.moveTo(start.x, start.y);
  1058. this.context.lineTo(join2.x, join2.y);
  1059. this.context.stroke();
  1060. this.context.beginPath();
  1061. this.context.moveTo(join1.x, join1.y);
  1062. this.context.lineTo(end.x, end.y);
  1063. this.context.stroke();
  1064. this.context.save();
  1065. if (coordinate.ratio == Constant.ratio) {
  1066. this.context.font = "36px Microsoft YaHei";
  1067. } else {
  1068. this.context.font = "12px Microsoft YaHei";
  1069. }
  1070. if (styleType == "style-1" || styleType == "style-3") {
  1071. this.context.fillStyle = "#000000";
  1072. this.context.strokeStyle = "#000000";
  1073. } else {
  1074. this.context.fillStyle = "#FFFFFF";
  1075. this.context.strokeStyle = "#FFFFFF";
  1076. }
  1077. this.context.textAlign = "center";
  1078. this.context.textBaseline = "middle";
  1079. this.context.miterLimit = 10;
  1080. this.context.direction = "ltr";
  1081. if (angle) {
  1082. this.context.translate(mid.x, mid.y);
  1083. this.context.rotate(angle);
  1084. this.context.fillText(measureValue, 0, 0);
  1085. } else {
  1086. this.context.fillText(measureValue, mid.x, mid.y);
  1087. }
  1088. this.context.restore();
  1089. } else {
  1090. this.context.beginPath();
  1091. this.context.moveTo(start.x, start.y);
  1092. this.context.lineTo(end.x, end.y);
  1093. this.context.stroke();
  1094. }
  1095. }
  1096. }
  1097. this.context.restore();
  1098. }
  1099. drawMeasureTxt(startPoint, endPoint) {
  1100. const _startPoint = coordinate.getScreenXY(startPoint);
  1101. const _endPoint = coordinate.getScreenXY(endPoint);
  1102. const measureInterval = 10;
  1103. const line = mathUtil.createLine1(_startPoint, _endPoint);
  1104. if (line == null) {
  1105. return;
  1106. }
  1107. let mid = {
  1108. x: (_startPoint.x + _endPoint.x) / 2,
  1109. y: (_startPoint.y + _endPoint.y) / 2,
  1110. };
  1111. const lines = mathUtil.getParallelLineForDistance(line, measureInterval);
  1112. let pLine = null;
  1113. let mid1 = mathUtil.getJoinLinePoint(mid, lines.line1);
  1114. let mid2 = mathUtil.getJoinLinePoint(mid, lines.line2);
  1115. if (mid.y < mid1.y) {
  1116. mathUtil.clonePoint(mid, mid2);
  1117. pLine = lines.line2;
  1118. } else {
  1119. mathUtil.clonePoint(mid, mid1);
  1120. pLine = lines.line1;
  1121. }
  1122. let measureDistance = mathUtil.getDistance(startPoint, endPoint);
  1123. if (measureService.unit == "ft") {
  1124. //measureDistance = mathUtil.getFixed(measureDistance / measureService.ftUnit, 2) + 'ft'
  1125. measureDistance =
  1126. " " +
  1127. uoMService.convert(
  1128. measureDistance,
  1129. "distance",
  1130. void 0,
  1131. "imperial",
  1132. 0.01,
  1133. true
  1134. ) +
  1135. " ";
  1136. } else {
  1137. measureDistance = mathUtil.getFixed(measureDistance, 2) + "m";
  1138. }
  1139. const fontWidth = this.context.measureText(measureDistance).width;
  1140. let vLine = mathUtil.getLineForPoint(line, mid);
  1141. const vLines = mathUtil.getParallelLineForDistance(vLine, fontWidth / 2);
  1142. let startJoin = mathUtil.getIntersectionPoint(vLines.line1, pLine);
  1143. startJoin = {
  1144. x: Math.round(startJoin.x),
  1145. y: Math.round(startJoin.y),
  1146. };
  1147. let endJoin = mathUtil.getIntersectionPoint(vLines.line2, pLine);
  1148. endJoin = {
  1149. x: Math.round(endJoin.x),
  1150. y: Math.round(endJoin.y),
  1151. };
  1152. if (startJoin.x < endJoin.x) {
  1153. mathUtil.clonePoint(mid, startJoin);
  1154. } else if (startJoin.x > endJoin.x) {
  1155. mathUtil.clonePoint(mid, endJoin);
  1156. } else if (startJoin.y < endJoin.y) {
  1157. mathUtil.clonePoint(mid, startJoin);
  1158. } else {
  1159. mathUtil.clonePoint(mid, endJoin);
  1160. }
  1161. //const fontStart = mathUtil.getDisPointsLine(line, mid, fontWidth / 2, fontWidth / 2)
  1162. // let a1, a2
  1163. let angle = null;
  1164. if (typeof line.a !== "undefined") {
  1165. angle = Math.atan(line.a);
  1166. } else if (line.hasOwnProperty("x")) {
  1167. angle = Math.PI / 2;
  1168. } else {
  1169. angle = 0;
  1170. }
  1171. this.context.save();
  1172. this.context.fillStyle = Style.Measure.txt;
  1173. this.context.translate(mid.x, mid.y);
  1174. this.context.rotate(angle);
  1175. this.context.fillText(measureDistance, 0, 0);
  1176. /*
  1177. if (fontStart.newpoint1.x > fontStart.newpoint2.x) {
  1178. this.context.translate(mid.x, mid.y)
  1179. this.context.rotate(angle)
  1180. //this.context.strokeText(measureDistance, 0, 0);
  1181. this.context.fillText(measureDistance, 0, 0)
  1182. // a1 = fontStart.newpoint2
  1183. // a2 = fontStart.newpoint1
  1184. } else if (fontStart.newpoint1.x < fontStart.newpoint2.x) {
  1185. this.context.translate(mid.x, mid.y)
  1186. this.context.rotate(angle)
  1187. //this.context.strokeText(measureDistance, 0, 0);
  1188. this.context.fillText(measureDistance, 0, 0)
  1189. // a1 = fontStart.newpoint1
  1190. // a2 = fontStart.newpoint2
  1191. } else if (fontStart.newpoint1.y < fontStart.newpoint2.y) {
  1192. this.context.translate(mid.x, mid.y)
  1193. this.context.rotate(angle)
  1194. //this.context.strokeText(measureDistance, 0, 0);
  1195. this.context.fillText(measureDistance, 0, 0)
  1196. // a2 = fontStart.newpoint2
  1197. // a1 = fontStart.newpoint1
  1198. } else {
  1199. this.context.translate(mid.x, mid.y)
  1200. this.context.rotate(angle)
  1201. //this.context.strokeText(measureDistance, 0, 0);
  1202. this.context.fillText(measureDistance, 0, 0)
  1203. // a2 = fontStart.newpoint1
  1204. // a1 = fontStart.newpoint2
  1205. }
  1206. */
  1207. this.context.restore();
  1208. }
  1209. setCanvasStyle(style) {
  1210. for (const key in style) {
  1211. if (key != "isFill" && key != "isStroke") {
  1212. this.context[key] = style[key];
  1213. }
  1214. }
  1215. }
  1216. rgb() {
  1217. //rgb颜色随机
  1218. const r = Math.floor(Math.random() * 256);
  1219. const g = Math.floor(Math.random() * 256);
  1220. const b = Math.floor(Math.random() * 256);
  1221. return `rgb(${r},${g},${b})`;
  1222. }
  1223. /*************************************************************************************家具**********************************************************************************************/
  1224. /***************************************************************************************************************************************************************************************/
  1225. }
  1226. const draw = new Draw();
  1227. export { draw };