UnifiedTransform.js - Combining CSS/SVG Transforms into a single Transform
UnifiedTransform.js is published as unifiedtransform. It parses and composes two-dimensional CSS/SVG transform operations into one affine matrix and can apply that matrix to points.
Use it to flatten nested 2D transforms, combine generated transform fragments, or convert between CSS and matrix layouts. Use DOMMatrix when the target runtime provides it and browser-native integration is desired, or a 3D matrix library for perspective and three-dimensional transforms.
import UnifiedTransform from 'unifiedtransform';const cm = new UnifiedTransform();cm.transform('translate(15px, 400px) scale(0.3)');cm.rotate(85);cm.transform('translateY(100px) rotate(33deg)');document.getElementById('newDom').style.transform = cm.toTransformString();
Which simplifies the HTML to:
<div id="newDom">rawr</div>
Recipes
Compose translation and scale
Methods are applied in call order and mutate the same UnifiedTransform instance.
Changing the call order changes the result. eval() returns a new two-element array and does not mutate its arguments.
Parse a CSS transform list
Numeric rotate() arguments are degrees. String rotations accept deg or rad; translations accept pixel strings.
import UnifiedTransform from 'unifiedtransform';const transform = new UnifiedTransform() .transform('translate(10px, 20px) rotate(90deg)');console.log(transform.eval(1, 0).map((value) => +value.toFixed(12)));// [10, 21]
The parser supports matrix, translate, translateX, translateY, scale, rotate, skewX, and skewY. Unsupported functions such as 3D or perspective transforms are ignored.
Rotate around a point
The optional second and third arguments to rotate() define the rotation center.
import UnifiedTransform from 'unifiedtransform';const transform = new UnifiedTransform().rotate(90, 5, 5);console.log(transform.eval(6, 5).map((value) => +value.toFixed(12)));// [5, 6]
All transformation methods mutate and return the receiver for chaining. There is no clone method; create a new instance before building an independent transform.
Methods
UnifiedTransform transform(str)
Parses a transform string and applies each transformation one after the other
UnifiedTransform translate(tx, ty)
Specifies a 2D translation by the vector (tx, ty)
UnifiedTransform scale(sx, sy = null)
Specifies a 2D scale operation by the (sx, sy) scaling vector described by the 2 parameters. If the second parameter is omitted, the scaling is applied symmetrically.
UnifiedTransform rotate(angle[, x, y])
Specifies a 2D rotation by the angle specified in the parameter about the origin of the element or optionally the given point (x, y)
UnifiedTransform skewX(degrees)
Specifies a 2D skew transformation along the X axis by the given angle.
UnifiedTransform skewY(degrees)
Specifies a 2D skew transformation along the Y axis by the given angle.
const UnifiedTransform = require('unifiedtransform');const transform = new UnifiedTransform();
ES modules
import UnifiedTransform, { UnifiedTransform as NamedUnifiedTransform } from 'unifiedtransform';const transform = new UnifiedTransform();
Standalone browser script
<script src="https://cdn.jsdelivr.net/npm/unifiedtransform@0.5.0/dist/unifiedtransform.min.js"></script><script> const transform = new UnifiedTransform();</script>
Native browser module
<script type="module"> import UnifiedTransform from 'https://cdn.jsdelivr.net/npm/unifiedtransform@0.5.0/dist/unifiedtransform.mjs'; const transform = new UnifiedTransform();</script>
The package has no runtime dependencies and supports Node.js 20 or newer.
CommonJS consumers can use the direct class export as well as its .default and
.UnifiedTransform aliases. These compatibility paths are covered by the test
suite and are part of the supported API.
Building the library
The implementation is written in strict TypeScript. The build emits CommonJS,
ES modules, a standalone browser bundle, source maps, and format-specific type
declarations without modifying source or documentation files.
After cloning the Git repository, run:
npm installnpm run build
Run a test
Testing the source against the shipped test suite is as easy as
npm run test
Copyright and Licensing
Copyright (c) 2026, Robert Eisele
Licensed under the MIT license.