-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add crop image utility 💞 (#84)
- Loading branch information
1 parent
92bd8e6
commit 5809c27
Showing
11 changed files
with
134 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@zoom-image/core": minor | ||
--- | ||
|
||
Add crop image utility 💞 |
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,35 @@ | ||
<script setup> | ||
import BundleSize from '../components/BundleSize.vue' | ||
</script> | ||
|
||
<BundleSize func="cropImage" /> | ||
|
||
Crop the current image based on the zoom level | ||
|
||
### Basic Usage | ||
|
||
```ts | ||
const croppedImage = cropImage({ | ||
currentZoom: number | ||
image: HTMLImageElement | ||
positionX: number | ||
positionY: number | ||
}) | ||
``` | ||
|
||
### Type Declaration | ||
|
||
```ts | ||
type CropImageArg = { | ||
// Zoom image element | ||
image: HTMLImageElement | ||
// Current zoom positionX returned from createZoomImageWheel | ||
positionX: number | ||
// Current zoom positionY returned from createZoomImageWheel | ||
positionY: number | ||
// Current zoom level from createZoomImageWheel | ||
currentZoom: number | ||
} | ||
|
||
function cropImage = (arg: CropImageArg) => string | ||
``` |
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 not shown.
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,33 @@ | ||
type CropImageArg = { | ||
currentZoom: number | ||
image: HTMLImageElement | ||
positionX: number | ||
positionY: number | ||
} | ||
|
||
export const cropImage = ({ image, positionX, positionY, currentZoom }: CropImageArg) => { | ||
const canvas = document.createElement("canvas") | ||
const scale = image.naturalWidth / (image.clientWidth * currentZoom) | ||
const croppedImageWidth = image.clientWidth * scale | ||
const croppedImageHeight = image.clientHeight * scale | ||
canvas.width = croppedImageWidth | ||
canvas.height = croppedImageHeight | ||
const canvasContext = canvas.getContext("2d") as CanvasRenderingContext2D | ||
|
||
const sx = Math.max(0, Math.abs(positionX) * scale) | ||
const sy = Math.max(0, Math.abs(positionY) * scale) | ||
|
||
canvasContext.drawImage( | ||
image, | ||
sx, | ||
sy, | ||
croppedImageWidth, | ||
croppedImageHeight, | ||
0, | ||
0, | ||
croppedImageWidth, | ||
croppedImageHeight, | ||
) | ||
|
||
return canvas.toDataURL() | ||
} |
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