如何使用比例渲染fabric.Image?

时间:2019-04-10 14:19:51

标签: fabricjs

Fabric版本是2.x 我试图创建显示图像类。但是,当我调整班级对象的大小(更改宽度和高度,而不是比例)时,图像始终具有默认大小。你能帮我吗?

    MyImage = fabric.util.createClass(fabric.Object, {

    image: null, //will be instance of Fabric.Image

    _render(ctx) {
        if (this.image) {

            this.image.set({
                scaleX: this.width / this.image.width,
                scaleY: this.height / this.image.height
            });

            this.image._render(ctx);
        }
    },
    });

1 个答案:

答案 0 :(得分:0)

解决。您需要使用render而不是_render。

    _render(ctx) {
        if (this.image) {
            ctx.save();

            ctx.translate(-this.width / 2, -this.height / 2);

            this.image.set({
                scaleX: this.width / this.image.width,
                scaleY: this.height / this.image.height
            });
            this.image.render(ctx);
            ctx.restore();
        }
    },