Skip to content

fix: update cropped image when crop is set from server side #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* #%L
* Image Crop Add-on
* %%
* Copyright (C) 2024 Flowing Code
* Copyright (C) 2024-2025 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -139,6 +139,7 @@ public String getImageAlt() {
*/
public void setCrop(Crop crop) {
setState("crop", crop);
getElement().executeJs("this._updateCroppedImage(this.crop)");
}

/**
Expand Down
124 changes: 60 additions & 64 deletions src/main/resources/META-INF/resources/frontend/src/image-crop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* #%L
* Image Crop Add-on
* %%
* Copyright (C) 2024 Flowing Code
* Copyright (C) 2024-2025 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,7 +21,7 @@
import { ReactAdapterElement, RenderHooks } from 'Frontend/generated/flow/ReactAdapter';
import { JSXElementConstructor, ReactElement, useRef, useEffect } from "react";
import React from 'react';
import { type Crop, ReactCrop, PixelCrop, makeAspectCrop, centerCrop } from "react-image-crop";
import { type Crop, ReactCrop, PixelCrop, PercentCrop, makeAspectCrop, centerCrop, convertToPixelCrop } from "react-image-crop";

class ImageCropElement extends ReactAdapterElement {

Expand Down Expand Up @@ -70,6 +70,7 @@ class ImageCropElement extends ReactAdapterElement {
height
)
setCrop(newcrop);
this._updateCroppedImage(newcrop);
}
}
};
Expand Down Expand Up @@ -122,69 +123,9 @@ class ImageCropElement extends ReactAdapterElement {
};

const onComplete = (c: PixelCrop) => {
croppedImageEncode(c);
this._updateCroppedImage(c);
};

const croppedImageEncode = (completedCrop: PixelCrop) => {
if (completedCrop) {

// get the image element
const image = imgRef.current;

// create a canvas element to draw the cropped image
const canvas = document.createElement("canvas");

// draw the image on the canvas
if (image) {
const ccrop = completedCrop;
const scaleX = image.naturalWidth / image.width;
const scaleY = image.naturalHeight / image.height;
const ctx = canvas.getContext("2d");
const pixelRatio = window.devicePixelRatio;
canvas.width = ccrop.width * pixelRatio;
canvas.height = ccrop.height * pixelRatio;

if (ctx) {
ctx.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0);
ctx.imageSmoothingQuality = "high";

ctx.save();

if (circularCrop) {
canvas.width = ccrop.width;
canvas.height = ccrop.height;

ctx.beginPath();

ctx.arc(ccrop.width / 2, ccrop.height / 2, ccrop.height / 2, 0, Math.PI * 2, true);
ctx.closePath();
ctx.clip();
}

ctx.drawImage(
image,
ccrop.x * scaleX,
ccrop.y * scaleY,
ccrop.width * scaleX,
ccrop.height * scaleY,
0,
0,
ccrop.width,
ccrop.height
);

ctx.restore();
}

// get the cropped image
let croppedImageDataUri = canvas.toDataURL("image/png", 1.0);

// dispatch the event containing cropped image
this.fireCroppedImageEvent(croppedImageDataUri);
}
}
}


return (
<ReactCrop
crop={crop}
Expand Down Expand Up @@ -219,6 +160,61 @@ class ImageCropElement extends ReactAdapterElement {
})
);
}

public _updateCroppedImage(crop: PixelCrop|PercentCrop) {
const image = this.querySelector("img");
if (crop && image) {

crop = convertToPixelCrop(crop, image.width, image.height);

// create a canvas element to draw the cropped image
const canvas = document.createElement("canvas");

// draw the image on the canvas
const ccrop = crop;
const scaleX = image.naturalWidth / image.width;
const scaleY = image.naturalHeight / image.height;
const ctx = canvas.getContext("2d");
const pixelRatio = window.devicePixelRatio;
canvas.width = ccrop.width * pixelRatio;
canvas.height = ccrop.height * pixelRatio;

if (ctx) {
ctx.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0);
ctx.imageSmoothingQuality = "high";
ctx.save();

if (this.circularCrop) {
canvas.width = ccrop.width;
canvas.height = ccrop.height;
ctx.beginPath();
ctx.arc(ccrop.width / 2, ccrop.height / 2, ccrop.height / 2, 0, Math.PI * 2, true);
ctx.closePath();
ctx.clip();
}

ctx.drawImage(
image,
ccrop.x * scaleX,
ccrop.y * scaleY,
ccrop.width * scaleX,
ccrop.height * scaleY,
0,
0,
ccrop.width,
ccrop.height
);

ctx.restore();

// get the cropped image
let croppedImageDataUri = canvas.toDataURL("image/png", 1.0);

// dispatch the event containing cropped image
this.fireCroppedImageEvent(croppedImageDataUri);
}
}
}
}

customElements.define("image-crop", ImageCropElement);
Loading