A small JS Library for doing affine transformations and (coming soon) other simple transformations I need for a bigger project. affine and polygon are currently the two exports.
npm install -g affine For your convenience, this project is stitched into one JS file, affine.js. The stitched file provides a way of requiring components. Here's an in-browser example:
<scriptsrc="affine.js"></script><script>varaffine=require('affine');varpolygon=require('polygon');// let's define some affine transformationsvarrot_left=newaffine.rotation(Math.PI/4);varrot_right=newaffine.rotation(-Math.PI/4);vargo_big=newaffine.scaling(2,4);vargo_small=newaffine.scaling(0.5,0.25);// copy the original transform and compose with all these vart=rot_left.copy();t.rightComposeWith(rot_right);t.rightComposeWith(go_big);t.rightComposeWith(go_small);// alternatively, we don't really need to make// all these new transforms; given one we can // rotate, etc., it with convenience functionst.rotate(0.234);t.rotate(-0.234);t.scale(1,2);t.scale(1,0.5);// a transformation also has exposed functions,// so you don't have to explicitly make new// transforms varsquare=polygon.factory.unitSquare();document.write("<h3>Square before</h3>");document.write(JSON.stringify(square));document.write("<h3>Square after (should be the same)</h3>");square.transform(t);document.write(JSON.stringify(square));</script>{affine, polygon} =require'affine'rot_left=newaffine.rotationMath.PI/4rot_right=newaffine.rotation-Math.PI/4go_big=newaffine.scaling2, 4go_small=newaffine.scaling0.5, 0.25# rightComposing a transform A with another, A'# desctructively replaces A with A'(A)t=rot_left.copy() t.rightComposeWith rot_right t.rightComposeWith go_big t.rightComposeWith go_small t.rotate0.234t.rotate-0.234t.scale1, 2t.scale1, 0.5square=polygon.factory.unitSquare() square.transform t # square should be the same, as the 4 affines # cancel each other out.console.log squareAll .js and .json files are auto-generated. Please edit the appropropriate .coffee files and run cake build before committing.