@visactor/vrender-animate
Version:
This module provides a graph-based animation system for VRender.
110 lines (104 loc) • 6.14 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: !0
}), exports.Update = void 0;
const custom_animate_1 = require("./custom-animate"), transient_1 = require("./transient"), static_truth_1 = require("./static-truth"), clipPathGeometryAttrs = {
x: !0,
y: !0,
x1: !0,
y1: !0,
width: !0,
height: !0
};
function includesChannel(channels, key) {
for (let i = 0; i < channels.length; i++) if (channels[i] === key) return !0;
return !1;
}
function filterExcludedChannels(diffAttrs, excludeChannels) {
if (!(null == excludeChannels ? void 0 : excludeChannels.length)) return diffAttrs;
const nextAttrs = {};
for (const key in diffAttrs) Object.prototype.hasOwnProperty.call(diffAttrs, key) && !includesChannel(excludeChannels, key) && (nextAttrs[key] = diffAttrs[key]);
return nextAttrs;
}
class Update extends custom_animate_1.ACustomAnimate {
constructor(from, to, duration, easing, params) {
super(from, to, duration, easing, params), this.updateFromAttrs = null, this.clipPathSyncKeys = null,
this.clipPathSyncParent = null, this.clipPathSyncChildIndex = -1, this.clipPathSyncDisabled = !1;
}
onBind() {
var _a;
super.onBind();
const targetContext = null !== (_a = this.target.context) && void 0 !== _a ? _a : {};
let {diffAttrs: diffAttrs = {}} = targetContext;
const {options: options} = this.params;
diffAttrs = filterExcludedChannels(diffAttrs, null == options ? void 0 : options.excludeChannels),
this.props = diffAttrs, this.updateFromAttrs = this.target.consumeTransientFromAttrsBeforePreventAnimate(diffAttrs),
this.clipPathSyncKeys = Object.keys(diffAttrs).filter((key => clipPathGeometryAttrs[key])),
this.clipPathSyncDisabled = !this.clipPathSyncKeys.length, this.syncParentClipPathToTarget();
}
trySyncStartProps() {
const updateFromAttrs = this.updateFromAttrs;
this.propKeys.forEach((key => {
this.fromProps[key] = updateFromAttrs && Object.prototype.hasOwnProperty.call(updateFromAttrs, key) ? updateFromAttrs[key] : this.animate.target.getComputedAttribute(key);
}));
}
onEnd(cb) {
var _a;
cb ? super.onEnd(cb) : (this.props && (0, static_truth_1.commitAnimationStaticAttrs)(this.target, null !== (_a = this.propKeys) && void 0 !== _a ? _a : Object.keys(this.props), this.animate, this.props),
this.syncParentClipPathToTarget(), super.onEnd());
}
update(end, ratio, out) {
if (this.onStart(), !this.props || !this.propKeys) return;
const easedRatio = this.easing(ratio);
this.runInterpolateUpdate(this.fromProps, this.props, easedRatio), this.syncParentClipPathToTarget(),
this.onUpdate(end, easedRatio, out);
}
syncParentClipPathToTarget() {
var _a, _b;
if (this.clipPathSyncDisabled) return;
const target = this.target, parent = target.parent, path = null === (_a = null == parent ? void 0 : parent.attribute) || void 0 === _a ? void 0 : _a.path;
if (!(null === (_b = null == parent ? void 0 : parent.attribute) || void 0 === _b ? void 0 : _b.clip) || !Array.isArray(path) || !path.length) return;
const childIndex = this.getClipPathSyncChildIndex(parent);
if (childIndex < 0 || childIndex >= path.length) return;
const clipGraphic = path[childIndex];
if (!(null == clipGraphic ? void 0 : clipGraphic.attribute) || clipGraphic.type !== target.type || !this.isClipPathStaticTarget(clipGraphic)) return;
const syncAttrs = this.buildClipPathTransientAttrs(clipGraphic);
syncAttrs && ((0, transient_1.applyAnimationFrameAttributes)(clipGraphic, syncAttrs),
clipGraphic.addUpdatePositionTag(), clipGraphic.addUpdateShapeAndBoundsTag());
}
getClipPathSyncChildIndex(parent) {
if (this.clipPathSyncParent === parent && this.clipPathSyncChildIndex >= 0) return this.clipPathSyncChildIndex;
const target = this.target;
let childIndex = -1;
return parent.forEachChildren(((child, index) => child === target && (childIndex = index,
!0))), this.clipPathSyncParent = parent, this.clipPathSyncChildIndex = childIndex,
childIndex;
}
isClipPathStaticTarget(clipGraphic) {
var _a, _b;
const target = this.target, targetFinalAttrs = this.getTargetFinalAttrs(), clipGraphicFinalAttrs = clipGraphic.getFinalAttribute(), clipFinalAttrs = null !== (_a = null != clipGraphicFinalAttrs ? clipGraphicFinalAttrs : clipGraphic.baseAttributes) && void 0 !== _a ? _a : clipGraphic.attribute, keys = null !== (_b = this.clipPathSyncKeys) && void 0 !== _b ? _b : [];
return !!(keys.length && targetFinalAttrs && clipFinalAttrs) && keys.every((key => {
var _a, _b;
return this.isSameClipPathValue(clipFinalAttrs[key], null !== (_a = targetFinalAttrs[key]) && void 0 !== _a ? _a : null === (_b = target.attribute) || void 0 === _b ? void 0 : _b[key]);
}));
}
getTargetFinalAttrs() {
var _a, _b, _c;
const target = this.target;
return null !== (_c = null !== (_b = null === (_a = target.context) || void 0 === _a ? void 0 : _a.finalAttrs) && void 0 !== _b ? _b : target.getFinalAttribute()) && void 0 !== _c ? _c : null;
}
isSameClipPathValue(a, b) {
return "number" == typeof a && "number" == typeof b ? Math.abs(a - b) < 1e-8 : a === b;
}
buildClipPathTransientAttrs(clipGraphic) {
var _a;
const target = this.target, attrs = {};
return (null !== (_a = this.clipPathSyncKeys) && void 0 !== _a ? _a : []).forEach((key => {
var _a;
const nextValue = null === (_a = target.attribute) || void 0 === _a ? void 0 : _a[key];
Object.prototype.hasOwnProperty.call(clipGraphic.attribute, key) && void 0 !== nextValue && !this.isSameClipPathValue(clipGraphic.attribute[key], nextValue) && (attrs[key] = nextValue);
})), Object.keys(attrs).length ? attrs : null;
}
}
exports.Update = Update;
//# sourceMappingURL=update.js.map