Draw.js 44 KB

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