Stretch Transform is a geometric transformation that distorts a plane or 3D space in a rubbery way.
For a more detailed explanation of this project read this on my website.
Via NPM:
npm i stretch-transform
Or in your html file:
<script src="StretchTransform.js"></script>
var myTransform = new StretchTransform();
Usually you'll give two points as parameters. The first point (origin) will be transformed exactly to the second point (target).
myTransform.addAnchor([100, 200], [300, 300]);
If you give only point as a parameter origin and target will be set to that position. You could change either of them later on.
myTransform.addAnchor([100, 200]);
StretchTransform also works in 3D space. In fact, it always does.
myTransform.addAnchor([100, 200, -150], [300, 300, 0]);
var result = myTransform.transform([200, 200, 50]);
- StretchTransform
- addAnchor
- addAnchor
- removeAnchor
- removeAnchor
- getAnchorCount
- getAnchor
- getAnchorByPos
- getAnchorByOriginPos
- getAnchorByTargetPos
- getAnchorOrigin
- setAnchorOrigin
- getAnchorTarget
- setAnchorTarget
- getWeightingExponent1
- setWeightingExponent1
- getWeightingExponent2
- setWeightingExponent2
- transform
- updateAnchorMatrices
- Anchor
new StretchTransform() creates an empty StretchTransform.
Adds an Anchor where origin and target is the same. You can change either of them later on.
Parameters
p
Array Array [x, y, z] that will be used for origin and target position. Z coordinate is optional.
Returns Anchor The new anchor
Adds an Anchor. pOrigin will be transformed to pTarget.
Parameters
pOrigin
Array Array [x, y, z] for the origin position. Z coordinate is optional.pTarget
Array Array [x, y, z] for the target position. Z coordinate is optional.
Returns Anchor The new anchor
Removes an Anchor giving the anchor
Parameters
anchor
Anchor Anchor to remove
Removes an Anchor giving the index of the anchor.
Parameters
i
Number Index of the anchor
Returns Number of anchors added to the MultiTransform
Parameters
i
Number Index of the anchor to return.
Parameters
p
Array point [x, y, z] to search for an anchor (either origin or target position). Z coordinate is optional.tolerance
Number Radius around Anchor
Returns Number Index of the found anchor or -1 if nothing was found at the specified position
Parameters
p
Array point [x, y, z] to search for the origin position of an anchor. Z coordinate is optional.tolerance
Number Radius around Anchor
Returns Number Index of the found anchor or -1 if nothing was found at the specified position
Parameters
p
Array point [x, y, z] to search for the target position of an anchor. Z coordinate is optional.tolerance
Number Radius around Anchor
Returns Number Index of the found anchor or -1 if nothing was found at the specified position
Parameters
i
Number Index of the anchor.
Returns Array The origin position.
Parameters
Parameters
i
Number Index of the anchor.
Returns Array The target position.
Parameters
Returns Number
Exponent of the weighting function. Defines how the relations from one anchor to all others are cumulated. The closer the other anchor lies, the stronger it is weighted.
Parameters
val
Number Usually something between 0 and 2. Default = 1.
Returns Number
Exponent of the weighting function when applying all anchor matrices to a point.
Parameters
val
Number Usually 1 or higher. Default = 2.
Main function of the class. Transforms a point on the plane and returns its new position.
Parameters
p
Array Point given as an Array [x, y, z] to be transformed. Z coordinate is optional.
Returns Array Transformed point as an Array.
It's usually not necessary to call this method. If anchors and parameters are always set with the given methods (setAnchorOrigin(), ...), this method will be called automatically. It calculates a transformation matrix for each anchor. This matrix reflects the translation of the anchor and the rotation and scaling depending on the (possibly) changed positions of all other anchors.
An Anchor has an origin an a target position. Usually you won't have to deal with it directly. Still, there are some functions which could come handy.
Parameters
pOrigin
pTarget
Returns Array The origin position.
Parameters
p
Array New origin position [x, y, z]. Z coordinate is optional.
Returns Array The target position.
Parameters
p
Array New target position [x, y, z]. Z coordinate is optional.
Returns Array The transformation matrix of this anchor.