Skip to content

Commit 2c845fb

Browse files
authored
Merge pull request #29 from Pascal-Institute/develop
version up
2 parents 4c84cd5 + 852b11f commit 2c845fb

File tree

13 files changed

+190
-190
lines changed

13 files changed

+190
-190
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,20 @@ npm run build
4747

4848
## 3. Application Specification
4949

50-
### Availiable file extension
50+
### 3-1. Availiable file extension
5151

5252
```bash
5353
png jpg jpeg webp gif bmp ico tiff tif
5454
```
5555

56-
#### extension converting
56+
#### 3-2. extension converting
5757

58-
### Image Processing
58+
### 3-3. Image Processing
5959

6060
#### resize
6161

62+
#### crop
63+
6264
#### filter
6365

6466
#### rotate

css/styles.css

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,6 @@ select#extensionComboBox {
130130
background-color: transparent;
131131
}
132132

133-
#cropBtn {
134-
position: fixed;
135-
top: 122px;
136-
right: 0px;
137-
}
138133

139134
#undoBtn {
140135
position: fixed;

imgkit/index.js

Lines changed: 82 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ class Parameter {
100100

101101
class ImageLayer {
102102
static drawFlag = false;
103-
static cropFlag = false;
104103
static dragFlag = false;
105104

106105
constructor() {
@@ -164,10 +163,35 @@ class ImageLayer {
164163

165164
document.addEventListener("keydown", (event) => {
166165
if (
167-
(event.ctrlKey && event.key === "d") ||
168-
(event.key === "Delete" && document.activeElement === this.imgPanel)
166+
(event.ctrlKey && event.key === "d") ||
167+
(event.key === "Delete" && document.activeElement === this.imgPanel)
169168
) {
170-
deleteImagePanel();
169+
deleteImagePanel();
170+
} else if (
171+
document.activeElement === this.imgPanel &&
172+
(event.key === "ArrowLeft" || event.key === "ArrowRight")
173+
) {
174+
if (event.ctrlKey && event.key === "ArrowLeft") {
175+
Parameter.num = 0;
176+
imageLayerQueue[Parameter.num].imgPanel.focus();
177+
imageLayerQueue[Parameter.num].updateFocus();
178+
imageLayerQueue[Parameter.num].updateSio();
179+
} else if (event.ctrlKey && event.key === "ArrowRight") {
180+
Parameter.num = imageLayerQueue.length - 1;
181+
imageLayerQueue[Parameter.num].imgPanel.focus();
182+
imageLayerQueue[Parameter.num].updateFocus();
183+
imageLayerQueue[Parameter.num].updateSio();
184+
} else if (event.key === "ArrowLeft" && Parameter.num > 0) {
185+
Parameter.num--;
186+
imageLayerQueue[Parameter.num].imgPanel.focus();
187+
imageLayerQueue[Parameter.num].updateFocus();
188+
imageLayerQueue[Parameter.num].updateSio();
189+
} else if (event.key === "ArrowRight" && Parameter.num < imageLayerQueue.length - 1) {
190+
Parameter.num++;
191+
imageLayerQueue[Parameter.num].imgPanel.focus();
192+
imageLayerQueue[Parameter.num].updateFocus();
193+
imageLayerQueue[Parameter.num].updateSio();
194+
}
171195
}
172196
});
173197

@@ -337,7 +361,7 @@ class ImageLayer {
337361
event.clientY - this.canvas.getBoundingClientRect().top
338362
);
339363
this.canvas.addEventListener("mousemove", (evt) => {
340-
if (ImageLayer.dragFlag && ImageLayer.drawFlag) {
364+
if (ImageLayer.dragFlag) {
341365
this.ctx.lineTo(
342366
evt.x - this.canvas.getBoundingClientRect().left,
343367
evt.y - this.canvas.getBoundingClientRect().top
@@ -348,40 +372,66 @@ class ImageLayer {
348372
this.ctx.closePath();
349373
}
350374

351-
if (ImageLayer.cropFlag) {
375+
if (document.body.style.cursor === "crosshair") {
352376
var ctxs = this.canvas.getContext("2d");
353-
this.realPosX = event.clientX;
354-
this.realPosY = event.clientY;
355-
this.initialX =
356-
event.clientX - this.canvas.getBoundingClientRect().left;
357-
this.initialY = event.clientY - this.canvas.getBoundingClientRect().top;
377+
const rect = this.canvas.getBoundingClientRect();
378+
const startX = event.clientX - rect.left;
379+
const startY = event.clientY - rect.top;
380+
this.initialX = startX;
381+
this.initialY = startY;
358382
ctxs.setLineDash([2]);
359383

360-
this.canvas.addEventListener("mousemove", (evt) => {
361-
if (ImageLayer.dragFlag && ImageLayer.cropFlag) {
362-
ctxs.clearRect(
363-
0,
364-
0,
365-
this.canvas.clientWidth,
366-
this.canvas.clientHeight
367-
);
368-
this.cropWidth = evt.clientX - event.clientX;
369-
this.cropHeight = evt.clientY - event.clientY;
370-
ctxs.drawImage(this.image, 0, 0);
371-
ctxs.strokeRect(
372-
this.initialX,
373-
this.initialY,
374-
this.cropWidth,
375-
this.cropHeight
376-
);
377-
}
378-
});
384+
const mouseMoveHandler = (evt) => {
385+
ctxs.clearRect(0, 0, this.canvas.clientWidth, this.canvas.clientHeight);
386+
ctxs.drawImage(this.image, 0, 0);
387+
const currX = evt.clientX - rect.left;
388+
const currY = evt.clientY - rect.top;
389+
// Calculate top-left and width/height regardless of drag direction
390+
const x = Math.min(startX, currX);
391+
const y = Math.min(startY, currY);
392+
const w = Math.abs(currX - startX);
393+
const h = Math.abs(currY - startY);
394+
this.initialX = x;
395+
this.initialY = y;
396+
this.cropWidth = w;
397+
this.cropHeight = h;
398+
ctxs.strokeRect(x, y, w, h);
399+
};
400+
const mouseUpHandler = (evt) => {
401+
this.canvas.removeEventListener("mousemove", mouseMoveHandler);
402+
document.removeEventListener("mouseup", mouseUpHandler);
403+
ImageLayer.dragFlag = false;
404+
};
405+
this.canvas.addEventListener("mousemove", mouseMoveHandler);
406+
document.addEventListener("mouseup", mouseUpHandler);
379407
}
380408
});
381409

382410
this.canvas.addEventListener("mouseup", (event) => {
383-
if (ImageLayer.drawFlag) {
384-
//paste
411+
ImageLayer.dragFlag = false;
412+
if (document.body.style.cursor === "crosshair") {
413+
// cropWidth/cropHeight는 항상 양수, initialX/initialY는 항상 좌상단
414+
const cropX = Math.round(this.initialX);
415+
const cropY = Math.round(this.initialY);
416+
const cropW = Math.round(this.cropWidth);
417+
const cropH = Math.round(this.cropHeight);
418+
419+
if (
420+
cropW > 0 &&
421+
cropH > 0 &&
422+
this.buffer &&
423+
this.information &&
424+
(cropX + cropW) <= this.information.width &&
425+
(cropY + cropH) <= this.information.height
426+
) {
427+
sharp(this.buffer)
428+
.extract({ left: cropX, top: cropY, width: cropW, height: cropH })
429+
.toBuffer((err, buf, info) => {
430+
if (!err && buf && info) {
431+
this.updatePreviewImg(buf, info);
432+
}
433+
});
434+
}
385435
}
386436
});
387437

@@ -793,7 +843,6 @@ module.exports = {
793843
ImageLayer: ImageLayer,
794844
Parameter: Parameter,
795845
drawFlag: ImageLayer.drawFlag,
796-
cropFlag: ImageLayer.cropFlag,
797846
dragFlag: ImageLayer.dragFlag,
798847
imageLayerQueue: imageLayerQueue,
799848
};

index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
<div id="toolBox1">
2020
<button title="resize" id="resizeBtn">
2121
<img class="icon" src="assets/resize.ico" />
22+
</button>
23+
<button title="crop" id="cropBtn">
24+
<img class="icon" src="assets/crop.ico" />
25+
</button>
2226
<button title="filter" id="filterBtn">
2327
<img class="icon" src="assets/filter.ico" />
2428
</button>

main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ app.whenReady().then(() => {
7878
// Open the DevTools.(only develop)
7979
// mainWindow.webContents.openDevTools();
8080

81-
["resizeImgREQ", "filterImgREQ", "rotateImgREQ", "paintImgREQ"].forEach(
81+
["resizeImgREQ", "cropImgREQ", "filterImgREQ", "rotateImgREQ", "paintImgREQ"].forEach(
8282
(item, index, arr) => {
8383
ipcMain.on(item, (event) => {
8484
mainWindow
@@ -289,7 +289,7 @@ app.whenReady().then(() => {
289289
dialog.showMessageBox({
290290
title: "About",
291291
buttons: ["Ok"],
292-
message: `Author : Pascal Institute\nVersion : v${pkg.version}\nLicense : MIT License\n`,
292+
message: `Author : ${pkg.author}\nVersion : v${pkg.version}\nLicense : ${pkg.license}\n`,
293293
});
294294
},
295295
},

0 commit comments

Comments
 (0)