gpt4 book ai didi

javascript - Fabric JS线点对点

转载 作者:行者123 更新时间:2023-12-02 21:25:23 25 4
gpt4 key购买 nike

我现在可以自由绘画,有几种不同的颜色。我想自由绘制线条部分而不是完全自由绘制。我需要在 map 上标记水和煤气管线,例如 6 条线,每条线都从最后一条线的末尾开始。

function enableFreeDrawing() {
removeEvents();
canvas.isDrawingMode = true;
canvas.freeDrawingBrush.color = e.target.value;
}

document.getElementById('colorpicker').addEventListener('click', function (e) {
canvas.freeDrawingBrush.color = e.target.value;
canvas.freeDrawingBrush.width = 2;
});

请建议我如何使用例如从每个点到另一个点绘制线条。 6次点击,回车或双击完成

最佳答案

在 Canvas 元素上,我会在每次单击时创建一个指示性圆点,并在创建新点时连接这些点。

var canvas = new fabric.Canvas("canvas");
var waterPipePoints = [];
var waterPipeLines = [];

canvas.on("mouse:down", function(event) {
var pointer = canvas.getPointer(event.e);
var positionX = pointer.x;
var positionY = pointer.y;

// Add small circle as an indicative point
var circlePoint = new fabric.Circle({
radius: 5,
fill: "blue",
left: positionX,
top: positionY,
selectable: false,
originX: "center",
originY: "center",
hoverCursor: "auto"
});

canvas.add(circlePoint);

// Store the points to draw the lines
waterPipePoints.push(circlePoint);
console.log(waterPipePoints);
if (waterPipePoints.length > 1) {
// Just draw a line using the last two points, so we don't need to clear
// and re-render all the lines
var startPoint = waterPipePoints[waterPipePoints.length - 2];
var endPoint = waterPipePoints[waterPipePoints.length - 1];

var waterLine = new fabric.Line(
[
startPoint.get("left"),
startPoint.get("top"),
endPoint.get("left"),
endPoint.get("top")
],
{
stroke: "blue",
strokeWidth: 4,
hasControls: false,
hasBorders: false,
selectable: false,
lockMovementX: true,
lockMovementY: true,
hoverCursor: "default",
originX: "center",
originY: "center"
}
);

waterPipeLines.push(waterLine);

canvas.add(waterLine);
}
});

canvas.renderAll();

请检查此沙盒示例:https://codesandbox.io/s/stackoverflow-60753858-fabric-js-1720-26twl

关于javascript - Fabric JS线点对点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60753858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com