-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
282629a
commit 51b2785
Showing
6 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
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,46 @@ | ||
--- | ||
id: fitbox | ||
title: FitBox | ||
sidebar_label: FitBox | ||
slug: /shapes/fibox | ||
--- | ||
|
||
The `FitBox` components allows you to automatically scale drawings so they fit into a destination rectangle. | ||
|
||
| Name | Type | Description | | ||
|:-----|:---------|:---------------------------------------------------| | ||
| src | `SKRect` | Bounding rectangle of the drawing before scaling | | ||
| dst | `SKRect` | Bounding rectangle of the drawing after scale | | ||
| fit? | `Fit` | Method to make the image fit into the rectangle. Value can be `contain`, `fill`, `cover` `fitHeight`, `fitWidth`, `scaleDown`, `none` (default is `contain`) | | ||
|
||
## Example | ||
|
||
Consider the following SVG export. | ||
Its bounding source rectangle is `0, 0, 664, 308`: | ||
|
||
```xml | ||
<svg width="664" height="308" viewBox="0 0 664 308" fill="none" xmlns="http://www.w3.org/2000/svg"> | ||
<path d="M 170.1 215.5 C 165 222.3..." fill="black"/> | ||
</svg> | ||
``` | ||
|
||
We would like to automatically scale that path to our canvas of size `256 x 256`: | ||
|
||
```tsx twoslash | ||
import {Canvas, FitBox, Path, rect} from "@shopify/react-native-skia"; | ||
|
||
const Hello = () => { | ||
return ( | ||
<Canvas style={{ width: 256, height: 256 }}> | ||
<FitBox src={rect(0, 0, 664, 308)} dst={rect(0, 0, 256, 256)}> | ||
<Path path="M 170.1 215.5 C 165 222.3..." /> | ||
</FitBox> | ||
</Canvas> | ||
); | ||
} | ||
``` | ||
|
||
### Result | ||
|
||
![Hello Skia](assets/fitbox/hello.png) | ||
|
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 |
---|---|---|
|
@@ -95,6 +95,7 @@ const sidebars = { | |
"shapes/ellipses", | ||
"shapes/vertices", | ||
"shapes/patch", | ||
"shapes/fitbox", | ||
], | ||
}, | ||
{ | ||
|
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,26 @@ | ||
import type { ReactNode } from "react"; | ||
import React, { useMemo } from "react"; | ||
|
||
import type { Fit } from "../image"; | ||
import { rect2rect, fitRects } from "../image"; | ||
import type { SkRect } from "../../../skia"; | ||
import { Group } from "../Group"; | ||
|
||
interface FitProps { | ||
fit: Fit; | ||
src: SkRect; | ||
dst: SkRect; | ||
children: ReactNode | ReactNode[]; | ||
} | ||
|
||
export const FitBox = ({ fit, src, dst, children }: FitProps) => { | ||
const transform = useMemo(() => { | ||
const rects = fitRects(fit, src, dst); | ||
return rect2rect(rects.src, rects.dst); | ||
}, [dst, fit, src]); | ||
return <Group transform={transform}>{children}</Group>; | ||
}; | ||
|
||
FitBox.defaultProps = { | ||
fit: "contain", | ||
}; |
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