Skip to content

Commit

Permalink
feat(Html): Apply transformContext when using mode='canvas' (alig…
Browse files Browse the repository at this point in the history
…n with `Svg` and `Canvas`)
  • Loading branch information
techniq committed Feb 12, 2025
1 parent dcf6c2e commit 2b46908
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-pumas-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'layerchart': minor
---

feat(Html): Apply `transformContext` when using `mode='canvas'` (align with `Svg` and `Canvas`)
23 changes: 22 additions & 1 deletion packages/layerchart/src/lib/components/layout/Html.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
import { cls } from '@layerstack/tailwind';
import { chartContext } from '../ChartContext.svelte';
import { transformContext } from '../TransformContext.svelte';
/** The layer's outermost `<div>` tag. Useful for bindings. */
export let element: HTMLDivElement | undefined = undefined;
Expand All @@ -23,9 +24,27 @@
/** A string passed to the `aria-describedby` property on the `<div>` tag. */
export let describedBy: string | undefined = undefined;
const { padding } = chartContext();
/**
* Translate children to center (useful for radial layouts)
*/
export let center: boolean | 'x' | 'y' = false;
$: roleVal = role || (label || labelledBy || describedBy ? 'figure' : undefined);
const { width, height, padding } = chartContext();
const { mode, scale, translate } = transformContext();
$: transform = center
? `translate(${center === 'x' || center === true ? $width / 2 : 0}, ${center === 'y' || center === true ? $height / 2 : 0})`
: '';
$: if (mode === 'canvas') {
const center = { x: $width / 2, y: $height / 2 };
const newTranslate = {
x: $translate.x * $scale + center.x - center.x * $scale,
y: $translate.y * $scale + center.y - center.y * $scale,
};
transform = `translate(${newTranslate.x}px,${newTranslate.y}px) scale(${$scale})`;
}
</script>

<div
Expand All @@ -36,6 +55,8 @@
pointerEvents === false && 'pointer-events-none',
$$props.class
)}
style:transform
style:transform-origin="top left"
style:z-index={zIndex}
style:pointer-events={pointerEvents === false ? 'none' : null}
style:top="{$padding.top}px"
Expand Down
1 change: 0 additions & 1 deletion packages/layerchart/src/lib/components/layout/Svg.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
export let center: boolean | 'x' | 'y' = false;
const { containerWidth, containerHeight, width, height, padding } = chartContext();
const { mode, scale, translate } = transformContext();
$: transform = center
Expand Down

0 comments on commit 2b46908

Please # to comment.