base64
Blob object.1. Install & download.
# NPM $ npm install pure-js-cropper
2. Import the PureJsCropper function:
<script type="module"> import PureJsCropper from "./PureJsCropper.js"; </script>
3. Create a container element in your HTML where the cropper will live. You’ll also need a button to trigger the crop action and an <img> tag to display the result.
<div id="cropper"></div> <button id="cropBtn">Crop (base64)</button> <button id="cropBtnBlob">Crop (Blob)</button> <img id="result" />
4. Create a new instance of PureJsCropper and load an image.
// Target the container div
const cropperContainer = document.getElementById("cropper");
// Instantiate the cropper
const cropper = new PureJsCropper(cropperContainer, {
width: "100%", // Make it responsive
height: "400px",
});
// Load the image you want to crop
cropper.loadImage("path/to/your/image.jpg"); 5. The crop() method is what you’ll use to get the final image. By default, it returns a base64 string, which is useful for directly setting the src of an <img> tag.
document.getElementById("cropBtn").addEventListener("click", () => {
const base64String = cropper.crop();
document.getElementById("result").src = base64String;
}); 6. Pass false to the crop() method if you need a Blob object. This will return a Promise that resolves with the Blob.
document.getElementById("cropBtnBlob").addEventListener("click", () => {
cropper.crop(false).then((blob) => {
const objectURL = URL.createObjectURL(blob);
document.getElementById("result").src = objectURL;
// Now you can use the 'blob' object to upload
});
}); 7. Customize the initial appearance and constraints of the crop box by passing an options object during instantiation.
const cropper = new PureJsCropper(cropperContainer, {
width: "300px",
height: "300px",
border: "1px dashed #53535c",
minCropBoxWidth: 30,
minCropBoxHeight: 30
}); The post Vanilla JS Image Cropper with Image Zoom & Pan – PureJsCropper appeared first on CSS Script.
2019’s Ready or Not was a breath of fresh air: a simple, savage game of…
The fact that Slay the Spire 2's Early Access debut plays so similarly to the…
In honor and support of Women’s History Month, state Rep. Joanna McClinton, the first woman…
The Live Nation-Ticketmaster trial is back on. Dozens of states are expected to move forward…
Less slop please. | Image: Spotify Spotify Premium users in New Zealand will be the…
MACHESNEY PARK, Ill. (WTVO) — Students in Harlem High School's welding program are learning about…
This website uses cookies.