feat: 限制元素在画布内拖地

master
JINGYJ 9 months ago
parent 7df7fda0ed
commit c0f96ae2a7

@ -105,6 +105,41 @@ const fontAttr = reactive({
overline: false overline: false
}); });
//
const getCanvasBoundaries = () => {
const canvasObj = canvasEditor.canvas.getObjects()[1];
return {
width: canvasObj.width,
height: canvasObj.height
};
};
const constrainObjectWithinCanvas = obj => {
const { width, height } = getCanvasBoundaries();
//
const objLeft = obj.left;
const objTop = obj.top;
const objWidth = obj.width * obj.scaleX;
const objHeight = obj.height * obj.scaleY;
//
if (objLeft < 0) {
obj.left = 0;
}
if (objTop < 0) {
obj.top = 0;
}
if (objLeft + objWidth > width) {
obj.left = width - objWidth;
}
if (objTop + objHeight > height) {
obj.top = height - objHeight;
}
//
obj.setCoords();
};
const getObjectAttr = e => { const getObjectAttr = e => {
const activeObject = canvasEditor.canvas.getActiveObject(); const activeObject = canvasEditor.canvas.getActiveObject();
// obj // obj
@ -159,6 +194,14 @@ const init = () => {
event.on("selectCancel", selectCancel); event.on("selectCancel", selectCancel);
event.on("selectOne", getObjectAttr); event.on("selectOne", getObjectAttr);
canvasEditor.canvas.on("object:modified", getObjectAttr); canvasEditor.canvas.on("object:modified", getObjectAttr);
// object:moving
canvasEditor.canvas.on("object:moving", e => {
const activeObject = e.target;
if (activeObject) {
constrainObjectWithinCanvas(activeObject);
canvasEditor.canvas.renderAll();
}
});
}; };
// //
@ -260,6 +303,7 @@ onBeforeUnmount(() => {
event.off("selectCancel", selectCancel); event.off("selectCancel", selectCancel);
event.off("selectOne", getObjectAttr); event.off("selectOne", getObjectAttr);
canvasEditor.canvas.off("object:modified", getObjectAttr); canvasEditor.canvas.off("object:modified", getObjectAttr);
canvasEditor.canvas.off("object:moving");
}); });
</script> </script>
<template> <template>

@ -65,7 +65,7 @@
} }
.point_detail_wrap { .point_detail_wrap {
.deviceOfPoint_wrap { .deviceOfPoint_wrap {
background-color: red; background-color: goldenrod;
height: calc(100vh - 290px); height: calc(100vh - 290px);
} }
.footer_btns { .footer_btns {

Loading…
Cancel
Save