Skip to content
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 7 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions .api-report/mafs.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

```ts

/// <reference types="react" />

import { JSX as JSX_2 } from 'react/jsx-runtime';
import { KatexOptions } from 'katex';
import * as React_2 from 'react';
Expand Down Expand Up @@ -82,6 +80,29 @@ export interface Filled {
weight?: number;
}

// @public (undocumented)
function Image_2({ href, x, y, width, height, anchor, preserveAspectRatio, svgImageProps, }: ImageProps): JSX_2.Element;
export { Image_2 as Image }

// @public (undocumented)
export interface ImageProps {
// Warning: (ae-forgotten-export) The symbol "Anchor" needs to be exported by the entry point index.d.ts
anchor?: Anchor;
// (undocumented)
height: number;
// (undocumented)
href: string;
preserveAspectRatio?: string;
// (undocumented)
svgImageProps?: React.SVGProps<SVGImageElement>;
// (undocumented)
width: number;
// (undocumented)
x: number;
// (undocumented)
y: number;
}

// @public (undocumented)
export type Interval = [min: number, max: number];

Expand Down
56 changes: 56 additions & 0 deletions docs/app/guides/display/images/page.tsx
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>&lt;image&gt;</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
2 changes: 2 additions & 0 deletions docs/app/guides/guides.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
TextIcon,
CursorArrowIcon,
PlayIcon,
ImageIcon,
} from "@radix-ui/react-icons"

import {
Expand Down Expand Up @@ -62,6 +63,7 @@ export const Guides: Section[] = [
{ title: "Plots", icon: FunctionIcon, slug: "plots" },
{ title: "Text", icon: TextIcon, slug: "text" },
{ title: "Vectors", icon: ArrowTopRightIcon, slug: "vectors" },
{ title: "Images", icon: ImageIcon, slug: "images" },
{ separator: true },
{ title: "Transform", icon: RotateCounterClockwiseIcon, slug: "transform" },
{ title: "Debug", icon: DebugIcon, slug: "debug" },
Expand Down
4 changes: 2 additions & 2 deletions docs/components/PropTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ function PropType({ prop }: { prop: DocgenProp }) {
return (
<div className="flex flex-col gap-1">
{prop.description && (
<p className="text-gray-800 dark:text-slate-200 markdown">
<div className="text-gray-800 dark:text-slate-200 markdown">
<ReactMarkdown>{prop.description}</ReactMarkdown>
</p>
</div>
)}
<div className="text-gray-600 dark:text-slate-400">{typeNode}</div>
</div>
Expand Down
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 docs/components/guide-examples/display/images/ImageExample.tsx
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.
20 changes: 20 additions & 0 deletions e2e/generated-vrt.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import SimpleTransform from "../docs/components/guide-examples/utility/SimpleTra
import CartesianCoordinatesConfigExample from "../docs/components/guide-examples/display/coordinates/CartesianCoordinatesConfigExample"
import CartesianCoordinatesExample from "../docs/components/guide-examples/display/coordinates/CartesianCoordinatesExample"
import PolarCoordinatesExample from "../docs/components/guide-examples/display/coordinates/PolarCoordinatesExample"
import ImageAnchorExample from "../docs/components/guide-examples/display/images/ImageAnchorExample"
import ImageExample from "../docs/components/guide-examples/display/images/ImageExample"
import VectorExample from "../docs/components/guide-examples/display/vectors/VectorExample"
import ContainViewbox from "../docs/components/guide-examples/display/viewbox/ContainViewbox"
import StretchViewbox from "../docs/components/guide-examples/display/viewbox/StretchViewbox"
Expand Down Expand Up @@ -381,6 +383,24 @@ test("guide-examples/display/coordinates/PolarCoordinatesExample", async ({ moun
</TestContextProvider>,
))

test("guide-examples/display/images/ImageAnchorExample", async ({ mount, page }) =>
await visualTest(
mount,
page,
<TestContextProvider value={{ overrideHeight: 500 }}>
<ImageAnchorExample />
</TestContextProvider>,
))

test("guide-examples/display/images/ImageExample", async ({ mount, page }) =>
await visualTest(
mount,
page,
<TestContextProvider value={{ overrideHeight: 500 }}>
<ImageExample />
</TestContextProvider>,
))

test("guide-examples/display/vectors/VectorExample", async ({ mount, page }) =>
await visualTest(
mount,
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Copy link
Owner Author

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.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CleanShot 2024-10-20 at 15 43 14@2x

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 61 additions & 0 deletions src/display/Image.tsx
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 }}
/>
)
}
3 changes: 3 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export type { PointProps } from "./display/Point"
export { Vector } from "./display/Vector"
export type { VectorProps } from "./display/Vector"

export { Image } from "./display/Image"
export type { ImageProps } from "./display/Image"

export { Text } from "./display/Text"
export type { TextProps, CardinalDirection } from "./display/Text"

Expand Down
57 changes: 57 additions & 0 deletions src/math.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type Interval = [min: number, max: number]
export type Anchor = "tl" | "tc" | "tr" | "cl" | "cc" | "cr" | "bl" | "bc" | "br"

export function round(value: number, precision = 0): number {
const multiplier = Math.pow(10, precision || 0)
Expand All @@ -24,3 +25,59 @@ export function range(min: number, max: number, step = 1): number[] {
export function clamp(number: number, min: number, max: number): number {
return Math.min(Math.max(number, min), max)
}

/**
* Given an anchor and a bounding box (x, y, width, height), compute the x and y coordinates of the
* anchor such that rendering an element at those coordinates will align the element with the anchor.
*/
export function computeAnchor(
anchor: Anchor,
x: number,
y: number,
width: number,
height: number,
): [number, number] {
let actualX = x
let actualY = y

switch (anchor) {
case "tl":
actualX = x
actualY = y
break
case "tc":
actualX = x - width / 2
actualY = y
break
case "tr":
actualX = x - width
actualY = y
break
case "cl":
actualX = x
actualY = y + height / 2
break
case "cc":
actualX = x - width / 2
actualY = y + height / 2
break
case "cr":
actualX = x - width
actualY = y + height / 2
break
case "bl":
actualX = x
actualY = y + height
break
case "bc":
actualX = x - width / 2
actualY = y + height
break
case "br":
actualX = x - width
actualY = y + height
break
}

return [actualX, actualY]
}
10 changes: 10 additions & 0 deletions src/typings/env.d.ts
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
}
4 changes: 0 additions & 4 deletions src/typings/styles.d.ts

This file was deleted.

Loading
Loading