Javascript 在Processingjs画布元素中实现预加载程序

Javascript 在Processingjs画布元素中实现预加载程序,javascript,html,canvas,processing,processing.js,Javascript,Html,Canvas,Processing,Processing.js,如何为processingjs画布元素实现预加载程序 两种情况-资产已加载,但js仍在计算/渲染视图;资产尚未加载 有人创建了processingjs标签!rep lt 1500的用户还不能做到这一点:(这可能是一个非常古老的片段,它可以帮助您在加载所有图像后调用回调。文件夹参数用于加载低分辨率或高分辨率图片 pics = { 'Trans100' : "trans100.png", 'Trans101' : "trans101.png", 'TransPanel' :

如何为processingjs画布元素实现预加载程序

两种情况-资产已加载,但js仍在计算/渲染视图;资产尚未加载


有人创建了processingjs标签!rep lt 1500的用户还不能做到这一点:(

这可能是一个非常古老的片段,它可以帮助您在加载所有图像后调用回调。文件夹参数用于加载低分辨率或高分辨率图片

pics = {
  'Trans100'    : "trans100.png",
  'Trans101'    : "trans101.png",
  'TransPanel'  : "transPanel.png"
}

function oAttrs(o) { var a, out = ""; for (a in o){ out += a + " ";} return trim(out);}

function imageLoader(pics, folder, onready){
  this.nextPic  = 0;
  this.pics     = pics;
  this.folder   = folder;
  this.names    = oAttrs(pics).split(" ");
  this.onReady  = onready;
  this.loadNext();
}

  imageLoader.prototype.loadNext = function (){
    if (this.nextPic === this.names.length) {
      this.onReady();
    } else {
      this.loadImage(this.pics[this.names[this.nextPic]]);
    }
  };

  imageLoader.prototype.loadImage = function (file){
    this.nextPic += 1;
    var pic       = new Image();
    pic.onload    = this.loadNext.bind(this);
    pic.onerror   = function(){console.error("ERROR: " + file);};
    pic.src       = this.folder + file;
  };


// example: 

new imageLoader(pics, "images", function(){
  console.log("Images loaded");
})