Strech the background image to canvas size with Fabric.js

天大地大妈咪最大 提交于 2020-01-15 07:30:07

问题


I am using Fabric.js and Canvas to design some cards. I need to strech the background image to the canvas size. But it is not working. Below is my code.

SCRIPT

    <script src="/fabric/dist/fabric.js"></script>
    ...
    <script>
    function myBg(){
      var canvas =  this.__canvas = new fabric.Canvas('myCanvas');

        canvas.setBackgroundImage('images/download.jpg', canvas.renderAll.bind(canvas), 
        {
            backgroundImageOpacity: 0.5,
            backgroundImageStretch: true
        });

        //this.__canvases.push(canvas); this gives me 'Uncaught TypeError: Cannot read property 'push' of undefined'
        canvas.renderAll();
    }

    </script>

HTML

    <canvas id="myCanvas" width="544px" height="682px">
    <div id="Layer4"  onclick="myBg();"><img src="images/Layer4.png"> </div>

It is loading the image but not streching! Any clues?

来源:https://stackoverflow.com/questions/29005435/strech-the-background-image-to-canvas-size-with-fabric-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!