Fabric.js 将背景移到中心

Fabric.js move background to center

我正在尝试移动背景图片,使其中心位于屏幕中央,而不是左上角。到目前为止,此代码尝试将其居中,但它取决于 window 的大小并且不一致。

canvas.backgroundImage.left = -(canvas.backgroundImage.width / 2)
canvas.backgroundImage.top = -(canvas.backgroundImage.height / 2)

Fabric 中的任何对象,包括背景图像,都可以使用 center() 方法居中。

canvas.backgroundImage.center()

您也可以调用centerH()centerV()进行水平或垂直居中。

http://fabricjs.com/docs/fabric.Object.html#center