MotionPathPlugin.getGlobalMatrix
MotionPathPlugin.getGlobalMatrix( element:Element, inverse:Boolean, adjustGOffset:Boolean ) : Matrix2D
Gets the Matrix2D that would be used to convert the element's local coordinate space into the global coordinate space. So, for example, if you take a point {x:0, y:0} and apply() the matrix to it, the resulting point would be the viewport coordinates of that element's top left corner.
Parameters
element: Element
The element whose global matrix should be returned. This should be an element reference (like myElem), not a selector string (like "#myElem").
inverse: Boolean
[optional] if
true, the inverse of the matrix will be returned, meaning that it'll produce the exact opposite transformation (like calling inverse() on the matrix)adjustGOffset: Boolean
[optional] if
true, the x/y offsets of<g>elements will be ignored (they behave differently than most other elements).
Returns : Matrix2D
An object with a, b, c, d, e, and f properties just like an SVGMatrix.
Details
Gets the Matrix2D that would be used to convert the element's local coordinate space into the global coordinate space. So, for example, if you take a point {x:0, y:0} and apply the matrix to it, the resulting point would be the window/viewport coordinates of that element's top left corner.
What's a Matrix2D?
A Matrix2D is just an object with a, b, c, d, e, and f properties representing a 2D transformation matrix (very similar to an SVGMatrix). It contains all rotation, scale, skew, and x/y translation data and it's useful for converting between coordinate spaces. It has an apply() method that accepts a point (like matrix.apply({x:0, y:100})) and returns a new point with the matrix transforms applied.
Sample code
let matrix = MotionPathPlugin.getGlobalMatrix(element),
// 0,0 is the top left corner, but you can use any local coordinates.
localPoint = { x: 0, y: 0 },
// get a new point with the matrix transformations applied.
globalPoint = matrix.apply(localPoint);
gsap.to("#dot", {
x: globalPoint.x,
y: globalPoint.y,
ease: "power1.inOut",
});
Demo
<h1>getGlobalMatrix()</h1>
<p>Drag the box below. When released, the green dot will animate to the top left corner using getGlobalMatrix() to calculate the position even though it's scaled and rotated. Feel free to change the transforms or nest the #dragme element as deeply as you want inside transformed containers.</p><br>
<div id="dragme">Drag me</div>
<div id="dot"></div>body {
padding: 10px 15px;
text-align: center;
}
p {
font-weight: 400;
max-width: 900px;
display: inline-block;
text-align: left;
color: var(--color-surface75);
}
#dragme {
display: inline-block;
width: 200px;
height: 140px;
background-color: var(--field-bg);
border: 2px solid var(--color-surface-white);
border-radius: 16px;
box-shadow: 0 12px 32px rgba(0, 0, 0, 0.45);
color: var(--color-surface-white);
font-size: 2rem;
font-weight: 600;
line-height: 140px;
text-align: center;
cursor: grab;
user-select: none;
transition: border-color 0.2s, box-shadow 0.2s;
}
#dragme:hover {
border-color: var(--color-surface-white);
}
#dragme:active {
cursor: grabbing;
border-color: var(--color-shockingly-green);
box-shadow: 0 12px 32px rgba(0, 0, 0, 0.45), var(--focus-ring);
}
#dot {
position: absolute;
top: 0;
left: 0;
width: 14px;
height: 14px;
border-radius: 50%;
transform: translate(-50%, -50%);
background: var(--gradient-macha);
box-shadow: 0 0 14px rgba(10, 228, 72, 0.55);
}
.gsap-3-logo {
width: 20vw;
max-width: 150px;
position: fixed;
bottom: 15px;
right: 15px;
}gsap.registerPlugin(Draggable, MotionPathPlugin);
let dragme = document.querySelector("#dragme");
// apply some transforms to make it more impressive
gsap.to(dragme, {scale: 0.7, rotation: -20, y: 10, duration: 1.5});
// make it draggable
Draggable.create(dragme, {onRelease: moveDot, bounds: window});
// here is where the magic is...
function moveDot() {
let matrix = MotionPathPlugin.getGlobalMatrix(dragme),
// 0,0 is the top left corner, but try any local coordinates like {x: 200, y: 140} for the bottom right corner.
localPoint = {x: 0, y: 0},
// the matrix has an apply() method that applies the matrix transformations on a point, returning a new (translated) one.
globalPoint = matrix.apply(localPoint);
gsap.to("#dot", {
x: globalPoint.x,
y: globalPoint.y,
ease: "power1.inOut"
});
}