简体   繁体   English

无法缩放或旋转fabricJs Image、Rect、Ellipse

[英]Can't scale or rotate fabricJs Image, Rect, Ellipse

As in the title, I'm using fabricJS 4.6.0 with VueJs 3 .如标题所示,我将fabricJS 4.6.0VueJs 3一起使用。

I'm trying to simply setup fabric with resizable rectangles.我正在尝试简单地设置具有可调整大小的矩形的结构。

My problem is accessing the object canvas outside the initFabric method.我的问题是在 initFabric 方法之外访问 object canvas 。

I can't resize or rotate the rectangle, just move it.我无法调整或旋转矩形,只需移动它。

The only way to make it works is declare canvas as const if I use this.canvas doesn't work.使它工作的唯一方法是声明 canvas 为 const 如果我使用this.canvas不起作用。

here the jsfiddle https://jsfiddle.net/8vzc3bj5/19/这里是jsfiddle https://jsfiddle.net/8vzc3bj5/19/

initFabric: function () {
    var canvasEl = this.$refs.canvas;
    canvasEl.width = 1920;
    canvasEl.height = 1080;

    const canvas = new fabric.Canvas(canvasEl, {
        // isDrawingMode: true,
        centeredScaling: true,
        backgroundColor: "#fff",
        selectionBorderColor: "rgba(255, 255, 255, 0.8)",
        selectionColor: "rgba(255, 255, 255, 0.8)",
        selectionLineWidth: 2,
        borderColor: "gray",
        cornerColor: "black",
        cornerSize: 12,
        transparentCorners: true,
    });

    canvas.on("selection:created", this.handleSelection);
    canvas.on("selection:updated", this.handleSelection);

    canvas.on("after:render", this.updateCanvas);
    canvas.on("object:modified", this.updateCanvas);
    canvas.on("object:added", this.updateCanvas);
    canvas.on("object:removed", this.updateCanvas);

    canvas.on("mouse:wheel", (opt) => {
        var delta = opt.e.deltaY;
        var zoom = canvas.getZoom();
        zoom *= 0.999 ** delta;
        if (zoom > 20) zoom = 20;
        if (zoom < 0.01) zoom = 0.01;
        canvas.zoomToPoint(
            { x: opt.e.offsetX, y: opt.e.offsetY },
            zoom
        );
        opt.e.preventDefault();
        opt.e.stopPropagation();
    });

    this.$nextTick(() => {
        var rect = new fabric.Rect({
            left: 100,
            top: 50,
            fill: "yellow",
            width: 200,
            height: 100,
            stroke: "lightgreen",
            strokeWidth: 4,
            zoomX: 1,
            zoomY: 1,
            dirty: true,
        });
        canvas.add(rect);
        rect.center();
        canvas.setActiveObject(rect);
        canvas.bringToFront(rect);
        canvas.renderAll();
    });
    this.resizeCanvas();
},

Thanks谢谢

Fabricjs does not like having the canvas converted to a proxy. Fabricjs 不喜欢将 canvas 转换为代理。 One way to get around it is to use a global variable instead of using the data.解决它的一种方法是使用全局变量而不是使用数据。 You can also just remove canvas from the data() declaration, which will make it still accessible throughout the template and code, but not make it reactive.您也可以从 data() 声明中删除 canvas ,这将使其在整个模板和代码中仍然可以访问,但不会使其反应。

See original post Fabricjs, selection handles shown but not clickable before selected together with other shapes请参阅原始帖子Fabricjs,显示选择手柄但在与其他形状一起选择之前不可点击

I think this issue has something to do with vuejs reactivity.我认为这个问题与 vuejs 的反应性有关。

Maybe you could use const for the entire logic and assign the const to data variable at the end of logic.也许您可以将 const 用于整个逻辑,并在逻辑结束时将 const 分配给 data 变量。

this.canvas = canvas

Here is the working demo of it.这是它的工作演示。

https://jsfiddle.net/akshayd121/hjgotm7d/1/ https://jsfiddle.net/akshayd121/hjgotm7d/1/

Found a similar question here, although rotate and scale work in the above demo example.在这里找到了一个类似的问题,尽管在上面的演示示例中旋转和缩放工作。 Some of them have pointed out that there is some issue with this approach in terms of editing text, rotating and scaling.他们中的一些人指出,这种方法在编辑文本、旋转和缩放方面存在一些问题。

Fabric JS Canvas loses editability and resizability when assigned to Vue data attribute Fabric JS Canvas 在分配给 Vue 数据属性时失去了可编辑性和可调整性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM