diff --git a/src/Components/SVG/SVG.tsx b/src/Components/SVG/SVG.tsx index e79827c..7af91b1 100644 --- a/src/Components/SVG/SVG.tsx +++ b/src/Components/SVG/SVG.tsx @@ -1,12 +1,12 @@ import './SVG.scss'; import * as React from 'react'; -import { UncontrolledReactSVGPanZoom } from 'react-svg-pan-zoom'; +import { ReactSVGPanZoom, Tool, TOOL_PAN, Value } from 'react-svg-pan-zoom'; import { Container } from './Elements/Container'; import { ContainerModel } from '../../Interfaces/IContainerModel'; import { Selector } from './Elements/Selector/Selector'; import { BAR_WIDTH } from '../Bar/Bar'; import { DepthDimensionLayer } from './Elements/DepthDimensionLayer'; -import { SHOW_DIMENSIONS_PER_DEPTH } from '../../utils/default'; +import { MAX_FRAMERATE, SHOW_DIMENSIONS_PER_DEPTH } from '../../utils/default'; import { SymbolLayer } from './Elements/SymbolLayer'; import { ISymbolModel } from '../../Interfaces/ISymbolModel'; @@ -54,8 +54,20 @@ export function SVG(props: ISVGProps): JSX.Element { viewerWidth: window.innerWidth - BAR_WIDTH, viewerHeight: window.innerHeight }); + const [tool, setTool] = React.useState(TOOL_PAN); + const [value, setValue] = React.useState({} as Value); + const svgViewer = React.useRef(null); + + // Framerate limiter + const delta = React.useRef(0); + const timer = React.useRef(performance.now()); + const renderCounter = React.useRef(0); + // Debug: FPS counter + // const startTimer = React.useRef(Date.now()); + // console.log(renderCounter.current / ((Date.now() - startTimer.current) / 1000)); UseSVGAutoResizer(setViewer); + UseFitOnce(svgViewer); const xmlns = ''; const properties = { @@ -73,9 +85,24 @@ export function SVG(props: ISVGProps): JSX.Element { return (
- { + // Framerate limiter + const newTimer = performance.now(); + delta.current += (newTimer - timer.current) / 1000; + timer.current = newTimer; + if (delta.current <= (1 / MAX_FRAMERATE)) { + return; + } + + renderCounter.current = renderCounter.current + 1; + delta.current = delta.current % (1 / MAX_FRAMERATE); + setValue(value); + }} background={'#ffffff'} defaultTool='pan' miniatureProps={{ @@ -93,7 +120,12 @@ export function SVG(props: ISVGProps): JSX.Element { {/* leave this at the end so it can be removed during the svg export */} - +
); } +function UseFitOnce(svgViewer: React.RefObject) { + React.useEffect(() => { + svgViewer?.current?.fitToViewer(); + }, []); +} diff --git a/src/utils/default.ts b/src/utils/default.ts index 49e7c56..792723a 100644 --- a/src/utils/default.ts +++ b/src/utils/default.ts @@ -33,6 +33,7 @@ export const DEFAULT_SYMBOL_HEIGHT = 32; export const ENABLE_SHORTCUTS = true; export const MAX_HISTORY = 200; export const APPLY_BEHAVIORS_ON_CHILDREN = true; +export const MAX_FRAMERATE = 120; /** * Returns the default editor state given the configuration