使用fabric.js框架绘制多边形



我正在尝试使用以下代码绘制一个自由多边形:http://jsfiddle.net/e5Xth/3/

canvas.on('mouse:move', function (options) {
        if (lines[0] !== null && drawingObject.type == "roof") {
            setStartingPoint(options);
            lines[lineCounter - 1].set({
                x2: x,
                y2: y
            });
            canvas.renderAll();
        }
    });

在这个jsfiddle中工作得很好,但在我的页面上使用相同的代码,线条并不是在我"真正绘制"的地方绘制的多边形。它们"出生"在左上角的所有地方,但当我点击形成多边形时,它会正确绘制。

有人能帮我吗?

问题在于鼠标按下事件,您正在设置右上角的orginx和originay。我在下面给出了更新后的代码。

function mousedown(event) {
                    setStartingPoint(event);
                    var points = [x, y, x, y];
                    var line = new fabric.Line(points, {
                        left: x,
                        top: y,
                        strokeWidth: 1,
                        selectable: false,
                        stroke: '#40648E'
                    });
                    lines.push(line);
                    canvas.add(lines[lineCounter]);
                    var rect = new fabric.Rect({
                        left: x-2.5,
                        top: y-2.5,
                        width: 5,
                        height: 5,
                        fill: '#fff',
                        stroke: '#000',
                        strokeWidth: 2,
                        selectable: false
                    });
                    canvas.add(rect);
                    lineCounter++;
                }
            }
 function mousemove(event) {
                    canvas.defaultCursor = "crosshair";
                    if (lines[0] !== null) {
                        setStartingPoint(event);
                        if (lines[lineCounter - 1] != null) {
                            lines[lineCounter - 1].set({
                                "x2": x,
                                "y2": y
                            });
                        }
                        canvas.renderAll();
                    }
                }

最新更新