0

I am using react-konva and I want to crop my selected image when edit button clicked. Can anyone please guide me how I can achieve this ?

this is the Rect I am using to crop the portion of the image. Here in this code onShapeChange function saves the crop value of the image in
canvas editor.

{(isCropping && 
                <>
                    {React.createElement(`Rect`, {
                        ref: cropRef,
                        key: selectedShape.id,
                        id: selectedShape.id,
                        ...selectedShape.attributes,
                        draggable: false,
                        onTransformEnd: (e) => {
                            const node = cropRef.current;
                            const scaleX = node.scaleX();
                            const scaleY = node.scaleY();
                            node.scaleX(1);
                            node.scaleY(1);
                            const newShape = {
                                ...selectedShape,
                                attributes:
                                {
                                    ...selectedShape.attributes,
                                    crop: {
                     
                    x: node.x() - selectedShape.attributes.x,
                                        y: node.y() - selectedShape.attributes.y,
                                        // width: this.state.rect.attrs.width,
                                        // height: this.state.rect.attrs.height

                                        // x: node.x(),
                                        // y: node.y(),
                                        width: Math.max(5, node.width() * scaleX),
                                        height: Math.max(node.height() * scaleY),
                                    }
                                }
                            }
                            console.log('newShape in cropper', newShape, 'SelectedShape', selectedShape);
                            onShapeChange({
                                id: selectedShape.id,
                                index: selectedReportItem.index,
                                reportIndex: selectedReportItem.reportIndex,
                                newItem: newShape,
                            })
                            setIsCropping(false);
                        }
                    }, null)}
                    <Transformer
                        ref={croptrRef}
                        rotateEnabled={false}
                        flipEnabled={false}
                        boundBoxFunc={(oldBox, newBox) => {
                            // limit resize
                            if (newBox.width < 5 || newBox.height < 5) {
                                return oldBox;
                            }
                            return newBox;
                        }}
                    />
                </>
            }
Amir
  • 92
  • 1
  • 7
  • Crop how? What did you try? What do you want to see when edit is clicked? `Konva.Image` has `crop` properties. Just use them. – lavrton Feb 14 '22 at 16:49
  • @lavrton actually I am building a canvas editor for my report generation where I am adding multiple images. I want to show crop anchors on my image when 'Edit Image' button is clicked for the selected imge. When the crops is completed then save the new (cropped) image to my storage. – Amir Feb 15 '22 at 16:46
  • I am using Transformer to resize the selected shape/image but did not get a way to crop image. – Amir Feb 15 '22 at 16:48
  • I am adding new reactangle on the selected image and after resizing the reactangle, I want that portion of the image, so to achieve this, I am giving crop = {x: rectX - imageX, y: rectY - imageY, heigth: rectHeight, width: rectWidth} to Konva.Image. the crop image shows correct for the first time but when I do crop ore , now the image is not showing correct. The crop property apply on the very original image which I don't want.Please help! – Amir Feb 19 '22 at 09:35
  • Provide a code sample of what you tried. Make online demo. – lavrton Feb 19 '22 at 12:55
  • I have edited my question with code sample which I a using for cropping – Amir Feb 21 '22 at 07:14

0 Answers0