Draw.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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. drawBackGround(color) {
  31. this.context.save();
  32. this.context.fillStyle = color;
  33. this.context.fillRect(
  34. 0,
  35. 0,
  36. this.context.canvas.width,
  37. this.context.canvas.height
  38. );
  39. this.context.restore();
  40. }
  41. drawRoad(vector, isTemp) {
  42. this.context.save();
  43. this.context.beginPath();
  44. this.context.lineCap = "round"; //线段端点的样式
  45. //this.context.lineJoin= 'miter';
  46. this.context.strokeStyle = Style.Road.strokeStyle;
  47. const selectItem = stateService.getSelectItem();
  48. const draggingItem = stateService.getDraggingItem();
  49. const focusItem = stateService.getFocusItem();
  50. if (selectItem && selectItem.type == VectorType.Road) {
  51. if (vector.vectorId == selectItem.vectorId) {
  52. this.context.strokeStyle = Style.Select.Road.strokeStyle;
  53. }
  54. } else if (draggingItem && draggingItem.type == VectorType.Road) {
  55. if (vector.vectorId == draggingItem.vectorId) {
  56. this.context.strokeStyle = Style.Select.Road.strokeStyle;
  57. }
  58. } else if (focusItem && focusItem.type == VectorType.Road) {
  59. if (vector.vectorId == focusItem.vectorId) {
  60. this.context.strokeStyle = Style.Focus.Road.strokeStyle;
  61. }
  62. }
  63. let point1, point2;
  64. if (isTemp) {
  65. this.context.globalAlpha = 0.3;
  66. point1 = coordinate.getScreenXY(vector.start);
  67. point2 = coordinate.getScreenXY(vector.end);
  68. this.context.lineWidth = Constant.defaultRoadWidth;
  69. } else {
  70. let start = dataService.getPoint(vector.startId);
  71. let end = dataService.getPoint(vector.endId);
  72. point1 = coordinate.getScreenXY(start);
  73. point2 = coordinate.getScreenXY(end);
  74. this.drawEdge(vector, isTemp);
  75. this.drawText(
  76. { x: (start.x + end.x) / 2, y: (start.y + end.y) / 2 },
  77. vector.vectorId
  78. );
  79. this.context.lineWidth = vector.width;
  80. }
  81. this.context.beginPath();
  82. this.context.moveTo(point1.x, point1.y);
  83. this.context.lineTo(point2.x, point2.y);
  84. this.context.stroke();
  85. this.context.restore();
  86. }
  87. drawEdge(vector) {
  88. this.context.save();
  89. this.context.beginPath();
  90. this.context.lineCap = "round"; //线段端点的样式
  91. this.context.strokeStyle = Style.Road.strokeStyle;
  92. const selectItem = stateService.getSelectItem();
  93. const draggingItem = stateService.getDraggingItem();
  94. const focusItem = stateService.getFocusItem();
  95. if (selectItem && selectItem.type == VectorType.Road) {
  96. if (vector.vectorId == selectItem.vectorId) {
  97. this.context.strokeStyle = Style.Select.Road.strokeStyle;
  98. }
  99. } else if (draggingItem && draggingItem.type == VectorType.Road) {
  100. if (vector.vectorId == draggingItem.vectorId) {
  101. this.context.strokeStyle = Style.Select.Road.strokeStyle;
  102. }
  103. } else if (focusItem && focusItem.type == VectorType.Road) {
  104. if (vector.vectorId == focusItem.vectorId) {
  105. this.context.strokeStyle = Style.Focus.Road.strokeStyle;
  106. }
  107. }
  108. const leftEdge = dataService.getEdge(vector.leftEdgeId);
  109. const rightEdge = dataService.getEdge(vector.rightEdgeId);
  110. let point1, point2;
  111. this.context.globalAlpha = 0.3;
  112. this.context.lineWidth = 1;
  113. this.context.beginPath();
  114. point1 = coordinate.getScreenXY(leftEdge.start);
  115. point2 = coordinate.getScreenXY(leftEdge.end);
  116. this.context.moveTo(point1.x, point1.y);
  117. this.context.lineTo(point2.x, point2.y);
  118. this.context.stroke();
  119. point1 = coordinate.getScreenXY(rightEdge.start);
  120. point2 = coordinate.getScreenXY(rightEdge.end);
  121. this.context.moveTo(point1.x, point1.y);
  122. this.context.lineTo(point2.x, point2.y);
  123. this.context.stroke();
  124. this.context.restore();
  125. }
  126. drawPoint(vector) {
  127. const pt = coordinate.getScreenXY({ x: vector.x, y: vector.y });
  128. const selectItem = stateService.getSelectItem();
  129. const draggingItem = stateService.getDraggingItem();
  130. const focusItem = stateService.getFocusItem();
  131. let radius = Style.Point.radius;
  132. if (
  133. (draggingItem &&
  134. draggingItem.type == VectorType.RoadCorner &&
  135. vector.vectorId == draggingItem.vectorId) ||
  136. (selectItem &&
  137. selectItem.type == VectorType.RoadCorner &&
  138. vector.vectorId == selectItem.vectorId)
  139. ) {
  140. this.context.save();
  141. this.context.lineWidth = Style.Select.Point.lineWidth * coordinate.ratio;
  142. this.context.strokeStyle = Style.Select.Point.strokeStyle;
  143. this.context.fillStyle = Style.Select.Point.fillStyle;
  144. radius = Style.Select.Point.radius;
  145. } else if (
  146. focusItem &&
  147. focusItem.type == VectorType.RoadCorner &&
  148. vector.vectorId == focusItem.vectorId
  149. ) {
  150. this.context.save();
  151. this.context.lineWidth = Style.Focus.Point.lineWidth * coordinate.ratio;
  152. this.context.strokeStyle = Style.Focus.Point.strokeStyle;
  153. this.context.fillStyle = Style.Focus.Point.fillStyle;
  154. radius = Style.Focus.Point.radius;
  155. } else {
  156. return;
  157. }
  158. this.context.beginPath();
  159. this.context.arc(
  160. pt.x,
  161. pt.y,
  162. radius * coordinate.ratio,
  163. 0,
  164. Math.PI * 2,
  165. true
  166. );
  167. this.context.stroke();
  168. this.context.fill();
  169. this.context.restore();
  170. this.drawText(vector, vector.vectorId);
  171. }
  172. // 文字
  173. drawText(position, txt, angle) {
  174. this.context.save();
  175. this.setCanvasStyle(Style.Font);
  176. if (coordinate.ratio == Constant.ratio) {
  177. this.context.font = "36px Microsoft YaHei";
  178. } else {
  179. this.context.font = "12px Microsoft YaHei";
  180. }
  181. let pt = coordinate.getScreenXY(position);
  182. if (angle) {
  183. this.context.translate(pt.x, pt.y);
  184. this.context.rotate(angle);
  185. //this.context.strokeText(txt, 0, 0)
  186. this.context.fillText(txt, 0, 0);
  187. } else {
  188. //this.context.strokeText(txt, pt.x, pt.y)
  189. this.context.fillText(txt, pt.x, pt.y);
  190. }
  191. this.context.restore();
  192. }
  193. drawTag(geometry, styleType, hide) {
  194. this.context.save();
  195. this.context.lineWidth = Style.Tag.lineWidth * coordinate.ratio;
  196. this.context.strokeStyle = Style.Tag.strokeStyle;
  197. this.context.fillStyle = Style.Tag.fillStyle;
  198. if (styleType) {
  199. if (styleType == "style-1") {
  200. this.context.lineWidth =
  201. Style.DownLoad.style1.Tag.lineWidth * coordinate.ratio;
  202. this.context.strokeStyle = Style.DownLoad.style1.Tag.strokeStyle;
  203. this.context.fillStyle = Style.DownLoad.style1.Tag.fillStyle;
  204. } else if (styleType == "style-2") {
  205. this.context.lineWidth =
  206. Style.DownLoad.style2.Tag.lineWidth * coordinate.ratio;
  207. this.context.strokeStyle = Style.DownLoad.style2.Tag.strokeStyle;
  208. this.context.fillStyle = Style.DownLoad.style2.Tag.fillStyle;
  209. } else if (styleType == "style-3") {
  210. this.context.lineWidth =
  211. Style.DownLoad.style3.Tag.lineWidth * coordinate.ratio;
  212. this.context.strokeStyle = Style.DownLoad.style3.Tag.strokeStyle;
  213. this.context.fillStyle = Style.DownLoad.style3.Tag.fillStyle;
  214. } else if (styleType == "style-4") {
  215. this.context.lineWidth =
  216. Style.DownLoad.style4.Tag.lineWidth * coordinate.ratio;
  217. this.context.strokeStyle = Style.DownLoad.style4.Tag.strokeStyle;
  218. this.context.fillStyle = Style.DownLoad.style4.Tag.fillStyle;
  219. }
  220. } else {
  221. const selectItem = stateService.getSelectItem();
  222. const draggingItem = stateService.getDraggingItem();
  223. const focusItem = stateService.getFocusItem();
  224. if (selectItem && selectItem.type == VectorType.Tag) {
  225. if (geometry.vectorId == selectItem.vectorId) {
  226. this.context.strokeStyle = Style.Select.Tag.strokeStyle;
  227. this.context.fillStyle = Style.Select.Tag.fillStyle;
  228. }
  229. } else if (draggingItem && draggingItem.type == VectorType.Tag) {
  230. if (geometry.vectorId == draggingItem.vectorId) {
  231. this.context.strokeStyle = Style.Select.Tag.strokeStyle;
  232. this.context.fillStyle = Style.Select.Tag.fillStyle;
  233. }
  234. }
  235. if (focusItem && focusItem.type == VectorType.Tag) {
  236. if (geometry.vectorId == focusItem.vectorId) {
  237. this.context.strokeStyle = Style.Focus.Tag.strokeStyle;
  238. this.context.fillStyle = Style.Focus.Tag.fillStyle;
  239. }
  240. }
  241. }
  242. //正在添加
  243. if (geometry.adding) {
  244. let points2d = geometry.points2d;
  245. let points = [];
  246. for (let i = 0; i < points2d.length; ++i) {
  247. points[i] = coordinate.getScreenXY({
  248. x: points2d[i].x,
  249. y: points2d[i].y,
  250. });
  251. }
  252. this.context.strokeStyle = Style.Tag.strokeStyle_adding;
  253. this.context.fillStyle = Style.Tag.fillStyle_adding;
  254. this.context.beginPath();
  255. this.context.moveTo(points[0].x, points[0].y);
  256. this.context.lineTo(points[1].x, points[1].y);
  257. this.context.lineTo(points[2].x, points[2].y);
  258. this.context.lineTo(points[3].x, points[3].y);
  259. this.context.closePath();
  260. this.context.stroke();
  261. for (let i = 4; i < points.length - 1; i += 2) {
  262. this.context.moveTo(points[i].x, points[i].y);
  263. this.context.lineTo(points[i + 1].x, points[i + 1].y);
  264. }
  265. this.context.stroke();
  266. } else {
  267. const fontSize = coordinate.ratio == Constant.ratio ? 36 : 12;
  268. this.context.font = `400 ${fontSize}px Microsoft YaHei`;
  269. //根据文字的长度,更新标注范围
  270. let title = geometry.title;
  271. if (!hide && (title == null || title.trim() == "")) {
  272. console.log(dataService.$app.config);
  273. // title = '请输入名称'
  274. title = dataService.$app.config.i18n("cad.input");
  275. }
  276. geometry.des += "";
  277. if (geometry.des != "") {
  278. geometry.sideWidth = Math.max(
  279. this.context.measureText(title).width,
  280. this.context.measureText(
  281. parseFloat(geometry.des.replace(",", "")).toFixed(2)
  282. ).width
  283. );
  284. geometry.setPoints2d();
  285. }
  286. let points2d = geometry.points2d;
  287. let points = [];
  288. for (let i = 0; i < points2d.length; ++i) {
  289. points[i] = coordinate.getScreenXY({
  290. x: points2d[i].x,
  291. y: points2d[i].y,
  292. });
  293. }
  294. let pt = { x: geometry.center.x, y: geometry.center.y };
  295. pt = coordinate.getScreenXY({
  296. x: geometry.center.x,
  297. y: geometry.center.y,
  298. });
  299. const fontWidth1 = this.context.measureText(title).width;
  300. const line1 = mathUtil.createLine1(
  301. {
  302. x: (points[0].x + points[3].x) / 2,
  303. y: (points[0].y + points[3].y) / 2,
  304. },
  305. {
  306. x: (points[2].x + points[1].x) / 2,
  307. y: (points[2].y + points[1].y) / 2,
  308. }
  309. );
  310. let fontWidth2 = this.context.measureText(geometry.des + "m²").width;
  311. if (geometry.des != "" && geometry.unit == "ft") {
  312. fontWidth2 = this.context.measureText(
  313. parseFloat(geometry.des.replace(",", "")).toFixed(2) + "ft²"
  314. ).width;
  315. }
  316. const line2 = mathUtil.createLine1(points[2], points[3]);
  317. const fontStart1 = mathUtil.getDisPointsLine(
  318. line1,
  319. pt,
  320. fontWidth1 / 2,
  321. fontWidth1 / 2
  322. );
  323. const fontStart2 = mathUtil.getDisPointsLine(
  324. line2,
  325. {
  326. x: (points[2].x + points[3].x) / 2,
  327. y: (points[2].y + points[3].y) / 2,
  328. },
  329. fontWidth2 / 2,
  330. fontWidth2 / 2
  331. );
  332. if (fontStart1.newpoint1.x < fontStart1.newpoint2.x) {
  333. this.context.fillText(
  334. title,
  335. fontStart1.newpoint1.x,
  336. fontStart1.newpoint1.y
  337. );
  338. if (geometry.des) {
  339. if (measureService.unit == "ft" && geometry.unit == "m") {
  340. let area = uoMService.convert(
  341. geometry.des,
  342. "area",
  343. void 0,
  344. "imperial",
  345. 0.01,
  346. false
  347. );
  348. this.context.fillText(
  349. parseFloat(area.replace(",", "")).toFixed(2),
  350. fontStart2.newpoint1.x + fontSize / 1.5,
  351. fontStart2.newpoint1.y
  352. );
  353. } else if (measureService.unit == "m" && geometry.unit == "ft") {
  354. let area = uoMService.convertBack(
  355. geometry.des,
  356. "area",
  357. 7,
  358. "imperial",
  359. 0.01,
  360. false
  361. );
  362. this.context.fillText(
  363. parseFloat(area.replace(",", "")).toFixed(2),
  364. fontStart2.newpoint1.x + fontSize / 1.5,
  365. fontStart2.newpoint1.y
  366. );
  367. } else if (geometry.unit == "m") {
  368. this.context.fillText(
  369. parseFloat(geometry.des).toFixed(2) + "m²",
  370. fontStart2.newpoint1.x,
  371. fontStart2.newpoint1.y
  372. );
  373. } else if (geometry.unit == "ft") {
  374. this.context.fillText(
  375. parseFloat(geometry.des.replace(",", "")).toFixed(2) + "ft²",
  376. fontStart2.newpoint1.x,
  377. fontStart2.newpoint1.y
  378. );
  379. }
  380. }
  381. } else {
  382. this.context.fillText(
  383. title,
  384. fontStart1.newpoint2.x,
  385. fontStart1.newpoint2.y
  386. );
  387. if (geometry.des) {
  388. if (measureService.unit == "ft" && geometry.unit == "m") {
  389. let area = uoMService.convert(
  390. geometry.des,
  391. "area",
  392. void 0,
  393. "imperial",
  394. 0.01,
  395. false
  396. );
  397. this.context.fillText(
  398. parseFloat(area.replace(",", "")).toFixed(2),
  399. fontStart2.newpoint2.x + fontSize / 1.5,
  400. fontStart2.newpoint2.y
  401. );
  402. } else if (measureService.unit == "m" && geometry.unit == "ft") {
  403. let area = uoMService.convertBack(
  404. geometry.des,
  405. "area",
  406. 7,
  407. "imperial",
  408. 0.01,
  409. false
  410. );
  411. this.context.fillText(
  412. parseFloat(area.replace(",", "")).toFixed(2),
  413. fontStart2.newpoint2.x + fontSize / 1.5,
  414. fontStart2.newpoint2.y
  415. );
  416. } else if (geometry.unit == "m") {
  417. this.context.fillText(
  418. parseFloat(geometry.des).toFixed(2) + "m²",
  419. fontStart2.newpoint2.x,
  420. fontStart2.newpoint2.y
  421. );
  422. } else if (geometry.unit == "ft") {
  423. this.context.fillText(
  424. parseFloat(geometry.des.replace(",", "")).toFixed(2) + "ft²",
  425. fontStart2.newpoint2.x,
  426. fontStart2.newpoint2.y
  427. );
  428. }
  429. }
  430. }
  431. }
  432. this.context.restore();
  433. }
  434. drawCircle(element) {
  435. // let radius = null;
  436. // const twoPi = Math.PI * 2;
  437. // const pt = coordinate.getScreenXY(element);
  438. // this.context.save();
  439. // if (element.name == ElementEvents.StartAddRoad) {
  440. // radius = Style.Element.StartAddRoad.radius * coordinate.ratio;
  441. // this.context.strokeStyle = Style.Element.StartAddRoad.strokeStyle;
  442. // this.context.fillStyle = Style.Element.StartAddRoad.fillStyle;
  443. // } else if (element.name == ElementEvents.StartSymbolPoints) {
  444. // radius = Style.Element.StartSymbolPoints.radius * coordinate.ratio;
  445. // this.context.strokeStyle = Style.Element.StartSymbolPoints.strokeStyle;
  446. // this.context.fillStyle = Style.Element.StartSymbolPoints.fillStyle;
  447. // } else if (element.name == ElementEvents.EndSymbolPoints) {
  448. // radius = Style.Element.EndSymbolPoints.radius * coordinate.ratio;
  449. // this.context.strokeStyle = Style.Element.EndSymbolPoints.strokeStyle;
  450. // this.context.fillStyle = Style.Element.EndSymbolPoints.fillStyle;
  451. // } else if (element.name == "pano") {
  452. // radius = Style.Pano.radius * coordinate.ratio;
  453. // this.context.strokeStyle = Style.Pano.strokeStyle;
  454. // this.context.fillStyle = Style.Pano.fillStyle;
  455. // this.context.lineWidth = Style.Pano.lineWidth;
  456. // }
  457. // this.context.beginPath();
  458. // this.context.arc(pt.x, pt.y, radius, 0, twoPi, true);
  459. // this.context.stroke();
  460. // this.context.fill();
  461. // this.context.restore();
  462. }
  463. drawLine(element) {
  464. let start = coordinate.getScreenXY(element.start);
  465. let end = coordinate.getScreenXY(element.end);
  466. this.context.save();
  467. if (element.name == ElementEvents.VCheckLinesX) {
  468. this.context.lineWidth =
  469. Style.Element.VCheckLinesX.lineWidth * coordinate.ratio;
  470. this.context.strokeStyle = Style.Element.VCheckLinesX.strokeStyle;
  471. this.context.setLineDash([3, 2, 2]);
  472. start.y = 0;
  473. end.y = this.context.canvas.clientHeight;
  474. } else if (element.name == ElementEvents.VCheckLinesY) {
  475. this.context.lineWidth =
  476. Style.Element.VCheckLinesY.lineWidth * coordinate.ratio;
  477. this.context.strokeStyle = Style.Element.VCheckLinesY.strokeStyle;
  478. this.context.setLineDash([3, 2, 2]);
  479. start.x = 0;
  480. end.x = this.context.canvas.clientWidth;
  481. } else if (element.name == ElementEvents.NewRoad) {
  482. this.context.lineWidth =
  483. Style.Element.NewRoad.lineWidth * coordinate.ratio;
  484. this.context.strokeStyle = Style.Element.NewRoad.strokeStyle;
  485. if (element.state == "error") {
  486. this.context.strokeStyle = Style.Element.NewRoad.errorStrokeStyle;
  487. }
  488. } else if (element.name == ElementEvents.CheckLinesX) {
  489. this.context.lineWidth =
  490. Style.Element.CheckLinesX.lineWidth * coordinate.ratio;
  491. this.context.strokeStyle = Style.Element.CheckLinesX.strokeStyle;
  492. this.context.setLineDash([3, 2, 2]);
  493. } else if (element.name == ElementEvents.CheckLinesY) {
  494. this.context.lineWidth =
  495. Style.Element.CheckLinesY.lineWidth * coordinate.ratio;
  496. this.context.strokeStyle = Style.Element.CheckLinesY.strokeStyle;
  497. this.context.setLineDash([3, 2, 2]);
  498. } else if (element.name == ElementEvents.SignLine1) {
  499. this.context.lineWidth =
  500. Style.Element.SignLine1.lineWidth * coordinate.ratio;
  501. this.context.strokeStyle = Style.Element.SignLine1.strokeStyle;
  502. this.context.setLineDash([3, 2, 2]);
  503. } else if (element.name == ElementEvents.SignLine2) {
  504. this.context.lineWidth =
  505. Style.Element.SignLine2.lineWidth * coordinate.ratio;
  506. this.context.strokeStyle = Style.Element.SignLine2.strokeStyle;
  507. this.context.setLineDash([3, 2, 2]);
  508. }
  509. this.context.beginPath();
  510. this.context.moveTo(start.x, start.y);
  511. this.context.lineTo(end.x, end.y);
  512. this.context.stroke();
  513. this.context.restore();
  514. // if (element.name == ElementEvents.NewRoad) {
  515. // //添加测量值
  516. // this.drawMeasureTxt(element.start, element.end);
  517. // }
  518. // this.context.save();
  519. // this.context.lineWidth = Style.Point.lineWidth * coordinate.ratio;
  520. // this.context.strokeStyle = Style.Point.strokeStyle;
  521. // this.context.fillStyle = Style.Point.fillStyle;
  522. // let radius = Style.Point.radius;
  523. // this.context.beginPath();
  524. // this.context.arc(
  525. // start.x,
  526. // start.y,
  527. // radius * coordinate.ratio,
  528. // 0,
  529. // Math.PI * 2,
  530. // true
  531. // );
  532. // this.context.stroke();
  533. // this.context.fill();
  534. // this.context.restore();
  535. }
  536. //由多个点构成,里面的坐标都已经是屏幕坐标
  537. drawMeasure(points, dir, styleType) {
  538. this.context.save();
  539. this.context.strokeStyle = Style.Measure.strokeStyle;
  540. this.context.lineWidth = Style.Measure.lineWidth * coordinate.ratio;
  541. if (styleType) {
  542. if (styleType == "style-1") {
  543. this.context.lineWidth =
  544. Style.DownLoad.style1.Measure.lineWidth * coordinate.ratio;
  545. this.context.strokeStyle = Style.DownLoad.style1.Measure.strokeStyle;
  546. } else if (styleType == "style-2") {
  547. this.context.lineWidth =
  548. Style.DownLoad.style1.Measure.lineWidth * coordinate.ratio;
  549. this.context.strokeStyle = Style.DownLoad.style1.Measure.strokeStyle;
  550. } else if (styleType == "style-3") {
  551. this.context.lineWidth =
  552. Style.DownLoad.style1.Measure.lineWidth * coordinate.ratio;
  553. this.context.strokeStyle = Style.DownLoad.style1.Measure.strokeStyle;
  554. } else if (styleType == "style-4") {
  555. this.context.lineWidth =
  556. Style.DownLoad.style1.Measure.lineWidth * coordinate.ratio;
  557. this.context.strokeStyle = Style.DownLoad.style1.Measure.strokeStyle;
  558. }
  559. }
  560. for (let i = 0; i < points.length - 1; ++i) {
  561. let start = coordinate.getScreenXY(points[i]);
  562. let end = coordinate.getScreenXY(points[i + 1]);
  563. let angle = 0;
  564. if (dir == "top") {
  565. start.y = measureService.region.top * coordinate.ratio;
  566. end.y = measureService.region.top * coordinate.ratio;
  567. } else if (dir == "bottom") {
  568. start.y = measureService.region.bottom * coordinate.ratio;
  569. end.y = measureService.region.bottom * coordinate.ratio;
  570. } else if (dir == "left") {
  571. start.x = measureService.region.left * coordinate.ratio;
  572. end.x = measureService.region.left * coordinate.ratio;
  573. angle = (-90 / 180) * Math.PI;
  574. } else if (dir == "right") {
  575. start.x = measureService.region.right * coordinate.ratio;
  576. end.x = measureService.region.right * coordinate.ratio;
  577. angle = (90 / 180) * Math.PI;
  578. }
  579. let line = mathUtil.createLine1(start, end);
  580. if (line == null) {
  581. continue;
  582. }
  583. let lines = mathUtil.getParallelLineForDistance(
  584. line,
  585. 6 * coordinate.ratio
  586. );
  587. let start1 = mathUtil.getJoinLinePoint(start, lines.line1);
  588. let end1 = mathUtil.getJoinLinePoint(end, lines.line1);
  589. let start2 = mathUtil.getJoinLinePoint(start, lines.line2);
  590. let end2 = mathUtil.getJoinLinePoint(end, lines.line2);
  591. this.context.beginPath();
  592. this.context.moveTo(start1.x, start1.y);
  593. this.context.lineTo(start2.x, start2.y);
  594. this.context.stroke();
  595. this.context.beginPath();
  596. this.context.moveTo(end1.x, end1.y);
  597. this.context.lineTo(end2.x, end2.y);
  598. this.context.stroke();
  599. let mid = {
  600. x: (start.x + end.x) / 2,
  601. y: (start.y + end.y) / 2,
  602. };
  603. let vLine = mathUtil.getVerticalLine(line, mid);
  604. lines = mathUtil.getParallelLineForDistance(vLine, 22 * coordinate.ratio);
  605. let join1 = mathUtil.getIntersectionPoint(line, lines.line1);
  606. let join2 = mathUtil.getIntersectionPoint(line, lines.line2);
  607. if (
  608. mathUtil.getDistance(start, join1) < mathUtil.getDistance(start, mid)
  609. ) {
  610. let measureValue = mathUtil.getDistance(points[i], points[i + 1]);
  611. if (measureService.unit == "ft") {
  612. measureValue =
  613. " " +
  614. uoMService.convert(
  615. measureValue,
  616. "distance",
  617. void 0,
  618. "imperial",
  619. 0.01,
  620. true
  621. ) +
  622. " ";
  623. //measureValue = mathUtil.getFixed(measureValue / measureService.ftUnit, 2) + 'ft'
  624. } else {
  625. measureValue = mathUtil.getFixed(measureValue, 2) + "m";
  626. }
  627. if (
  628. mathUtil.getDistance(start, end) >
  629. this.context.measureText(measureValue).width
  630. ) {
  631. this.context.beginPath();
  632. this.context.moveTo(start.x, start.y);
  633. this.context.lineTo(join1.x, join1.y);
  634. this.context.stroke();
  635. this.context.beginPath();
  636. this.context.moveTo(join2.x, join2.y);
  637. this.context.lineTo(end.x, end.y);
  638. this.context.stroke();
  639. this.context.save();
  640. if (coordinate.ratio == Constant.ratio) {
  641. this.context.font = "36px Microsoft YaHei";
  642. } else {
  643. this.context.font = "12px Microsoft YaHei";
  644. }
  645. if (styleType == "style-1" || styleType == "style-3") {
  646. this.context.fillStyle = "#000000";
  647. this.context.strokeStyle = "#000000";
  648. } else {
  649. this.context.fillStyle = "#FFFFFF";
  650. this.context.strokeStyle = "#FFFFFF";
  651. }
  652. this.context.textAlign = "center";
  653. this.context.textBaseline = "middle";
  654. this.context.miterLimit = 10;
  655. this.context.direction = "ltr";
  656. if (angle) {
  657. this.context.translate(mid.x, mid.y);
  658. this.context.rotate(angle);
  659. this.context.fillText(measureValue, 0, 0);
  660. } else {
  661. this.context.fillText(measureValue, mid.x, mid.y);
  662. }
  663. this.context.restore();
  664. } else {
  665. this.context.beginPath();
  666. this.context.moveTo(start.x, start.y);
  667. this.context.lineTo(end.x, end.y);
  668. this.context.stroke();
  669. }
  670. } else {
  671. let measureValue = mathUtil.getDistance(points[i], points[i + 1]);
  672. if (measureService.unit == "ft") {
  673. //measureValue = mathUtil.getFixed(measureValue / measureService.ftUnit, 2) + 'ft'
  674. measureValue =
  675. " " +
  676. uoMService.convert(
  677. measureValue,
  678. "distance",
  679. void 0,
  680. "imperial",
  681. 0.01,
  682. true
  683. ) +
  684. " ";
  685. } else {
  686. measureValue = mathUtil.getFixed(measureValue, 2) + "m";
  687. }
  688. if (
  689. mathUtil.getDistance(start, end) >
  690. this.context.measureText(measureValue).width
  691. ) {
  692. this.context.beginPath();
  693. this.context.moveTo(start.x, start.y);
  694. this.context.lineTo(join2.x, join2.y);
  695. this.context.stroke();
  696. this.context.beginPath();
  697. this.context.moveTo(join1.x, join1.y);
  698. this.context.lineTo(end.x, end.y);
  699. this.context.stroke();
  700. this.context.save();
  701. if (coordinate.ratio == Constant.ratio) {
  702. this.context.font = "36px Microsoft YaHei";
  703. } else {
  704. this.context.font = "12px Microsoft YaHei";
  705. }
  706. if (styleType == "style-1" || styleType == "style-3") {
  707. this.context.fillStyle = "#000000";
  708. this.context.strokeStyle = "#000000";
  709. } else {
  710. this.context.fillStyle = "#FFFFFF";
  711. this.context.strokeStyle = "#FFFFFF";
  712. }
  713. this.context.textAlign = "center";
  714. this.context.textBaseline = "middle";
  715. this.context.miterLimit = 10;
  716. this.context.direction = "ltr";
  717. if (angle) {
  718. this.context.translate(mid.x, mid.y);
  719. this.context.rotate(angle);
  720. this.context.fillText(measureValue, 0, 0);
  721. } else {
  722. this.context.fillText(measureValue, mid.x, mid.y);
  723. }
  724. this.context.restore();
  725. } else {
  726. this.context.beginPath();
  727. this.context.moveTo(start.x, start.y);
  728. this.context.lineTo(end.x, end.y);
  729. this.context.stroke();
  730. }
  731. }
  732. }
  733. this.context.restore();
  734. }
  735. drawMeasureTxt(startPoint, endPoint) {
  736. const _startPoint = coordinate.getScreenXY(startPoint);
  737. const _endPoint = coordinate.getScreenXY(endPoint);
  738. const measureInterval = 10;
  739. const line = mathUtil.createLine1(_startPoint, _endPoint);
  740. if (line == null) {
  741. return;
  742. }
  743. let mid = {
  744. x: (_startPoint.x + _endPoint.x) / 2,
  745. y: (_startPoint.y + _endPoint.y) / 2,
  746. };
  747. const lines = mathUtil.getParallelLineForDistance(line, measureInterval);
  748. let pLine = null;
  749. let mid1 = mathUtil.getJoinLinePoint(mid, lines.line1);
  750. let mid2 = mathUtil.getJoinLinePoint(mid, lines.line2);
  751. if (mid.y < mid1.y) {
  752. mathUtil.clonePoint(mid, mid2);
  753. pLine = lines.line2;
  754. } else {
  755. mathUtil.clonePoint(mid, mid1);
  756. pLine = lines.line1;
  757. }
  758. let measureDistance = mathUtil.getDistance(startPoint, endPoint);
  759. if (measureService.unit == "ft") {
  760. //measureDistance = mathUtil.getFixed(measureDistance / measureService.ftUnit, 2) + 'ft'
  761. measureDistance =
  762. " " +
  763. uoMService.convert(
  764. measureDistance,
  765. "distance",
  766. void 0,
  767. "imperial",
  768. 0.01,
  769. true
  770. ) +
  771. " ";
  772. } else {
  773. measureDistance = mathUtil.getFixed(measureDistance, 2) + "m";
  774. }
  775. const fontWidth = this.context.measureText(measureDistance).width;
  776. let vLine = mathUtil.getLineForPoint(line, mid);
  777. const vLines = mathUtil.getParallelLineForDistance(vLine, fontWidth / 2);
  778. let startJoin = mathUtil.getIntersectionPoint(vLines.line1, pLine);
  779. startJoin = {
  780. x: Math.round(startJoin.x),
  781. y: Math.round(startJoin.y),
  782. };
  783. let endJoin = mathUtil.getIntersectionPoint(vLines.line2, pLine);
  784. endJoin = {
  785. x: Math.round(endJoin.x),
  786. y: Math.round(endJoin.y),
  787. };
  788. if (startJoin.x < endJoin.x) {
  789. mathUtil.clonePoint(mid, startJoin);
  790. } else if (startJoin.x > endJoin.x) {
  791. mathUtil.clonePoint(mid, endJoin);
  792. } else if (startJoin.y < endJoin.y) {
  793. mathUtil.clonePoint(mid, startJoin);
  794. } else {
  795. mathUtil.clonePoint(mid, endJoin);
  796. }
  797. //const fontStart = mathUtil.getDisPointsLine(line, mid, fontWidth / 2, fontWidth / 2)
  798. // let a1, a2
  799. let angle = null;
  800. if (typeof line.a !== "undefined") {
  801. angle = Math.atan(line.a);
  802. } else if (line.hasOwnProperty("x")) {
  803. angle = Math.PI / 2;
  804. } else {
  805. angle = 0;
  806. }
  807. this.context.save();
  808. this.context.fillStyle = Style.Measure.txt;
  809. this.context.translate(mid.x, mid.y);
  810. this.context.rotate(angle);
  811. this.context.fillText(measureDistance, 0, 0);
  812. /*
  813. if (fontStart.newpoint1.x > fontStart.newpoint2.x) {
  814. this.context.translate(mid.x, mid.y)
  815. this.context.rotate(angle)
  816. //this.context.strokeText(measureDistance, 0, 0);
  817. this.context.fillText(measureDistance, 0, 0)
  818. // a1 = fontStart.newpoint2
  819. // a2 = fontStart.newpoint1
  820. } else if (fontStart.newpoint1.x < fontStart.newpoint2.x) {
  821. this.context.translate(mid.x, mid.y)
  822. this.context.rotate(angle)
  823. //this.context.strokeText(measureDistance, 0, 0);
  824. this.context.fillText(measureDistance, 0, 0)
  825. // a1 = fontStart.newpoint1
  826. // a2 = fontStart.newpoint2
  827. } else if (fontStart.newpoint1.y < fontStart.newpoint2.y) {
  828. this.context.translate(mid.x, mid.y)
  829. this.context.rotate(angle)
  830. //this.context.strokeText(measureDistance, 0, 0);
  831. this.context.fillText(measureDistance, 0, 0)
  832. // a2 = fontStart.newpoint2
  833. // a1 = fontStart.newpoint1
  834. } else {
  835. this.context.translate(mid.x, mid.y)
  836. this.context.rotate(angle)
  837. //this.context.strokeText(measureDistance, 0, 0);
  838. this.context.fillText(measureDistance, 0, 0)
  839. // a2 = fontStart.newpoint1
  840. // a1 = fontStart.newpoint2
  841. }
  842. */
  843. this.context.restore();
  844. }
  845. setCanvasStyle(style) {
  846. for (const key in style) {
  847. if (key != "isFill" && key != "isStroke") {
  848. this.context[key] = style[key];
  849. }
  850. }
  851. }
  852. /*************************************************************************************家具**********************************************************************************************/
  853. /***************************************************************************************************************************************************************************************/
  854. }
  855. const draw = new Draw();
  856. export { draw };