Draw.js 45 KB

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