Isolate Cursor Pos debug text on canvas to prevent rerenders

This commit is contained in:
blessedcoolant 2022-11-22 17:46:33 +13:00
parent 78217f5ef9
commit 2fcc7d9b36
2 changed files with 34 additions and 8 deletions

View File

@ -2,6 +2,7 @@ import { createSelector } from '@reduxjs/toolkit';
import { useAppSelector } from 'app/store'; import { useAppSelector } from 'app/store';
import _ from 'lodash'; import _ from 'lodash';
import { canvasSelector } from 'features/canvas/store/canvasSelectors'; import { canvasSelector } from 'features/canvas/store/canvasSelectors';
import IAICanvasStatusTextCursorPos from './IAICanvasStatusText/IAICanvasStatusTextCursorPos';
const roundToHundreth = (val: number): number => { const roundToHundreth = (val: number): number => {
return Math.round(val * 100) / 100; return Math.round(val * 100) / 100;
@ -15,16 +16,11 @@ const selector = createSelector(
stageCoordinates: { x: stageX, y: stageY }, stageCoordinates: { x: stageX, y: stageY },
boundingBoxDimensions: { width: boxWidth, height: boxHeight }, boundingBoxDimensions: { width: boxWidth, height: boxHeight },
boundingBoxCoordinates: { x: boxX, y: boxY }, boundingBoxCoordinates: { x: boxX, y: boxY },
cursorPosition,
stageScale, stageScale,
shouldShowCanvasDebugInfo, shouldShowCanvasDebugInfo,
layer, layer,
} = canvas; } = canvas;
const { cursorX, cursorY } = cursorPosition
? { cursorX: cursorPosition.x, cursorY: cursorPosition.y }
: { cursorX: -1, cursorY: -1 };
return { return {
activeLayerColor: activeLayerColor:
layer === 'mask' ? 'var(--status-working-color)' : 'inherit', layer === 'mask' ? 'var(--status-working-color)' : 'inherit',
@ -42,7 +38,6 @@ const selector = createSelector(
)}`, )}`,
canvasDimensionsString: `${stageWidth}×${stageHeight}`, canvasDimensionsString: `${stageWidth}×${stageHeight}`,
canvasScaleString: Math.round(stageScale * 100), canvasScaleString: Math.round(stageScale * 100),
cursorCoordinatesString: `(${cursorX}, ${cursorY})`,
shouldShowCanvasDebugInfo, shouldShowCanvasDebugInfo,
}; };
}, },
@ -52,6 +47,7 @@ const selector = createSelector(
}, },
} }
); );
const IAICanvasStatusText = () => { const IAICanvasStatusText = () => {
const { const {
activeLayerColor, activeLayerColor,
@ -62,7 +58,6 @@ const IAICanvasStatusText = () => {
canvasCoordinatesString, canvasCoordinatesString,
canvasDimensionsString, canvasDimensionsString,
canvasScaleString, canvasScaleString,
cursorCoordinatesString,
shouldShowCanvasDebugInfo, shouldShowCanvasDebugInfo,
} = useAppSelector(selector); } = useAppSelector(selector);
@ -84,7 +79,7 @@ const IAICanvasStatusText = () => {
<div>{`Bounding Box Position: ${boundingBoxCoordinatesString}`}</div> <div>{`Bounding Box Position: ${boundingBoxCoordinatesString}`}</div>
<div>{`Canvas Dimensions: ${canvasDimensionsString}`}</div> <div>{`Canvas Dimensions: ${canvasDimensionsString}`}</div>
<div>{`Canvas Position: ${canvasCoordinatesString}`}</div> <div>{`Canvas Position: ${canvasCoordinatesString}`}</div>
<div>{`Cursor Position: ${cursorCoordinatesString}`}</div> <IAICanvasStatusTextCursorPos />
</> </>
)} )}
</div> </div>

View File

@ -0,0 +1,31 @@
import { createSelector } from '@reduxjs/toolkit';
import { useAppSelector } from 'app/store';
import { canvasSelector } from 'features/canvas/store/canvasSelectors';
import React from 'react';
import _ from 'lodash';
const cursorPositionSelector = createSelector(
[canvasSelector],
(canvas) => {
const { cursorPosition } = canvas;
const { cursorX, cursorY } = cursorPosition
? { cursorX: cursorPosition.x, cursorY: cursorPosition.y }
: { cursorX: -1, cursorY: -1 };
return {
cursorCoordinatesString: `(${cursorX}, ${cursorY})`,
};
},
{
memoizeOptions: {
resultEqualityCheck: _.isEqual,
},
}
);
export default function IAICanvasStatusTextCursorPos() {
const { cursorCoordinatesString } = useAppSelector(cursorPositionSelector);
return <div>{`Cursor Position: ${cursorCoordinatesString}`}</div>;
}