Draw.js 34 KB

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