I have a div that i want to project on a svg path. This path has 4 points (just as a dom-element) but is not a rectangle.
I've tried GSAP FLIP but this transforms to the boundingbox of the path.
https://codepen.io/KoentjeV/pen/mdQNgLr
<svg fill="none" xmlns="http://www.w3.org/2000/svg" id="thesvg" width="500" height="500" viewbox="0 0 500 500">
<path d="M0 103.85 L0 335.11L199.29 231.26 L199.29 0L0 103.85Z" fill="#6B8EFF" id="left" />
<path d="M201.29 231.26L400.57 335.11V103.85L201.29 0V231.26Z" fill="#1242FE" id="right" />
<path d="M1 336.11L200.29 232.26L399.57 336.11L200.29 431.56L1 336.11Z" fill="#5073FF" id="bottom" />
</svg>
<div class="toBeTransformed"></div>
.toBeTransformed {
width: 300px;
height: 300px;
position: absolute;
bottom: 50px;
right: 50px;
background: linear-gradient(180deg, #ffff00 0%, #e5e5e5 100%);
}
Flip.fit(".toBeTransformed", "#bottom", {
scale: true,
duration: 1.5,
delay: 1,
repeat: -1,
yoyo: 1
});
All credits to MvG's answer: "Finding the Transform matrix from 4 projected points (with Javascript)" on Math Exchange. I can't remotely explain the math behind.
The function explained will calculate a 3D matrix according to the 4 defined projection points and the target element's dimensions.
I've slightly modified the script to work with projection points in clockwise order. (original function expects an order of: top-left, top-right, bottom-left, bottom-right).
To get the required 4 projection points I converted the
<path>to a<polygon>.This way we can easily get all points as a point array
3D transforms are not supported for svg elements
In your case this is not a huge deal as you're projecting a HTMl element.