【问题标题】:Fabric JS parent-child objectsFabric JS 父子对象
【发布时间】:2015-05-09 07:21:39
【问题描述】:

我正在尝试将织物 JS 用于座位表应用程序。我为平面图和每个用户都有单独的对象。我可以彼此独立地移动它们。我希望用户成为平面图的父级,这样当我移动平面图对象时,所有用户都会随之移动。有没有办法用面料做到这一点?

【问题讨论】:

    标签: javascript html canvas fabricjs


    【解决方案1】:

    找到了一种方法;虽然我仍然想知道结构框架是否有办法自动处理这个问题,而不是自己为所有父子关系创建它。

    // Create canvas
    canvas = new fabric.Canvas('c');
    img = document.getElementById("cur_image");
    
    // Create parent object
    background = new fabric.Image(img, {
        left: 0,
        top: 0,
        angle: 0,
        opacity: 1.0
    });
    canvas.add(background);
    
    // Connect move handler that will move the children
    background.on('moving', backgroundMoveHandler);
    
    children = [];
    function addItem() {
        var rect = new fabric.Rect({
            left: 100,
            top: 100,
            fill: 'red',
            width: 50,
            height: 50
        });
        canvas.add(rect);
        children.push(rect);
    }
    
    // Add some children
    addItem();
    addItem();
    
    // Whenever the parent is moved, move the children as well.
    function backgroundMoveHandler(options) {
        var x = options.e.movementX;
        var y = options.e.movementY;
        $.each(children, function(i, obj) {
            obj.set('left', obj.left + x);
            obj.set('top', obj.top + y);
            obj.setCoords();
        });
    }
    

    【讨论】:

    • 我似乎无法在 moving 事件中获得 movementX/Y 属性。不明确的。为什么?我在做rect.on("moving"...)
    猜你喜欢
    • 1970-01-01
    • 2018-05-04
    • 2017-04-06
    • 1970-01-01
    • 2018-03-30
    • 1970-01-01
    • 2012-10-23
    • 2020-03-27
    • 2020-01-13
    相关资源
    最近更新 更多