--我试图在react中实现fabric.js。我有用fabric.js绘制线条的js代码,我想把它转换成反应代码。有人能帮我吗?这是我的js密码。它在纯js中运行良好,但没有反应。;
let canvas = new fabric.Canvas("canvas", {
width: 600,
height: 400,
backgroundColor: "#808080"
});
let lineBtn = document.getElementById("adding-line");
let newLineCoor = {};
let line;
let isMouseDown = false;
let addingLineBtnClicked = false;
const activateAddingLine = () => {
if (addingLineBtnClicked === false) {
addingLineBtnClicked = true;
canvas.on("mouse:down", startAddingLine);
canvas.on("mouse:move", startDrawingLine);
canvas.on("mouse:up", stopDrawingLine);
canvas.selection = false;
// canvas.defaultCursor = "auto";
canvas.hoverCursor = "auto";
canvas.getObjects().forEach((element) => {
if (element.id === "added-line") {
element.set({
selectable: false
});
}
});
}
};
lineBtn.addEventListener("click", activateAddingLine);
const startAddingLine = (o) => {
isMouseDown = true;
let pointer = canvas.getPointer(o.e);
line = new fabric.Line([pointer.x, pointer.y, pointer.x, pointer.y], {
id: "added-line",
stroke: "red",
strokeWidth: 3,
selectable: false
});
canvas.add(line);
canvas.requestRenderAll();
};
const startDrawingLine = (o) => {
if (isMouseDown === true) {
let pointer = canvas.getPointer(o.e);
line.set({
x2: pointer.x,
y2: pointer.y
});
canvas.requestRenderAll();
}
};
const stopDrawingLine = () => {
line.setCoords();
isMouseDown = false;
};
const deactiveLine = () => {
canvas.off("mouse:down", startAddingLine);
canvas.off("mouse:move", startDrawingLine);
canvas.off("mouse:up", stopDrawingLine);
canvas.getObjects().forEach((element) => {
if (element.id === "added-line") {
element.set({
selectable: true
});
}
});
canvas.hoverCursor = "all-scroll";
addingLineBtnClicked = false;
};
document
.getElementById("deactivate-line")
.addEventListener("click", deactiveLine);
const addingControlPoints = (o) => {
let obj = o.target;
if (!obj) {
return;
}
let pointer1 = new fabric.Circle({
radius: obj.strokeWidth * 2,
fill: "blue",
opacity: 0.7,
top: newLineCoor.y1,
left: newLineCoor.x1,
originX: "center",
originY: "center"
});
let pointer2 = new fabric.Circle({
radius: obj.strokeWidth * 2,
fill: "blue",
opacity: 0.7,
top: newLineCoor.y2,
left: newLineCoor.x2,
originX: "center",
originY: "center"
});
canvas.add(pointer1, pointer2);
canvas.requestRenderAll();
};
const updateNewLineCoordinate = (o) => {
let obj = o.target;
let centerX = obj.getCenterPoint().x;
let centerY = obj.getCenterPoint().y;
let x1Offset = obj.calcLinePoint().x1;
let y1Offset = obj.calcLinePoint().y1;
let x2Offset = obj.calcLinePoint().x2;
let y2Offset = obj.calcLinePoint().y2;
newLineCoor = {
x1: centerX + x1Offset,
y1: centerY + y1Offset,
x2: centerX + x2Offset,
y2: centerX + y2Offset
};
};
canvas.on("mouse:dblclick", addingControlPoints);
canvas.on("object:moved", updateNewLineCoordinate);
react实现中的问题,即我维护每个变量的状态,但我不能在画布上绘制。
发布于 2022-10-17 06:21:54
现在起作用了。但需要改进
// ! Adding Shape tools
const [openShapeTool, setOpenShapeTool] = useState(false);
// * line func
let line;
const [addLine, setAddLine] = useState(false);
// const [isMouseDown, setIsMouseDown] = useState(false);
let isMouseDown = false;
const handleDrawLine = () => {
if (addLine === false) {
setAddLine(true)
}
}
const startAddingLine = (o) => {
isMouseDown = true
let pointer = canvas.getPointer(o.e);
line = new fabric.Line([pointer.x, pointer.y, pointer.x, pointer.y], {
id: "added-line",
stroke: "red",
strokeWidth: 3,
selectable: false
});
canvas.add(line);
canvas.renderAll();
}
const startDrawingLine = (o) => {
if(isMouseDown){
let pointer = canvas.getPointer(o.e);
line?.set({
x2: pointer.x,
y2: pointer.y
});
canvas.renderAll();
}
}
const stopDrawingLine = () => {
console.log('stop')
line.setCoords();
isMouseDown = false
}
useEffect(() => {
if (addLine) {
canvas.on("mouse:down", startAddingLine);
canvas.on("mouse:move", startDrawingLine);
canvas.on("mouse:up", stopDrawingLine);
canvas.selection = false;
canvas.hoverCursor = "auto";
canvas.getObjects().forEach((element) => {
if (element.id === "added-line") {
element.set({
selectable: false
});
}
});
}
}, [addLine])
发布于 2022-10-17 11:53:30
只要用反应-制造Just国家预防机制。
https://stackoverflow.com/questions/74086779
复制相似问题