gpt4 book ai didi

javascript - Fabric.js 偏移边界框问题

转载 作者:行者123 更新时间:2023-12-03 00:04:22 24 4
gpt4 key购买 nike

我使用 fabric.js 创建了一个包含组内对象的动画。但是,当我将组添加到 Canvas 时,该组的边界框的位置向左偏移。关于如何解决这个问题有什么想法吗?

enter image description here

var canvas = new fabric.Canvas("canvas");
canvas.setWidth(554);
canvas.setHeight(312);

const rect = new fabric.Rect({
width: 200,
height: 50
})

const rect2 = new fabric.Rect({
top: 50,
width: 200,
height: 50,
fill: 'blue'
})

rect.animate({width: 300
}, {
duration: 300,
onChange: canvas.renderAll.bind(canvas)
})

rect2.animate({width: 300}, {
duration: 200,
onChange: canvas.renderAll.bind(canvas)
})

const group = new fabric.Group([rect, rect2], {
top: 70,
width: 300,
})

canvas.add(group)
canvas.setActiveObject(group)
canvas.renderAll()
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.5.0/fabric.min.js"></script>
<canvas id="canvas" width="600" height="500" style="border:1px solid #ccc;z-index:99"></canvas>

最佳答案

使用addWithUpdate更新组在 animationonComplete 事件中.

演示

const canvas = new fabric.Canvas("canvas");
canvas.setWidth(554);
canvas.setHeight(312);

const rect = new fabric.Rect({
width: 200,
height: 50
})

const rect2 = new fabric.Rect({
top: 50,
width: 200,
height: 50,
fill: 'blue'
})

rect.animate({
width: 300
}, {
duration: 300,
onChange: canvas.renderAll.bind(canvas),
onComplete: () => {
group.addWithUpdate();
}
})

rect2.animate({
width: 300
}, {
duration: 200,
onChange: canvas.renderAll.bind(canvas),
onComplete: () => {
group.addWithUpdate();
}
})

const group = new fabric.Group([rect, rect2], {
top: 70
})

canvas.add(group)
canvas.setActiveObject(group)
canvas.renderAll()
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.6.0/fabric.js"></script>
<canvas id="canvas" width="600" height="500" style="border:1px solid #ccc;z-index:99"></canvas>

关于javascript - Fabric.js 偏移边界框问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55032693/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com