Skip to content

Commit

Permalink
[sc] hide offscreen cards
Browse files Browse the repository at this point in the history
  • Loading branch information
a-type committed Jun 6, 2024
1 parent 1ab0222 commit 5a52d33
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
1 change: 1 addition & 0 deletions apps/star-chart/web/src/components/canvas/AutoPan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export class AutoPan extends EventSubscriber<{
};

private loop = () => {
console.log('autopan loop');
const autoPan = this.getAutoPan();

if (vectorLength(autoPan)) {
Expand Down
28 changes: 28 additions & 0 deletions apps/star-chart/web/src/components/canvas/CanvasObject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
createContext,
CSSProperties,
ReactNode,
RefObject,
useCallback,
useContext,
useEffect,
Expand Down Expand Up @@ -48,6 +49,7 @@ export function CanvasObjectRoot({
}: CanvasObjectRootProps) {
const ref = useRef<HTMLDivElement>(null);
useRerasterize(ref);
useHideOffscreen(ref);

const register = useRegister(canvasObject.id, canvasObject.metadata);
const finalRef = useMergedRef(ref, register);
Expand Down Expand Up @@ -222,3 +224,29 @@ export function useCanvasObject({

return canvasObject;
}

function useHideOffscreen(ref: RefObject<HTMLDivElement | null>) {
useEffect(() => {
if (!ref.current) return;
const observer = new IntersectionObserver(
([entry]) => {
if (!entry.isIntersecting) {
ref.current?.style.setProperty('visibility', 'hidden');
} else {
ref.current?.style.setProperty('visibility', 'visible');
}
},
{
root: null,
rootMargin: '0px',
threshold: 0,
},
);

observer.observe(ref.current);

return () => {
observer.disconnect();
};
}, [ref]);
}
18 changes: 14 additions & 4 deletions apps/star-chart/web/src/components/canvas/Wire.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface WireProps extends Omit<SVGProps<SVGPathElement>, 'ref'> {
targetPosition: LiveVector2;
className?: string;
hoverClassName?: string;
onTap?: (relativePosition: Vector2, info: CanvasGestureInfo) => void;
onTap?: (info: CanvasGestureInfo) => void;
id: string;
metadata?: any;
}
Expand Down Expand Up @@ -53,10 +53,17 @@ export function Wire({

state.event.preventDefault();
state.event.stopPropagation();
onTap?.(worldPos, {
onTap?.({
shift: state.shiftKey,
ctrlOrMeta: state.ctrlKey || state.metaKey,
alt: state.altKey,
delta: viewport.viewportDeltaToWorld({
x: state.delta[0],
y: state.delta[1],
}),
worldPosition: worldPos,
intentional: state.intentional,
targetId: id,
});
}
},
Expand Down Expand Up @@ -100,7 +107,7 @@ export function Wire({
fill="none"
opacity="50%"
className={clsx(
'touch-none',
'touch-none content-visibility-auto',
onTap && hovered ? hoverClassName : 'stroke-transparent',
onTap ? 'cursor-pointer' : '',
)}
Expand All @@ -110,7 +117,10 @@ export function Wire({
id={id}
d={curve}
fill="none"
className={clsx('pointer-events-none', className)}
className={clsx(
'pointer-events-none content-visibility-auto',
className,
)}
data-hovered={hovered}
{...rest}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function ConnectionWire({ connection }: ConnectionWireProps) {

const canvas = useCanvas();
const onTap = useCallback(
(pos: Vector2, info: CanvasGestureInfo) => {
(info: CanvasGestureInfo) => {
if (info.shift) {
canvas.selections.add(id);
} else {
Expand Down

0 comments on commit 5a52d33

Please sign in to comment.