Skip to content

Commit f5f551c

Browse files
committed
More tweaks
1 parent 5ff6f76 commit f5f551c

File tree

4 files changed

+85
-36
lines changed

4 files changed

+85
-36
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,55 @@
11
"use client"
22

3-
import { Coordinates, Image, Mafs } from "mafs"
3+
import {
4+
Coordinates,
5+
Debug,
6+
Image,
7+
Mafs,
8+
useMovablePoint,
9+
} from "mafs"
410

511
import mafs from "./mafs.png"
612

7-
export default function VectorExample() {
13+
export default function ImageExample() {
14+
const origin = useMovablePoint([3, 3])
15+
const padding = 0.1
16+
817
return (
918
<Mafs viewBox={{ x: [-1, 7], y: [-1, 5] }}>
1019
<Coordinates.Cartesian />
1120
<Image
1221
src={mafs.src}
13-
anchor="cc"
14-
x={2}
15-
y={2}
22+
anchor="tl"
23+
x={origin.x + padding}
24+
y={origin.y - padding}
25+
width={2}
26+
height={2}
27+
/>
28+
<Image
29+
src={mafs.src}
30+
anchor="tr"
31+
x={origin.x - padding}
32+
y={origin.y - padding}
33+
width={2}
34+
height={2}
35+
/>
36+
<Image
37+
src={mafs.src}
38+
anchor="bl"
39+
x={origin.x + padding}
40+
y={origin.y + padding}
41+
width={2}
42+
height={2}
43+
/>
44+
<Image
45+
src={mafs.src}
46+
anchor="br"
47+
x={origin.x - padding}
48+
y={origin.y + padding}
1649
width={2}
1750
height={2}
1851
/>
52+
{origin.element}
1953
</Mafs>
2054
)
2155
}

src/display/Image.tsx

+33-18
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,42 @@ export function Image({
2121
preserveAspectRatio,
2222
svgImageProps,
2323
}: ImageProps) {
24-
const [actualX, actualY] = computeAnchor(anchor, x, y, width, height)
24+
const [anchorX, anchorY] = computeAnchor(anchor, x, y, width, height)
2525

26-
const scaleX = width < 0 ? -1 : 1
27-
const scaleY = height < 0 ? -1 : 1
26+
const transform = [
27+
"var(--mafs-view-transform)",
28+
"var(--mafs-user-transform)",
29+
// Ensure the image is not upside down (since Mafs has the y-axis pointing,
30+
// while SVG has it pointing down)
31+
"scaleY(-1)",
32+
].join(" ")
2833

29-
console.log(actualX - (width < 0 ? -width : 0))
34+
const debug = false
3035

3136
return (
32-
<image
33-
href={src}
34-
x={actualX - (width < 0 ? -width : 0)}
35-
y={actualY - (height < 0 ? -height : 0)}
36-
width={Math.abs(width)}
37-
height={Math.abs(height)}
38-
preserveAspectRatio={preserveAspectRatio}
39-
{...svgImageProps}
40-
style={{
41-
transform: [`var(--mafs-view-transform)`, `var(--mafs-user-transform)`, `scaleY(-1) `].join(
42-
" ",
43-
),
44-
}}
45-
/>
37+
<>
38+
{debug && (
39+
<rect
40+
x={anchorX}
41+
y={-anchorY}
42+
width={width}
43+
height={height}
44+
fill="none"
45+
stroke="red"
46+
vectorEffect={"non-scaling-stroke"}
47+
style={{ transform }}
48+
/>
49+
)}
50+
<image
51+
href={src}
52+
x={anchorX}
53+
y={-anchorY}
54+
width={width}
55+
height={height}
56+
preserveAspectRatio={preserveAspectRatio}
57+
{...svgImageProps}
58+
style={{ transform }}
59+
/>
60+
</>
4661
)
4762
}

src/math.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -43,39 +43,39 @@ export function computeAnchor(
4343
switch (anchor) {
4444
case "tl":
4545
actualX = x
46-
actualY = -y
46+
actualY = y
4747
break
4848
case "tc":
4949
actualX = x - width / 2
50-
actualY = -y
50+
actualY = y
5151
break
5252
case "tr":
5353
actualX = x - width
54-
actualY = -y
54+
actualY = y
5555
break
5656
case "cl":
5757
actualX = x
58-
actualY = -y - height / 2
58+
actualY = y + height / 2
5959
break
6060
case "cc":
6161
actualX = x - width / 2
62-
actualY = -y - height / 2
62+
actualY = y + height / 2
6363
break
6464
case "cr":
6565
actualX = x - width
66-
actualY = -y - height / 2
66+
actualY = y + height / 2
6767
break
6868
case "bl":
6969
actualX = x
70-
actualY = -y - height
70+
actualY = y + height
7171
break
7272
case "bc":
7373
actualX = x - width / 2
74-
actualY = -y - height
74+
actualY = y + height
7575
break
7676
case "br":
7777
actualX = x - width
78-
actualY = -y - height
78+
actualY = y + height
7979
break
8080
}
8181

src/view/Mafs.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,13 @@ function MafsCanvas({
174174
const ySpan = yMax - yMin
175175

176176
const viewTransform = React.useMemo(() => {
177-
const scaleX = round((1 / xSpan) * width, 5)
178-
const scaleY = round((-1 / ySpan) * height, 5)
177+
const scaleX = (1 / xSpan) * width
178+
const scaleY = (-1 / ySpan) * height
179179
return vec.matrixBuilder().scale(scaleX, scaleY).get()
180180
}, [height, width, xSpan, ySpan])
181181

182-
const viewBoxX = round((xMin / (xMax - xMin)) * width, 10)
183-
const viewBoxY = round((yMax / (yMin - yMax)) * height, 10)
182+
const viewBoxX = (xMin / (xMax - xMin)) * width
183+
const viewBoxY = (yMax / (yMin - yMax)) * height
184184

185185
const inverseViewTransform = vec.matrixInvert(viewTransform)
186186

0 commit comments

Comments
 (0)