@visactor/vrender-animate
Version:
This module provides a graph-based animation system for VRender.
47 lines (43 loc) • 3.06 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: !0
}), exports.InputText = void 0;
const custom_animate_1 = require("./custom-animate"), transient_1 = require("./transient");
class InputText extends custom_animate_1.ACustomAnimate {
constructor(from, to, duration, easing, params) {
super(from, to, duration, easing, params), this.fromText = "", this.toText = "",
this.showCursor = !1, this.cursorChar = "|", this.blinkCursor = !0, this.beforeText = "",
this.afterText = "", void 0 !== (null == params ? void 0 : params.showCursor) && (this.showCursor = params.showCursor),
void 0 !== (null == params ? void 0 : params.cursorChar) && (this.cursorChar = params.cursorChar),
void 0 !== (null == params ? void 0 : params.blinkCursor) && (this.blinkCursor = params.blinkCursor),
void 0 !== (null == params ? void 0 : params.beforeText) && (this.beforeText = params.beforeText),
void 0 !== (null == params ? void 0 : params.afterText) && (this.afterText = params.afterText);
}
onFirstRun() {
var _a, _b;
const fromProps = this.getLastProps(), toProps = this.getEndProps(), fromText = null !== (_a = fromProps.text) && void 0 !== _a ? _a : "", toText = null !== (_b = toProps.text) && void 0 !== _b ? _b : "";
this.valid = !0, this.fromText = fromText.toString(), this.toText = toText.toString(),
this.toText || "" === this.toText || (this.valid = !1);
}
onEnd(cb) {
super.onEnd(cb), cb || (this.showCursor && !this.blinkCursor ? this.target.setAttribute("text", this.beforeText + this.toText + this.cursorChar + this.afterText) : this.target.setAttribute("text", this.beforeText + this.toText + this.afterText));
}
onUpdate(end, ratio, out) {
if (!this.valid) return;
const totalChars = this.toText.length, fromChars = this.fromText.length;
let currentLength, currentText;
fromChars > totalChars ? (currentLength = Math.round(fromChars - (fromChars - totalChars) * ratio),
currentText = this.fromText.substring(0, currentLength)) : (currentLength = Math.round(fromChars + (totalChars - fromChars) * ratio),
currentText = this.toText.startsWith(this.fromText) ? this.toText.substring(0, currentLength) : currentLength <= fromChars ? this.fromText.substring(0, currentLength) : this.toText.substring(0, currentLength - fromChars + Math.min(fromChars, currentLength)));
let displayText = this.beforeText + currentText + this.afterText;
if (this.showCursor) if (this.blinkCursor) {
const blinkRate = .1;
Math.floor(ratio / blinkRate) % 2 == 0 && (displayText = this.beforeText + currentText + this.cursorChar + this.afterText);
} else displayText = this.beforeText + currentText + this.cursorChar + this.afterText;
(0, transient_1.applyAnimationTransientAttributes)(this.target, {
text: displayText
});
}
}
exports.InputText = InputText;
//# sourceMappingURL=input-text.js.map