@eva/react-eva-lottie
Version:
React EVA是一个让开发同学能够在React技术体系下,利用EVA JS的游戏研发能力,开发动画、游戏类场景的框架。它可以让开发同学用熟悉的JSX和Hooks语法编写动画、游戏场景的代码。
143 lines (127 loc) • 3.83 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
var _excluded = ["src", "resource", "width", "height", "repeats", "autoPlay", "children", "components"];
import { useImperativeHandle, useRef, useMemo, forwardRef } from 'react';
import { addSystem, EvaRefObject, useComponent, useResource, useComponents } from '@eva/react-eva';
import { Lottie as LottieComponent, LottieSystem } from '@eva/plugin-renderer-lottie';
addSystem(new LottieSystem());
export var LottieRefObject = /*#__PURE__*/function (_EvaRefObject) {
_inheritsLoose(LottieRefObject, _EvaRefObject);
function LottieRefObject(_ref) {
return _EvaRefObject.call(this, _ref) || this;
}
var _proto = LottieRefObject.prototype;
_proto.play = function play(params, options) {
if (params === void 0) {
params = [];
}
if (options === void 0) {
options = {
repeats: 1
};
}
var lottie = this.Lottie;
lottie.play(params, options);
return this;
};
_proto.onTap = function onTap(name, fn) {
var lottie = this.Lottie;
lottie.onTap(name, fn);
return this;
};
_proto.on = function on(eventName, fn) {
var lottie = this.Lottie;
lottie.on(eventName, fn);
return this;
};
_proto.off = function off(eventName, fn) {
var lottie = this.Lottie;
lottie.off(eventName, fn);
return this;
};
_proto.once = function once(eventName, fn) {
var lottie = this.Lottie;
lottie.once(eventName, fn);
return this;
};
return LottieRefObject;
}(EvaRefObject);
export function useLottieResource(_ref2) {
var _ref2$name = _ref2.name,
name = _ref2$name === void 0 ? undefined : _ref2$name,
json = _ref2.json;
var jsonType = typeof json === 'string' ? 'json' : json.type;
var jsonUrl = typeof json === 'string' ? json : json.url;
var src = useMemo(function () {
return {
json: {
type: jsonType,
url: jsonUrl
}
};
}, [jsonUrl]);
return useResource({
name: name,
type: 'LOTTIE',
src: src
});
}
export function useLottie(_ref3) {
var resource = _ref3.resource,
width = _ref3.width,
height = _ref3.height,
autoPlay = _ref3.autoPlay,
repeats = _ref3.repeats;
var component = useComponent(LottieComponent, {
resource: resource,
width: width,
height: height
});
useMemo(function () {
if (autoPlay) {
component.play([], {
repeats: repeats !== null && repeats !== void 0 ? repeats : 1
});
}
}, []);
return component;
}
var Lottie = /*#__PURE__*/forwardRef(function (_ref4, ref) {
var src = _ref4.src,
resource = _ref4.resource,
width = _ref4.width,
height = _ref4.height,
repeats = _ref4.repeats,
autoPlay = _ref4.autoPlay,
children = _ref4.children,
_ref4$components = _ref4.components,
components = _ref4$components === void 0 ? [] : _ref4$components,
props = _objectWithoutPropertiesLoose(_ref4, _excluded);
var _ref = useRef(null);
useImperativeHandle(ref, function () {
return new LottieRefObject(_ref);
}, []);
if (src) {
if (typeof src === 'string') {
resource = useLottieResource({
json: src
});
} else {
resource = useLottieResource(src);
}
}
var component = useLottie({
resource: resource,
width: width,
height: height,
repeats: repeats,
autoPlay: autoPlay
});
var _components = useComponents.apply(void 0, [component].concat(components));
return /*#__PURE__*/React.createElement("gameobject", _extends({
ref: _ref,
components: _components
}, props), children);
});
export default Lottie;