Skip to content

Commit 8a54447

Browse files
authored
Merge pull request #65 from MasterCarl/master
Annotation creation using click instead of drag
2 parents 6742ad7 + 16016b2 commit 8a54447

File tree

4 files changed

+48
-15
lines changed

4 files changed

+48
-15
lines changed

README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,20 @@ ReactDOM.render(<App />, rootElement);
5757

5858
## ReactPictureAnnotation Props
5959

60-
| Name | Type | Comment | required |
61-
| --------------- | ----------------------------------------------------------------------------------------------- | ------------------------------------------ | -------- |
62-
| onChange | `(annotationData: IAnnotation[]) => void` | Called every time the shape changes. ||
63-
| onSelected | `(id: string or null) => void` | Called each time the selection is changed. ||
64-
| width | `number` | Width of the canvas. ||
65-
| height | `number` | Height of the canvas. ||
66-
| image | `string` | Image to be annotated. ||
67-
| inputElement | `(value: string,onChange: (value: string) => void,onDelete: () => void) => React.ReactElement;` | Customizable input control. | X |
68-
| annotationData | `Array<IAnnotation>` | Control the marked areas on the page. | X |
69-
| annotationStyle | `IShapeStyle` | Control the mark style | X |
70-
| selectedId | `string or null` | Selected markId | X |
71-
| scrollSpeed | `number` | Speed of wheel zoom, default 0.0005 | X |
72-
| marginWithInput | `number` | Margin between input and mark, default 1 | X |
60+
| Name | Type | Comment | required |
61+
| --------------------- | ----------------------------------------------------------------------------------------------- | ------------------------------------------ | -------- |
62+
| onChange | `(annotationData: IAnnotation[]) => void` | Called every time the shape changes. ||
63+
| onSelected | `(id: string or null) => void` | Called each time the selection is changed. ||
64+
| width | `number` | Width of the canvas. ||
65+
| height | `number` | Height of the canvas. ||
66+
| image | `string` | Image to be annotated. ||
67+
| inputElement | `(value: string,onChange: (value: string) => void,onDelete: () => void) => React.ReactElement;` | Customizable input control. | X |
68+
| annotationData | `Array<IAnnotation>` | Control the marked areas on the page. | X |
69+
| annotationStyle | `IShapeStyle` | Control the mark style | X |
70+
| selectedId | `string or null` | Selected markId | X |
71+
| scrollSpeed | `number` | Speed of wheel zoom, default 0.0005 | X |
72+
| marginWithInput | `number` | Margin between input and mark, default 1 | X |
73+
| defaultAnnotationSize | `number[]` | Size for annotations created by clicking. | X |
7374

7475
## IShapeStyle
7576

src/ReactPictureAnnotation.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ interface IReactPictureAnnotationProps {
2424
height: number;
2525
image: string;
2626
annotationStyle: IShapeStyle;
27+
defaultAnnotationSize?: number[];
2728
inputElement: (
2829
value: string,
2930
onChange: (value: string) => void,
@@ -85,6 +86,11 @@ export default class ReactPictureAnnotation extends React.Component<
8586
get annotationStyle() {
8687
return this.props.annotationStyle;
8788
}
89+
90+
get defaultAnnotationSize() {
91+
return this.props.defaultAnnotationSize;
92+
}
93+
8894
public shapes: IShape[] = [];
8995
public scaleState = defaultState;
9096
public currentTransformer: ITransformer;

src/annotation/CreatingAnnotationState.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ReactPictureAnnotation } from "index";
2+
import { IShape } from "Shape";
23
import { IAnnotationState } from "./AnnotationState";
34
import { DefaultAnnotationState } from "./DefaultAnnotationState";
45

@@ -32,11 +33,35 @@ export default class CreatingAnnotationState implements IAnnotationState {
3233
) {
3334
shapes.push(data);
3435
} else {
35-
this.context.selectedId = null;
36-
onShapeChange();
36+
if (data && this.applyDefaultAnnotationSize(data)) {
37+
shapes.push(data);
38+
onShapeChange();
39+
} else {
40+
this.context.selectedId = null;
41+
onShapeChange();
42+
}
3743
}
3844
setAnnotationState(new DefaultAnnotationState(this.context));
3945
};
4046

47+
private applyDefaultAnnotationSize = (shape: IShape) => {
48+
if (this.context.selectedId) {
49+
// Don't capture clicks meant to de-select another annotation.
50+
return false;
51+
}
52+
if (
53+
!this.context.defaultAnnotationSize ||
54+
this.context.defaultAnnotationSize.length !== 2
55+
) {
56+
return false;
57+
}
58+
const [width, height] = this.context.defaultAnnotationSize;
59+
shape.adjustMark({
60+
width,
61+
height,
62+
});
63+
return true;
64+
};
65+
4166
public onMouseLeave = () => this.onMouseUp();
4267
}

stories/index.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ storiesOf("Hello World", module)
6666
shapeStrokeStyle: "#2193ff",
6767
transformerBackground: "black",
6868
}}
69+
defaultAnnotationSize={[120, 90]}
6970
image="https://bequank.oss-cn-beijing.aliyuncs.com/landpage/large/60682895_p0_master1200.jpg"
7071
inputElement={(value, onChange, onDelete) => (
7172
<DefaultInputSection

0 commit comments

Comments
 (0)