Fabric.js - 如何为canvas加载的事件提供回调

时间:2013-10-13 10:11:08

标签: javascript html5 canvas html5-canvas fabricjs

我从JSON对象加载画布并调用toDataURL但我需要一个回调,它将在加载画布后执行,因为如果画布上有图像,toDataURL函数会返回一个空白页面,因为它是在加载图像之前调用。

 canvas.loadFromJSON(json);

 // I need an event handler here which will be executed after canvas is loaded

 var dataURL = canvas.toDataURL();

1 个答案:

答案 0 :(得分:4)

您可以使用来自loadFromJSON(url,callback)

的回调
<pre>
       canvas.loadFromJSON(url, function () {
            //first render
            canvas.renderAll.bind(canvas);
            //before call to zoomOut();
            zoomOut();
        });



 function zoomOut() {
         var objects = canvas.getObjects();
         console.debug(objects);
    ......
    }