123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934 |
- import { dataService } from "../Service/DataService.js";
- import { stateService } from "../Service/StateService.js";
- import { measureService } from "../Service/MeasureService";
- import { coordinate } from "../Coordinate.js";
- import Style from "../Style.js";
- import VectorType from "../enum/VectorType.js";
- import SelectState from "../enum/SelectState.js";
- import { mathUtil } from "../Util/MathUtil.js";
- import ElementEvents from "../enum/ElementEvents.js";
- import Constant from "../Constant.js";
- export default class Draw {
- constructor() {
- this.context = null;
- }
- initContext(canvas) {
- if (canvas) {
- this.context = canvas.getContext("2d");
- } else {
- this.context = null;
- }
- }
- clear() {
- this.context.clearRect(
- 0,
- 0,
- this.context.canvas.width,
- this.context.canvas.height
- );
- }
- drawBackGround(color) {
- this.context.save();
- this.context.fillStyle = color;
- this.context.fillRect(
- 0,
- 0,
- this.context.canvas.width,
- this.context.canvas.height
- );
- this.context.restore();
- }
- drawRoad(vector, isTemp) {
- this.context.save();
- this.context.beginPath();
- this.context.lineCap = "round"; //线段端点的样式
- //this.context.lineJoin= 'miter';
- this.context.strokeStyle = Style.Road.strokeStyle;
- const selectItem = stateService.getSelectItem();
- const draggingItem = stateService.getDraggingItem();
- const focusItem = stateService.getFocusItem();
- if (selectItem && selectItem.type == VectorType.Road) {
- if (vector.vectorId == selectItem.vectorId) {
- this.context.strokeStyle = Style.Select.Road.strokeStyle;
- }
- } else if (draggingItem && draggingItem.type == VectorType.Road) {
- if (vector.vectorId == draggingItem.vectorId) {
- this.context.strokeStyle = Style.Select.Road.strokeStyle;
- }
- } else if (focusItem && focusItem.type == VectorType.Road) {
- if (vector.vectorId == focusItem.vectorId) {
- this.context.strokeStyle = Style.Focus.Road.strokeStyle;
- }
- }
- let point1, point2;
- if (isTemp) {
- this.context.globalAlpha = 0.3;
- point1 = coordinate.getScreenXY(vector.start);
- point2 = coordinate.getScreenXY(vector.end);
- this.context.lineWidth = Constant.defaultRoadWidth;
- } else {
- let start = dataService.getPoint(vector.startId);
- let end = dataService.getPoint(vector.endId);
- point1 = coordinate.getScreenXY(start);
- point2 = coordinate.getScreenXY(end);
- this.drawEdge(vector, isTemp);
- this.drawText(
- { x: (start.x + end.x) / 2, y: (start.y + end.y) / 2 },
- vector.vectorId
- );
- this.context.lineWidth = vector.width;
- }
- this.context.beginPath();
- this.context.moveTo(point1.x, point1.y);
- this.context.lineTo(point2.x, point2.y);
- this.context.stroke();
- this.context.restore();
- }
- drawEdge(vector) {
- this.context.save();
- this.context.beginPath();
- this.context.lineCap = "round"; //线段端点的样式
- this.context.strokeStyle = Style.Road.strokeStyle;
- const selectItem = stateService.getSelectItem();
- const draggingItem = stateService.getDraggingItem();
- const focusItem = stateService.getFocusItem();
- if (selectItem && selectItem.type == VectorType.Road) {
- if (vector.vectorId == selectItem.vectorId) {
- this.context.strokeStyle = Style.Select.Road.strokeStyle;
- }
- } else if (draggingItem && draggingItem.type == VectorType.Road) {
- if (vector.vectorId == draggingItem.vectorId) {
- this.context.strokeStyle = Style.Select.Road.strokeStyle;
- }
- } else if (focusItem && focusItem.type == VectorType.Road) {
- if (vector.vectorId == focusItem.vectorId) {
- this.context.strokeStyle = Style.Focus.Road.strokeStyle;
- }
- }
- const leftEdge = dataService.getEdge(vector.leftEdgeId);
- const rightEdge = dataService.getEdge(vector.rightEdgeId);
- let point1, point2;
- this.context.globalAlpha = 0.3;
- this.context.lineWidth = 1;
- this.context.beginPath();
- point1 = coordinate.getScreenXY(leftEdge.start);
- point2 = coordinate.getScreenXY(leftEdge.end);
- this.context.moveTo(point1.x, point1.y);
- this.context.lineTo(point2.x, point2.y);
- this.context.stroke();
- point1 = coordinate.getScreenXY(rightEdge.start);
- point2 = coordinate.getScreenXY(rightEdge.end);
- this.context.moveTo(point1.x, point1.y);
- this.context.lineTo(point2.x, point2.y);
- this.context.stroke();
- this.context.restore();
- }
- drawPoint(vector) {
- const pt = coordinate.getScreenXY({ x: vector.x, y: vector.y });
- const selectItem = stateService.getSelectItem();
- const draggingItem = stateService.getDraggingItem();
- const focusItem = stateService.getFocusItem();
- let radius = Style.Point.radius;
- if (
- (draggingItem &&
- draggingItem.type == VectorType.RoadCorner &&
- vector.vectorId == draggingItem.vectorId) ||
- (selectItem &&
- selectItem.type == VectorType.RoadCorner &&
- vector.vectorId == selectItem.vectorId)
- ) {
- this.context.save();
- this.context.lineWidth = Style.Select.Point.lineWidth * coordinate.ratio;
- this.context.strokeStyle = Style.Select.Point.strokeStyle;
- this.context.fillStyle = Style.Select.Point.fillStyle;
- radius = Style.Select.Point.radius;
- } else if (
- focusItem &&
- focusItem.type == VectorType.RoadCorner &&
- vector.vectorId == focusItem.vectorId
- ) {
- this.context.save();
- this.context.lineWidth = Style.Focus.Point.lineWidth * coordinate.ratio;
- this.context.strokeStyle = Style.Focus.Point.strokeStyle;
- this.context.fillStyle = Style.Focus.Point.fillStyle;
- radius = Style.Focus.Point.radius;
- } else {
- return;
- }
- this.context.beginPath();
- this.context.arc(
- pt.x,
- pt.y,
- radius * coordinate.ratio,
- 0,
- Math.PI * 2,
- true
- );
- this.context.stroke();
- this.context.fill();
- this.context.restore();
- this.drawText(vector, vector.vectorId);
- }
- // 文字
- drawText(position, txt, angle) {
- this.context.save();
- this.setCanvasStyle(Style.Font);
- if (coordinate.ratio == Constant.ratio) {
- this.context.font = "36px Microsoft YaHei";
- } else {
- this.context.font = "12px Microsoft YaHei";
- }
- let pt = coordinate.getScreenXY(position);
- if (angle) {
- this.context.translate(pt.x, pt.y);
- this.context.rotate(angle);
- //this.context.strokeText(txt, 0, 0)
- this.context.fillText(txt, 0, 0);
- } else {
- //this.context.strokeText(txt, pt.x, pt.y)
- this.context.fillText(txt, pt.x, pt.y);
- }
- this.context.restore();
- }
- drawTag(geometry, styleType, hide) {
- this.context.save();
- this.context.lineWidth = Style.Tag.lineWidth * coordinate.ratio;
- this.context.strokeStyle = Style.Tag.strokeStyle;
- this.context.fillStyle = Style.Tag.fillStyle;
- if (styleType) {
- if (styleType == "style-1") {
- this.context.lineWidth =
- Style.DownLoad.style1.Tag.lineWidth * coordinate.ratio;
- this.context.strokeStyle = Style.DownLoad.style1.Tag.strokeStyle;
- this.context.fillStyle = Style.DownLoad.style1.Tag.fillStyle;
- } else if (styleType == "style-2") {
- this.context.lineWidth =
- Style.DownLoad.style2.Tag.lineWidth * coordinate.ratio;
- this.context.strokeStyle = Style.DownLoad.style2.Tag.strokeStyle;
- this.context.fillStyle = Style.DownLoad.style2.Tag.fillStyle;
- } else if (styleType == "style-3") {
- this.context.lineWidth =
- Style.DownLoad.style3.Tag.lineWidth * coordinate.ratio;
- this.context.strokeStyle = Style.DownLoad.style3.Tag.strokeStyle;
- this.context.fillStyle = Style.DownLoad.style3.Tag.fillStyle;
- } else if (styleType == "style-4") {
- this.context.lineWidth =
- Style.DownLoad.style4.Tag.lineWidth * coordinate.ratio;
- this.context.strokeStyle = Style.DownLoad.style4.Tag.strokeStyle;
- this.context.fillStyle = Style.DownLoad.style4.Tag.fillStyle;
- }
- } else {
- const selectItem = stateService.getSelectItem();
- const draggingItem = stateService.getDraggingItem();
- const focusItem = stateService.getFocusItem();
- if (selectItem && selectItem.type == VectorType.Tag) {
- if (geometry.vectorId == selectItem.vectorId) {
- this.context.strokeStyle = Style.Select.Tag.strokeStyle;
- this.context.fillStyle = Style.Select.Tag.fillStyle;
- }
- } else if (draggingItem && draggingItem.type == VectorType.Tag) {
- if (geometry.vectorId == draggingItem.vectorId) {
- this.context.strokeStyle = Style.Select.Tag.strokeStyle;
- this.context.fillStyle = Style.Select.Tag.fillStyle;
- }
- }
- if (focusItem && focusItem.type == VectorType.Tag) {
- if (geometry.vectorId == focusItem.vectorId) {
- this.context.strokeStyle = Style.Focus.Tag.strokeStyle;
- this.context.fillStyle = Style.Focus.Tag.fillStyle;
- }
- }
- }
- //正在添加
- if (geometry.adding) {
- let points2d = geometry.points2d;
- let points = [];
- for (let i = 0; i < points2d.length; ++i) {
- points[i] = coordinate.getScreenXY({
- x: points2d[i].x,
- y: points2d[i].y,
- });
- }
- this.context.strokeStyle = Style.Tag.strokeStyle_adding;
- this.context.fillStyle = Style.Tag.fillStyle_adding;
- this.context.beginPath();
- this.context.moveTo(points[0].x, points[0].y);
- this.context.lineTo(points[1].x, points[1].y);
- this.context.lineTo(points[2].x, points[2].y);
- this.context.lineTo(points[3].x, points[3].y);
- this.context.closePath();
- this.context.stroke();
- for (let i = 4; i < points.length - 1; i += 2) {
- this.context.moveTo(points[i].x, points[i].y);
- this.context.lineTo(points[i + 1].x, points[i + 1].y);
- }
- this.context.stroke();
- } else {
- const fontSize = coordinate.ratio == Constant.ratio ? 36 : 12;
- this.context.font = `400 ${fontSize}px Microsoft YaHei`;
- //根据文字的长度,更新标注范围
- let title = geometry.title;
- if (!hide && (title == null || title.trim() == "")) {
- console.log(dataService.$app.config);
- // title = '请输入名称'
- title = dataService.$app.config.i18n("cad.input");
- }
- geometry.des += "";
- if (geometry.des != "") {
- geometry.sideWidth = Math.max(
- this.context.measureText(title).width,
- this.context.measureText(
- parseFloat(geometry.des.replace(",", "")).toFixed(2)
- ).width
- );
- geometry.setPoints2d();
- }
- let points2d = geometry.points2d;
- let points = [];
- for (let i = 0; i < points2d.length; ++i) {
- points[i] = coordinate.getScreenXY({
- x: points2d[i].x,
- y: points2d[i].y,
- });
- }
- let pt = { x: geometry.center.x, y: geometry.center.y };
- pt = coordinate.getScreenXY({
- x: geometry.center.x,
- y: geometry.center.y,
- });
- const fontWidth1 = this.context.measureText(title).width;
- const line1 = mathUtil.createLine1(
- {
- x: (points[0].x + points[3].x) / 2,
- y: (points[0].y + points[3].y) / 2,
- },
- {
- x: (points[2].x + points[1].x) / 2,
- y: (points[2].y + points[1].y) / 2,
- }
- );
- let fontWidth2 = this.context.measureText(geometry.des + "m²").width;
- if (geometry.des != "" && geometry.unit == "ft") {
- fontWidth2 = this.context.measureText(
- parseFloat(geometry.des.replace(",", "")).toFixed(2) + "ft²"
- ).width;
- }
- const line2 = mathUtil.createLine1(points[2], points[3]);
- const fontStart1 = mathUtil.getDisPointsLine(
- line1,
- pt,
- fontWidth1 / 2,
- fontWidth1 / 2
- );
- const fontStart2 = mathUtil.getDisPointsLine(
- line2,
- {
- x: (points[2].x + points[3].x) / 2,
- y: (points[2].y + points[3].y) / 2,
- },
- fontWidth2 / 2,
- fontWidth2 / 2
- );
- if (fontStart1.newpoint1.x < fontStart1.newpoint2.x) {
- this.context.fillText(
- title,
- fontStart1.newpoint1.x,
- fontStart1.newpoint1.y
- );
- if (geometry.des) {
- if (measureService.unit == "ft" && geometry.unit == "m") {
- let area = uoMService.convert(
- geometry.des,
- "area",
- void 0,
- "imperial",
- 0.01,
- false
- );
- this.context.fillText(
- parseFloat(area.replace(",", "")).toFixed(2),
- fontStart2.newpoint1.x + fontSize / 1.5,
- fontStart2.newpoint1.y
- );
- } else if (measureService.unit == "m" && geometry.unit == "ft") {
- let area = uoMService.convertBack(
- geometry.des,
- "area",
- 7,
- "imperial",
- 0.01,
- false
- );
- this.context.fillText(
- parseFloat(area.replace(",", "")).toFixed(2),
- fontStart2.newpoint1.x + fontSize / 1.5,
- fontStart2.newpoint1.y
- );
- } else if (geometry.unit == "m") {
- this.context.fillText(
- parseFloat(geometry.des).toFixed(2) + "m²",
- fontStart2.newpoint1.x,
- fontStart2.newpoint1.y
- );
- } else if (geometry.unit == "ft") {
- this.context.fillText(
- parseFloat(geometry.des.replace(",", "")).toFixed(2) + "ft²",
- fontStart2.newpoint1.x,
- fontStart2.newpoint1.y
- );
- }
- }
- } else {
- this.context.fillText(
- title,
- fontStart1.newpoint2.x,
- fontStart1.newpoint2.y
- );
- if (geometry.des) {
- if (measureService.unit == "ft" && geometry.unit == "m") {
- let area = uoMService.convert(
- geometry.des,
- "area",
- void 0,
- "imperial",
- 0.01,
- false
- );
- this.context.fillText(
- parseFloat(area.replace(",", "")).toFixed(2),
- fontStart2.newpoint2.x + fontSize / 1.5,
- fontStart2.newpoint2.y
- );
- } else if (measureService.unit == "m" && geometry.unit == "ft") {
- let area = uoMService.convertBack(
- geometry.des,
- "area",
- 7,
- "imperial",
- 0.01,
- false
- );
- this.context.fillText(
- parseFloat(area.replace(",", "")).toFixed(2),
- fontStart2.newpoint2.x + fontSize / 1.5,
- fontStart2.newpoint2.y
- );
- } else if (geometry.unit == "m") {
- this.context.fillText(
- parseFloat(geometry.des).toFixed(2) + "m²",
- fontStart2.newpoint2.x,
- fontStart2.newpoint2.y
- );
- } else if (geometry.unit == "ft") {
- this.context.fillText(
- parseFloat(geometry.des.replace(",", "")).toFixed(2) + "ft²",
- fontStart2.newpoint2.x,
- fontStart2.newpoint2.y
- );
- }
- }
- }
- }
- this.context.restore();
- }
- drawCircle(element) {
- // let radius = null;
- // const twoPi = Math.PI * 2;
- // const pt = coordinate.getScreenXY(element);
- // this.context.save();
- // if (element.name == ElementEvents.StartAddRoad) {
- // radius = Style.Element.StartAddRoad.radius * coordinate.ratio;
- // this.context.strokeStyle = Style.Element.StartAddRoad.strokeStyle;
- // this.context.fillStyle = Style.Element.StartAddRoad.fillStyle;
- // } else if (element.name == ElementEvents.StartSymbolPoints) {
- // radius = Style.Element.StartSymbolPoints.radius * coordinate.ratio;
- // this.context.strokeStyle = Style.Element.StartSymbolPoints.strokeStyle;
- // this.context.fillStyle = Style.Element.StartSymbolPoints.fillStyle;
- // } else if (element.name == ElementEvents.EndSymbolPoints) {
- // radius = Style.Element.EndSymbolPoints.radius * coordinate.ratio;
- // this.context.strokeStyle = Style.Element.EndSymbolPoints.strokeStyle;
- // this.context.fillStyle = Style.Element.EndSymbolPoints.fillStyle;
- // } else if (element.name == "pano") {
- // radius = Style.Pano.radius * coordinate.ratio;
- // this.context.strokeStyle = Style.Pano.strokeStyle;
- // this.context.fillStyle = Style.Pano.fillStyle;
- // this.context.lineWidth = Style.Pano.lineWidth;
- // }
- // this.context.beginPath();
- // this.context.arc(pt.x, pt.y, radius, 0, twoPi, true);
- // this.context.stroke();
- // this.context.fill();
- // this.context.restore();
- }
- drawLine(element) {
- let start = coordinate.getScreenXY(element.start);
- let end = coordinate.getScreenXY(element.end);
- this.context.save();
- if (element.name == ElementEvents.VCheckLinesX) {
- this.context.lineWidth =
- Style.Element.VCheckLinesX.lineWidth * coordinate.ratio;
- this.context.strokeStyle = Style.Element.VCheckLinesX.strokeStyle;
- this.context.setLineDash([3, 2, 2]);
- start.y = 0;
- end.y = this.context.canvas.clientHeight;
- } else if (element.name == ElementEvents.VCheckLinesY) {
- this.context.lineWidth =
- Style.Element.VCheckLinesY.lineWidth * coordinate.ratio;
- this.context.strokeStyle = Style.Element.VCheckLinesY.strokeStyle;
- this.context.setLineDash([3, 2, 2]);
- start.x = 0;
- end.x = this.context.canvas.clientWidth;
- } else if (element.name == ElementEvents.NewRoad) {
- this.context.lineWidth =
- Style.Element.NewRoad.lineWidth * coordinate.ratio;
- this.context.strokeStyle = Style.Element.NewRoad.strokeStyle;
- if (element.state == "error") {
- this.context.strokeStyle = Style.Element.NewRoad.errorStrokeStyle;
- }
- } else if (element.name == ElementEvents.CheckLinesX) {
- this.context.lineWidth =
- Style.Element.CheckLinesX.lineWidth * coordinate.ratio;
- this.context.strokeStyle = Style.Element.CheckLinesX.strokeStyle;
- this.context.setLineDash([3, 2, 2]);
- } else if (element.name == ElementEvents.CheckLinesY) {
- this.context.lineWidth =
- Style.Element.CheckLinesY.lineWidth * coordinate.ratio;
- this.context.strokeStyle = Style.Element.CheckLinesY.strokeStyle;
- this.context.setLineDash([3, 2, 2]);
- } else if (element.name == ElementEvents.SignLine1) {
- this.context.lineWidth =
- Style.Element.SignLine1.lineWidth * coordinate.ratio;
- this.context.strokeStyle = Style.Element.SignLine1.strokeStyle;
- this.context.setLineDash([3, 2, 2]);
- } else if (element.name == ElementEvents.SignLine2) {
- this.context.lineWidth =
- Style.Element.SignLine2.lineWidth * coordinate.ratio;
- this.context.strokeStyle = Style.Element.SignLine2.strokeStyle;
- this.context.setLineDash([3, 2, 2]);
- }
- this.context.beginPath();
- this.context.moveTo(start.x, start.y);
- this.context.lineTo(end.x, end.y);
- this.context.stroke();
- this.context.restore();
- // if (element.name == ElementEvents.NewRoad) {
- // //添加测量值
- // this.drawMeasureTxt(element.start, element.end);
- // }
- // this.context.save();
- // this.context.lineWidth = Style.Point.lineWidth * coordinate.ratio;
- // this.context.strokeStyle = Style.Point.strokeStyle;
- // this.context.fillStyle = Style.Point.fillStyle;
- // let radius = Style.Point.radius;
- // this.context.beginPath();
- // this.context.arc(
- // start.x,
- // start.y,
- // radius * coordinate.ratio,
- // 0,
- // Math.PI * 2,
- // true
- // );
- // this.context.stroke();
- // this.context.fill();
- // this.context.restore();
- }
- //由多个点构成,里面的坐标都已经是屏幕坐标
- drawMeasure(points, dir, styleType) {
- this.context.save();
- this.context.strokeStyle = Style.Measure.strokeStyle;
- this.context.lineWidth = Style.Measure.lineWidth * coordinate.ratio;
- if (styleType) {
- if (styleType == "style-1") {
- this.context.lineWidth =
- Style.DownLoad.style1.Measure.lineWidth * coordinate.ratio;
- this.context.strokeStyle = Style.DownLoad.style1.Measure.strokeStyle;
- } else if (styleType == "style-2") {
- this.context.lineWidth =
- Style.DownLoad.style1.Measure.lineWidth * coordinate.ratio;
- this.context.strokeStyle = Style.DownLoad.style1.Measure.strokeStyle;
- } else if (styleType == "style-3") {
- this.context.lineWidth =
- Style.DownLoad.style1.Measure.lineWidth * coordinate.ratio;
- this.context.strokeStyle = Style.DownLoad.style1.Measure.strokeStyle;
- } else if (styleType == "style-4") {
- this.context.lineWidth =
- Style.DownLoad.style1.Measure.lineWidth * coordinate.ratio;
- this.context.strokeStyle = Style.DownLoad.style1.Measure.strokeStyle;
- }
- }
- for (let i = 0; i < points.length - 1; ++i) {
- let start = coordinate.getScreenXY(points[i]);
- let end = coordinate.getScreenXY(points[i + 1]);
- let angle = 0;
- if (dir == "top") {
- start.y = measureService.region.top * coordinate.ratio;
- end.y = measureService.region.top * coordinate.ratio;
- } else if (dir == "bottom") {
- start.y = measureService.region.bottom * coordinate.ratio;
- end.y = measureService.region.bottom * coordinate.ratio;
- } else if (dir == "left") {
- start.x = measureService.region.left * coordinate.ratio;
- end.x = measureService.region.left * coordinate.ratio;
- angle = (-90 / 180) * Math.PI;
- } else if (dir == "right") {
- start.x = measureService.region.right * coordinate.ratio;
- end.x = measureService.region.right * coordinate.ratio;
- angle = (90 / 180) * Math.PI;
- }
- let line = mathUtil.createLine1(start, end);
- if (line == null) {
- continue;
- }
- let lines = mathUtil.getParallelLineForDistance(
- line,
- 6 * coordinate.ratio
- );
- let start1 = mathUtil.getJoinLinePoint(start, lines.line1);
- let end1 = mathUtil.getJoinLinePoint(end, lines.line1);
- let start2 = mathUtil.getJoinLinePoint(start, lines.line2);
- let end2 = mathUtil.getJoinLinePoint(end, lines.line2);
- this.context.beginPath();
- this.context.moveTo(start1.x, start1.y);
- this.context.lineTo(start2.x, start2.y);
- this.context.stroke();
- this.context.beginPath();
- this.context.moveTo(end1.x, end1.y);
- this.context.lineTo(end2.x, end2.y);
- this.context.stroke();
- let mid = {
- x: (start.x + end.x) / 2,
- y: (start.y + end.y) / 2,
- };
- let vLine = mathUtil.getVerticalLine(line, mid);
- lines = mathUtil.getParallelLineForDistance(vLine, 22 * coordinate.ratio);
- let join1 = mathUtil.getIntersectionPoint(line, lines.line1);
- let join2 = mathUtil.getIntersectionPoint(line, lines.line2);
- if (
- mathUtil.getDistance(start, join1) < mathUtil.getDistance(start, mid)
- ) {
- let measureValue = mathUtil.getDistance(points[i], points[i + 1]);
- if (measureService.unit == "ft") {
- measureValue =
- " " +
- uoMService.convert(
- measureValue,
- "distance",
- void 0,
- "imperial",
- 0.01,
- true
- ) +
- " ";
- //measureValue = mathUtil.getFixed(measureValue / measureService.ftUnit, 2) + 'ft'
- } else {
- measureValue = mathUtil.getFixed(measureValue, 2) + "m";
- }
- if (
- mathUtil.getDistance(start, end) >
- this.context.measureText(measureValue).width
- ) {
- this.context.beginPath();
- this.context.moveTo(start.x, start.y);
- this.context.lineTo(join1.x, join1.y);
- this.context.stroke();
- this.context.beginPath();
- this.context.moveTo(join2.x, join2.y);
- this.context.lineTo(end.x, end.y);
- this.context.stroke();
- this.context.save();
- if (coordinate.ratio == Constant.ratio) {
- this.context.font = "36px Microsoft YaHei";
- } else {
- this.context.font = "12px Microsoft YaHei";
- }
- if (styleType == "style-1" || styleType == "style-3") {
- this.context.fillStyle = "#000000";
- this.context.strokeStyle = "#000000";
- } else {
- this.context.fillStyle = "#FFFFFF";
- this.context.strokeStyle = "#FFFFFF";
- }
- this.context.textAlign = "center";
- this.context.textBaseline = "middle";
- this.context.miterLimit = 10;
- this.context.direction = "ltr";
- if (angle) {
- this.context.translate(mid.x, mid.y);
- this.context.rotate(angle);
- this.context.fillText(measureValue, 0, 0);
- } else {
- this.context.fillText(measureValue, mid.x, mid.y);
- }
- this.context.restore();
- } else {
- this.context.beginPath();
- this.context.moveTo(start.x, start.y);
- this.context.lineTo(end.x, end.y);
- this.context.stroke();
- }
- } else {
- let measureValue = mathUtil.getDistance(points[i], points[i + 1]);
- if (measureService.unit == "ft") {
- //measureValue = mathUtil.getFixed(measureValue / measureService.ftUnit, 2) + 'ft'
- measureValue =
- " " +
- uoMService.convert(
- measureValue,
- "distance",
- void 0,
- "imperial",
- 0.01,
- true
- ) +
- " ";
- } else {
- measureValue = mathUtil.getFixed(measureValue, 2) + "m";
- }
- if (
- mathUtil.getDistance(start, end) >
- this.context.measureText(measureValue).width
- ) {
- this.context.beginPath();
- this.context.moveTo(start.x, start.y);
- this.context.lineTo(join2.x, join2.y);
- this.context.stroke();
- this.context.beginPath();
- this.context.moveTo(join1.x, join1.y);
- this.context.lineTo(end.x, end.y);
- this.context.stroke();
- this.context.save();
- if (coordinate.ratio == Constant.ratio) {
- this.context.font = "36px Microsoft YaHei";
- } else {
- this.context.font = "12px Microsoft YaHei";
- }
- if (styleType == "style-1" || styleType == "style-3") {
- this.context.fillStyle = "#000000";
- this.context.strokeStyle = "#000000";
- } else {
- this.context.fillStyle = "#FFFFFF";
- this.context.strokeStyle = "#FFFFFF";
- }
- this.context.textAlign = "center";
- this.context.textBaseline = "middle";
- this.context.miterLimit = 10;
- this.context.direction = "ltr";
- if (angle) {
- this.context.translate(mid.x, mid.y);
- this.context.rotate(angle);
- this.context.fillText(measureValue, 0, 0);
- } else {
- this.context.fillText(measureValue, mid.x, mid.y);
- }
- this.context.restore();
- } else {
- this.context.beginPath();
- this.context.moveTo(start.x, start.y);
- this.context.lineTo(end.x, end.y);
- this.context.stroke();
- }
- }
- }
- this.context.restore();
- }
- drawMeasureTxt(startPoint, endPoint) {
- const _startPoint = coordinate.getScreenXY(startPoint);
- const _endPoint = coordinate.getScreenXY(endPoint);
- const measureInterval = 10;
- const line = mathUtil.createLine1(_startPoint, _endPoint);
- if (line == null) {
- return;
- }
- let mid = {
- x: (_startPoint.x + _endPoint.x) / 2,
- y: (_startPoint.y + _endPoint.y) / 2,
- };
- const lines = mathUtil.getParallelLineForDistance(line, measureInterval);
- let pLine = null;
- let mid1 = mathUtil.getJoinLinePoint(mid, lines.line1);
- let mid2 = mathUtil.getJoinLinePoint(mid, lines.line2);
- if (mid.y < mid1.y) {
- mathUtil.clonePoint(mid, mid2);
- pLine = lines.line2;
- } else {
- mathUtil.clonePoint(mid, mid1);
- pLine = lines.line1;
- }
- let measureDistance = mathUtil.getDistance(startPoint, endPoint);
- if (measureService.unit == "ft") {
- //measureDistance = mathUtil.getFixed(measureDistance / measureService.ftUnit, 2) + 'ft'
- measureDistance =
- " " +
- uoMService.convert(
- measureDistance,
- "distance",
- void 0,
- "imperial",
- 0.01,
- true
- ) +
- " ";
- } else {
- measureDistance = mathUtil.getFixed(measureDistance, 2) + "m";
- }
- const fontWidth = this.context.measureText(measureDistance).width;
- let vLine = mathUtil.getLineForPoint(line, mid);
- const vLines = mathUtil.getParallelLineForDistance(vLine, fontWidth / 2);
- let startJoin = mathUtil.getIntersectionPoint(vLines.line1, pLine);
- startJoin = {
- x: Math.round(startJoin.x),
- y: Math.round(startJoin.y),
- };
- let endJoin = mathUtil.getIntersectionPoint(vLines.line2, pLine);
- endJoin = {
- x: Math.round(endJoin.x),
- y: Math.round(endJoin.y),
- };
- if (startJoin.x < endJoin.x) {
- mathUtil.clonePoint(mid, startJoin);
- } else if (startJoin.x > endJoin.x) {
- mathUtil.clonePoint(mid, endJoin);
- } else if (startJoin.y < endJoin.y) {
- mathUtil.clonePoint(mid, startJoin);
- } else {
- mathUtil.clonePoint(mid, endJoin);
- }
- //const fontStart = mathUtil.getDisPointsLine(line, mid, fontWidth / 2, fontWidth / 2)
- // let a1, a2
- let angle = null;
- if (typeof line.a !== "undefined") {
- angle = Math.atan(line.a);
- } else if (line.hasOwnProperty("x")) {
- angle = Math.PI / 2;
- } else {
- angle = 0;
- }
- this.context.save();
- this.context.fillStyle = Style.Measure.txt;
- this.context.translate(mid.x, mid.y);
- this.context.rotate(angle);
- this.context.fillText(measureDistance, 0, 0);
- /*
- if (fontStart.newpoint1.x > fontStart.newpoint2.x) {
- this.context.translate(mid.x, mid.y)
- this.context.rotate(angle)
- //this.context.strokeText(measureDistance, 0, 0);
- this.context.fillText(measureDistance, 0, 0)
- // a1 = fontStart.newpoint2
- // a2 = fontStart.newpoint1
- } else if (fontStart.newpoint1.x < fontStart.newpoint2.x) {
- this.context.translate(mid.x, mid.y)
- this.context.rotate(angle)
- //this.context.strokeText(measureDistance, 0, 0);
- this.context.fillText(measureDistance, 0, 0)
- // a1 = fontStart.newpoint1
- // a2 = fontStart.newpoint2
- } else if (fontStart.newpoint1.y < fontStart.newpoint2.y) {
- this.context.translate(mid.x, mid.y)
- this.context.rotate(angle)
- //this.context.strokeText(measureDistance, 0, 0);
- this.context.fillText(measureDistance, 0, 0)
- // a2 = fontStart.newpoint2
- // a1 = fontStart.newpoint1
- } else {
- this.context.translate(mid.x, mid.y)
- this.context.rotate(angle)
- //this.context.strokeText(measureDistance, 0, 0);
- this.context.fillText(measureDistance, 0, 0)
- // a2 = fontStart.newpoint1
- // a1 = fontStart.newpoint2
- }
- */
- this.context.restore();
- }
- setCanvasStyle(style) {
- for (const key in style) {
- if (key != "isFill" && key != "isStroke") {
- this.context[key] = style[key];
- }
- }
- }
- /*************************************************************************************家具**********************************************************************************************/
- /***************************************************************************************************************************************************************************************/
- }
- const draw = new Draw();
- export { draw };
|