- createElement
- getComputedTranslateXY
- ancestorFinder
- watch
- SVGLength
- math.createAffine
- math.random
- math.shuffle
- classAction
- prefixBrowser
- eventBinder
- rAF
- wordWrapper
- createPromise
- debounce
- throttle
utils.createElement([option, insert]);
import { utils } from 'utils';
let el = utils.createElement({
type: 'a',
link: 'image.png',
style: 'my-first-style my-second-style',
attributes: {'target': '_blank', 'title': 'this is awesome'},
content: 'click here'
},{
target: document.getElementById('target'),
type: 'sibling',
method: 'after'
});
This function has two optionals parameters you can use with object type argument. The first one is set in order to define element configuration wherheas the second one is used in order to configure the insertion in the DOM. Several optional properties can be set inside of them:
option | type | explanation | exemple |
---|---|---|---|
type - default: 'div' | String | Element type you want to create |
|
id | String | Id that you may want to set on the element. |
|
style | String | You can set one or multiple classes by adding them in string separated by space. |
|
Object | You can also set inline style using an object listing all the style you want to apply, separated by comma. |
|
|
attributes | Object | List of attributes to apply. Think about `target` or `title` for <a> element or `alt` for <img> element. Data attributes can also be set. |
|
src | String | Link to the image for src attribute on media elements. |
|
link | String | Target you want to link with href attribute on link elements (internal or external links are OK) |
|
content | String | String that will be added to the content of the new element. |
|
HTMLNode | Node that will be added to the content of the new element |
|
option | type | value | explanation | exemple |
---|---|---|---|---|
target | HTMLNode | Target element that is the reference for adding the new element in the DOM. |
|
|
type - default: 'sibling' | String | sibling | The new element will be adding as a sibling of the targeted element. |
|
container | The new element will be adding as a child of targeted element. |
|
||
method default: 'after' | String | before | The new element will be added before the target in case of sibling type or as a first child of the target in case of container type. |
|
after | The new element will be added after the target in case of sibling type or as a last child of the target in case of container type. |
|
Get CSS translate state of DOM element.
Look for a parent element that matches for a specific class.
Watch function let you create a proxy object in order to listen for properties values changes. Then, you can create an object with many properties, update them and trigger some functions if these properties are listened. This is useful to avoid regenerate the DOM for listening and let modules interact under the hood.
let proxy = utils.watch({});
import { utils } from 'utils';
const obj = {
value1: 0,
value2: false
}
// create proxy
let proxy = utils.watch(obj);
// add a listener on the proxy
let listener = proxy.addListener('value1', function(){
console.log('change');
});
proxy.value1 = 10; // console -> 'change'
// remove a listener
proxy.removeListener(listener);
proxy.value1 = 5; // console -> รธ
To start using this function, you need to watch an object. Then, the watch
function will return a proxy object that you should interact with. Another module in code may want to intercept a changement on the proxy properties values. To do so, the proxy returned hold some methods:
In order to listen for a property value changement. This method need two arguments:
- a string calling the value you want to listen for
- a function you want to trigger when a modification has been detected on the value listened.
This method return a reference to the listener. Once you call this method on the proxy's property, the function will be triggered each time the value will be changed.
In order to remove a listener for a property. Since you already have created the listener, you only have to remove the listener by passing the listener reference in the proxy's removeListener method: proxy.removeListener(listenerReference);
A function to measure the length of an SVG path since SVGPathElement.getTotalLength() is not longer available in SVG 2. The function take an element as attribute and return the length in pixel.
import { utils } from 'utils';
const SVG = document.getElementByID('mySVG');
const SVGLength = utils.SVGLength(SVG);
SVG.style.strokeDasharray = SVG.style.strokeDashoffset = SVGLength;
This function let you generate an affine function (AX + B)
with two couples of coordinates. This is usefull if you want to find a point related to another following a rule given by other coordinates.
This function let you get the multiplying factor variable A
.
This function let you get the origin coordinate variable B
.
By using a closure, you can directly create an affine function that let you get a point on the line drawn by the function (ax+b). To do, you only need to initiate the closure passing two coordinate systems (X
and Y
), then, by passing an X
variable at any moment, you will get the point.
utils.math.createAffine.a(array(number, number), array(number, number));
utils.math.createAffine.b(number, array(number, number));
utils.math.createAffine.func(array(number, number), array(number, number));
import { utils } from 'utils';
const vars = {
x: [0, -500],
y: [100, 500]
};
//get affine variables
const affineVarA = utils.math.createAffine.a(vars.x, vars.y);
const affineVarB = utils.math.createAffine.b(vars.x, affineVarA);
//get closure function
const affineFunc = utils.math.createAffine.func(vars.x, vars.y);
affineFunc.getPoint(50);
The most simple way to get the affine function is to use utils.math.affine.func(x,y)
giving two arguments (X
and Y
coordinates systems) to the function. This will return a closure function. Using this closure function, you can call the getPoint()
method. getPoint
has an unique parameter: the X variable in the affine function (A*X+B)
.
const affineFunc = utils.math.createAffine.func([1000, -100], [2000, 100]);
console.log(affineFunc.getPoint(1500)) // display -> 0
Thanks to @NathalieVidon who helped me a lot with maths concepts on this function.
This function give you the power to get a random number between two other number. You need to pass two argument to the function: a min and a max. These arguments are optional, by default min
argument is set to 0
and max
is argument is set to 1
.
utils.math.random([number, number]);
import { utils } from 'utils';
let random = utils.math.random(0, 10);
Shuffle array