Draw.js 39 KB

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