简体   繁体   English

Fabric.js 将背景移动到中心

[英]Fabric.js move background to center

I'm attempting to move the background image so that it's center is positioned in the center of the screen rather than in the top left corner.我正在尝试移动背景图像,使其中心位于屏幕中央而不是左上角。 So far, this code attempts to center it but it's dependent on the size of the window and is inconsistent.到目前为止,此代码尝试将其居中,但它取决于 window 的大小并且不一致。

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

Any object in Fabric, including background images, can be centered with the center() method. Fabric 中的任何 object,包括背景图像,都可以使用center()方法居中。

canvas.backgroundImage.center()

You can also call centerH() or centerV() for horizontal or vertical centering.您还可以调用centerH()centerV()进行水平或垂直居中。

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

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

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