@shopify/react-native-skia
Version:
High-performance React Native Graphics using Skia
82 lines (80 loc) • 3.29 kB
JavaScript
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(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 React from "react";
import SkiaPictureViewNativeComponent from "../specs/SkiaPictureViewNativeComponent";
import { SkiaViewApi } from "./api";
import { SkiaViewNativeId } from "./SkiaViewNativeId";
const NativeSkiaPictureView = SkiaPictureViewNativeComponent;
export class SkiaPictureView extends React.Component {
constructor(props) {
super(props);
_defineProperty(this, "_nativeId", void 0);
this._nativeId = SkiaViewNativeId.current++;
const {
picture,
onSize
} = props;
if (picture) {
assertSkiaViewApi();
SkiaViewApi.setJsiProperty(this._nativeId, "picture", picture);
}
if (onSize) {
assertSkiaViewApi();
SkiaViewApi.setJsiProperty(this._nativeId, "onSize", onSize);
}
}
get nativeId() {
return this._nativeId;
}
componentDidUpdate(prevProps) {
const {
picture,
onSize
} = this.props;
if (picture !== prevProps.picture) {
assertSkiaViewApi();
SkiaViewApi.setJsiProperty(this._nativeId, "picture", picture);
}
if (onSize !== prevProps.onSize) {
assertSkiaViewApi();
SkiaViewApi.setJsiProperty(this._nativeId, "onSize", onSize);
}
}
/**
* Creates a snapshot from the canvas in the surface
* @param rect Rect to use as bounds. Optional.
* @returns An Image object.
*/
makeImageSnapshot(rect) {
assertSkiaViewApi();
return SkiaViewApi.makeImageSnapshot(this._nativeId, rect);
}
/**
* Sends a redraw request to the native SkiaView.
*/
redraw() {
assertSkiaViewApi();
SkiaViewApi.requestRedraw(this._nativeId);
}
render() {
const {
mode,
debug = false,
...viewProps
} = this.props;
return /*#__PURE__*/React.createElement(NativeSkiaPictureView, _extends({
collapsable: false,
nativeID: `${this._nativeId}`,
mode: mode !== null && mode !== void 0 ? mode : "default",
debug: debug
}, viewProps));
}
}
const assertSkiaViewApi = () => {
if (SkiaViewApi === null || SkiaViewApi.setJsiProperty === null || SkiaViewApi.callJsiMethod === null || SkiaViewApi.requestRedraw === null || SkiaViewApi.makeImageSnapshot === null) {
throw Error("Skia View Api was not found.");
}
};
//# sourceMappingURL=SkiaPictureView.js.map