pex-io

Basic file i/o for the pex library.

Works both in Plask and any browser.

Usage

npm install pex-io
var io = require('pex-io')
io.loadImage(url, function (err, img) { })

//or import functions individually

var loadImage = require('pex-io/loadImage')
loadImage(url, function (err, img) { })

Async

From v2 all methods are async if called without callback as a second parameter. e.g.:

loadText('hello.txt', (err, text) => {
  console.log(text)
})

Can be now written as

const text = await loadText('hello.txt')
console.log(text)

API

io.load(resources, callback)

Loads list of resources provided as a hash map of { name: { type: url }}.

var resources = {
  hdrImg: { binary: __dirname + '/tex.hdr'}
  img: { image: __dirname + '/tex.jpg'},
  data: { json: __dirname + '/data.json'},
  hello: { text: __dirname + '/hello.txt'}
}

io.load(resources, function(err, res) {
    res.hdrImg //{ArrayBuffer}
    res.img    //{Image} in a Browser or {SkCanvas} in Plask
    res.data   //{JSON}
    res.hello  //{String}
})

io.loadBinary(url, callback)

Loads binary data as an ArrayBuffer from url addess in a Browser or file path in Plask.

io.loadBinary('panorama.hdr', function (err, arrayBuffer) { })

io.loadImage(url, callback)

Loads a HTML Image from an url in a Borwser or SkCanvas from a file path in Plask.

io.loadimage('texture.jpg', function (err, image) { })

How to enable CORS:

io.loadimage({ url: 'texture.jpg', crossOrigin: 'anonymous' }, function (err, image) { })

io.loadJSON(url, callback)

Loads a JSON file from an url url in a Browser of from a file path in Plask.

io.loadJSON('data.json', function (err, json) { })

io.loadText(url, callback)

Loads a text file from an url url in a Browser of from a file path in Plask.

io.loadJSON('data.csv', function (err, string) { })