All Projects → jlmakes → Rematrix

jlmakes / Rematrix

Licence: mit
Matrix transformations made easy.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Rematrix

Transform
Base Monogame objects for managing relative transforms.
Stars: ✭ 19 (-94.65%)
Mutual labels:  matrix, transform
Ugm
Ubpa Graphics Mathematics
Stars: ✭ 178 (-49.86%)
Mutual labels:  matrix, transform
journal
a blogging platform built on [matrix]
Stars: ✭ 71 (-80%)
Mutual labels:  matrix
Pyrr
3D mathematical functions using NumPy
Stars: ✭ 282 (-20.56%)
Mutual labels:  matrix
Phytouch
Smooth scrolling, rotation, pull to refresh, page transition and any motion for the web - 丝般顺滑的触摸运动方案
Stars: ✭ 2,854 (+703.94%)
Mutual labels:  transform
synapse-s3-storage-provider
Synapse storage provider to fetch and store media in Amazon S3
Stars: ✭ 58 (-83.66%)
Mutual labels:  matrix
Data Structures Algorithms
My implementation of 85+ popular data structures and algorithms and interview questions in Python 3 and C++
Stars: ✭ 273 (-23.1%)
Mutual labels:  matrix
simple-matrix-bot-lib
An easy to use bot library for the Matrix ecosystem written in Python. https://matrix.to/#/#simplematrixbotlib:matrix.org
Stars: ✭ 27 (-92.39%)
Mutual labels:  matrix
Fractalistic
A framework agnostic, developer friendly wrapper around Fractal
Stars: ✭ 309 (-12.96%)
Mutual labels:  transform
Nhl Led Scoreboard
🚨 Display NHL live score, stats, and more of your favorite teams, on a Raspberry Pi driven RGB LED matrix. 🚨
Stars: ✭ 266 (-25.07%)
Mutual labels:  matrix
Xbmc Adult
main xbmc-adult Kodi repo for Frodo, Gotham, Helix, Isengar, Krypton, Leia and Matrix
Stars: ✭ 283 (-20.28%)
Mutual labels:  matrix
Mirage
A fancy, customizable, keyboard-operable Qt/QML & Python Matrix chat client for encrypted and decentralized communication.
Stars: ✭ 257 (-27.61%)
Mutual labels:  matrix
matrix-register-bot
Bot that offers two step registrations to a matrix-synapse server
Stars: ✭ 25 (-92.96%)
Mutual labels:  matrix
Matrix Dimension
An open source integration manager for matrix clients, like Element.
Stars: ✭ 277 (-21.97%)
Mutual labels:  matrix
metalsmith-babel
A Metalsmith plugin to compile JavaScript with Babel
Stars: ✭ 19 (-94.65%)
Mutual labels:  transform
Element Desktop
A glossy Matrix collaboration client for desktop.
Stars: ✭ 290 (-18.31%)
Mutual labels:  matrix
dotfiles
💻 🍚 🔳 🔲 My riced-up Kali dotfiles – off-white | dark leet | chrome lambo
Stars: ✭ 55 (-84.51%)
Mutual labels:  matrix
stockwell
Stockwell transform for Python
Stars: ✭ 38 (-89.3%)
Mutual labels:  transform
Graphene
A thin layer of graphic data types
Stars: ✭ 268 (-24.51%)
Mutual labels:  matrix
Vue To React
🛠️ 👉 Try to transform Vue component to React component
Stars: ✭ 326 (-8.17%)
Mutual labels:  transform


Rematrix

Matrix transformations made easy.

Build status Coverage Downloads Version 1.3 kB min+gzip MIT license

Browser compatibility matrix



Introduction

Imagine a HTML element that may have a CSS transform applied. If we want to add 45° of Z-rotation, we have no way to handle this safely in CSS—we’d just risk overwriting an existing transform. So we decide to use JavaScript, and check the current transform...

getComputedStyle(element) returns the computed styles, and inspecting the transform property shows:

'matrix3d(0.707107, 0.707107, 0, 0, -0.707107, 0.707107, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1)'

It’s here we discover that browsers actually use transformation matrices under the hood to describe rotation, translation, scale and shear. This means if we wish to manage CSS transforms with JavaScript (without overwriting existing transformations), we’re stuck working with matrices.

Rematrix is an easy way to create and combine matrix transformations that work seamlessly with CSS.


Installation

Browser

A simple and fast way to get started is to include this script on your page:

<script src="https://unpkg.com/rematrix"></script>

If you use this method in production, be sure to specify a fixed version number, and use the minified distribution; e.g: https://unpkg.com/[email protected]/dist/rematrix.min.js. This improves performance, but also prevents library changes from impacting your project.

This will create the global variable Rematrix.

Module

npm install rematrix

CommonJS

const Rematrix = require('rematrix')

ES2015

import * as Rematrix from 'rematrix'


Guide

Creating Transforms

Most API methods look a lot like CSS, so for example, in CSS if we would write transform: rotateZ(45deg), we can create the same transformation in JavaScript using Rematrix like this:

Rematrix.rotateZ(45)

This returns a 45° rotation along the Z-axis, represented as an array of 16 values:

[0.707107, 0.707107, 0, 0, -0.707107, 0.707107, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]

These 16 values represent our transformation matrix in column-major order.


Combining Transforms (Using Multiplication)

Where Rematrix really outshines CSS, is the ability to combine transforms — using matrix multiplication. We’ll recreate the same 45° rotation along the Z-axis, but using separate matrices this time:

let r1 = Rematrix.rotateZ(20)
let r2 = Rematrix.rotateZ(25)

let product = Rematrix.multiply(r1, r2)

Here product describes the same array of 16 values (seen above):

[0.707107, 0.707107, 0, 0, -0.707107, 0.707107, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]

Better Multiplication (Using Reduce)

There’s a good chance we’ll need to multiply quite a few matrices together, so its helpful to store them in an array in order to use Array.prototype.reduce to multiply them all in one line:

let r1 = Rematrix.rotateZ(20)
let r2 = Rematrix.rotateZ(65)
let r3 = Rematrix.rotateZ(-40)

let product = [r1, r2, r3].reduce(Rematrix.multiply)

Order is important. For example, rotating 45° along the Z-axis, followed by translating 500 pixels along the Y-axis... is not the same as translating 500 pixels along the Y-axis, followed by rotating 45° along on the Z-axis.

Preserving Transforms

Before applying any of our transforms, we should capture the existing transform of our element using Rematrix.fromString(), e.g:

let element = document.querySelector('#example')
let style = getComputedStyle(element).transform

let transform = Rematrix.fromString(style)

let r1 = Rematrix.rotateZ(20)
let r2 = Rematrix.rotateZ(65)
let r3 = Rematrix.rotateZ(-40)

let product = [transform, r1, r2, r3].reduce(Rematrix.multiply)

By passing the computed transform styles to Rematrix.fromString(), we create a matrix of the existing transform. We can now factor this into our multiplication.

The existing transformation has been deliberately placed at the start of the array to ensure the computed transform is the foundation for the succeeding transformations.

Applying Transforms

We can turn our matrix into valid CSS using Rematrix.toString(), which we can apply to our element’s style, e.g:

element.style.transform = Rematrix.toString(product)

And that concludes this introduction to Rematrix. Please explore the finished Live Demo on JSFiddle.



API Reference


format(source) ⇒ number[]

Transformation matrices in the browser come in two flavors:

  • matrix using 6 values (short)
  • matrix3d using 16 values (long)

This utility follows this conversion guide to expand short form matrices to their equivalent long form.

Param Description
source A number[] with length 6 or 16


fromString(source) ⇒ number[]

Converts a CSS Transform to array.

Param Description
source A string containing a matrix or matrix3d property value.


identity() ⇒ number[]

Returns a matrix representing no transformation. The product of any matrix multiplied by the identity matrix will be the original matrix.

Tip: Similar to how 5 * 1 === 5, where 1 is the identity.


inverse(source) ⇒ number[]

Returns a matrix representing the inverse transformation of the source matrix. The product of any matrix multiplied by its inverse will be the identity matrix.

Tip: Similar to how 5 * (1/5) === 1, where 1/5 is the inverse.

Param Description
source A number[] with length 6 or 16


multiply(matrixA, matrixB) ⇒ number[]

Returns a matrix representing the combined transformations of both arguments.

Note: Order is important. For example, rotating 45° along the Z-axis, followed by translating 500 pixels along the Y-axis... Is not the same as translating 500 pixels along the Y-axis, followed by rotating 45° along on the Z-axis.

Param Description
matrixA A number[] with length 6 or 16
matrixB A number[] with length 6 or 16


perspective(distance) ⇒ number[]

Returns a matrix representing perspective.

Param Description
distance A number measured in pixels.


rotate(angle) ⇒ number[]

Returns a matrix representing Z-axis rotation.

Tip: This is just an alias for Rematrix.rotateZ for parity with CSS

Param Description
angle A number measured in degrees.


rotateX(angle) ⇒ number[]

Returns a matrix representing X-axis rotation.

Param Description
angle A number measured in degrees.


rotateY(angle) ⇒ number[]

Returns a matrix representing Y-axis rotation.

Param Description
angle A number measured in degrees.


rotateZ(angle) ⇒ number[]

Returns a matrix representing Z-axis rotation.

Param Description
angle A number measured in degrees.


scale(scalar, [scalarY]) ⇒ number[]

Returns a matrix representing 2D scaling. The first argument is used for both X and Y-axis scaling, unless an optional second argument is provided to explicitly define Y-axis scaling.

Param Description
scalar A number decimal multiplier.
[scalarY] A number decimal multiplier. (Optional)


scaleX(scalar) ⇒ number[]

Returns a matrix representing X-axis scaling.

Param Description
scalar A number decimal multiplier.


scaleY(scalar) ⇒ number[]

Returns a matrix representing Y-axis scaling.

Param Description
scalar A number decimal multiplier.


scaleZ(scalar) ⇒ number[]

Returns a matrix representing Z-axis scaling.

Param Description
scalar A number decimal multiplier.


skew(angleX, [angleY]) ⇒ number[]

Returns a matrix representing shear. The first argument defines X-axis shearing, and an optional second argument defines Y-axis shearing.

Param Description
angleX A number measured in degrees.
[angleY] A number measured in degrees. (Optional)


skewX(angle) ⇒ number[]

Returns a matrix representing X-axis shear.

Param Description
angle A number measured in degrees.


skewY(angle) ⇒ number[]

Returns a matrix representing Y-axis shear.

Param Description
angle A number measured in degrees.


toString(source) ⇒ string

Returns a CSS Transform property value equivalent to the source matrix.

Param Description
source A number[] with length 6 or 16


translate(distanceX, [distanceY]) ⇒ number[]

Returns a matrix representing 2D translation. The first argument defines X-axis translation, and an optional second argument defines Y-axis translation.

Param Description
distanceX A number measured in pixels.
[distanceY] A number measured in pixels. (Optional)


translate3d(distanceX, distanceY, distanceZ) ⇒ number[]

Returns a matrix representing 3D translation. The first argument defines X-axis translation, the second argument defines Y-axis translation, and the third argument defines Z-axis translation.

Param Description
distanceX A number measured in pixels.
distanceY A number measured in pixels.
distanceZ A number measured in pixels.


translateX(distance) ⇒ number[]

Returns a matrix representing X-axis translation.

Param Description
distance A number measured in pixels.


translateY(distance) ⇒ number[]

Returns a matrix representing Y-axis translation.

Param Description
distance A number measured in pixels.


translateZ(distance) ⇒ number[]

Returns a matrix representing Z-axis translation.

Param Description
distance A number measured in pixels.



Copyright 2021 Julian Lloyd.
Open source under the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].