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