@markerjs/markerjs3 3.0.0-alpha.4 → 3.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  # marker.js 3 — Add image annotation to your web apps
2
2
 
3
- > **Note**: marker.js 3 is in the early pre-release stage of its lifecycle and will have many additions and breaking changes before the official 3.0 release.
3
+ > **Note**: marker.js 3 is in the pre-release stage of its lifecycle and will have many additions and, potentially, breaking changes before the official 3.0 release.
4
4
  >
5
5
  > If you need a stable image annotation library, consider using [marker.js 2](https://markerjs.com) for now.
6
6
 
7
- marker.js 3 is a JavaScript browser library to enable image annotation in your web applications. Add marker.js 3 to your web app and instantly enable users to annotate and mark up images. You can save, share or otherwise process the results.
7
+ marker.js 3 is a JavaScript browser library to enable image annotation in your web applications. Add marker.js 3 to your web app and enable users to annotate and mark up images. You can save, share or otherwise process the results.
8
8
 
9
9
  ## Installation
10
10
 
@@ -12,7 +12,7 @@ marker.js 3 is a JavaScript browser library to enable image annotation in your w
12
12
  npm install @markerjs/markerjs3
13
13
  ```
14
14
 
15
- or
15
+ or
16
16
 
17
17
  ```
18
18
  yarn add @markerjs/markerjs3
@@ -20,17 +20,99 @@ yarn add @markerjs/markerjs3
20
20
 
21
21
  ## Usage
22
22
 
23
- *coming soon...*
23
+ Unlike the previous version, marker.js 3 is a "headless" (sort of) library. In this case "headless" means that it doesn't come with any toolbars, property editors, and placement preconceptions. This way the library focuses on the core features you need, and you can make it feel totally native in the application you are building without resorting to any tricks or hacks.
24
+
25
+ With that out of the way, here are the simplest usage scenarios...
26
+
27
+ ### MarkerArea (The Editor)
28
+
29
+ Import `MarkerArea` from `@markerjs/markerjs3`:
30
+
31
+ ```js
32
+ import { MarkerArea } from '@markerjs/markerjs3';
33
+ ```
34
+
35
+ In the code below we assume that you have an `HTMLImageElement` as `targetImage`. It can be a reference to an image you already have on the page or you can simply create it with something like this:
36
+
37
+ ```js
38
+ const targetImg = document.createElement('img');
39
+ targetImg.src = './sample.jpg';
40
+ ```
41
+
42
+ Now you just need to create an instance of `MarkerArea`, set its `targetImage` property and add it to the page:
43
+
44
+ ```js
45
+ const markerArea = new MarkerArea();
46
+ markerArea.targetImage = targetImg;
47
+ editorContainerDiv.appendChild(markerArea);
48
+ ```
49
+
50
+ To initiate creation of a marker you just call `createMarker()` and pass it the name (or type) of the marker you want to create. So, if you have a button with id `addFrameButton` you can make it create a new `FrameMarker` with something like this:
51
+
52
+ ```js
53
+ document.querySelector("#addButton")!.addEventListener("click", () => {
54
+ markerArea.createMarker("FrameMarker");
55
+ });
56
+ ```
57
+
58
+ And whenever you want to save state (current annotation) you just call `getState()`:
59
+
60
+ ```js
61
+ document.querySelector("#saveStateButton")!.addEventListener("click", () => {
62
+ const state = markerArea.getState();
63
+ console.log(state);
64
+ });
65
+ ```
66
+
67
+ ### Rendering static images
68
+
69
+ To render the annotation as a static image you use `Renderer`.
70
+
71
+ ```js
72
+ import { MarkerArea, Renderer } from '@markerjs/markerjs3';
73
+ ```
74
+
75
+ Just create an instance of it and pass the annotation state to the `rasterize()` method:
76
+
77
+ ```js
78
+ const renderer = new Renderer();
79
+ renderer.targetImage = targetImg;
80
+ const dataUrl = await renderer.rasterize(markerArea.getState());
81
+
82
+ const img = document.createElement('img');
83
+ img.src = dataUrl;
84
+
85
+ someDiv.appendChild(img);
86
+ ```
87
+
88
+ ### MarkerView (The Viewer)
89
+
90
+ To show dynamic annotation overlays on top of the original image you use `MarkerView`.
91
+
92
+ ```js
93
+ import { MarkerView } from '@markerjs/markerjs3';
94
+
95
+ const markerView = new MarkerView();
96
+ markerView.targetImage = targetImg;
97
+ viewerContainer.appendChild(markerView);
98
+
99
+ markerView.show(savedState);
100
+ ```
24
101
 
25
102
  ## Demos
26
103
 
27
- *coming soon...*
104
+ Check out the "work-in-progress" demo [here](https://github.com/ailon/markerjs3-wip-demo). It covers most of the available features with no extra bells or whistles.
105
+
106
+ While it's made with React it is purposefully light on React-specific stuff and "best practices" to just focus on marker.js 3 related things.
107
+
108
+ _more demos coming soon..._
28
109
 
29
110
  ## More docs and tutorials
30
111
 
31
- *coming soon...*
112
+ _coming soon..._
32
113
 
33
114
  ## License
115
+
34
116
  Linkware (see [LICENSE](https://github.com/ailon/markerjs3/blob/master/LICENSE) for details) - the UI displays a small link back to the marker.js 3 website which should be retained.
35
117
 
36
- Alternative licenses are available through the [marker.js website](https://markerjs.com).
118
+ Alternative licenses are available through the [marker.js website](https://markerjs.com).
package/markerjs3.d.ts CHANGED
@@ -236,6 +236,14 @@ declare class MarkerBase {
236
236
  * @param scaleY - vertical scale
237
237
  */
238
238
  scale(scaleX: number, scaleY: number): void;
239
+ /**
240
+ * Returns markers bounding box.
241
+ *
242
+ * Override to return a custom bounding box.
243
+ *
244
+ * @returns rectangle fitting the marker.
245
+ */
246
+ getBBox(): DOMRect;
239
247
  }
240
248
 
241
249
  /**
@@ -357,6 +365,7 @@ declare class RectangularBoxMarkerBase extends MarkerBase {
357
365
  * @param scaleY - vertical scale
358
366
  */
359
367
  scale(scaleX: number, scaleY: number): void;
368
+ getBBox(): DOMRect;
360
369
  }
361
370
 
362
371
  declare class ShapeOutlineMarkerBase extends RectangularBoxMarkerBase {
@@ -403,8 +412,7 @@ interface ShapeMarkerBaseState extends ShapeOutlineMarkerBaseState {
403
412
  declare abstract class ShapeMarkerBase extends ShapeOutlineMarkerBase {
404
413
  static title: string;
405
414
  protected _fillColor: string;
406
- get fillColor(): string;
407
- set fillColor(value: string);
415
+ protected applyFillColor(): void;
408
416
  constructor(container: SVGGElement);
409
417
  createVisual(): void;
410
418
  /**
@@ -861,7 +869,7 @@ declare class TextMarker extends RectangularBoxMarkerBase {
861
869
  /**
862
870
  * Text padding from the bounding box.
863
871
  */
864
- protected padding: number;
872
+ padding: number;
865
873
  /**
866
874
  * Text's bounding box where text should fit and/or be anchored to.
867
875
  */
@@ -879,6 +887,7 @@ declare class TextMarker extends RectangularBoxMarkerBase {
879
887
  * Sets (adjusts) the stencil's size.
880
888
  */
881
889
  setSize(): void;
890
+ protected setSizeFromTextSize(): void;
882
891
  private textSizeChanged;
883
892
  /**
884
893
  * Sets the text color.
@@ -949,6 +958,11 @@ declare class CalloutMarker extends TextMarker {
949
958
  private tipBase2Position;
950
959
  private _calloutVisual;
951
960
  constructor(container: SVGGElement);
961
+ protected applyStrokeColor(): void;
962
+ protected applyStrokeWidth(): void;
963
+ protected applyStrokeDasharray(): void;
964
+ protected applyOpacity(): void;
965
+ protected applyFillColor(): void;
952
966
  protected getPath(): string;
953
967
  private setTipPoints;
954
968
  createVisual(): void;
@@ -987,6 +1001,139 @@ declare class EllipseMarker extends ShapeMarkerBase {
987
1001
  getState(): ShapeMarkerBaseState;
988
1002
  }
989
1003
 
1004
+ type ImageType = 'svg' | 'bitmap';
1005
+ /**
1006
+ * Represents image marker's state.
1007
+ */
1008
+ interface ImageMarkerBaseState extends RectangularBoxMarkerBaseState {
1009
+ imageType?: ImageType;
1010
+ svgString?: string;
1011
+ imageSrc?: string;
1012
+ }
1013
+
1014
+ declare class ImageMarkerBase extends RectangularBoxMarkerBase {
1015
+ static title: string;
1016
+ /**
1017
+ * Main SVG or image element of the stencil.
1018
+ */
1019
+ protected SVGImage?: SVGSVGElement | SVGImageElement;
1020
+ protected imageType: ImageType;
1021
+ protected _svgString?: string;
1022
+ get svgString(): string | undefined;
1023
+ set svgString(value: string | undefined);
1024
+ protected _imageSrc?: string;
1025
+ get imageSrc(): string | undefined;
1026
+ set imageSrc(value: string | undefined);
1027
+ /**
1028
+ * Natural (real) width of the image.
1029
+ */
1030
+ protected naturalWidth: number;
1031
+ /**
1032
+ * Natural (real) height of the image.
1033
+ */
1034
+ protected naturalHeight: number;
1035
+ constructor(container: SVGGElement);
1036
+ protected createImage(): void;
1037
+ createVisual(): void;
1038
+ adjustImage(): void;
1039
+ private isDescendant;
1040
+ ownsTarget(el: EventTarget): boolean;
1041
+ setSize(): void;
1042
+ getState(): ImageMarkerBaseState;
1043
+ protected applyStrokeColor(): void;
1044
+ restoreState(state: ImageMarkerBaseState): void;
1045
+ /**
1046
+ * Scales marker. Used after the image resize.
1047
+ *
1048
+ * @param scaleX - horizontal scale
1049
+ * @param scaleY - vertical scale
1050
+ */
1051
+ scale(scaleX: number, scaleY: number): void;
1052
+ }
1053
+
1054
+ /**
1055
+ * Used to represent user-set images.
1056
+ */
1057
+ declare class CustomImageMarker extends ImageMarkerBase {
1058
+ /**
1059
+ * String type name of the marker type.
1060
+ */
1061
+ static typeName: string;
1062
+ /**
1063
+ * Marker type title (display name) used for accessibility and other attributes.
1064
+ */
1065
+ static title: string;
1066
+ }
1067
+
1068
+ /**
1069
+ * Check mark marker.
1070
+ */
1071
+ declare class CheckImageMarker extends ImageMarkerBase {
1072
+ /**
1073
+ * String type name of the marker type.
1074
+ */
1075
+ static typeName: string;
1076
+ /**
1077
+ * Marker type title (display name) used for accessibility and other attributes.
1078
+ */
1079
+ static title: string;
1080
+ constructor(container: SVGGElement);
1081
+ }
1082
+
1083
+ /**
1084
+ * X mark marker.
1085
+ */
1086
+ declare class XImageMarker extends ImageMarkerBase {
1087
+ /**
1088
+ * String type name of the marker type.
1089
+ */
1090
+ static typeName: string;
1091
+ /**
1092
+ * Marker type title (display name) used for accessibility and other attributes.
1093
+ */
1094
+ static title: string;
1095
+ constructor(container: SVGGElement);
1096
+ }
1097
+
1098
+ interface CaptionFrameMarkerState extends TextMarkerState, ShapeMarkerBaseState {
1099
+ }
1100
+
1101
+ declare class CaptionFrameMarker extends TextMarker {
1102
+ static typeName: string;
1103
+ static title: string;
1104
+ private _outerFrameVisual;
1105
+ private _captionFrameVisual;
1106
+ private _frameVisual;
1107
+ constructor(container: SVGGElement);
1108
+ protected applyStrokeColor(): void;
1109
+ protected applyStrokeWidth(): void;
1110
+ protected applyStrokeDasharray(): void;
1111
+ protected applyOpacity(): void;
1112
+ protected applyFillColor(): void;
1113
+ protected getPaths(width?: number, height?: number): {
1114
+ frame: string;
1115
+ caption: string;
1116
+ };
1117
+ createVisual(): void;
1118
+ adjustVisual(): void;
1119
+ protected adjustTextPosition(): void;
1120
+ protected adjustFrameVisual(): void;
1121
+ ownsTarget(el: EventTarget): boolean;
1122
+ setSize(): void;
1123
+ protected setSizeFromTextSize(): void;
1124
+ hideVisual(): void;
1125
+ showVisual(): void;
1126
+ getState(): CaptionFrameMarkerState;
1127
+ restoreState(state: MarkerBaseState): void;
1128
+ /**
1129
+ * Scales marker. Used after the image resize.
1130
+ *
1131
+ * @param scaleX - horizontal scale
1132
+ * @param scaleY - vertical scale
1133
+ */
1134
+ scale(scaleX: number, scaleY: number): void;
1135
+ }
1136
+
990
1137
  interface MarkerEditorProperties<TMarkerType extends MarkerBase = MarkerBase> {
991
1138
  /**
992
1139
  * SVG container for the marker and editor elements.
@@ -1252,6 +1399,8 @@ declare class MarkerArea extends HTMLElement {
1252
1399
  private hideOutline;
1253
1400
  private onPointerUp;
1254
1401
  private finishMarqueeSelection;
1402
+ private adjustMarqueeSelectOutline;
1403
+ private hideMarqueeSelectOutline;
1255
1404
  private onPointerOut;
1256
1405
  private onKeyUp;
1257
1406
  private attachEvents;
@@ -1727,9 +1876,127 @@ declare class FreehandMarkerEditor<TMarkerType extends FreehandMarker = Freehand
1727
1876
  deselect(): void;
1728
1877
  }
1729
1878
 
1879
+ /**
1880
+ * Text changed event handler type.
1881
+ */
1882
+ type TextChangedHandler = (text: string) => void;
1883
+ type BlurHandler = () => void;
1884
+ /**
1885
+ * Represents a text block editor element.
1886
+ */
1887
+ declare class TextBlockEditor {
1888
+ private textEditor;
1889
+ private isInFocus;
1890
+ private _width;
1891
+ /**
1892
+ * Returns editor width in pixels.
1893
+ */
1894
+ get width(): number;
1895
+ /**
1896
+ * Sets editor width in pixels.
1897
+ */
1898
+ set width(value: number);
1899
+ private _height;
1900
+ /**
1901
+ * Returns editor height in pixels.
1902
+ */
1903
+ get height(): number;
1904
+ /**
1905
+ * Sets editor height in pixels.
1906
+ */
1907
+ set height(value: number);
1908
+ private _left;
1909
+ /**
1910
+ * Returns the horizontal (X) location of the editor's left corner (in pixels).
1911
+ */
1912
+ get left(): number;
1913
+ /**
1914
+ * Sets the horizontal (X) location of the editor's left corner (in pixels).
1915
+ */
1916
+ set left(value: number);
1917
+ private _top;
1918
+ /**
1919
+ * Returns the vertical (Y) location of the editor's top left corner (in pixels).
1920
+ */
1921
+ get top(): number;
1922
+ /**
1923
+ * Sets the vertical (Y) location of the editor's top left corner (in pixels).
1924
+ */
1925
+ set top(value: number);
1926
+ private _text;
1927
+ /**
1928
+ * Returns the text block text.
1929
+ */
1930
+ get text(): string;
1931
+ /**
1932
+ * Sets the text block text.
1933
+ */
1934
+ set text(value: string);
1935
+ private _fontFamily;
1936
+ /**
1937
+ * Returns text block's font family.
1938
+ */
1939
+ get fontFamily(): string;
1940
+ /**
1941
+ * Sets the text block's font family.
1942
+ */
1943
+ set fontFamily(value: string);
1944
+ private _fontSize;
1945
+ /**
1946
+ * Returns text block's font size.
1947
+ */
1948
+ get fontSize(): string;
1949
+ /**
1950
+ * Sets text block's font size.
1951
+ */
1952
+ set fontSize(value: string);
1953
+ private _textColor;
1954
+ /**
1955
+ * Returns text block's font color.
1956
+ */
1957
+ get textColor(): string;
1958
+ /**
1959
+ * Returns text block's font color.
1960
+ */
1961
+ set textColor(value: string);
1962
+ private _bgColor;
1963
+ get bgColor(): string;
1964
+ set bgColor(value: string);
1965
+ /**
1966
+ * Text changed event handler.
1967
+ */
1968
+ onTextChanged?: TextChangedHandler;
1969
+ onBlur?: BlurHandler;
1970
+ /**
1971
+ * Creates a new text block editor instance.
1972
+ */
1973
+ constructor();
1974
+ private isSetupCompleted;
1975
+ private setup;
1976
+ /**
1977
+ * Returns editor's UI,
1978
+ * @returns UI in a div element.
1979
+ */
1980
+ getEditorUi(): HTMLDivElement;
1981
+ /**
1982
+ * Focuses text editing in the editor.
1983
+ */
1984
+ focus(): void;
1985
+ /**
1986
+ * Unfocuses the editor.
1987
+ */
1988
+ blur(): void;
1989
+ }
1990
+
1730
1991
  declare class TextMarkerEditor<TMarkerType extends TextMarker = TextMarker> extends RectangularBoxMarkerBaseEditor<TMarkerType> {
1731
- private textBlockEditorContainer;
1732
- private textBlockEditor;
1992
+ protected textBlockEditorContainer: SVGForeignObjectElement;
1993
+ protected textBlockEditor: TextBlockEditor;
1994
+ set color(color: string);
1995
+ get color(): string;
1996
+ set fontFamily(font: string);
1997
+ get fontFamily(): string;
1998
+ set fontSize(size: FontSize);
1999
+ get fontSize(): FontSize;
1733
2000
  constructor(properties: MarkerEditorProperties<TMarkerType>);
1734
2001
  private _pointerDownTime;
1735
2002
  private _pointerDownPoint;
@@ -1777,6 +2044,29 @@ declare class CalloutMarkerEditor<TMarkerType extends CalloutMarker = CalloutMar
1777
2044
  protected resize(point: IPoint): void;
1778
2045
  }
1779
2046
 
2047
+ declare class ImageMarkerEditor<TMarkerType extends ImageMarkerBase = ImageMarkerBase> extends RectangularBoxMarkerBaseEditor<TMarkerType> {
2048
+ constructor(properties: MarkerEditorProperties<TMarkerType>);
2049
+ /**
2050
+ * Handles pointer (mouse, touch, stylus, etc.) down event.
2051
+ *
2052
+ * @param point - event coordinates.
2053
+ * @param target - direct event target element.
2054
+ */
2055
+ pointerDown(point: IPoint, target?: EventTarget): void;
2056
+ /**
2057
+ * Handles pointer (mouse, touch, stylus, etc.) up event.
2058
+ *
2059
+ * @param point - event coordinates.
2060
+ * @param target - direct event target element.
2061
+ */
2062
+ pointerUp(point: IPoint): void;
2063
+ }
2064
+
2065
+ declare class CaptionFrameMarkerEditor<TMarkerType extends CaptionFrameMarker = CaptionFrameMarker> extends TextMarkerEditor<TMarkerType> {
2066
+ constructor(properties: MarkerEditorProperties<TMarkerType>);
2067
+ protected setSize(): void;
2068
+ }
2069
+
1780
2070
  interface MarkerViewEventMap {
1781
2071
  /**
1782
2072
  * Viewer initialized.
@@ -1858,6 +2148,7 @@ declare class MarkerView extends HTMLElement {
1858
2148
  declare class Renderer {
1859
2149
  private _mainCanvas?;
1860
2150
  private _editingTarget?;
2151
+ private _renderHelperContainer?;
1861
2152
  private _targetWidth;
1862
2153
  get targetWidth(): number;
1863
2154
  set targetWidth(value: number);
@@ -1916,4 +2207,4 @@ declare class Renderer {
1916
2207
  rasterize(state: AnnotationState, targetCanvas?: HTMLCanvasElement): Promise<string>;
1917
2208
  }
1918
2209
 
1919
- export { type AnnotationState, ArrowMarker, ArrowMarkerEditor, type ArrowMarkerState, type ArrowType, CalloutMarker, CalloutMarkerEditor, type CalloutMarkerState, type ColorType, CoverMarker, EllipseFrameMarker, EllipseMarker, FrameMarker, FreehandMarker, FreehandMarkerEditor, type FreehandMarkerState, Grip, HighlightMarker, type IPoint, LineMarker, LinearMarkerBase, type LinearMarkerBaseState, LinearMarkerEditor, MarkerArea, type MarkerAreaEventData, type MarkerAreaEventMap, MarkerBase, MarkerBaseEditor, type MarkerBaseState, type MarkerEditorProperties, type MarkerEditorState, MarkerView, type MarkerViewEventData, type MarkerViewEventMap, MeasurementMarker, PolygonMarker, PolygonMarkerEditor, type PolygonMarkerState, RectangularBoxMarkerBase, type RectangularBoxMarkerBaseState, Renderer, ResizeGrip, RotateGrip, ShapeMarkerBase, type ShapeMarkerBaseState, ShapeMarkerEditor, ShapeOutlineMarkerBase, type ShapeOutlineMarkerBaseState, ShapeOutlineMarkerEditor, SvgHelper, TextMarker, TextMarkerEditor, type TextMarkerState };
2210
+ export { type AnnotationState, ArrowMarker, ArrowMarkerEditor, type ArrowMarkerState, type ArrowType, CalloutMarker, CalloutMarkerEditor, type CalloutMarkerState, CaptionFrameMarker, CaptionFrameMarkerEditor, type CaptionFrameMarkerState, CheckImageMarker, type ColorType, CoverMarker, CustomImageMarker, EllipseFrameMarker, EllipseMarker, type FontSize, FrameMarker, FreehandMarker, FreehandMarkerEditor, type FreehandMarkerState, Grip, HighlightMarker, type IPoint, ImageMarkerBase, type ImageMarkerBaseState, ImageMarkerEditor, type ImageType, LineMarker, LinearMarkerBase, type LinearMarkerBaseState, LinearMarkerEditor, MarkerArea, type MarkerAreaEventData, type MarkerAreaEventMap, MarkerBase, MarkerBaseEditor, type MarkerBaseState, type MarkerEditorProperties, type MarkerEditorState, MarkerView, type MarkerViewEventData, type MarkerViewEventMap, MeasurementMarker, PolygonMarker, PolygonMarkerEditor, type PolygonMarkerState, RectangularBoxMarkerBase, type RectangularBoxMarkerBaseState, Renderer, ResizeGrip, RotateGrip, ShapeMarkerBase, type ShapeMarkerBaseState, ShapeMarkerEditor, ShapeOutlineMarkerBase, type ShapeOutlineMarkerBaseState, ShapeOutlineMarkerEditor, SvgHelper, TextMarker, TextMarkerEditor, type TextMarkerState, XImageMarker };