Comments (4)
I've updated the example here: https://codesandbox.io/s/react-image-crop-demo-with-react-hooks-y831o?file=/src/App.tsx
async function onDownloadCropClick() {
const image = imgRef.current
const previewCanvas = previewCanvasRef.current
if (!image || !previewCanvas || !completedCrop) {
throw new Error('Crop canvas does not exist')
}
// This will size relative to the uploaded image
// size. If you want to size according to what they
// are looking at on screen, remove scaleX + scaleY
const scaleX = image.naturalWidth / image.width
const scaleY = image.naturalHeight / image.height
const offscreen = new OffscreenCanvas(
completedCrop.width * scaleX,
completedCrop.height * scaleY,
)
const ctx = offscreen.getContext('2d')
if (!ctx) {
throw new Error('No 2d context')
}
ctx.drawImage(
previewCanvas,
0,
0,
previewCanvas.width,
previewCanvas.height,
0,
0,
offscreen.width,
offscreen.height,
)
// You might want { type: "image/jpeg", quality: <0 to 1> } to
// reduce image size
const blob = await offscreen.convertToBlob({
type: 'image/png',
})
if (blobUrlRef.current) {
URL.revokeObjectURL(blobUrlRef.current)
}
blobUrlRef.current = URL.createObjectURL(blob)
hiddenAnchorRef.current!.href = blobUrlRef.current
hiddenAnchorRef.current!.click()
}
Note the comment about scaleX
+ scaleY
, remove those to get a crop exactly as the user sees it. Otherwise it will be based on the uploaded image which could be a lot bigger than that.
from react-image-crop.
Sorry @dominictobias I forgot to reply, this worked a treat thank-you.
from react-image-crop.
The issue is that for correct quality on devices with a larger pixel ratio than 1 (window.devicePixelRatio
) like retina screens, you have to have a larger canvas than is actually rendered.
I was lazy and didn't resize it down to the correct value on click of the Download button.
What I should do is if (window.devicePixelRatio !== 1)
create an Offscreen Canvas of the actual crop size and copy the preview canvas to it (ctx.drawImage
can resize as its copying).
But yes your way is the simple solution of always rendering it at the actual size which works too (at the expense of some visual quality on retina devices) ;)
from react-image-crop.
I am keen to improve quality if possible... If I were to use my method and the offscreen canvas, would that give me better quality output, or is it not as simple as that?
from react-image-crop.
Related Issues (20)
- Memory leak risk due to event listeners not removed on componentWillUnmount HOT 1
- mask does not fully cover the image when the image to a smaller aspect ratio HOT 2
- Multiple crop selection on an image at the same time HOT 1
- Can the cropper be used for outpainting?
- React Crop as Dialog Box
- The previous image layout still be kept after switch image HOT 1
- Feature request: Add the option to rotate the crop selection
- Proportional Stretching of Crop Box
- iPhone 15's 24 MP (res 4284 X 5712) image resolution is making the image black after cropping. HOT 1
- Library not supporting image cropping in iOS WebView HOT 6
- Add whitespace around image being cropped?
- Cropped area not accounting for zoom of containing elements properly HOT 6
- Deprecation Warnings with SASS 1.77.7 HOT 2
- Maximum Scale Out to produce only complete image without any transparent parts added to result HOT 4
- reload saved cropped image, onComplete returning incorrect crop, but percentCrop is correct.
- Height styling not working with percentages
- While trying to rotate the image, the image itself is not fitting inside the container, full image is not visible after rotating to 90 or 270 degree HOT 4
- expand image to fit container HOT 1
- Problems when importing from react image crop using moduleResolution: "nodenext" in tsconfig.json HOT 2
- when load a cropped image, there is a strange crop range projection on the image HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-image-crop.