Knovour
3/22/2016 - 9:51 AM

P5.js with custom ajax preload. https://github.com/processing/p5.js/issues/1074#issuecomment-153567328

'use strict'

new p5(ctx => {
  const file = {
    load(cb) {
      let tmp = [];
      d3.tsv("data.tsv", (err, list) => {
        const length = list.length;
        // you CANNOT overwrite tmp with another object, it must be updated with the data.
        for(let i = 0; i < length; i++)
          tmp[i] = list[i];
        
        cb(tmp);
      });
  
      return tmp;
    }
  }
  ctx.__proto__.registerPreloadMethod('load', file);
  
  let data;
  ctx.preload = () => {
    data = file.load();
  }
  
  ctx.setup = () => {
    console.log(data);
  }
  
  ctx.draw = () => {
    
  }
})
'use strict'

const file = {
  load(cb) {
    let tmp = [];
    d3.tsv("data.tsv", (err, list) => {
      const length = list.length;
      // you CANNOT overwrite tmp with another object, it must be updated with the data.
      for(let i = 0; i < length; i++)
        tmp[i] = list[i];
      
      cb(tmp);
    });

    return tmp;
  }
}
p5.prototype.registerPreloadMethod('load', file);

let data;
function preload() {
  data = file.load();
}

function setup() {
  console.log(data);
}

function draw() {
  
}