kute.js/src/components/clipProperty.js
thednp 2a5bac2bb3 Changes V2.2.0:
* major JSDoc write up
* removed ESLint `no-bitwise` exception, it only applies to specific functions and not the entire code
* the `SVGCubicMorph` component will remove un-necessary `Z` path commands when is the case for better out of the box animation
* fixed a minor disambiguation with `filterEffects` and `drop-shadow` property and its `dropshadow` interpolation function
* TypeScript strong: all files are modules, easy to implement in any third party app
* updated `CubicBezier` and SVGPathCommander
* code cleanup
2021-12-08 23:43:31 +02:00

53 lines
1.6 KiB
JavaScript

import getStyleForProperty from '../process/getStyleForProperty';
import trueDimension from '../util/trueDimension';
import numbers from '../interpolation/numbers';
import { onStartClip } from './clipPropertyBase';
// Component Functions
/**
* Returns the current property computed style.
* @param {string} tweenProp the property name
* @returns {string | number[]} computed style for property
*/
function getClip(tweenProp/* , value */) {
const { element } = this;
const currentClip = getStyleForProperty(element, tweenProp);
const width = getStyleForProperty(element, 'width');
const height = getStyleForProperty(element, 'height');
return !/rect/.test(currentClip) ? [0, width, height, 0] : currentClip;
}
/**
* Returns the property tween object.
* @param {string} _ the property name
* @param {string} value the property value
* @returns {number[]} the property tween object
*/
function prepareClip(/* tweenProp, */_, value) {
if (value instanceof Array) {
return value.map((x) => trueDimension(x));
}
let clipValue = value.replace(/rect|\(|\)/g, '');
clipValue = /,/g.test(clipValue) ? clipValue.split(',') : clipValue.split(/\s/);
return clipValue.map((x) => trueDimension(x));
}
// All Component Functions
const clipFunctions = {
prepareStart: getClip,
prepareProperty: prepareClip,
onStart: onStartClip,
};
// Component Full
const ClipProperty = {
component: 'clipProperty',
property: 'clip',
defaultValue: [0, 0, 0, 0],
Interpolate: { numbers },
functions: clipFunctions,
Util: { trueDimension },
};
export default ClipProperty;