Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Although classes and chainable methods can lead to cleaner code and might be easier for rapid prototyping, they ultimately are not as good for module interoperability as plain arrays. For modules I would recommend gl-vec2, gl-vec3, gl-mat4, etc. as it's more modular and generic. Examples:
https://www.npmjs.org/package/delaunay-triangulate
https://www.npmjs.org/package/simplify-path
https://www.npmjs.org/package/chaikin-smooth
https://www.npmjs.org/package/bezier
https://www.npmjs.org/package/adaptive-bezier-curve
https://www.npmjs.org/package/triangulate-contours
https://www.npmjs.org/package/bunny
https://www.npmjs.org/package/parse-svg-path
etc.
--
This is a small vector module, built with the optimized codebase of gl-matrix but using objects and JavaScript paradigms instead of typed arrays.
With node:
npm install vecmath
... in code ...
var Matrix4 = require('vecmath').Matrix4;
Or you can grab the UMD build from the build
folder, which will work with RequireJS or a non-module app.
The reason gl-matrix is so blazingly fast is mostly because its code is highly optimized. It unrolls loops, uses inline code everywhere, and takes advantage of JS variable caching.
Thanks to optimizations on JS engines (V8, SpiderMonkey, etc), hidden classes and inline caches are really fast nowadays (and will probably only get faster). As you can see in the following benchmarks, class-based Vectors actually outperform array access:
http://jsperf.com/gl-matrix-vs-objects/4
http://jsperf.com/gl-matrix-vs-vecmath
It's important to realize that the difference between property and array accessors is negligible for the vast majority of applications, so you shouldn't be sacrificing code readability and maintainability for a change that won't make a dent in the long run.
Using a typed array (if available) for matrices was definitely the right choice of gl-matrix, and so we do the same for WebGL compatibility. Matrix objects have val
, which is the backing array (Float32Array, or falls back to Array for old browsers).
var mat = new Matrix4();
gl.uniformMatrix4fv(loc, false, mat.val);
Just like in gl-matrix, the API tries to encourage re-using vectors to avoid allocations. So often your code will look like this:
myVec.add( tmp.set(0, 10, 50) );
However, if you need to create new objects (out of laziness or some other reason), all functions that take a vector/quaternion type will support lightweight objects as well:
myVec.add({ x:0, y: 10, z: 50 });
These are usually a lot faster to allocate than a Vector class, or a Float32Array (in the case of gl-matrix).
If you find yourself creating a lot of objects, you should use a Pool to reduce allocations.
The library does its best to stay consistent with the gl-matrix API, but also includes a couple extra features:
Vector3.project(projMatrix)
: this method is useful for projecting a 3D point into 2D spaceVector3.unproject(viewport, invProjMatrix)
: useful for unprojecting a 2D point into 3D spacemath-vector2
, math-vector3
, etc. since vector2
is already taken.MIT
FAQs
Highly optimized vector/matrix math (using objects)
We found that vecmath demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.