@shopify/react-native-skia
Version:
High-performance React Native Graphics using Skia
72 lines (68 loc) • 2.68 kB
JavaScript
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import Rea from "../external/reanimated/ReanimatedProxy";
import { HAS_REANIMATED_3 } from "../external/reanimated/renderHelpers";
import { ReanimatedRecorder } from "./Recorder/ReanimatedRecorder";
import { Container, StaticContainer } from "./StaticContainer";
import { visit } from "./Recorder/Visitor";
import "../skia/NativeSetup";
import "../views/api";
// create local reference for `strictGlobal` option in Worklets
const {
SkiaViewApi
} = globalThis;
const nativeDrawOnscreen = (nativeId, recorder, picture) => {
"worklet";
//const start = performance.now();
recorder.play(picture);
//const end = performance.now();
//console.log("Recording time: ", end - start);
SkiaViewApi.setJsiProperty(nativeId, "picture", picture);
};
class NativeReanimatedContainer extends Container {
constructor(Skia, nativeId) {
super(Skia);
this.nativeId = nativeId;
_defineProperty(this, "mapperId", null);
_defineProperty(this, "picture", void 0);
this.picture = Skia.Picture.MakePicture(null);
}
redraw() {
if (this.mapperId !== null) {
Rea.stopMapper(this.mapperId);
}
if (this.unmounted) {
return;
}
const recorder = new ReanimatedRecorder(this.Skia);
const {
nativeId,
picture
} = this;
visit(recorder, this.root);
const sharedValues = recorder.getSharedValues();
const sharedRecorder = recorder.getRecorder();
// Draw first frame
Rea.runOnUI(() => {
"worklet";
nativeDrawOnscreen(nativeId, sharedRecorder, picture);
})();
// Animate
if (sharedValues.length > 0) {
this.mapperId = Rea.startMapper(() => {
"worklet";
sharedRecorder.applyUpdates(sharedValues);
nativeDrawOnscreen(nativeId, sharedRecorder, picture);
}, sharedValues);
}
}
}
export const createContainer = (Skia, nativeId) => {
if (HAS_REANIMATED_3 && nativeId !== -1) {
return new NativeReanimatedContainer(Skia, nativeId);
} else {
return new StaticContainer(Skia, nativeId);
}
};
//# sourceMappingURL=Container.native.js.map