feat: add dev tools for renderer

This commit is contained in:
Peng Xiao
2022-06-02 12:24:20 +08:00
parent 3afaa9c03b
commit 7a70231f5e
11 changed files with 85 additions and 13 deletions

View File

@@ -0,0 +1,66 @@
import { useRendererContext } from '@tldraw/react'
import { observer } from 'mobx-react-lite'
import React from 'react'
import ReactDOM from 'react-dom'
const printPoint = (point: number[]) => {
return `[${point.map(d => d.toFixed(2)).join(', ')}]`
}
export const DevTools = observer(() => {
const {
viewport: {
camera: { point, zoom },
},
inputs,
} = useRendererContext()
const canvasAnchorRef = React.useRef<HTMLElement | null>()
const statusbarAnchorRef = React.useRef<HTMLElement | null>()
React.useEffect(() => {
const canvasAnchor = document.getElementById('tl-dev-tools-canvas-anchor')
canvasAnchorRef.current = canvasAnchor
const statusbarAnchor = document.getElementById('tl-statusbar-anchor')
statusbarAnchorRef.current = statusbarAnchor
}, [])
const originPoint = canvasAnchorRef.current
? ReactDOM.createPortal(
<svg className="tl-renderer-dev-tools tl-grid">
<circle cx={point[0] * zoom} cy={point[1] * zoom} r="4" fill="red" />
</svg>,
canvasAnchorRef.current
)
: null
const rendererStatusText = [
['Z', zoom.toFixed(2)],
['MP', printPoint(inputs.currentPoint)],
['MS', printPoint(inputs.currentScreenPoint)],
['VP', printPoint(point)],
]
.map(p => p.join(''))
.join('|')
const rendererStatus = statusbarAnchorRef.current
? ReactDOM.createPortal(
<div
style={{
flex: 1,
}}
>
{rendererStatusText}
</div>,
statusbarAnchorRef.current
)
: null
return (
<>
{originPoint}
{rendererStatus}
</>
)
})

View File

@@ -0,0 +1 @@
export * from './Devtools'