0.0.2 • Published 9 years ago

typed-struct-js v0.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
9 years ago

typed-struct-js

Inplementation of typed structure (es7 like) in JS using typed array and arrayBuffer...

How to use

//Import the module
var Struct = require("typed-struct-js");

//Let's define Vector

var Vector = new Struct({
	x:"Float32",
	y:"Float32"
});

//And color too 

var Color = new Struct({
	red: "Uint8",
	green: "Uint8",
	blue: "Uint8"
});

//Let's get some red;

var red = Color.get();
red.set("red",255);

//Let's get a random Vector;

var direction = Vector.get();
direction.set("x",Math.random()*2-1).set("y",Math.random()*2-1);
console.log("x " + direction.get("x"), "y " + direction.get("y"));

//Let's get deeper
//Now we define a pixel

var Pixel = new Struct({
	pos:Vector,
	color:Color
});

//And change it a bit

var point = Pixel.get();
point.get("pos").set("x",10).set("y",25);
point.get("color").set("green",255);

//Now it's time to use Array for fast access;
//This is supposed to be THE point of this lib

var triangle = Pixel.Array(3);
triangle.get(0).get("pos").set("x", 50).set("y", 0);
triangle.get(1).get("pos").set("x", 0).set("y", 100);
triangle.get(2).get("pos").set("x", 100).set("y", 100);
for(var i=0;i<.triangle.length,i++) {
	triangle.get(i).get("color").set("blue",i*85).set("red",255-i*85);
}