面料JS线从点到点

问题描述 投票:-1回答:1

我现在可以自由绘画一些不同的颜色。我想自由绘制线条零件,而不是完全自由绘制。我需要在地图上用例如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次,然后按Enter或双击完成

javascript canvas fabricjs fabric
1个回答
0
投票

在画布元素上,我将在每次单击时创建一个指示性的圆点,并在创建新点时将这些点连接起来。

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();

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

© www.soinside.com 2019 - 2024. All rights reserved.