-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Image component #174
Merged
Merged
Add Image component #174
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b106832
Add Image component
stevenpetryk c6c11f7
More docs
stevenpetryk 0521dbf
More tweaks
stevenpetryk 8f3dc5c
Update fixtures
stevenpetryk 1ec0dc7
Update fixtures
stevenpetryk 449bfa3
Fix linters
stevenpetryk 3e0a87b
Final tweaks
stevenpetryk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This snapshot sketches me out. Why is it not aligned? Doesn't repro when I actually use Safari locally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.