Last active
March 22, 2016 10:02
-
-
Save Knovour/57b761d08ff758396ab6 to your computer and use it in GitHub Desktop.
P5.js with custom ajax preload. https://github.com/processing/p5.js/issues/1074#issuecomment-153567328
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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() { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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 = () => { | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment