-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for embedding images in Mafs views. ![](https://github.com/user-attachments/assets/f2d19d91-fdcc-4672-9493-b8a62c53f7b9)
- Loading branch information
1 parent
36fc4ae
commit 7750e9a
Showing
24 changed files
with
316 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { PropTable } from "components/PropTable" | ||
|
||
import CodeAndExample from "components/CodeAndExample" | ||
import ImageExample from "guide-examples/display/images/ImageExample" | ||
import ImageAnchorExample from "guide-examples/display/images/ImageAnchorExample" | ||
import type { Metadata } from "next" | ||
|
||
export const metadata: Metadata = { | ||
title: "Images", | ||
} | ||
|
||
function Images() { | ||
return ( | ||
<> | ||
<p> | ||
Images in Mafs are just wrappers around the SVG <code>image</code> element, with some | ||
quality of life improvements tacked on (see below). | ||
</p> | ||
|
||
<CodeAndExample example={ImageExample} /> | ||
|
||
<PropTable of={"Image"} /> | ||
|
||
<h2> | ||
Comparison with SVG <code><image></code> | ||
</h2> | ||
|
||
<p> | ||
The SVG <code>image</code> element is a low-level way to include external images in an SVG. | ||
It has a few downsides: | ||
</p> | ||
|
||
<ul> | ||
<li>Negative widths and heights lead to undefined behavior.</li> | ||
<li> | ||
The x and y attributes correspond to the top left of the image and is not configurable. | ||
</li> | ||
</ul> | ||
|
||
<p> | ||
Mafs handles negative heights and widths the way you'd expect; by making the image grow in | ||
the <code>-x</code> and <code>-y</code> directions. | ||
</p> | ||
|
||
<p> | ||
Additionally, the <code>anchor</code> attribute of <code>Image</code> allows you to declare | ||
whether the image's x and y coordinates refer to the corners, center of edges, or center of | ||
the image. | ||
</p> | ||
|
||
<CodeAndExample example={ImageAnchorExample} /> | ||
</> | ||
) | ||
} | ||
|
||
export default Images |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
docs/components/guide-examples/display/images/ImageAnchorExample.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
"use client" | ||
|
||
import { | ||
Coordinates, | ||
Image, | ||
Mafs, | ||
useMovablePoint, | ||
} from "mafs" | ||
|
||
import image from "./mafs.png" | ||
|
||
export default function VectorExample() { | ||
const center = useMovablePoint([2, 2]) | ||
return ( | ||
<Mafs viewBox={{ x: [-1, 7], y: [-1, 5] }}> | ||
<Coordinates.Cartesian /> | ||
<Image | ||
href={image.src ?? image} | ||
anchor="tl" | ||
x={center.x + 0.1} | ||
y={center.y - 0.1} | ||
width={1} | ||
height={1} | ||
/> | ||
<Image | ||
href={image.src ?? image} | ||
anchor="tr" | ||
x={center.x - 0.1} | ||
y={center.y - 0.1} | ||
width={1} | ||
height={1} | ||
/> | ||
<Image | ||
href={image.src ?? image} | ||
anchor="bl" | ||
x={center.x + 0.1} | ||
y={center.y + 0.1} | ||
width={1} | ||
height={1} | ||
/> | ||
<Image | ||
href={image.src ?? image} | ||
anchor="br" | ||
x={center.x - 0.1} | ||
y={center.y + 0.1} | ||
width={1} | ||
height={1} | ||
/> | ||
{center.element} | ||
</Mafs> | ||
) | ||
} |
29 changes: 29 additions & 0 deletions
29
docs/components/guide-examples/display/images/ImageExample.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
"use client" | ||
|
||
import { | ||
Mafs, | ||
Image, | ||
Coordinates, | ||
useMovablePoint, | ||
} from "mafs" | ||
|
||
import image from "./mafs.png" | ||
|
||
export default function ImageExample() { | ||
const origin = useMovablePoint([1, 1]) | ||
|
||
return ( | ||
<Mafs viewBox={{ x: [-1, 7], y: [-1, 5] }}> | ||
<Coordinates.Cartesian /> | ||
<Image | ||
href={image.src ?? image} | ||
anchor="bl" | ||
x={origin.x} | ||
y={origin.y} | ||
width={2} | ||
height={2} | ||
/> | ||
{origin.element} | ||
</Mafs> | ||
) | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+11 KB
...ots/guide-examples-display-images-ImageAnchorExample-1-Mobile-Chrome-darwin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+16.3 KB
...ots/guide-examples-display-images-ImageAnchorExample-1-Mobile-Safari-darwin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+18 KB
...napshots/guide-examples-display-images-ImageAnchorExample-1-chromium-darwin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+28.4 KB
...snapshots/guide-examples-display-images-ImageAnchorExample-1-firefox-darwin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+24.8 KB
...-snapshots/guide-examples-display-images-ImageAnchorExample-1-webkit-darwin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+9.26 KB
...snapshots/guide-examples-display-images-ImageExample-1-Mobile-Chrome-darwin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+13 KB
...snapshots/guide-examples-display-images-ImageExample-1-Mobile-Safari-darwin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+14.9 KB
....tsx-snapshots/guide-examples-display-images-ImageExample-1-chromium-darwin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+28.4 KB
...c.tsx-snapshots/guide-examples-display-images-ImageExample-1-firefox-darwin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+20.4 KB
...ec.tsx-snapshots/guide-examples-display-images-ImageExample-1-webkit-darwin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { Anchor, computeAnchor } from "../math" | ||
|
||
export interface ImageProps { | ||
href: string | ||
x: number | ||
y: number | ||
/** | ||
* Indicate where, in the image (top, bottom, left, right, center), the x and | ||
* y coordinate refers to. | ||
*/ | ||
anchor?: Anchor | ||
width: number | ||
height: number | ||
/** | ||
* Whether to preserve the aspect ratio of the image. By default, the image | ||
* will be centered and scaled to fit the width and height. If you want to | ||
* squish the image to be the same shape as the box, set this to "none". | ||
* | ||
* This is passed directly to the `preserveAspectRatio` attribute of the SVG | ||
* `<image>` element. | ||
* | ||
* See [preserveAspectRatio](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/preserveAspectRatio) on MDN. | ||
*/ | ||
preserveAspectRatio?: string | ||
|
||
svgImageProps?: React.SVGProps<SVGImageElement> | ||
} | ||
|
||
export function Image({ | ||
href, | ||
x, | ||
y, | ||
width, | ||
height, | ||
anchor = "bl", | ||
preserveAspectRatio, | ||
svgImageProps, | ||
}: ImageProps) { | ||
const [anchorX, anchorY] = computeAnchor(anchor, x, y, width, height) | ||
|
||
const transform = [ | ||
"var(--mafs-view-transform)", | ||
"var(--mafs-user-transform)", | ||
// Ensure the image is not upside down (since Mafs has the y-axis pointing | ||
// up, while SVG has it pointing down). | ||
"scaleY(-1)", | ||
].join(" ") | ||
|
||
return ( | ||
<image | ||
href={href} | ||
x={anchorX} | ||
y={-anchorY} | ||
width={width} | ||
height={height} | ||
preserveAspectRatio={preserveAspectRatio} | ||
{...svgImageProps} | ||
style={{ transform }} | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
declare module "*.css" { | ||
const content: { [className: string]: string } | ||
export default content | ||
} | ||
|
||
declare module "*.png" { | ||
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */ | ||
const content: any | ||
export default content | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.