|
@@ -1,7 +1,8 @@
|
|
|
/**
|
|
|
- * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
|
|
|
+ * Copyright (c) 2020-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
|
|
|
*
|
|
|
* @author David Sehnal <david.sehnal@gmail.com>
|
|
|
+ * @author Alexander Rose <alexander.rose@weirdbyte.de>
|
|
|
*/
|
|
|
|
|
|
import * as React from 'react';
|
|
@@ -25,7 +26,7 @@ export interface ScreenshotPreviewProps {
|
|
|
const _ScreenshotPreview = (props: ScreenshotPreviewProps) => {
|
|
|
const { plugin, cropFrameColor } = props;
|
|
|
|
|
|
- const helper = plugin.helpers.viewportScreenshot!;
|
|
|
+ const helper = plugin.helpers.viewportScreenshot;
|
|
|
const [currentCanvas, setCurrentCanvas] = useState<HTMLCanvasElement | null>(null);
|
|
|
const canvasRef = useRef<HTMLCanvasElement | null>(null);
|
|
|
const propsRef = useRef(props);
|
|
@@ -70,8 +71,8 @@ const _ScreenshotPreview = (props: ScreenshotPreviewProps) => {
|
|
|
subscribe(plugin.state.data.behaviors.isUpdating, v => {
|
|
|
if (!v) isDirty = true;
|
|
|
});
|
|
|
- subscribe(helper.behaviors.values, () => isDirty = true);
|
|
|
- subscribe(helper.behaviors.cropParams, () => isDirty = true);
|
|
|
+ subscribe(helper?.behaviors.values, () => isDirty = true);
|
|
|
+ subscribe(helper?.behaviors.cropParams, () => isDirty = true);
|
|
|
|
|
|
let resizeObserver: any = void 0;
|
|
|
if (typeof ResizeObserver !== 'undefined') {
|
|
@@ -108,7 +109,9 @@ export const ScreenshotPreview = React.memo(_ScreenshotPreview, (prev, next) =>
|
|
|
|
|
|
declare const ResizeObserver: any;
|
|
|
|
|
|
-function drawPreview(helper: ViewportScreenshotHelper, target: HTMLCanvasElement, customBackground?: string, borderColor?: string, borderWidth?: number) {
|
|
|
+function drawPreview(helper: ViewportScreenshotHelper | undefined, target: HTMLCanvasElement, customBackground?: string, borderColor?: string, borderWidth?: number) {
|
|
|
+ if (!helper) return;
|
|
|
+
|
|
|
const { canvas, width, height } = helper.getPreview()!;
|
|
|
const ctx = target.getContext('2d');
|
|
|
if (!ctx) return;
|
|
@@ -151,9 +154,9 @@ function drawPreview(helper: ViewportScreenshotHelper, target: HTMLCanvasElement
|
|
|
|
|
|
function ViewportFrame({ plugin, canvas, color = 'rgba(255, 87, 45, 0.75)' }: { plugin: PluginContext, canvas: HTMLCanvasElement | null, color?: string }) {
|
|
|
const helper = plugin.helpers.viewportScreenshot;
|
|
|
- const params = useBehavior(helper?.behaviors.values!);
|
|
|
- const cropParams = useBehavior(helper?.behaviors.cropParams!);
|
|
|
- const crop = useBehavior(helper?.behaviors.relativeCrop!);
|
|
|
+ const params = useBehavior(helper?.behaviors.values);
|
|
|
+ const cropParams = useBehavior(helper?.behaviors.cropParams);
|
|
|
+ const crop = useBehavior(helper?.behaviors.relativeCrop);
|
|
|
const cropFrameRef = useRef<Viewport>({ x: 0, y: 0, width: 0, height: 0 });
|
|
|
useBehavior(params?.resolution.name === 'viewport' ? plugin.canvas3d?.resized : void 0);
|
|
|
|
|
@@ -161,7 +164,7 @@ function ViewportFrame({ plugin, canvas, color = 'rgba(255, 87, 45, 0.75)' }: {
|
|
|
const [start, setStart] = useState([0, 0]);
|
|
|
const [current, setCurrent] = useState([0, 0]);
|
|
|
|
|
|
- if (!helper || !canvas) return null;
|
|
|
+ if (!helper || !canvas || !crop) return null;
|
|
|
|
|
|
const { width, height } = helper.getSizeAndViewport();
|
|
|
|
|
@@ -267,7 +270,7 @@ function ViewportFrame({ plugin, canvas, color = 'rgba(255, 87, 45, 0.75)' }: {
|
|
|
|
|
|
function finish() {
|
|
|
const cropFrame = cropFrameRef.current;
|
|
|
- if (cropParams.auto) {
|
|
|
+ if (cropParams?.auto) {
|
|
|
helper?.behaviors.cropParams.next({ ...cropParams, auto: false });
|
|
|
}
|
|
|
helper?.behaviors.relativeCrop.next({
|