提交 7fe21fe5 编写于 作者: Q qq_41923622

Thu Feb 6 14:45:00 CST 2025 inscode

上级 cac48b2a
...@@ -71,7 +71,7 @@ const useDownload = () => { ...@@ -71,7 +71,7 @@ const useDownload = () => {
annotations.push(annotation); annotations.push(annotation);
}) })
// 打印标注信息 // 打印标注信息
console.log('用户标注信息:', annotations,canvas.backgroundImage._element.currentSrc); console.log('用户标注信息:', annotations,canvas.backgroundImage, canvas.backgroundImage._element.currentSrc);
let currentSrc = canvas.backgroundImage._element.currentSrc let currentSrc = canvas.backgroundImage._element.currentSrc
let currentSrcSplit = currentSrc.split('/') let currentSrcSplit = currentSrc.split('/')
...@@ -108,6 +108,10 @@ const useDownload = () => { ...@@ -108,6 +108,10 @@ const useDownload = () => {
}; };
data[deviceId+'_'+imgkey]={ data[deviceId+'_'+imgkey]={
annotations:annotations, annotations:annotations,
imageWidth: canvas.backgroundImage.width,
imageHeight: canvas.backgroundImage.height,
originalCanvasWidth: canvas.getWidth(),
originalCanvasHeight: canvas.getHeight(),
imgUrl: currentSrc, imgUrl: currentSrc,
imgTitle: imageTitle, imgTitle: imageTitle,
} }
......
...@@ -7,25 +7,35 @@ import {fabric} from 'fabric'; ...@@ -7,25 +7,35 @@ import {fabric} from 'fabric';
import ArrowClass from '../helpers/Arrow'; import ArrowClass from '../helpers/Arrow';
function annotationsInit(annotations,canvas,historyRef){ function annotationsInit(imgData,canvas,historyRef){
// 获取当前图片和画布尺寸
const currentImageWidth = canvas.backgroundImage.width;
const currentImageHeight = canvas.backgroundImage.height;
const currentCanvasWidth = canvas.getWidth();
const currentCanvasHeight = canvas.getHeight();
let imageWidth = imgData.originalCanvasWidth||imgData.imgWidth;
let imageHeight = imgData.originalCanvasHeight||imgData.imgHeight;
// 计算缩放比例
const scaleX = currentCanvasWidth / imageWidth;
const scaleY = currentCanvasHeight / imageHeight;
console.log("scaleX,scaleY ",scaleX,scaleY,currentCanvasWidth,currentCanvasHeight,currentImageWidth,currentImageHeight,imageWidth,imageHeight,imgData)
// 遍历标注信息,重新创建元素并添加到画布上 // 遍历标注信息,重新创建元素并添加到画布上
annotations.forEach((annotation) => { imgData.annotations.forEach((annotation) => {
let newObject; let newObject;
switch (annotation.type) { switch (annotation.type) {
case 'rect': case 'rect':
newObject = new fabric.Rect({ newObject = new fabric.Rect({
left: annotation.left, left: annotation.left* scaleX,
top: annotation.top, top: annotation.top* scaleY,
originX: 'left', originX: 'left',
originY: 'top', originY: 'top',
width: annotation.width, width: annotation.width* scaleX,
height: annotation.height, height: annotation.height* scaleY,
angle: annotation.angle||0, angle: annotation.angle||0,
fill: annotation.fill || 'rgba(255,0,0,0)', fill: annotation.fill || 'rgba(255,0,0,0)',
stroke: annotation.stroke, stroke: annotation.stroke,
strokeWidth: annotation.size, strokeWidth: annotation.size* Math.max(scaleX, scaleY),
transparentCorners: false, transparentCorners: false,
opacity: annotation.opacity || 1 opacity: annotation.opacity || 1
}); });
...@@ -34,9 +44,9 @@ function annotationsInit(annotations,canvas,historyRef){ ...@@ -34,9 +44,9 @@ function annotationsInit(annotations,canvas,historyRef){
case 'i-text': case 'i-text':
case 'text': case 'text':
newObject = new fabric.IText(annotation.text, { newObject = new fabric.IText(annotation.text, {
left: annotation.left, left: annotation.left* scaleX,
top: annotation.top, top: annotation.top* scaleY,
width: annotation.width, width: annotation.width* scaleX,
fontSize: annotation.fontSize, fontSize: annotation.fontSize,
fill: annotation.fill, fill: annotation.fill,
id: Math.random() * 4 + '_' + Date.now(), id: Math.random() * 4 + '_' + Date.now(),
...@@ -45,31 +55,40 @@ function annotationsInit(annotations,canvas,historyRef){ ...@@ -45,31 +55,40 @@ function annotationsInit(annotations,canvas,historyRef){
break; break;
case 'circle': case 'circle':
newObject = new fabric.Circle({ newObject = new fabric.Circle({
left: annotation.left, left: annotation.left* scaleX,
top: annotation.top, top: annotation.top* scaleY,
radius: annotation.width / 2, radius: (annotation.width / 2)* Math.max(scaleX, scaleY),
angle: annotation.angle||0, angle: annotation.angle||0,
fill: annotation.fill||'', fill: annotation.fill||'',
originX: 'left', originX: 'left',
originY: 'top', originY: 'top',
stroke: annotation.stroke, stroke: annotation.stroke,
strokeWidth: annotation.size, strokeWidth: annotation.size* Math.max(scaleX, scaleY),
opacity: annotation.opacity || 1 opacity: annotation.opacity || 1
}); });
break; break;
case 'arrow' : case 'arrow' :
newObject = new ArrowClass(annotation.pointList||[annotation.left,annotation.top,annotation.left,annotation.top], { newObject = new ArrowClass(annotation.pointList||[annotation.left,annotation.top,annotation.left,annotation.top], {
strokeWidth: annotation.size, strokeWidth: annotation.size* Math.max(scaleX, scaleY),
stroke: annotation.stroke, stroke: annotation.stroke,
id: new Date().valueOf() + '_' + Math.random() * 4, id: new Date().valueOf() + '_' + Math.random() * 4,
}); });
newObject.strokeUniform = true; newObject.strokeUniform = true;
break; break;
case 'path' : case 'path' :
newObject = new fabric.Path(annotation.path, { const adjustedPath = annotation.path.map((segment) => {
if (typeof segment[1] === 'number') {
segment[1] *= scaleX;
}
if (typeof segment[2] === 'number') {
segment[2] *= scaleY;
}
return segment;
});
newObject = new fabric.Path(adjustedPath, {
fill: annotation.fill, fill: annotation.fill,
stroke: annotation.stroke, stroke: annotation.stroke,
strokeWidth: annotation.size, strokeWidth: annotation.size* Math.max(scaleX, scaleY),
opacity: annotation.opacity || 1 opacity: annotation.opacity || 1
}); });
break; break;
...@@ -107,7 +126,7 @@ export const useUpload = () => { ...@@ -107,7 +126,7 @@ export const useUpload = () => {
setTimeout(()=>{ setTimeout(()=>{
if(imgData){ if(imgData){
annotationsInit(imgData.annotations,canvas,historyRef); annotationsInit(imgData,canvas,historyRef);
} }
}) })
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册