简体   繁体   中英

How to render image on canvas in reactjs

I have added crop image function in my app using react-image-crop but the problem is when i try to crop the image it does not render the right image position on preview I have no idea why. I don't have any experience using canvas before.

This is how My image looks like.

图片上传

My code to render image on canvas looks something like this.

export function image64toCanvasRef (canvasRef, image64, pixelCrop) {
    console.log('imagePixelCrop', pixelCrop);
    let canvas = canvasRef // document.createElement('canvas');
    canvas.width = pixelCrop.width
    canvas.height = pixelCrop.height
    const ctx = canvas.getContext('2d')
    const image = new Image()
    image.src = image64
    image.onload = function () {
        ctx.drawImage(
            image,
            pixelCrop.x,
            pixelCrop.y,
            pixelCrop.width,
            pixelCrop.height,
            0,
            0,
            pixelCrop.width,
            pixelCrop.height
        )
    }
}

Here this is im getting in pixel crop.

aspect: undefined
height: 92.5
unit: "px"
width: 164.5
x: 70
y: 71

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM