简体   繁体   English

Fabricjs:克隆子类 object 使克隆不可见

[英]Fabricjs: cloning a subclassed object makes the clone invisible

I want to make a copy of a fabric object that is a sub-class of a regular fabric class (ie a new object with the same properties as the original).我想制作织物 object 的副本,它是常规织物 class 的子类(即具有与原始织物相同属性的新 object)。 Cloning seemed to be the way to go, and indeed cloning a regular object, such as a Rect, works fine.克隆似乎是通往 go 的途径,并且确实克隆了常规的 object,例如 Rect,效果很好。 But if I use the same code to clone an instance of a sub-class, the resulting object is not visible on the canvas (although it is listed in the results of canvas.getObjects() ).但是,如果我使用相同的代码克隆子类的实例,则生成的 object 在 canvas 上不可见(尽管它列在canvas.getObjects()的结果中)。

What am I doing wrong?我究竟做错了什么?

Here is a minimal example.这是一个最小的例子。 This is also at https://codepen.io/micrology/pen/dyKPbRO Running this displays 3 rectangles, two red but only one yellow.这也在https://codepen.io/micrology/pen/dyKPbRO运行它显示 3 个矩形,两个红色但只有一个黄色。 There should be 2 yellow rectangles.应该有 2 个黄色矩形。

        var canvas = new fabric.Canvas('canvas')

        var rect = new fabric.Rect({
            top: 100,
            left: 100,
            width: 60,
            height: 70,
            fill: 'red',
        })
        canvas.add(rect)
        var clonedRect
        rect.clone((obj) => {
            clonedRect = obj
            clonedRect.set({left: obj.left + 10, top: obj.top + 10})
        })
        canvas.add(clonedRect)

        var CustomRect = fabric.util.createClass(fabric.Rect, {
            type: 'rect',
            initialize: function (options) {
                this.callSuper('initialize', options)
            },
        })
        var customRect = new CustomRect({
            top: 200,
            left: 200,
            width: 50,
            height: 50,
            fill: '#ffff00',
        })
        canvas.add(customRect)

        var clonedCustomRect
        customRect.clone((obj) => {
            clonedCustomRect = obj
            clonedCustomRect.set({left: obj.left + 10, top: obj.top + 10})
        })
        canvas.add(clonedCustomRect)

For people still having this problem, there is currently an open discussion.对于仍然有这个问题的人,目前有一个公开讨论。

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

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