11# Cropt
22
3- A delightful JS image cropper with zero dependencies .
3+ A delightful image cropper optimized for both mobile and desktop use .
44
5- Originally based on [ Foliotek/Croppie] ( https://github.com/Foliotek/Croppie ) ,
6- but rewritten as a modern ES module with a simpler API, better image scaling, and native TypeScript support.
5+ * Includes TypeScript definitions.
6+ * Published as a native ES module.
7+ * Zero dependencies.
8+ * Originally based on [ Croppie] ( https://github.com/Foliotek/Croppie ) , but rewritten in TypeScript with a simpler API and lots of bug fixes and polish.
79
810## Installation
911
10- ```
12+ ``` sh
1113npm install cropt
1214```
1315
1416## Usage
1517
16181 . Include the ` src/cropt.css ` stylesheet on your page.
17- 2 . Add a ` div ` element to your HTML to hold the Cropt instance.
18- 3 . Import Cropt and bind it to an image:
19+ 2 . Add a ` <div> ` element with a unique ID to your HTML to hold the Cropt instance.
20+ 3 . Import Cropt and instantiate it with a reference to the ` <div> ` element and an object for options.
21+ 4 . Bind to an image URL.
22+
23+ ``` html
24+ <div id =" cropper" ></div >
25+ ```
1926
2027``` javascript
2128import { Cropt } from " cropt" ;
29+ import " cropt/src/cropt.css" ;
30+
31+ let croptEl = document .getElementById (' cropper' );
32+ let cropt = new Cropt (croptEl, {
33+ viewport: { width: 250 , height: 250 },
34+ });
35+ cropt .bind (" path/to/image.jpg" );
36+ ```
37+
38+ ### Binding from a file input
39+
40+ To let users pick an image from their device, bind from a file ` <input> ` element's ` change ` event using ` URL.createObjectURL() ` :
41+
42+ ``` html
43+ <input type =" file" id =" fileInput" accept =" image/*" />
44+ ```
45+
46+ ``` javascript
47+ document .getElementById (' fileInput' ).addEventListener (' change' , (e ) => {
48+ const file = e .target .files [0 ];
49+ if (! file) return ;
50+
51+ const url = URL .createObjectURL (file);
52+ cropt .bind (url).then (() => URL .revokeObjectURL (url));
53+ });
54+ ```
55+
56+ The object URL can be revoked after binding because Cropt has already loaded the image data into the DOM at that point.
57+
58+ ### Uploading the cropped image
59+
60+ Use ` toBlob() ` to get the cropped image as a ` Blob ` , then send it to your server with ` fetch ` :
61+
62+ ``` javascript
63+ async function upload () {
64+ const blob = await cropt .toBlob (500 , " image/webp" ); // longest side scaled to 500px
65+
66+ const body = new FormData ();
67+ body .append (' image' , blob, ' crop.webp' );
2268
23- let c = new Cropt ( document . getElementById ( ' demo ' ), options );
24- c . bind ( " path/to/image.jpg " );
69+ await fetch ( ' /upload ' , { method : ' POST ' , body } );
70+ }
2571```
2672
2773### Sizing
@@ -74,7 +120,7 @@ The optional second argument can be:
74120### ` getState(): CroptState `
75121
76122Returns the current crop state as a ` CroptState ` object with fields ` x ` , ` y ` , ` zoom ` , ` width ` , and ` height ` .
77- This can be stored and later passed to ` bind() ` to restore the crop position, zoom level, and viewport size.
123+ This can be stored alongside the original image and later passed to ` bind() ` to restore the crop position, zoom level, and viewport size.
78124
79125``` javascript
80126// Save state when the user is done cropping
@@ -90,7 +136,7 @@ Deconstructs a Cropt instance and removes the elements from the DOM.
90136
91137### ` refresh(): void `
92138
93- Recalculate points for the image. Necessary if the instance was initially bound to a hidden element .
139+ Recalculate points for the image. Necessary if the instance was bound while hidden, or if it has been hidden and re-shown .
94140
95141### ` toCanvas(size: number | null = null): Promise<HTMLCanvasElement> `
96142
@@ -99,18 +145,18 @@ If `size` is specified, the cropped image will be scaled with its longest side s
99145
100146### ` toBlob(size: number | null = null, type = "image/webp", quality = 1): Promise<Blob> `
101147
102- Returns a Promise resolving to a ` Blob ` object for the cropped image.
148+ Returns a ` Promise ` resolving to a ` Blob ` object for the cropped image.
103149If ` size ` is specified, the cropped image will be scaled with its longest side set to this value.
104150The ` type ` and ` quality ` parameters are passed directly to the corresponding
105- [ HTMLCanvasElement.toBlob()] ( https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob ) method parameters.
151+ [ ` HTMLCanvasElement.toBlob() ` ] ( https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob ) method parameters.
106152
107153### ` setOptions(options: CroptOptions): void `
108154
109155Allows options to be dynamically changed on an existing Cropt instance.
110156
111157### ` setZoom(value: number): void `
112158
113- Set the zoom of a Cropt instance. The value must be between 0 and 1, and is restricted to the min/max set by Cropt .
159+ Set the zoom of a Cropt instance. The value must be between 0 and 1, and is clamped to the min/max zoom calculated for the current image .
114160
115161## Visibility and binding
116162
@@ -127,9 +173,7 @@ myModal.addEventListener('shown.bs.modal', () => {
127173});
128174```
129175
130- If you have issues getting the correct result, and your Cropt instance is shown inside a modal,
131- try taking it out of the modal and see if the issue persists.
132- If not, make sure that your bind method is called after the modal finishes opening.
176+ If your Cropt instance is inside a modal, make sure ` bind() ` is called after the modal finishes opening.
133177
134178If a Cropt instance needs to be hidden and then re-shown, call the ` refresh() ` method to recalculate properties for the displayed image.
135179
0 commit comments