Free open-source image editor for the browser
import { Button } from "@roprgm/ui/button";
import { IconButton } from "@roprgm/ui/icon-button";
import { Tooltip, TooltipContent, TooltipTrigger } from "@roprgm/ui/tooltip";
import { createContext, type ReactNode, useContext } from "react";
import { Canvas } from "vgpu-react";
import { Icon } from "@/components/icons/icon";
import type { Point } from "@/core/image/frame";
import { usePanZoom } from "@/hooks/use-pan-zoom";
import { useEditorSession } from "./session";
const Viewport = createContext<ReturnType<typeof usePanZoom> | null>(null);
export function useViewport() {
const viewport = useContext(Viewport);
if (!viewport) {
throw new Error("An editor viewport is required.");
}
return viewport;
}
/** Zoom as Photoshop counts it: 100% shows one image pixel per device pixel. */
function ZoomControl() {
const { view, scale, zoomBy, zoomTo, resetView } = useViewport();
const percent = Math.round(scale * devicePixelRatio * 100);
// Fit toggles to 100%; any other zoom returns to fit.
const fitted = view.zoom === 1;
return (
<div className="absolute right-3 bottom-3">
<div className="flex items-center rounded-full material-panel bg-level-4/80 p-0.5 backdrop-blur-sm">
<IconButton
label="Zoom out"
side="top"
className="size-6 rounded-full p-0"
onClick={() => zoomBy(1 / 1.25)}
>
<Icon viewBox="0 0 20 20" className="size-3.5">
<path d="M5 10h10" />
</Icon>
</IconButton>
<Tooltip>
<TooltipTrigger
render={
<Button
variant="ghost"
className="h-6 min-w-14 rounded-full px-1 text-foreground tabular-nums"
onClick={() =>
fitted ? zoomTo(1 / (scale * devicePixelRatio)) : resetView()
}
>
{percent}%
</Button>
}
/>
<TooltipContent side="top">
{fitted ? "Zoom to 100%" : "Fit to view"}
</TooltipContent>
</Tooltip>
<IconButton
label="Zoom in"
side="top"
className="size-6 rounded-full p-0"
onClick={() => zoomBy(1.25)}
>
<Icon viewBox="0 0 20 20" className="size-3.5">
<path d="M10 5v10M5 10h10" />
</Icon>
</IconButton>
</div>
</div>
);
}
/**
* The canvas region: a camera over `size` for everything inside, and the zoom control.
* Put the pannable content in a ViewportStage; other children float over it without panning.
*/
export function EditorViewport({
size,
constrain = true,
children,
}: {
size: Point;
constrain?: boolean;
children: ReactNode;
}) {
const { camera } = useEditorSession();
const viewport = usePanZoom(camera, size, { constrain, padding: 24 });
return (
<section
className="@container relative min-h-0 min-w-0 flex-1 overflow-hidden"
aria-label="Image canvas"
>
<Viewport value={viewport}>
{children}
<ZoomControl />
</Viewport>
</section>
);
}
/** The area that pans and zooms: the GPU canvas, with tool overlays drawn over it as siblings. */
export function ViewportStage({ children }: { children: ReactNode }) {
const viewport = useViewport();
return (
<div
ref={viewport.ref}
{...viewport.handlers}
data-pan-mode={viewport.panMode}
className="relative size-full cursor-grab touch-none active:cursor-grabbing data-[pan-mode=true]:[&_*]:cursor-grab! data-[pan-mode=true]:active:[&_*]:cursor-grabbing!"
>
<Canvas className="absolute inset-0 size-full">{children}</Canvas>
</div>
);
}