Skip to content

Commit 0ec4d8d

Browse files
committed
Optimized brush, and eraser, closes #62
1 parent d715e33 commit 0ec4d8d

File tree

6 files changed

+80
-40
lines changed

6 files changed

+80
-40
lines changed

client/src/components/annotator/Annotation.vue

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,21 @@ export default {
203203
}
204204
return this.compoundPath;
205205
},
206+
createUndoAction(actionName) {
207+
if (this.compoundPath == null) this.createCompoundPath();
208+
209+
let copy = this.compoundPath.clone();
210+
copy.visible = false;
211+
this.pervious.push(copy);
212+
213+
let action = new UndoAction({
214+
name: "Annotaiton " + this.annotation.id,
215+
action: actionName,
216+
func: this.undoCompound,
217+
args: {}
218+
});
219+
this.addUndo(action);
220+
},
206221
simplifyPath() {
207222
let flatten = 1;
208223
let simplify = this.simplify;
@@ -235,43 +250,39 @@ export default {
235250
this.compoundPath.remove();
236251
this.compoundPath = this.pervious.pop();
237252
},
238-
unite(compound) {
253+
/**
254+
* Unites current annotation path with anyother path.
255+
* @param {paper.CompoundPath} compound compound to unite current annotation path with
256+
* @param {boolean} simplify simplify compound after unite
257+
* @param {undoable} undoable add an undo action
258+
*/
259+
unite(compound, simplify = true, undoable = true) {
239260
if (this.compoundPath == null) this.createCompoundPath();
240261
241262
let newCompound = this.compoundPath.unite(compound);
263+
if (undoable) this.createUndoAction("Unite");
242264
243-
this.compoundPath.visible = false;
244-
this.pervious.push(this.compoundPath);
245-
246-
let action = new UndoAction({
247-
name: "Annotaiton " + this.annotation.id,
248-
action: "United",
249-
func: this.undoCompound,
250-
args: {}
251-
});
252-
this.addUndo(action);
253-
265+
this.compoundPath.remove();
254266
this.compoundPath = newCompound;
255-
this.simplifyPath();
267+
268+
if (simplify) this.simplifyPath();
256269
},
257-
subtract(compound) {
270+
/**
271+
* Subtract current annotation path with anyother path.
272+
* @param {paper.CompoundPath} compound compound to subtract current annotation path with
273+
* @param {boolean} simplify simplify compound after subtraction
274+
* @param {undoable} undoable add an undo action
275+
*/
276+
subtract(compound, simplify = true, undoable = true) {
258277
if (this.compoundPath == null) this.createCompoundPath();
259278
260279
let newCompound = this.compoundPath.subtract(compound);
280+
if (undoable) this.createUndoAction("Subtract");
261281
262-
this.compoundPath.visible = false;
263-
this.pervious.push(this.compoundPath);
264-
265-
let action = new UndoAction({
266-
name: "Annotaiton " + this.annotation.id,
267-
action: "Subtract",
268-
func: this.undoCompound,
269-
args: {}
270-
});
271-
this.addUndo(action);
272-
282+
this.compoundPath.remove();
273283
this.compoundPath = newCompound;
274-
this.simplifyPath();
284+
285+
if (simplify) this.simplifyPath();
275286
},
276287
setColor() {
277288
if (this.compoundPath == null) return;

client/src/components/annotator/tools/BrushTool.vue

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,23 @@ export default {
5454
this.moveBrush(event.point);
5555
},
5656
onMouseDown() {
57+
this.$parent.currentAnnotation.createUndoAction("Unite");
5758
this.draw();
5859
},
60+
onMouseUp() {
61+
this.$parent.currentAnnotation.simplifyPath();
62+
},
5963
onMouseDrag(event) {
6064
this.moveBrush(event.point);
6165
this.draw();
6266
},
67+
/**
68+
* Unites current brush with selected annotation
69+
*/
6370
draw() {
64-
this.$parent.uniteCurrentAnnotation(this.brush.path);
71+
// Undo action, will be handled on mouse down
72+
// Simplify, will be handled on mouse up
73+
this.$parent.currentAnnotation.unite(this.brush.path, false, false);
6574
},
6675
decreaseRadius() {
6776
if (!this.isActive) return;

client/src/components/annotator/tools/EraserTool.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,16 @@ export default {
5959
this.erase();
6060
},
6161
onMouseDown() {
62+
this.$parent.currentAnnotation.createUndoAction("Subtract");
6263
this.erase();
6364
},
65+
onMouseUp() {
66+
this.$parent.currentAnnotation.simplifyPath();
67+
},
6468
erase() {
65-
this.$parent.subtractCurrentAnnotation(this.eraser.brush);
69+
// Undo action, will be handled on mouse down
70+
// Simplify, will be handled on mouse up
71+
this.$parent.currentAnnotation.subtract(this.eraser.brush, false, false);
6672
},
6773
decreaseRadius() {
6874
if (!this.isActive) return;

client/src/components/annotator/tools/MagicWandTool.vue

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,24 @@ export default {
2525
};
2626
},
2727
methods: {
28+
/**
29+
* Exports settings
30+
* @returns {object} current settings
31+
*/
2832
export() {
2933
return {
3034
threshold: this.wand.threshold,
3135
blur: this.wand.blur
3236
};
3337
},
38+
/**
39+
* Creates MagicWand selection
40+
* @param {number} x x position
41+
* @param {number} y y position
42+
* @param {number} thr threashold
43+
* @param {number} rad radius blur
44+
* @returns {paper.CompoundPath} create selection
45+
*/
3446
flood(x, y, thr, rad) {
3547
let image = {
3648
data: this.imageInfo.data.data,
@@ -61,10 +73,14 @@ export default {
6173
}
6274
return null;
6375
},
76+
onMouseUp() {
77+
// this.$parent.currentAnnotation.simplifyPath();
78+
},
6479
onMouseDown(event) {
6580
let x = Math.round(this.imageInfo.width / 2 + event.point.x);
6681
let y = Math.round(this.imageInfo.height / 2 + event.point.y);
6782
83+
// Check if valid coordinates
6884
if (
6985
x > this.imageInfo.width ||
7086
y > this.imageInfo.height ||
@@ -74,17 +90,16 @@ export default {
7490
return;
7591
}
7692
93+
// Create shape and apply to current annotation
7794
let path = this.flood(x, y, this.wand.threshold, this.wand.blur);
78-
7995
if (event.modifiers.shift) {
80-
this.$parent.subtractCurrentAnnotation(path);
96+
this.$parent.currentAnnotation.unite(path);
8197
} else {
82-
this.$parent.uniteCurrentAnnotation(path);
98+
this.$parent.currentAnnotation.unite(path);
8399
}
84100
85101
if (path != null) path.remove();
86102
},
87-
88103
onMouseDrag(event) {
89104
this.onMouseDown(event);
90105
}
@@ -95,6 +110,7 @@ export default {
95110
}
96111
},
97112
watch: {
113+
// Generate data whenever the image changes
98114
raster(raster) {
99115
if (raster == null) return;
100116
if (Object.keys(raster).length === 0) return;

client/src/components/annotator/tools/UndoButton.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ export default {
3535
},
3636
watch: {
3737
undoList() {
38-
this.iconColor =
39-
this.undoList.length === 0 ? this.color.disabled : this.color.enabled;
38+
this.disabled = this.undoList.length === 0;
39+
this.iconColor = this.disabled ? this.color.disabled : this.color.enabled;
4040
}
4141
},
4242
created() {

client/src/views/Annotator.vue

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -415,15 +415,13 @@ export default {
415415
return this.$refs.category[index];
416416
},
417417
// Current Annotation Operations
418-
uniteCurrentAnnotation(compound) {
418+
uniteCurrentAnnotation(compound, simplify = true, undoable = true) {
419419
if (this.currentAnnotation == null) return;
420-
421-
this.currentAnnotation.unite(compound);
420+
this.currentAnnotation.unite(compound, simplify, undoable);
422421
},
423-
subtractCurrentAnnotation(compound) {
422+
subtractCurrentAnnotation(compound, simplify = true, undoable = true) {
424423
if (this.currentCategory == null) return;
425-
426-
this.currentAnnotation.subtract(compound);
424+
this.currentAnnotation.subtract(compound, simplify, undoable);
427425
},
428426
429427
setCursor(newCursor) {

0 commit comments

Comments
 (0)