npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@diamondlovesyou/tsm

v0.7.4

Published

A TypeScript linear algebra library for your computer graphics

Downloads

9

Readme

TSM - A TypeScript vector and matrix math library

TSM is a a collection of vector and matrix classes written in Microsoft's new JavaScript superset TypeScript, which compiles to plain JavaScript. The library's design is influenced by both gl-matrix and GLM.

What's special about TSM?

  • TSM makes use of TypeScript's type annotations to reduce the number of possible bugs and has been used extensively in the development of a large-scale WebGL application (to be announced soon).

  • TSM makes use of JavaScript's new property definitions to enable GLSL-style swizzle operators:

      var v1 = new TSM.vec2();
      var q1 = new TSM.quat();
    
      v1.xy = [0, 1];
      q1.w = 1.0;
  • TSM offers both non-static and static methods for many operations:

      var v1 = new TSM.vec3([1, 2, 3]);
      var v2 = new TSM.vec3([4, 5, 6]);
    
      var v3 = TSM.vec3.sum(v1, v2);
      var v4 = v1.copy().add(v2);
    
      console.log(v3.equals(v4)); // output: "true"

Building TSM

This project includes a solution file for Visual Studio 2012. It also works with the free Express version of Visual Studio 2012 for the Web, which can be downloaded for free from the Microsoft website. Please install the TypeScript plugin for Visual Studio 2012 to get syntax highlighting and IntelliSense support: http://www.microsoft.com/en-us/download/details.aspx?id=34790.

If you do not want to or cannot use Visual Studio, you can use the command line tool which comes with the plugin instead:

tsc --target "ES5" --out "tsm-[x.y].js" -sourcemap -declarations tsm.ts
java -jar ../closure-compiler.jar < tsm-[x.y].js > tsm-[x.y].min.js

The target parameter is necessary for the swizzle operators (.xyz, etc.) to work.

After successful compilation, four files will be created:

  • tsm-[x.y].js: The compiled JavaScript source, readable
  • tsm-[x.y].min.js: The compiled JavaScript source, minified
  • tsm-[x.y].d.ts: The TypeScript declarations file, which contains a listing of all classes and methods
  • tsm-[x.y].js.map: A source map which maps the compiled JavaScript output to the original TypeScript source files

Using TSM

In Visual Studio

You can use a reference comment to the declarations file or a module import to get access to TSM classes.

Reference comment:

///<reference path='./path/to/TSM/tsm-[x.y].d.ts' />

Module import:

import TSM = module("path/to/TSM")

In JavaScript

If you want to use the generated JavaScript instead, simply load the compiled .js or .min.js file by use of a script tag:

<script src="/path/to/TSM/tsm-[x.y].js"></script>

Testing TSM

The solution contains a test application. Open TSM/Test/index.html in a browser and bring up the console. You should see the following output:

vec2(1,2,3) + vec2(4,5,6) = vec2(5,7,9)

Perspective projection, 45° FOV:
mat4([
   2.4142136573791504, 0, 0, 0
   0, 2.4142136573791504, 0, 0
   0, 0, -1.0202020406723022, -1
   0, 0, -2.0202019214630127, 0
]);

vec3(90,0,0).toQuat() = quat(0.8509035110473633,0,0,0.5253219604492188) 

If instead you receive an error message along the lines of "TSM is not defined", please make sure that your script tags are properly set up.

Documentation

Unfortunately, there is no way yet to automatically generate documentation from TypeScript sources. I'll see what I can do. Please refer to the declarations file in the meantime.

###General design notes

Swizzle operators return numeric arrays, not vector instances:

var v = new TSM.vec4([1, 2, 3, 4]);
var n = v.xyz; // n = [1, 2, 3]

If, instead, you want to create a new instance of a vector or a matrix, use the copy() method:

var v1 = new TSM.vec4([1, 2, 3, 4]);
var v2 = v1.copy();

You can also initialize a new vector with the values of another:

var v1 = new TSM.vec4([1, 2, 3, 4]);
var v2 = new TSM.vec4(v1.xyzw);

Or copy the values of one vector to another using the swizzle operators or the copy() method:

v2.xyzw = v1.xyzw; // same as v1.copy(v2)

The four basic arithmetic operations can be performed on vector instances or using static methods:

var v1 = new TSM.vec4([1, 2, 3, 4]);
var v2 = new TSM.vec4([5, 6, 7, 8]);

var v3 = TSM.vec4.product(v1, v2); // returns a new vec4 instance

v1.multiply(v2); // writes the result of the multiplication into v1
v2.multiply(v1); // writes the result of the multiplication into v2

The reason for all of these different ways of doing the same thing is that object allocation in JavaScript is slow and dynamic allocation shoud therefore be reduced to a minimum.

For this reason, static methods offer an optional destination parameter:

var v3 = TSM.vec3.cross(v1, v2) // allocates a new instance of vec3

is the same as:

var v3 = new TSM.vec3();
TSM.vec3.cross(v1, v2, v3) // writes into the existing instance

Matrices do not have swizzle operators. Instead, they provide the all(), row() and col() methods:

var m = new mat2([1, 2, 3, 4]);

var all = m.all();  // [1, 2, 3, 4]  
var row = m.row(0); // [1, 2]
var col = m.col(0); // [1, 3]