Skip to content

Commit cb8f470

Browse files
committed
fix: add image even if points not provided
1 parent 64237a1 commit cb8f470

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

js/init.js

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -165,35 +165,40 @@ function displayImage(image, points) {
165165

166166
img.src = imageUrl;
167167
img.className = "uploaded-image";
168+
// Crop image based on points
168169
img.onload = () => {
169170
URL.revokeObjectURL(imageUrl);
170171

171-
const canvas = document.createElement("canvas");
172-
const ctx = canvas.getContext("2d");
173-
174-
const width = points[1].x - points[0].x;
175-
const height = points[2].y - points[1].y;
176-
177-
canvas.width = width;
178-
canvas.height = height;
179-
180-
ctx.drawImage(
181-
img,
182-
points[0].x,
183-
points[0].y,
184-
width,
185-
height, // Source coordinates
186-
0,
187-
0,
188-
width,
189-
height // Destination coordinates
190-
);
191-
192-
const croppedImage = new Image();
193-
croppedImage.src = canvas.toDataURL();
194-
croppedImage.className = "uploaded-image";
195-
196-
scannedImage.append(croppedImage);
172+
if (points) {
173+
const canvas = document.createElement("canvas");
174+
const ctx = canvas.getContext("2d");
175+
176+
const width = points[1].x - points[0].x;
177+
const height = points[2].y - points[1].y;
178+
179+
canvas.width = width;
180+
canvas.height = height;
181+
182+
ctx.drawImage(
183+
img,
184+
points[0].x,
185+
points[0].y,
186+
width,
187+
height, // Source coordinates
188+
0,
189+
0,
190+
width,
191+
height // Destination coordinates
192+
);
193+
194+
const croppedImage = new Image();
195+
croppedImage.src = canvas.toDataURL();
196+
croppedImage.className = "uploaded-image";
197+
198+
scannedImage.append(croppedImage);
199+
} else {
200+
scannedImage.append(img);
201+
}
197202
};
198203
} else if (image.toCanvas) {
199204
scannedImage.append(image.toCanvas());

0 commit comments

Comments
 (0)