A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/infusion/Quaternion.js below:

rawify/Quaternion.js: The RAW JavaScript Quaternion library

Quaternion.js - ℍ in JavaScript

Quaternion.js is a well tested JavaScript library for 3D rotations. Quaternions can be used everywhere, from the rotation calculation of your mobile phone over computer games to the rotation of satellites and all by avoiding the Gimbal lock. The library comes with examples to make you get started much quicker without worrying about the math behind. But if you care, have a look at the Quaternion Introduction.

const Quaternion = require('quaternion');

const q = new Quaternion("99.3+8i");
c.mul(1,2,3,4).div([3,4,1]).sub(7, [1, 2, 3]);

In order to create a HTML element, which always rotates in 3D with your mobile device, all you need is the following snippet. Look at the examples folder for a complete version.

const deg = Math.PI / 180;
window.addEventListener("deviceorientation", function(ev) {

  // Update the rotation object
  const q = Quaternion.fromEulerLogical(ev.alpha * deg, ev.beta * deg, -ev.gamma * deg, 'ZXY');

  // Set the CSS style to the element you want to rotate
  elm.style.transform = q.toCSSTransform();

}, true);

Any function (see below) as well as the constructor of the Quaternion class parses its input like this.

You can pass either Objects, Doubles or Strings.

Calling the constructor with no parameters will create a unit quaternion.

new Quaternion() // 1 + 0i + 0j + 0k

The typical use case contains all quaternion parameters

new Quaternion(w, x, y, z)

Quaternion as angle and vector. Note: This is not equivalent to Quaternion.fromAxisAngle()!

new Quaternion(w, [x, y, z])

Quaternion as an object (it's ok to leave components out)

new Quaternion({w: w, x: x, y: y, z: z})

Quaternion out of a complex number, e.g. Complex.js, with y=z=0.

new Quaternion({re: real, im: imaginary})

Quaternion out of a 4 elements vector

new Quaternion([w, x, y, z])

Augmented Quaternion out of a 3 elements vector

new Quaternion([x, y, z])
new Quaternion('1 - 2i - 3j - 4k')
new Quaternion("123.45");
new Quaternion("15+3i");
new Quaternion("i");

Every stated parameter n in the following list of functions behaves in the same way as the constructor examples above

Note: Calling a method like add() without parameters passes a quaternion with all elements zero, not one!

Adds two quaternions Q1 and Q2

Subtracts a quaternions Q2 from Q1

Calculates the additive inverse, or simply it negates the quaternion

Calculates the length/modulus/magnitude or the norm of a quaternion

Calculates the squared length/modulus/magnitude or the norm of a quaternion

Normalizes the quaternion to have |Q| = 1 as long as the norm is not zero. Alternative names are the signum, unit or versor

Calculates the Hamilton product of two quaternions. Leaving out the imaginary part results in just scaling the quat.

Note: This function is not commutative, i.e. order matters!

Scales a quaternion by a scalar, faster than using multiplication

Calculates the dot product of two quaternions

Calculates the inverse of a quat for non-normalized quats such that Q-1 * Q = 1 and Q * Q-1 = 1

Multiplies a quaternion with the inverse of a second quaternion

Calculates the conjugate of a quaternion. If the quaternion is normalized, the conjugate is the inverse of the quaternion - but faster.

Calculates the power of a quaternion raised to the quaternion n

Calculates the natural exponentiation of the quaternion

Calculates the natural logarithm of the quaternion

Returns the real w part of the quaternion

Returns the imaginary part [x, y, z] of the quaternion as a 3D vector / array

Checks if two quats are the same

Checks if all parts of a quaternion are finite

Checks if any of the parts of the quaternion is not a number

Checks if the quaternion is a unit quaternion of length 1.

Gets the Quaternion as a well formatted string

Gets the actual quaternion as a 4D vector / array

Calculates the 3x3 rotation matrix for the current quat as a 9 element array or alternatively as a 2D array

Array toMatrix4(2D=false)

Calculates the homogeneous 4x4 rotation matrix for the current quat as a 16 element array or alternatively as a 2D array

Determines the homogeneous rotation matrix string used for CSS 3D transforms

Calculates the axis and angle representation of the current quaternion

Array toEuler(order="ZXY")

Calculates the Euler angles represented by the current quat in the given Tait-Bryan order. The returned triplet order matches the order string.

Clones the current object

Rotates a 3 component vector, represented as an array by the current quaternion in an efficient manner.

Returns a function to spherically interpolate between two quaternions. Called with a percentage [0-1], the function returns the interpolated Quaternion.

Quaternion.fromAxisAngle(axis, angle)

Gets a quaternion by a rotation given as an axis and angle

Quaternion.fromMatrix(matrix)

Gets a quaternion given a rotation matrix, either as a 1x9 array or a 3x3 array.

Quaternion.fromEuler(ϕ, θ, ψ[, order="ZXY"]) / Quaternion.fromEulerLogical(ψ, θ, ϕ[, order="YXZ"])

Euler angles are probably the reason to use quaternions. The definition is mostly sloppy and you can only decide how it was defined based on the matrix representation. A ZXY in one definition is the multiplication order read from right to left and in another the execution order from left to right. Quaternion.js provides two functions, fromEulerLogical(), where the angles and order are applied from left to right (logical application order) and fromEuler() which reverses the order of the argument list (technical multiplication order).

So for fromEulerLogical(ϕ, θ, ψ, "ZXY"), for example means first rotate around Z axis by ϕ then around X axis by θ and then around axis Y by ψ (RotY(ψ)RotX(θ)RotZ(ϕ)).

The order of fromEuler() can take the string value ZXY (default), XYZ / RPY, YXZ, ZYX / YPR, YZX, XZY, ZYZ, ZXZ, YXY, YZY, XYX, XZX.

Quaternion.fromVectors(u, v)

Calculates the quaternion to rotate vector u onto vector v, represented as 3 element arrays, which can be done elegantly using quaternions.

Gets a spherical random number

A quaternion zero instance (additive identity)

A quaternion one instance (multiplicative identity)

An imaginary number i instance

An imaginary number j instance

An imaginary number k instance

You can install Quaternion.js via npm:

Or with yarn:

Alternatively, download or clone the repository:

git clone https://github.com/rawify/Quaternion.js

Include the quaternion.min.js file in your project:

<script src="quaternion.min.js"></script>
<script>
    console.log(Quaternion("1 + 2i - 3j + 4k"));
</script>

Or in a Node.js project:

const Quaternion = require('quaternion');

or

import Quaternion from 'quaternion';

As every library I publish, Quaternion.js is also built to be as small as possible after compressing it with Google Closure Compiler in advanced mode. Thus the coding style orientates a little on maxing-out the compression rate. Please make sure you keep this style if you plan to extend the library.

After cloning the Git repository run:

npm install
npm run build

Testing the source against the shipped test suite is as easy as

Copyright (c) 2025, Robert Eisele Licensed under the MIT license.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4