Skip to content

Commit

Permalink
Merge pull request #1108 from silx-kit/fix-annotations-overflow
Browse files Browse the repository at this point in the history
Prevent annotations and other `Html` elements from overflowing canvas
  • Loading branch information
axelboc authored May 9, 2022
2 parents e78c50d + eaecc6b commit 8a20458
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 40 deletions.
2 changes: 2 additions & 0 deletions packages/lib/src/vis/shared/AxisSystem.module.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.axisSystem {
position: absolute;
pointer-events: none;
display: grid;
grid-template-areas:
'. top-axis .'
Expand Down
64 changes: 34 additions & 30 deletions packages/lib/src/vis/shared/AxisSystem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getVisibleDomains } from '../utils';
import Axis from './Axis';
import styles from './AxisSystem.module.css';
import { useAxisSystemContext } from './AxisSystemProvider';
import Overlay from './Overlay';
import Html from './Html';

interface Props {
axisOffsets: AxisOffsets;
Expand All @@ -16,6 +16,7 @@ interface Props {
function AxisSystem(props: Props) {
const { axisOffsets, title } = props;

const gl = useThree((state) => state.gl);
const canvasSize = useThree((state) => state.size);
const { width, height } = canvasSize;

Expand All @@ -26,35 +27,38 @@ function AxisSystem(props: Props) {
);

return (
<Overlay
className={styles.axisSystem}
style={{
// Take over space reserved for axis by VisCanvas
top: -axisOffsets.top,
left: -axisOffsets.left,
width: width + axisOffsets.left + axisOffsets.right,
height: height + axisOffsets.bottom + axisOffsets.top,
gridTemplateColumns: `${axisOffsets.left}px 1fr ${axisOffsets.right}px`,
gridTemplateRows: `${axisOffsets.top}px 1fr ${axisOffsets.bottom}px`,
}}
>
{title && <p className={styles.title}>{title}</p>}
<Axis
type="abscissa"
config={abscissaConfig}
domain={xVisibleDomain}
canvasSize={canvasSize}
svgSize={{ width, height: axisOffsets.bottom }}
/>
<Axis
type="ordinate"
config={ordinateConfig}
domain={yVisibleDomain}
canvasSize={canvasSize}
svgSize={{ width: axisOffsets.left, height }}
flipAxis
/>
</Overlay>
// Append to `canvasWrapper` instead of default container `r3fRoot`, which hides overflow
<Html container={gl.domElement.parentElement?.parentElement || undefined}>
<div
className={styles.axisSystem}
style={{
// Take over space reserved for axis by VisCanvas
top: -axisOffsets.top,
left: -axisOffsets.left,
width: width + axisOffsets.left + axisOffsets.right,
height: height + axisOffsets.bottom + axisOffsets.top,
gridTemplateColumns: `${axisOffsets.left}px 1fr ${axisOffsets.right}px`,
gridTemplateRows: `${axisOffsets.top}px 1fr ${axisOffsets.bottom}px`,
}}
>
{title && <p className={styles.title}>{title}</p>}
<Axis
type="abscissa"
config={abscissaConfig}
domain={xVisibleDomain}
canvasSize={canvasSize}
svgSize={{ width, height: axisOffsets.bottom }}
/>
<Axis
type="ordinate"
config={ordinateConfig}
domain={yVisibleDomain}
canvasSize={canvasSize}
svgSize={{ width: axisOffsets.left, height }}
flipAxis
/>
</div>
</Html>
);
}

Expand Down
4 changes: 1 addition & 3 deletions packages/lib/src/vis/shared/VisCanvas.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@
}

.canvasWrapper {
position: relative; /* for axis system */
width: 100%; /* fill container by default (unless size is passed in JSX) */
height: 100%;
margin: 0 auto;
}

.r3fRoot {
overflow: visible !important; /* show child axis grid, which is bigger than canvas */
background-color: var(--h5w-canvas--bgColor, transparent);
}

.floatingToolbar {
position: absolute;
top: auto !important;
left: auto !important;
right: 0;
bottom: 0;
display: flex;
Expand Down
12 changes: 5 additions & 7 deletions packages/lib/src/vis/shared/VisCanvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import type { AxisConfig } from '../models';
import { getSizeToFit, getAxisOffsets } from '../utils';
import AxisSystem from './AxisSystem';
import AxisSystemProvider from './AxisSystemProvider';
import Html from './Html';
import RatioEnforcer from './RatioEnforcer';
import ViewportCenterer from './ViewportCenterer';
import styles from './VisCanvas.module.css';
Expand Down Expand Up @@ -81,15 +80,14 @@ function VisCanvas(props: PropsWithChildren<Props>) {
{children}
<ViewportCenterer />
<RatioEnforcer visRatio={visRatio} />
<Html>
<div
ref={(elem) => setFloatingToolbar(elem || undefined)}
className={styles.floatingToolbar}
/>
</Html>
</InteractionsProvider>
</AxisSystemProvider>
</Canvas>

<div
ref={(elem) => setFloatingToolbar(elem || undefined)}
className={styles.floatingToolbar}
/>
</div>
)}
</div>
Expand Down

0 comments on commit 8a20458

Please sign in to comment.