Commit 3f2391a4 authored by hucy's avatar hucy

fix:不想写不想写

parent b54c6275
...@@ -20,7 +20,9 @@ export const getBoundingBox = function (path: Path[]) { ...@@ -20,7 +20,9 @@ export const getBoundingBox = function (path: Path[]) {
let min_x = 0, let min_x = 0,
min_y = 0, min_y = 0,
max_x = 0, max_x = 0,
max_y = 0; max_y = 0,
width = 0,
height = 0;
const center = { x: 0, y: 0 }; const center = { x: 0, y: 0 };
let resPath: Path[] = []; let resPath: Path[] = [];
const length = path.length; const length = path.length;
...@@ -60,13 +62,20 @@ export const getBoundingBox = function (path: Path[]) { ...@@ -60,13 +62,20 @@ export const getBoundingBox = function (path: Path[]) {
} else if (length >= 2) { } else if (length >= 2) {
if (min_x === max_x) { if (min_x === max_x) {
// 垂直的线 // 垂直的线
width = 0;
height = Decimal.sub(max_y, min_y).toNumber();
center.x = min_x; center.x = min_x;
center.y = Decimal.sub(max_y, min_y).div(2).plus(min_y).toNumber(); center.y = Decimal.sub(max_y, min_y).div(2).plus(min_y).toNumber();
} else if (min_y === max_y) { } else if (min_y === max_y) {
// 水平的线 // 水平的线
width = Decimal.sub(max_x, min_x).toNumber();
height = 0;
center.y = min_y; center.y = min_y;
center.x = Decimal.sub(max_x, min_x).div(2).plus(min_x).toNumber(); center.x = Decimal.sub(max_x, min_x).div(2).plus(min_x).toNumber();
} else { } else {
width = Decimal.sub(max_x, min_x).toNumber();
height = Decimal.sub(max_y, min_y).toNumber();
center.x = Decimal.sub(max_x, min_x).div(2).plus(min_x).toNumber(); center.x = Decimal.sub(max_x, min_x).div(2).plus(min_x).toNumber();
center.y = Decimal.sub(max_y, min_y).div(2).plus(min_y).toNumber(); center.y = Decimal.sub(max_y, min_y).div(2).plus(min_y).toNumber();
...@@ -98,5 +107,7 @@ export const getBoundingBox = function (path: Path[]) { ...@@ -98,5 +107,7 @@ export const getBoundingBox = function (path: Path[]) {
max_y, max_y,
path: resPath, path: resPath,
center, center,
width,
height,
}; };
}; };
...@@ -88,26 +88,29 @@ function createShape(itemData: any) { ...@@ -88,26 +88,29 @@ function createShape(itemData: any) {
const path = itemData.path || []; const path = itemData.path || [];
const fillColor = itemData.color || '#000000'; const fillColor = itemData.color || '#000000';
const name = itemData.name; const name = itemData.name;
const boundingBox = itemData.boundingBox || {};
const boundingBoxPath = itemData.boundingBox?.path || []; const boundingBoxPath = itemData.boundingBox?.path || [];
if (boundingBoxPath.length < 2) { if (boundingBoxPath.length < 2) {
console.warn('小于两个点,不能构成多边形,没有边界框'); console.warn('小于两个点,不能构成多边形,没有边界框');
} else { } else {
let group = new Konva.Group({ let group = new Konva.Group({
x: boundingBoxPath[0].x, x: 0,
y: boundingBoxPath[0].y, y: 0,
draggable: true, draggable: true,
}); });
// let rectBox = new Konva.Rect({ let rectBox = new Konva.Rect({
// x: group.x(), x: boundingBoxPath[0].x,
// y: group.y(), y: boundingBoxPath[0].y,
// width: state.boundingBox.width, width: boundingBox.width,
// height: state.boundingBox.height, height: boundingBox.height,
// stroke: 'black', stroke: 'red',
// strokeWidth: 10, // fill: 'grey',
// }); strokeWidth: 4,
});
// 如果添加到group,则坐标的相对位置时基于group的位置
let shape: any = new Konva.Shape({ let shape: any = new Konva.Shape({
sceneFunc: function (context, shape) { sceneFunc: function (context, shape) {
context.beginPath(); context.beginPath();
...@@ -133,6 +136,7 @@ function createShape(itemData: any) { ...@@ -133,6 +136,7 @@ function createShape(itemData: any) {
// stroke: 'white', // stroke: 'white',
// strokeWidth: 10, // strokeWidth: 10,
}); });
group.add(rectBox);
group.add(shape); group.add(shape);
return group; return group;
...@@ -270,6 +274,7 @@ function drawGrid() { ...@@ -270,6 +274,7 @@ function drawGrid() {
</script> </script>
<template> <template>
<div class="konva-main-page container-height center"> <div class="konva-main-page container-height center">
<div>666</div>
<div class="canvas-box"> <div class="canvas-box">
<canvas <canvas
id="canvas-grid" id="canvas-grid"
...@@ -290,6 +295,6 @@ function drawGrid() { ...@@ -290,6 +295,6 @@ function drawGrid() {
width: 1200px; width: 1200px;
height: 800px; height: 800px;
border: 1px solid #000; border: 1px solid #000;
background: pink; // background: pink;
} }
</style> </style>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment