简体   繁体   English

阻止渲染Fabric-js画布,直到完全填充为止

[英]Prevent render of fabric-js canvas until fully populated

In FabricJS canvas.loadFromJSON() seems to run some sort of renderAll function even if not specified? 在FabricJS中,canvas.loadFromJSON()似乎运行某种renderAll函数,即使未指定? As I'm both loading images from JSON and some from another function, I would like the canvas to stay empty until everything is loaded and then render everything simultaneously. 因为我既要从JSON加载图像,又要从另一个函数加载图像,所以我希望画布保持空白,直到加载完所有内容,然后同时渲染所有内容。

I have created a short fiddle with a simplified version: https://jsfiddle.net/Xikura/cas6j7b3/168/ 我用简化版创建了一个简短的小提琴: https : //jsfiddle.net/Xikura/cas6j7b3/168/

The fiddle toggles one of the layers then redraws the canvas, especially if you disable caching you can see that the props shows up on it's own, then the rest of the images a bit later. 小提琴在各层之间切换,然后重新绘制画布,特别是如果您禁用缓存,您会看到道具本身显示,然后其余图像稍后显示。 Quite minor one might say, but when there are between 10 and 30 images with various blend-settings, the loading could take some time until common images starts to get cached, moving around on the early visible props isn't doing my solution much good... 也许只有一个很小的说,但是当有10至30张具有各种混合设置的图像时,加载可能要花费一些时间,直到普通图像开始被缓存为止,在早期可见的道具上移动并不能很好地解决我的问题...

I see the documentation of loadFromJSON adds the renderAll() in it's callback, I trigger my own preload images in that same callback instead, to be able to add the other images. 我看到loadFromJSON文档在其回调中添加了renderAll(),而是在同一回调中触发了自己的预加载图像,以便能够添加其他图像。

if (!jsonCanvas) {
  // First load
  preload(images);
} else {
  // Loading from saved JSON
  canvas.loadFromJSON(jsonCanvas, function () {
    preload(images);
  });
}

Right now I can't seem to grasp why loadFromJSON seem to trigger some sort of render so the props are displayed first? 现在,我似乎无法理解为什么loadFromJSON似乎会触发某种渲染,因此首先显示道具? Does FabricJS have any functionality I could use to prevent a render until I trigger it myself with renderAll() ? 在我自己使用renderAll()触发渲染之前,FabricJS是否具有可用来阻止渲染的功能?

I found the FabricJS-setting which one would think would solve this: renderOnAddRemove which I set to false, it did fix some other render-difficulties I had earlier, but had no effect on the loadFromJSON-part. 我找到了一个FabricJS设置,认为该设置可以解决此问题:我设置为false的renderOnAddRemove ,它确实修复了我之前遇到的其他渲染困难,但对loadFromJSON-part没有影响。

So I have been working the whole day on this, but after completing my question I decided to give it a rest and move on to other problems. 因此,我整天都在工作,但是在完成我的问题后,我决定休息一下,然后继续处理其他问题。 By accident while trying to optimize the performance of my application I stumbeled upon this Improving FabricJS speed -site, which both answers my question and solves my problem. 尝试优化我的应用程序性能时,偶然地,我对这个“ 改善FabricJS速度”站点感到沮丧 ,它既回答了我的问题,又解决了我的问题。

By switching out loadFromJSON with fabric.util.enlivenObjects I'm able to trigger my own callback and not the (undocumented?) canvas.renderAll() which loadFromJSON calls. 通过使用fabric.util.enlivenObjects切换loadFromJSON ,我可以触发自己的回调,而不是loadFromJSON调用的(undocumented) canvas.renderAll()

  if (!jsonCanvas) {
    // First load
    preload(images);
  } else {
    // Loading from saved JSON
    fabric.util.enlivenObjects(jsonCanvas.objects, (objs) => {
      objs.forEach((item) => {
        canvas.add(item);
      });
      preload(images);
    })
  }

I updated my fiddle to include this fix. 我更新了小提琴以包含此修复程序。

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

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