Draw.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  1. import { dataService } from "../Service/DataService.js";
  2. import { stateService } from "../Service/StateService.js";
  3. import { coordinate } from "../Coordinate.js";
  4. import Style from "@/graphic/CanvasStyle/index.js";
  5. import { mathUtil } from "../Util/MathUtil.js";
  6. import { elementService } from "../Service/ElementService.js";
  7. import UIEvents from "@/graphic/enum/UIEvents.js";
  8. import VectorCategory from "@/graphic/enum/VectorCategory.js";
  9. import Settings from "@/graphic/Settings.js";
  10. import SVGIcons from "../CanvasStyle/ImageLabels/SVGIcons";
  11. import VectorStyle from "@/graphic/enum/VectorStyle.js";
  12. import VectorWeight from "@/graphic/enum/VectorWeight.js";
  13. const imgCache = {};
  14. const help = {
  15. getVectorStyle(vector, geoType = vector.geoType) {
  16. const geoId = vector?.vectorId;
  17. if (!geoId || Settings.screenMode) {
  18. return [Style[geoType], undefined];
  19. }
  20. const itemsEntry = [
  21. [stateService.getSelectItem(), "Select"],
  22. [stateService.getDraggingItem(), "Dragging"],
  23. [stateService.getFocusItem(), "Focus"],
  24. ];
  25. let currentAttr;
  26. return [
  27. itemsEntry.reduce((prev, [item, attr]) => {
  28. if (!item) return prev;
  29. const selected =
  30. geoId === item.vectorId ||
  31. (item.parent && Object.keys(item.parent).some((id) => id === geoId));
  32. if (selected && Style[attr]) {
  33. const style = Style[attr][geoType] || Style[attr][vector.category];
  34. if (style) {
  35. currentAttr = attr;
  36. return style;
  37. }
  38. }
  39. return prev;
  40. }, Style[geoType]),
  41. currentAttr,
  42. ];
  43. },
  44. setStyle(ctx, styles) {
  45. for (const style in styles) {
  46. if (typeof styles[style] === "function") {
  47. styles[style](ctx, vector);
  48. } else {
  49. ctx[style] = styles[style];
  50. }
  51. }
  52. },
  53. setVectorStyle(ctx, vector, geoType = vector.geoType) {
  54. let styles, attr;
  55. if (Array.isArray(geoType)) {
  56. for (const type of geoType) {
  57. [styles, attr] = help.getVectorStyle(vector, type);
  58. if (styles) {
  59. break;
  60. }
  61. }
  62. } else {
  63. [styles, attr] = help.getVectorStyle(vector, geoType);
  64. }
  65. help.setStyle(ctx, styles);
  66. return [styles, attr];
  67. },
  68. transformCoves(lines) {
  69. return lines.map((line) =>
  70. line.map((line) => ({
  71. start: coordinate.getScreenXY(line.start),
  72. end: coordinate.getScreenXY(line.end),
  73. controls: line.controls.map(coordinate.getScreenXY.bind(coordinate)),
  74. }))
  75. );
  76. },
  77. drawCove(ctx, curve) {
  78. if (curve.controls.length === 1) {
  79. ctx.quadraticCurveTo(
  80. curve.controls[0].x,
  81. curve.controls[0].y,
  82. curve.end.x,
  83. curve.end.y
  84. );
  85. } else {
  86. ctx.bezierCurveTo(
  87. curve.controls[0].x,
  88. curve.controls[0].y,
  89. curve.controls[1].x,
  90. curve.controls[1].y,
  91. curve.end.x,
  92. curve.end.y
  93. );
  94. }
  95. },
  96. drawCoves(ctx, coves) {
  97. for (const curve of coves) {
  98. ctx.beginPath();
  99. ctx.moveTo(curve.start.x, curve.start.y);
  100. help.drawCove(ctx, curve)
  101. ctx.stroke();
  102. }
  103. },
  104. getReal(data) {
  105. return (data * coordinate.ratio * coordinate.zoom) / coordinate.defaultZoom;
  106. },
  107. getImage(src) {
  108. if (imgCache[src]) {
  109. return imgCache[src];
  110. }
  111. const img = new Image();
  112. img.src = src;
  113. return (imgCache[src] = new Promise((resolve) => {
  114. img.onload = () => {
  115. resolve(img);
  116. };
  117. }));
  118. },
  119. getTextCenter(ctx, txt) {
  120. const text = ctx.measureText(txt);
  121. const height = text.actualBoundingBoxAscent + text.actualBoundingBoxDescent;
  122. return {
  123. width: text.width,
  124. height,
  125. x: text.width / 2,
  126. y: -height / 2,
  127. };
  128. },
  129. // 绘制圆角矩形
  130. roundRect(ctx, x, y, width, height, radius) {
  131. ctx.beginPath();
  132. ctx.moveTo(x + radius, y);
  133. ctx.lineTo(x + width - radius, y);
  134. ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
  135. ctx.lineTo(x + width, y + height - radius);
  136. ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
  137. ctx.lineTo(x + radius, y + height);
  138. ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
  139. ctx.lineTo(x, y + radius);
  140. ctx.quadraticCurveTo(x, y, x + radius, y);
  141. ctx.closePath();
  142. },
  143. getRealDistance(p1, p2) {
  144. return (
  145. Math.round((mathUtil.getDistance(p1, p2) * coordinate.res * 100) / coordinate.ratio) / 100
  146. );
  147. },
  148. getPerpendicularPoint(p1, p2, p3, d) {
  149. if (p1.x === p2.x) {
  150. return { x: p3.x + d, y: p3.y };
  151. } else if (p1.y === p2.y) {
  152. return { x: p3.x, y: p3.y + d };
  153. }
  154. // 计算通过 p1 和 p2 的直线的斜率和截距
  155. const slope = (p2.y - p1.y) / (p2.x - p1.x);
  156. const intercept = p1.y - slope * p1.x;
  157. // 计算垂直线的斜率和截距
  158. const perpendicularSlope = -1 / slope;
  159. const perpendicularIntercept = p3.y - perpendicularSlope * p3.x;
  160. // 计算垂足点 p0
  161. const x =
  162. (perpendicularIntercept - intercept) / (slope - perpendicularSlope);
  163. const y = slope * x + intercept;
  164. const p0 = { x, y };
  165. // 计算点 p4
  166. const distance = d; // 指定距离
  167. const dx = distance / Math.sqrt(1 + perpendicularSlope ** 2);
  168. const dy = perpendicularSlope * dx;
  169. return { x: p0.x + dx, y: p0.y + dy };
  170. },
  171. drawLineText(ctx, start, end, text, style) {
  172. if (start.x > end.x) {
  173. [start, end] = [end, start];
  174. }
  175. const angle =
  176. (Math.atan2(end.y - start.y, end.x - start.x) * 180) / Math.PI;
  177. const center = mathUtil.lineCenter(start, end);
  178. ctx.save();
  179. ctx.translate(center.x, center.y);
  180. ctx.rotate((angle * Math.PI) / 180);
  181. ctx.font = `${(style.fontSize || 10) * coordinate.ratio}px Microsoft YaHei`;
  182. const textCenter = help.getTextCenter(ctx, text);
  183. const padding = style.padding;
  184. help.roundRect(
  185. ctx,
  186. -textCenter.x - padding,
  187. textCenter.y - padding,
  188. textCenter.width + 2 * padding,
  189. textCenter.height + 2 * padding,
  190. textCenter.height / 2 + padding
  191. );
  192. ctx.fillStyle = style.backColor;
  193. ctx.fill();
  194. ctx.fillStyle = style.fillColor;
  195. ctx.fillText(text, -textCenter.x, -textCenter.y);
  196. ctx.restore();
  197. },
  198. isTriangleClockwise(p1, p2, p3) {
  199. const crossProduct = (p2.x - p1.x) * (p3.y - p1.y) - (p3.x - p1.x) * (p2.y - p1.y);
  200. return crossProduct < 0;
  201. },
  202. drawStyleLine(ctx, line, style = VectorStyle.SingleSolidLine, weight = VectorStyle.Thinning) {
  203. ctx.save();
  204. style = style || VectorStyle.SingleSolidLine
  205. ctx.beginPath();
  206. const lineWidth = Settings.lineWidth * (ctx.lineWidth || 1) * (weight === VectorWeight.Bold ? 2 : 1);
  207. switch (style) {
  208. case VectorStyle.PointDrawLine:
  209. case VectorStyle.SingleDashedLine:
  210. case VectorStyle.SingleSolidLine:
  211. ctx.lineWidth = lineWidth
  212. if (style === VectorStyle.SingleDashedLine) {
  213. ctx.setLineDash([8 * coordinate.ratio, 8 * coordinate.ratio]);
  214. } else if (style === VectorStyle.PointDrawLine) {
  215. ctx.setLineDash([6 * coordinate.ratio, 6* coordinate.ratio, 2 * coordinate.ratio]);
  216. }
  217. ctx.moveTo(line[0].x, line[0].y);
  218. ctx.lineTo(line[1].x, line[1].y);
  219. break
  220. // 单实线
  221. case VectorStyle.DoubleDashedLine:
  222. case VectorStyle.DoubleSolidLine:
  223. if (style === VectorStyle.DoubleDashedLine) {
  224. ctx.setLineDash([8 * coordinate.ratio, 8 * coordinate.ratio]);
  225. }
  226. const pd1 = help.getPerpendicularPoint(
  227. line[0], line[1], line[0], 4 * coordinate.ratio,
  228. )
  229. const pd2 = help.getPerpendicularPoint(
  230. line[0], line[1], line[1], 4 * coordinate.ratio,
  231. )
  232. const pd3 = help.getPerpendicularPoint(
  233. line[0], line[1], line[0], -4 * coordinate.ratio,
  234. )
  235. const pd4 = help.getPerpendicularPoint(
  236. line[0], line[1], line[1], -4 * coordinate.ratio,
  237. )
  238. ctx.moveTo(pd1.x, pd1.y);
  239. ctx.lineTo(pd2.x, pd2.y);
  240. ctx.stroke();
  241. ctx.moveTo(pd3.x, pd3.y);
  242. ctx.lineTo(pd4.x, pd4.y);
  243. break
  244. case VectorStyle.BrokenLine:
  245. const ldis = 5 * coordinate.ratio
  246. if (mathUtil.getDistance(...line) < ldis * 2) {
  247. ctx.moveTo(line[0].x, line[0].y);
  248. ctx.lineTo(line[1].x, line[1].y);
  249. } else {
  250. const start = mathUtil.translate(line[0], line[1], line[0], ldis)
  251. const end = mathUtil.translate(line[0], line[1], line[1], -ldis)
  252. const lineDis = mathUtil.getDistance(start, end)
  253. const len = Math.ceil(lineDis / (6 * coordinate.ratio))
  254. const split = lineDis / len
  255. const points = [start]
  256. let temp = start
  257. for (let i = 0; i < len ; i++) {
  258. temp = mathUtil.translate(temp, line[1], temp, split)
  259. points.push(temp)
  260. }
  261. ctx.moveTo(line[0].x, line[0].y);
  262. ctx.lineTo(start.x, start.y);
  263. for (let i = 0; i < points.length - 1; i++) {
  264. const vTop = help.getPerpendicularPoint(
  265. points[i],
  266. points[i + 1],
  267. mathUtil.lineCenter(points[i], points[i + 1]),
  268. (split * ((i%2) ? -1 : 1)) / 2
  269. )
  270. ctx.lineTo(vTop.x, vTop.y);
  271. }
  272. ctx.lineTo(end.x, end.y);
  273. ctx.lineTo(line[1].x, line[1].y);
  274. }
  275. ctx.lineWidth = lineWidth
  276. break
  277. case VectorStyle.Greenbelt:
  278. const dis = 4 * coordinate.ratio
  279. const size = 8 * coordinate.ratio
  280. const p1 = help.getPerpendicularPoint(
  281. line[0], line[1], line[0], dis
  282. )
  283. const p2 = help.getPerpendicularPoint(
  284. line[0], line[1], line[1], dis
  285. )
  286. const p3 = help.getPerpendicularPoint(
  287. p1, p2, p2, size
  288. )
  289. const p4 = help.getPerpendicularPoint(
  290. p1, p2, p1, size
  291. )
  292. ctx.beginPath()
  293. ctx.lineWidth = lineWidth
  294. ctx.moveTo(line[0].x, line[0].y);
  295. ctx.lineTo(line[1].x, line[1].y);
  296. ctx.stroke();
  297. ctx.beginPath()
  298. ctx.moveTo(p4.x, p4.y);
  299. ctx.lineTo(p1.x, p1.y);
  300. ctx.lineTo(p2.x, p2.y);
  301. ctx.lineTo(p3.x, p3.y);
  302. ctx.stroke();
  303. const rdis = 6 * coordinate.ratio
  304. const lineDis = mathUtil.getDistance(p3, p4)
  305. const len = Math.ceil(lineDis / rdis)
  306. const split = lineDis / len
  307. const points = [p3]
  308. const geo = [p4, {...p4, x: 999}, p3]
  309. let angle = (mathUtil.Angle1(...geo) / 180) * Math.PI
  310. const isClock = help.isTriangleClockwise(...geo) || angle === 0
  311. angle = isClock ? -angle : angle
  312. let temp = p3
  313. for (let i = 0; i < len; i++) {
  314. temp = mathUtil.translate(temp, p4, temp, split)
  315. points.push(temp)
  316. }
  317. for (let i = 0; i < points.length - 1; i++) {
  318. const center = mathUtil.lineCenter(points[i], points[i+1])
  319. ctx.beginPath()
  320. ctx.arc(center.x, center.y, split / 2, angle, angle + Math.PI, !isClock)
  321. ctx.stroke();
  322. }
  323. ctx.lineWidth = lineWidth
  324. break
  325. }
  326. ctx.stroke();
  327. ctx.restore();
  328. },
  329. };
  330. export default class Draw {
  331. constructor() {
  332. this.canvas = null;
  333. this.context = null;
  334. }
  335. initContext(canvas) {
  336. if (canvas) {
  337. this.canvas = canvas;
  338. this.context = canvas.getContext("2d");
  339. } else {
  340. this.context = null;
  341. this.canvas = null;
  342. }
  343. }
  344. clear() {
  345. this.context.clearRect(
  346. 0,
  347. 0,
  348. this.context.canvas.width,
  349. this.context.canvas.height
  350. );
  351. }
  352. drawBackGroundImg(vector) {
  353. if (!vector.display) {
  354. return;
  355. }
  356. const img = vector.imageData;
  357. const width = help.getReal(img.width);
  358. const height = help.getReal(img.height);
  359. const center = coordinate.getScreenXY(vector.center);
  360. this.context.save();
  361. this.context.drawImage(
  362. img,
  363. center.x - width / 2,
  364. center.y - height / 2,
  365. width,
  366. height
  367. );
  368. this.context.restore();
  369. }
  370. drawGrid(startX, startY, w, h, step1, step2) {
  371. this.context.save();
  372. this.context.beginPath();
  373. for (var x = startX; x <= w; x += step1) {
  374. this.context.moveTo(x, 0);
  375. this.context.lineTo(x, h);
  376. }
  377. for (var y = startY; y <= h; y += step1) {
  378. this.context.moveTo(0, y);
  379. this.context.lineTo(w, y);
  380. }
  381. this.context.strokeStyle = "rgba(0,0,0,0.1)";
  382. this.context.lineWidth = 0.5 * coordinate.ratio;
  383. this.context.stroke();
  384. this.context.beginPath();
  385. for (var x = startX; x <= w; x += step2) {
  386. this.context.moveTo(x, 0);
  387. this.context.lineTo(x, h);
  388. }
  389. for (var y = startY; y <= h; y += step2) {
  390. this.context.moveTo(0, y);
  391. this.context.lineTo(w, y);
  392. }
  393. this.context.strokeStyle = "rgba(0,0,0,0.2)";
  394. this.context.lineWidth = 1 * coordinate.ratio;
  395. this.context.stroke();
  396. this.context.restore();
  397. }
  398. drawRoad(vector, isTemp) {
  399. if (!isTemp && vector.display && vector.way !== "oneWay") {
  400. const ctx = this.context;
  401. const draw = (midDivide) => {
  402. const startScreen = coordinate.getScreenXY(midDivide.start);
  403. const endScreen = coordinate.getScreenXY(midDivide.end);
  404. ctx.beginPath();
  405. if (label) {
  406. help.setStyle(ctx, Style.Focus.Road)
  407. }
  408. ctx.moveTo(startScreen.x, startScreen.y);
  409. ctx.lineTo(endScreen.x, endScreen.y);
  410. ctx.stroke();
  411. };
  412. ctx.save();
  413. const [_, label] = help.setVectorStyle(ctx, vector);
  414. vector.midDivide.leftMidDivide && draw(vector.midDivide.leftMidDivide);
  415. vector.midDivide.rightMidDivide && draw(vector.midDivide.rightMidDivide);
  416. ctx.restore();
  417. }
  418. if (import.meta.env.DEV && !isTemp) {
  419. const startReal = isTemp
  420. ? vector.start
  421. : dataService.getRoadPoint(vector.startId);
  422. const endReal = isTemp
  423. ? vector.end
  424. : dataService.getRoadPoint(vector.endId);
  425. this.drawTextByInfo(
  426. { x: (startReal.x + endReal.x) / 2, y: (startReal.y + endReal.y) / 2 },
  427. vector.vectorId
  428. );
  429. }
  430. this.drawRoadEdge(vector, isTemp);
  431. vector.leftLanes && vector.leftLanes.forEach(this.drawLan.bind(this));
  432. vector.rightLanes && vector.rightLanes.forEach(this.drawLan.bind(this));
  433. vector.singleLanes && vector.singleLanes.forEach(this.drawLan.bind(this));
  434. }
  435. drawLan(lan) {
  436. const ctx = this.context;
  437. const start = coordinate.getScreenXY(lan.start);
  438. const end = coordinate.getScreenXY(lan.end);
  439. ctx.save();
  440. ctx.beginPath();
  441. help.setVectorStyle(ctx, null, "Lane");
  442. ctx.lineWidth *= Settings.lineWidth
  443. ctx.setLineDash(Style.Lane.dash);
  444. ctx.moveTo(start.x, start.y);
  445. ctx.lineTo(end.x, end.y);
  446. ctx.stroke();
  447. ctx.restore();
  448. if (import.meta.env.DEV) {
  449. // this.drawPoint(lan.start);
  450. // this.drawPoint(lan.end);
  451. }
  452. }
  453. drawRoadEdge(vector, isTemp) {
  454. //判断是否与road方向一致。角度足够小,路足够宽,有可能向量方向不一致
  455. const start = isTemp
  456. ? vector.start
  457. : dataService.getRoadPoint(vector.startId);
  458. const end = isTemp ? vector.end : dataService.getRoadPoint(vector.endId);
  459. const drawRoadEdgeChild = (edgeVector) => {
  460. const flag = mathUtil.isSameDirForVector(
  461. start,
  462. end,
  463. edgeVector.start,
  464. edgeVector.end
  465. );
  466. if (flag) {
  467. const point1 = coordinate.getScreenXY(edgeVector.start);
  468. const point2 = coordinate.getScreenXY(edgeVector.end);
  469. help.drawStyleLine(ctx, [point1, point2], edgeVector.style, edgeVector.weight)
  470. }
  471. if (import.meta.env.DEV) {
  472. this.drawTextByInfo(
  473. {
  474. x: (edgeVector.start.x + edgeVector.end.x) / 2,
  475. y: (edgeVector.start.y + edgeVector.end.y) / 2,
  476. },
  477. edgeVector.vectorId
  478. );
  479. }
  480. };
  481. const leftEdge = isTemp
  482. ? vector.leftEdge
  483. : dataService.getRoadEdge(vector.leftEdgeId);
  484. const rightEdge = isTemp
  485. ? vector.rightEdge
  486. : dataService.getRoadEdge(vector.rightEdgeId);
  487. const ctx = this.context;
  488. ctx.save();
  489. isTemp && (ctx.globalAlpha = 0.3);
  490. help.setVectorStyle(ctx, leftEdge);
  491. let [style, fo] = help.getVectorStyle(vector)
  492. fo && help.setStyle(ctx, style)
  493. drawRoadEdgeChild(leftEdge);
  494. help.setVectorStyle(ctx, rightEdge);
  495. fo && help.setStyle(ctx, style)
  496. drawRoadEdgeChild(rightEdge);
  497. ctx.restore();
  498. if (fo) {
  499. ctx.save()
  500. const p1 = coordinate.getScreenXY(leftEdge.start);
  501. const p2 = coordinate.getScreenXY(rightEdge.start);
  502. const p3 = coordinate.getScreenXY(leftEdge.end);
  503. const p4 = coordinate.getScreenXY(rightEdge.end);
  504. ctx.lineWidth = 1 * coordinate.ratio
  505. ctx.setLineDash([5 * coordinate.ratio, 5 * coordinate.ratio ]);
  506. ctx.strokeStyle = Style.Road.strokeStyle
  507. ctx.beginPath()
  508. ctx.moveTo(p1.x, p1.y)
  509. ctx.lineTo(p2.x, p2.y)
  510. ctx.stroke()
  511. ctx.beginPath()
  512. ctx.moveTo(p3.x, p3.y)
  513. ctx.lineTo(p4.x, p4.y)
  514. ctx.stroke()
  515. ctx.fillStyle = 'rgba(23, 121, 237, 0.30)'
  516. ctx.moveTo(p1.x, p1.y)
  517. ctx.lineTo(p2.x, p2.y)
  518. ctx.lineTo(p4.x, p4.y)
  519. ctx.lineTo(p3.x, p3.y)
  520. ctx.fill()
  521. ctx.restore()
  522. }
  523. if (import.meta.env.DEV) {
  524. // this.drawPoint(leftEdge.start);
  525. // this.drawPoint(leftEdge.end);
  526. // this.drawPoint(rightEdge.start);
  527. // this.drawPoint(rightEdge.end);
  528. }
  529. }
  530. drawCrossPoint(vector) {
  531. const start = coordinate.getScreenXY(
  532. dataService
  533. .getRoadEdge(vector.edgeInfo1.id)
  534. .getPosition(vector.edgeInfo1.dir)
  535. );
  536. const end = coordinate.getScreenXY(
  537. dataService
  538. .getRoadEdge(vector.edgeInfo2.id)
  539. .getPosition(vector.edgeInfo2.dir)
  540. );
  541. const pt2 = mathUtil.twoOrderBezier(
  542. 0.5,
  543. start,
  544. coordinate.getScreenXY({ x: vector.x, y: vector.y }),
  545. end
  546. );
  547. const pt = mathUtil.twoOrderBezier2(0.5, start, pt2, end);
  548. const extremePoint = coordinate.getScreenXY(vector.extremePoint);
  549. const ctx = this.context;
  550. ctx.save();
  551. help.setVectorStyle(ctx, vector)
  552. ctx.beginPath();
  553. ctx.arc(
  554. extremePoint.x,
  555. extremePoint.y,
  556. Style.CrossPoint.radius * coordinate.ratio,
  557. 0,
  558. Math.PI * 2,
  559. true
  560. );
  561. ctx.stroke();
  562. ctx.fill();
  563. ctx.restore();
  564. ctx.save();
  565. ctx.beginPath();
  566. help.setVectorStyle(ctx, null, "RoadEdge");
  567. //曲线
  568. // ctx.moveTo(start.x, start.y);
  569. // ctx.quadraticCurveTo(pt.x, pt.y, end.x, end.y);
  570. const [coves] = help.transformCoves([vector.curves]);
  571. help.drawCoves(ctx, coves);
  572. ctx.restore();
  573. }
  574. drawCurveRoad(vector) {
  575. const ctx = this.context;
  576. ctx.save();
  577. let midCovesArray
  578. const [_, foo] = help.setVectorStyle(ctx, vector);
  579. if (vector.display && vector.midDivide) {
  580. midCovesArray = help.transformCoves([
  581. vector.midDivide.leftMidDivideCurves,
  582. vector.midDivide.rightMidDivideCurves,
  583. ]);
  584. ctx.lineWidth *= Settings.lineWidth
  585. for (let coves of midCovesArray) {
  586. help.drawCoves(ctx, coves);
  587. }
  588. }
  589. ctx.restore();
  590. this.drawCurveRoadEdge(dataService.getCurveRoadEdge(vector.rightEdgeId), vector);
  591. this.drawCurveRoadEdge(dataService.getCurveRoadEdge(vector.leftEdgeId), vector);
  592. vector.leftLanesCurves &&
  593. vector.leftLanesCurves.forEach(this.drawCurveLan.bind(this));
  594. vector.rightLanesCurves &&
  595. vector.rightLanesCurves.forEach(this.drawCurveLan.bind(this));
  596. if (foo) {
  597. const leftEdge = dataService.getCurveRoadEdge(vector.leftEdgeId)
  598. const rightEdge = dataService.getCurveRoadEdge(vector.rightEdgeId)
  599. const p1 = coordinate.getScreenXY(leftEdge.start);
  600. const p2 = coordinate.getScreenXY(rightEdge.start);
  601. const p3 = coordinate.getScreenXY(leftEdge.end);
  602. const p4 = coordinate.getScreenXY(rightEdge.end);
  603. ctx.save();
  604. ctx.setLineDash([5 * coordinate.ratio, 5 * coordinate.ratio ]);
  605. ctx.lineWidth = 1 * coordinate.ratio
  606. ctx.strokeStyle = Style.Lane.strokeStyle
  607. ctx.beginPath()
  608. ctx.moveTo(p1.x, p1.y)
  609. ctx.lineTo(p2.x, p2.y)
  610. ctx.stroke()
  611. ctx.beginPath()
  612. ctx.moveTo(p3.x, p3.y)
  613. ctx.lineTo(p4.x, p4.y)
  614. ctx.stroke()
  615. if (midCovesArray) {
  616. const edgeCurves = help.transformCoves([
  617. leftEdge.curves,
  618. rightEdge.curves
  619. ]);
  620. edgeCurves[1] = edgeCurves[1].reverse().map(curve => ({
  621. start: curve.end,
  622. end: curve.start,
  623. controls: curve.controls.reverse()
  624. }))
  625. ctx.beginPath();
  626. ctx.setLineDash([])
  627. ctx.moveTo(edgeCurves[0][0].start.x, edgeCurves[0][0].start.y);
  628. edgeCurves[0].forEach(cuve => help.drawCove(ctx, cuve))
  629. ctx.lineTo(edgeCurves[1][0].start.x, edgeCurves[1][0].start.y)
  630. edgeCurves[1].forEach(cuve => help.drawCove(ctx, cuve))
  631. ctx.closePath()
  632. ctx.fillStyle = 'rgba(23, 121, 237, 0.30)'
  633. ctx.fill()
  634. }
  635. ctx.restore();
  636. }
  637. // if (import.meta.env.DEV) {
  638. vector.points.forEach(this.drawPoint.bind(this));
  639. // }
  640. }
  641. drawCurveRoadEdge(vector, roadVector) {
  642. const [coves] = help.transformCoves([vector.curves]);
  643. const ctx = this.context;
  644. const [style, select] = help.getVectorStyle(roadVector)
  645. ctx.save();
  646. help.setVectorStyle(ctx, vector);
  647. select && help.setStyle(ctx, style)
  648. ctx.lineWidth *= Settings.lineWidth
  649. help.drawCoves(ctx, coves);
  650. ctx.restore();
  651. if (import.meta.env.DEV) {
  652. // vector.points.forEach(this.drawPoint.bind(this));
  653. }
  654. }
  655. drawCurveLan(lines) {
  656. const [coves] = help.transformCoves([lines]);
  657. const ctx = this.context;
  658. ctx.save();
  659. help.setVectorStyle(ctx, null, "CurveLan");
  660. ctx.lineWidth *= Settings.lineWidth
  661. ctx.setLineDash(Style.Lane.dash);
  662. help.drawCoves(ctx, coves);
  663. ctx.restore();
  664. // if (import.meta.env.DEV) {
  665. lines.map((line) => {
  666. this.drawPoint(line.start);
  667. this.drawPoint(line.end);
  668. });
  669. // }
  670. }
  671. drawRoadPoint(vector) {
  672. this.drawPoint(vector);
  673. }
  674. drawArrow(vector) {
  675. const startReal = dataService.getPoint(vector.startId);
  676. const start = coordinate.getScreenXY(startReal);
  677. const endReal = dataService.getPoint(vector.endId);
  678. const end = coordinate.getScreenXY(endReal);
  679. const ctx = this.context;
  680. ctx.save();
  681. const [style] = help.setVectorStyle(this.context, vector);
  682. if (vector.color) {
  683. ctx.strokeStyle = vector.color;
  684. }
  685. const dires =
  686. vector.category === UIEvents.DoubleArrow
  687. ? [
  688. [start, end],
  689. [end, start],
  690. ]
  691. : [[start, end]];
  692. for (let [start, end] of dires) {
  693. const lines = mathUtil.getArrow(start, end);
  694. ctx.moveTo(lines[0].x, lines[0].y);
  695. ctx.lineTo(lines[1].x, lines[1].y);
  696. ctx.lineTo(lines[2].x, lines[2].y);
  697. }
  698. ctx.stroke();
  699. ctx.restore();
  700. }
  701. drawMagnifier(vector) {
  702. const ctx = this.context;
  703. ctx.save();
  704. const [style] = help.setVectorStyle(ctx, vector);
  705. const radius = vector.radius || style.radius;
  706. this.drawPoint({
  707. ...vector,
  708. ...vector.position,
  709. radius,
  710. });
  711. const pt = coordinate.getScreenXY(vector.position);
  712. // vector.setPopPosition();
  713. const target = {
  714. x: vector.popPosition.x,
  715. y: vector.popPosition.y,
  716. };
  717. const offset = radius / 2;
  718. const targetPts =[mathUtil.translate(pt, target, pt, radius), target];
  719. ctx.beginPath();
  720. ctx.moveTo(pt.x - offset, pt.y);
  721. ctx.lineTo(pt.x + offset, pt.y);
  722. ctx.stroke();
  723. ctx.beginPath();
  724. ctx.moveTo(pt.x, pt.y - offset);
  725. ctx.lineTo(pt.x, pt.y + offset);
  726. ctx.stroke();
  727. if (targetPts) {
  728. ctx.beginPath();
  729. ctx.moveTo(targetPts[0].x, targetPts[0].y);
  730. ctx.lineTo(targetPts[1].x, targetPts[1].y);
  731. ctx.stroke();
  732. let img, imgBound;
  733. if (vector.photoImage) {
  734. img = vector.photoImage;
  735. imgBound = [0, 0, img.width, img.height];
  736. } else {
  737. const size = help.getReal(style.target.realRadius);
  738. const backImg = dataService.getBackgroundImg();
  739. img = backImg.imageData;
  740. const imgCenter = coordinate.getScreenXY(backImg.center);
  741. const start = {
  742. x: imgCenter.x - help.getReal(img.width) / 2,
  743. y: imgCenter.y - help.getReal(img.height) / 2,
  744. };
  745. const ro = img.width / help.getReal(img.width);
  746. imgBound = [
  747. (pt.x - start.x - size) * ro,
  748. (pt.y - start.y - size) * ro,
  749. size * 2 * ro,
  750. size * 2 * ro,
  751. ];
  752. }
  753. const size = style.target.radius;
  754. ctx.beginPath();
  755. ctx.arc(target.x, target.y, size, 0, 2 * Math.PI);
  756. ctx.clip();
  757. ctx.drawImage(
  758. img,
  759. ...imgBound,
  760. target.x - size,
  761. target.y - size,
  762. size * 2,
  763. size * 2
  764. );
  765. ctx.strokeStyle = style.target.strokeStyle;
  766. ctx.lineWidth = style.target.lineWidth;
  767. ctx.stroke();
  768. }
  769. ctx.restore();
  770. }
  771. drawElliptic(element, radiusX = element.radiusX, radiusY = element.radiusY) {
  772. function drawEllipse(context, x, y, a, b) {
  773. const step = (a > b) ? 1 / a : 1 / b;
  774. context.beginPath();
  775. context.moveTo(x + a, y);
  776. for (let i = 0; i < 2 * Math.PI; i += step) {
  777. context.lineTo(x + a * Math.cos(i), y + b * Math.sin(i));
  778. }
  779. context.closePath();
  780. }
  781. const pt = coordinate.getScreenXY({ x: element.center.x, y: element.center.y });
  782. const ctx = this.context;
  783. ctx.save();
  784. const [_, label] = help.setVectorStyle(ctx, element);
  785. ctx.strokeStyle = element.color
  786. drawEllipse(
  787. ctx, pt.x, pt.y,
  788. (radiusX * coordinate.zoom) / coordinate.defaultZoom,
  789. (radiusY * coordinate.zoom) / coordinate.defaultZoom
  790. )
  791. ctx.stroke();
  792. ctx.fill();
  793. ctx.restore();
  794. }
  795. drawCircle(element) {
  796. this.context.save()
  797. const geo = [element.center, element.points[1], {...element.center, x: 999}]
  798. let angle = mathUtil.Angle(...geo)
  799. angle = help.isTriangleClockwise(...geo) ? -angle : angle
  800. const center = coordinate.getScreenXY(element.center)
  801. this.context.translate(center.x, center.y)
  802. this.context.rotate((angle / 180) * Math.PI)
  803. this.context.translate(-center.x, -center.y)
  804. this.drawElliptic(element, element.radiusX, element.radiusY)
  805. this.context.restore()
  806. const [_, label] = help.getVectorStyle(element);
  807. label && element.points.forEach((point) => this.drawPoint(point));
  808. }
  809. drawPoint(vector, screenSave) {
  810. const screenNotDrawTypes = [
  811. VectorCategory.Point.NormalPoint,
  812. ]
  813. if (!screenSave) {
  814. if ((Settings.screenMode && (!vector.category || screenNotDrawTypes.includes(vector.category))) ||
  815. (vector.category === VectorCategory.Point.TestBasePoint)) {
  816. return;
  817. }
  818. }
  819. const pt = coordinate.getScreenXY({ x: vector.x, y: vector.y });
  820. const ctx = this.context;
  821. ctx.save();
  822. let [style, attr] = help.setVectorStyle(ctx, vector, [
  823. vector.category,
  824. vector.geoType,
  825. "Point",
  826. ]);
  827. if (vector.category === VectorCategory.Point.NormalPoint) {
  828. const lineid = Object.keys(vector.parent)[0];
  829. let line;
  830. if (!(lineid && (line = dataService.getLine(lineid)))) {
  831. return;
  832. }
  833. const [stylea, attr] = help.getVectorStyle(line, line.category);
  834. style = {
  835. ...style,
  836. ...stylea
  837. }
  838. // if (!attr) {
  839. // return;
  840. // }
  841. }
  842. if (vector.color) {
  843. ctx.strokeStyle = vector.color;
  844. style = {
  845. ...style,
  846. strokeStyle: vector.color
  847. };
  848. }
  849. const draw = (style) => {
  850. const radius = vector.radius || style.radius;
  851. ctx.save();
  852. ctx.beginPath();
  853. ctx.arc(pt.x, pt.y, radius, 0, Math.PI * 2, true);
  854. help.setStyle(ctx, style);
  855. ctx.stroke();
  856. ctx.fill();
  857. ctx.restore();
  858. };
  859. let points = dataService.vectorData.points;
  860. let basePoints = []
  861. for (let key in points) {
  862. if (points[key].category == VectorCategory.Point.BasePoint) {
  863. basePoints.push(points[key].vectorId)
  864. }
  865. }
  866. if(basePoints.length==1){
  867. Settings.selectBasePointId = basePoints[0];
  868. }else{
  869. Settings.selectBasePointId =null
  870. }
  871. let focusItem = stateService.getFocusItem()
  872. // if (Settings.selectBasePointId === vector.vectorId && focusItem?.vectorId == vector.vectorId ) {
  873. if (Settings.selectBasePointId === vector.vectorId ) {
  874. style = {
  875. ...style,
  876. strokeStyle: "rgba(255,255,255,1)",
  877. out: style.out && {
  878. ...style.out,
  879. strokeStyle: "red",
  880. },
  881. };
  882. }
  883. draw(style);
  884. if (style.out) {
  885. draw(style.out);
  886. }
  887. if (vector.category === "BasePoint") {
  888. ctx.font = `${12 * coordinate.ratio}px Microsoft YaHei`
  889. const bound = help.getTextCenter(ctx, "基准点")
  890. const screen = coordinate.getScreenXY(vector)
  891. const textPt = coordinate.getXYFromScreenNotRatio({
  892. y: screen.y + bound.height + style.radius,
  893. x: screen.x - (bound.width / 2)
  894. })
  895. ctx.fillStyle = style.fillStyle
  896. this.drawTextByInfo(textPt, "基准点", 0, false);
  897. } else {
  898. if (import.meta.env.DEV) {
  899. if (vector.vectorId) {
  900. // this.drawTextByInfo(vector, vector.vectorId);
  901. }
  902. }
  903. }
  904. ctx.restore();
  905. }
  906. drawTextByInfo(position, txt, angle, setStyle = true) {
  907. const ctx = this.context;
  908. ctx.save();
  909. setStyle && help.setVectorStyle(ctx, null, "Text");
  910. const pt = coordinate.getScreenXY(position);
  911. const textCenter = help.getTextCenter(ctx, txt);
  912. // pt.x -= textCenter.x;
  913. // pt.y -= textCenter.y;
  914. if (angle) {
  915. ctx.translate(pt.x, pt.y);
  916. ctx.rotate(angle);
  917. ctx.translate(-textCenter.x, -textCenter.y);
  918. ctx.fillText(txt, 0, 0);
  919. } else {
  920. ctx.fillText(txt, pt.x, pt.y);
  921. }
  922. ctx.restore();
  923. }
  924. // 文字
  925. drawText(vector) {
  926. this.context.save();
  927. help.setVectorStyle(this.context, vector);
  928. this.context.fillStyle = vector.color;
  929. this.context.font = `${
  930. vector.fontSize * coordinate.ratio
  931. }px Microsoft YaHei`;
  932. const bound = help.getTextCenter(this.context, vector.value)
  933. // console.log(vector)
  934. const screen = coordinate.getScreenXY(vector.center)
  935. this.drawTextByInfo(
  936. // vector.center,
  937. coordinate.getXYFromScreenNotRatio({
  938. // y: screen.y + (bound.height + Style.Point.radius),
  939. y: screen.y + (bound.height + Style.Point.radius ),
  940. x: screen.x - (bound.width / 2)
  941. }),
  942. vector.value,
  943. -(vector.angle || 0),
  944. false
  945. );
  946. this.context.restore();
  947. vector.displayPoint && this.drawPoint({...vector.center, color: vector.color}, true)
  948. }
  949. drawSVG(vector) {
  950. const points = vector.points.map(coordinate.getScreenXY.bind(coordinate));
  951. const svgWidth = 64;
  952. const svgHidth = 64;
  953. const width = mathUtil.getDistance(points[0], points[1]);
  954. const height = mathUtil.getDistance(points[0], points[3]);
  955. const dires = [points[0], { ...points[0], x: 10000 }, points[1]];
  956. let angle = mathUtil.Angle(...dires) * (Math.PI / 180);
  957. angle = mathUtil.isClockwise(dires) ? angle : -angle;
  958. this.context.save();
  959. this.context.translate(points[0].x, points[0].y);
  960. this.context.rotate(angle);
  961. this.context.scale(width / svgWidth, height / svgHidth);
  962. const [style, label] = help.setVectorStyle(this.context, vector);
  963. this.context.lineWidth = style.lineWidth / (width / svgWidth);
  964. SVGIcons[vector.type].draw(this.context);
  965. this.context.restore();
  966. if (label) {
  967. this.context.save();
  968. this.context.beginPath();
  969. this.context.moveTo(points[0].x, points[0].y);
  970. this.context.lineTo(points[1].x, points[1].y);
  971. this.context.lineTo(points[2].x, points[2].y);
  972. this.context.lineTo(points[3].x, points[3].y);
  973. this.context.strokeStyle = style.strokeStyle
  974. this.context.lineWidth = 2 * coordinate.ratio
  975. this.context.setLineDash([6 * coordinate.ratio, 2 * coordinate.ratio]);
  976. this.context.closePath();
  977. this.context.stroke();
  978. this.context.restore();
  979. vector.points.forEach(point => this.drawPoint({...point, color: style.strokeStyle, radius: 5 }))
  980. }
  981. }
  982. drawLineText(vector, style) {
  983. const startReal = dataService.getPoint(vector.startId);
  984. const endReal = dataService.getPoint(vector.endId);
  985. help.drawLineText(
  986. this.context,
  987. coordinate.getScreenXY(startReal),
  988. coordinate.getScreenXY(endReal),
  989. (vector.value
  990. ? Math.round(vector.value * 100) / 100
  991. : help.getRealDistance(startReal, endReal)) + "m",
  992. style
  993. );
  994. }
  995. drawBaseLineLabel(vector) {
  996. const startReal = dataService.getPoint(vector.startId);
  997. const start = coordinate.getScreenXY(startReal);
  998. const endReal = dataService.getPoint(vector.endId);
  999. const end = coordinate.getScreenXY(endReal);
  1000. const point = mathUtil.translate(
  1001. end,
  1002. start,
  1003. end,
  1004. mathUtil.getDistance(start, end) / 3
  1005. );
  1006. const p4 = help.getPerpendicularPoint(
  1007. start,
  1008. end,
  1009. point,
  1010. 30 * coordinate.ratio
  1011. );
  1012. const ctx = this.context;
  1013. ctx.save()
  1014. ctx.beginPath();
  1015. const [style] = help.setVectorStyle(
  1016. this.context,
  1017. vector,
  1018. vector.category || vector.geoType
  1019. );
  1020. ctx.moveTo(point.x, point.y);
  1021. ctx.lineTo(p4.x, p4.y);
  1022. ctx.stroke();
  1023. const p5 = help.getPerpendicularPoint(
  1024. start,
  1025. end,
  1026. point,
  1027. 35 * coordinate.ratio
  1028. );
  1029. this.context.font = `${12 * coordinate.ratio}px Microsoft YaHei`;
  1030. help.drawLineText(
  1031. this.context,
  1032. help.getPerpendicularPoint(point, p5, p5, 10 * coordinate.ratio),
  1033. help.getPerpendicularPoint(point, p5, p5, -10 * coordinate.ratio),
  1034. "基准线",
  1035. {
  1036. padding: 6 * coordinate.ratio,
  1037. backColor: "rgba(0,0,0,0)",
  1038. fillColor: style.strokeStyle,
  1039. }
  1040. );
  1041. ctx.restore()
  1042. }
  1043. drawCurveLine(vector) {
  1044. // points CurveLine
  1045. const ctx = this.context;
  1046. ctx.save();
  1047. help.setVectorStyle(ctx, vector);
  1048. help.drawCoves(ctx, help.transformCoves([vector.points]));
  1049. ctx.restore();
  1050. if (import.meta.env.DEV) {
  1051. vector.points.forEach(this.drawPoint.bind(this));
  1052. }
  1053. }
  1054. drawLine(vector) {
  1055. const startReal = dataService.getPoint(vector.startId);
  1056. const start = coordinate.getScreenXY(startReal);
  1057. const endReal = dataService.getPoint(vector.endId);
  1058. const end = coordinate.getScreenXY(endReal);
  1059. this.context.save();
  1060. const [style, attr] = help.setVectorStyle(this.context, vector, [
  1061. vector.category,
  1062. vector.geoType,
  1063. "BaseLine",
  1064. ]);
  1065. if (style.dash) {
  1066. this.context.setLineDash(style.dash);
  1067. }
  1068. help.drawStyleLine(this.context, [start, end], vector.style, vector.weight)
  1069. switch (vector.category) {
  1070. case VectorCategory.Line.SingleArrowLine:
  1071. this.drawArrow(vector);
  1072. break;
  1073. case VectorCategory.Line.DoubleArrowLine:
  1074. this.drawArrow(vector);
  1075. break;
  1076. case VectorCategory.Line.BaseLine:
  1077. this.drawBaseLineLabel(vector);
  1078. break;
  1079. case VectorCategory.Line.FreeMeasureLine:
  1080. case VectorCategory.Line.MeasureLine:
  1081. case VectorCategory.Line.PositionLine:
  1082. this.drawLineText(vector, style.text);
  1083. break;
  1084. }
  1085. this.context.restore();
  1086. }
  1087. drawElementLine(element) {
  1088. let start = elementService.getPoint(element.startId);
  1089. start = coordinate.getScreenXY(start);
  1090. let end = elementService.getPoint(element.endId);
  1091. end = coordinate.getScreenXY(end);
  1092. this.context.save();
  1093. const [style] = help.setVectorStyle(
  1094. this.context,
  1095. element,
  1096. element.category || element.geoType
  1097. );
  1098. if (style.dash) {
  1099. this.context.setLineDash(style.dash);
  1100. }
  1101. this.context.beginPath();
  1102. this.context.moveTo(start.x, start.y);
  1103. this.context.lineTo(end.x, end.y);
  1104. this.context.stroke();
  1105. this.context.restore();
  1106. }
  1107. }
  1108. const draw = new Draw();
  1109. export { draw };