|
@@ -4,10 +4,14 @@ import {
|
|
BoundQueryPlugin,
|
|
BoundQueryPlugin,
|
|
setGenerateStartId,
|
|
setGenerateStartId,
|
|
inRevise,
|
|
inRevise,
|
|
|
|
+ Poi,
|
|
|
|
+ PoiAttrib,
|
|
} from "../../board";
|
|
} from "../../board";
|
|
import { PolygonsAttrib } from "./type";
|
|
import { PolygonsAttrib } from "./type";
|
|
import { Map } from "ol";
|
|
import { Map } from "ol";
|
|
import { boundingExtent } from "ol/extent";
|
|
import { boundingExtent } from "ol/extent";
|
|
|
|
+import "./icon";
|
|
|
|
+import { Scale, ScaleAttrib } from "../../board/packages/scale/scale";
|
|
|
|
|
|
const boundJoinMap = (
|
|
const boundJoinMap = (
|
|
boundQuery: BoundQueryPlugin,
|
|
boundQuery: BoundQueryPlugin,
|
|
@@ -28,6 +32,8 @@ const boundJoinMap = (
|
|
size: map.getSize(),
|
|
size: map.getSize(),
|
|
padding: [0, 0, 0, 0], // 根据需要调整边距
|
|
padding: [0, 0, 0, 0], // 根据需要调整边距
|
|
});
|
|
});
|
|
|
|
+
|
|
|
|
+ // console.log(`每个像素代表的米数: ${metersPerPixel}`);
|
|
};
|
|
};
|
|
|
|
|
|
const setBoardBound = () => {
|
|
const setBoardBound = () => {
|
|
@@ -49,6 +55,7 @@ const boundJoinMap = (
|
|
mapView = bindMap.getView();
|
|
mapView = bindMap.getView();
|
|
mapView.addEventListener("change:center", setBoardBound);
|
|
mapView.addEventListener("change:center", setBoardBound);
|
|
mapView.addEventListener("change:resolution", setBoardBound);
|
|
mapView.addEventListener("change:resolution", setBoardBound);
|
|
|
|
+
|
|
map.addEventListener("moveend", setBoardBound);
|
|
map.addEventListener("moveend", setBoardBound);
|
|
map = bindMap;
|
|
map = bindMap;
|
|
setBoardBound();
|
|
setBoardBound();
|
|
@@ -78,8 +85,22 @@ const boundJoinMap = (
|
|
};
|
|
};
|
|
};
|
|
};
|
|
|
|
|
|
|
|
+function captureMap(canvas: HTMLCanvasElement, map: Map, pixelRatio) {
|
|
|
|
+ // 创建一个临时canvas用于绘制截图
|
|
|
|
+ const size = map.getSize();
|
|
|
|
+ canvas.width = size[0] * pixelRatio;
|
|
|
|
+ canvas.height = size[1] * pixelRatio;
|
|
|
|
+ canvas.style.width = size[0] + "px";
|
|
|
|
+ canvas.style.height = size[1] + "px";
|
|
|
|
+
|
|
|
|
+ const mapContext = canvas.getContext("2d");
|
|
|
|
+ const mapCanvas = map.getViewport().querySelector("canvas")!;
|
|
|
|
+ mapContext.drawImage(mapCanvas, 0, 0, canvas.width, canvas.height);
|
|
|
|
+ return canvas.toDataURL();
|
|
|
|
+}
|
|
|
|
+
|
|
const initBoard = register(
|
|
const initBoard = register(
|
|
- { polygons: Polygons },
|
|
|
|
|
|
+ { polygons: Polygons, compass: Poi, scale: Scale },
|
|
{ bound: new BoundQueryPlugin() }
|
|
{ bound: new BoundQueryPlugin() }
|
|
);
|
|
);
|
|
setGenerateStartId(5000000);
|
|
setGenerateStartId(5000000);
|
|
@@ -90,10 +111,24 @@ export const createBoard = (
|
|
map?: Map;
|
|
map?: Map;
|
|
} = {}
|
|
} = {}
|
|
) => {
|
|
) => {
|
|
|
|
+ const compass: PoiAttrib = {
|
|
|
|
+ id: "0",
|
|
|
|
+ x: 0,
|
|
|
|
+ y: 0,
|
|
|
|
+ type: "compass",
|
|
|
|
+ };
|
|
|
|
+ const scale: ScaleAttrib = {
|
|
|
|
+ id: "0",
|
|
|
|
+ left: 20,
|
|
|
|
+ bottom: 20,
|
|
|
|
+ minWidth: 100,
|
|
|
|
+ maxWidth: 150,
|
|
|
|
+ };
|
|
|
|
+
|
|
const data = props.store || [
|
|
const data = props.store || [
|
|
{ id: "0", points: [], polygons: [], lines: [] },
|
|
{ id: "0", points: [], polygons: [], lines: [] },
|
|
];
|
|
];
|
|
- const board = initBoard(props.dom, { polygons: data }, false);
|
|
|
|
|
|
+ const board = initBoard(props.dom, { polygons: data, compass, scale }, false);
|
|
const mapJoin = boundJoinMap(board.bound, props.dom, props.map);
|
|
const mapJoin = boundJoinMap(board.bound, props.dom, props.map);
|
|
|
|
|
|
const setProps = (props: { dom?: HTMLDivElement; map?: Map } = {}) => {
|
|
const setProps = (props: { dom?: HTMLDivElement; map?: Map } = {}) => {
|
|
@@ -106,15 +141,31 @@ export const createBoard = (
|
|
}
|
|
}
|
|
};
|
|
};
|
|
setProps(props);
|
|
setProps(props);
|
|
|
|
+ let captureCanvas: HTMLCanvasElement;
|
|
|
|
+
|
|
|
|
+ const scaleEntity = board.tree.entrys.scale[0] as Scale;
|
|
|
|
+ scaleEntity.allowable = 0.00000000001;
|
|
|
|
+ scaleEntity.getScaleUnit = (data: number[]) => {
|
|
|
|
+ if (!props.map) return;
|
|
|
|
+ const mapView = props.map.getView();
|
|
|
|
+ const projection = mapView.getProjection();
|
|
|
|
+ const mpu = projection.getMetersPerUnit();
|
|
|
|
+ const metersPerPixel = data[0] * mpu;
|
|
|
|
+ return {
|
|
|
|
+ value: metersPerPixel,
|
|
|
|
+ unit: " m",
|
|
|
|
+ };
|
|
|
|
+ };
|
|
|
|
|
|
return {
|
|
return {
|
|
raw: board,
|
|
raw: board,
|
|
|
|
+ scale: scaleEntity,
|
|
setProps,
|
|
setProps,
|
|
getData() {
|
|
getData() {
|
|
return board.getData().polygons[0];
|
|
return board.getData().polygons[0];
|
|
},
|
|
},
|
|
setData(polygon: PolygonsAttrib & { id: string }) {
|
|
setData(polygon: PolygonsAttrib & { id: string }) {
|
|
- board.setData({ polygons: [polygon] });
|
|
|
|
|
|
+ board.setData({ polygons: [polygon], compass, scale });
|
|
},
|
|
},
|
|
destory() {
|
|
destory() {
|
|
mapJoin.destory();
|
|
mapJoin.destory();
|
|
@@ -126,6 +177,45 @@ export const createBoard = (
|
|
getPixelFromCoordinate(point: number[]) {
|
|
getPixelFromCoordinate(point: number[]) {
|
|
return board.tree.getPixelFromStage(point);
|
|
return board.tree.getPixelFromStage(point);
|
|
},
|
|
},
|
|
|
|
+ async toDataURL(pixelRatio = 1) {
|
|
|
|
+ const boardDataURL = board.tree.stage.toDataURL({ pixelRatio });
|
|
|
|
+ if (!props.map) {
|
|
|
|
+ return boardDataURL;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ const size = [board.tree.stage.width(), board.tree.stage.height()];
|
|
|
|
+ if (!captureCanvas) {
|
|
|
|
+ captureCanvas = document.createElement("canvas");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ captureCanvas.width = size[0] * pixelRatio;
|
|
|
|
+ captureCanvas.height = size[1] * pixelRatio;
|
|
|
|
+ captureCanvas.style.width = size[0] + "px";
|
|
|
|
+ captureCanvas.style.height = size[1] + "px";
|
|
|
|
+
|
|
|
|
+ const captureCtx = captureCanvas.getContext("2d");
|
|
|
|
+ captureCtx.clearRect(0, 0, captureCanvas.width, captureCanvas.height);
|
|
|
|
+
|
|
|
|
+ captureCtx.save();
|
|
|
|
+ captureMap(captureCanvas, props.map, pixelRatio);
|
|
|
|
+ captureCtx.restore();
|
|
|
|
+ return new Promise<string>((resolve, reject) => {
|
|
|
|
+ const image = new Image();
|
|
|
|
+ image.src = boardDataURL;
|
|
|
|
+ image.onload = () => {
|
|
|
|
+ captureCtx.drawImage(
|
|
|
|
+ image,
|
|
|
|
+ 0,
|
|
|
|
+ 0,
|
|
|
|
+ captureCanvas.width,
|
|
|
|
+ captureCanvas.height
|
|
|
|
+ );
|
|
|
|
+ const dataURL = captureCanvas.toDataURL("image/png");
|
|
|
|
+ resolve(dataURL);
|
|
|
|
+ };
|
|
|
|
+ image.onerror = reject;
|
|
|
|
+ });
|
|
|
|
+ },
|
|
};
|
|
};
|
|
};
|
|
};
|
|
|
|
|