nzk-react-components 0.4.3 → 0.4.4

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.
Files changed (90) hide show
  1. package/dist/assets/icons/bear.svg +177 -0
  2. package/dist/assets/icons/bear.svg.js +606 -0
  3. package/dist/assets/icons/bear.svg.js.map +1 -0
  4. package/dist/assets/icons/flamingo.svg +85 -0
  5. package/dist/assets/icons/flamingo.svg.js +287 -0
  6. package/dist/assets/icons/flamingo.svg.js.map +1 -0
  7. package/dist/assets/icons/giraffe.svg +259 -0
  8. package/dist/assets/icons/giraffe.svg.js +756 -0
  9. package/dist/assets/icons/giraffe.svg.js.map +1 -0
  10. package/dist/assets/icons/penguin.svg +231 -0
  11. package/dist/assets/icons/penguin.svg.js +718 -0
  12. package/dist/assets/icons/penguin.svg.js.map +1 -0
  13. package/dist/components/DrawingTool/DrawingTool.d.ts +29 -0
  14. package/dist/components/DrawingTool/DrawingTool.js +277 -0
  15. package/dist/components/DrawingTool/DrawingTool.js.map +1 -0
  16. package/dist/components/DrawingTool/DrawingTool.stories.d.ts +6 -0
  17. package/dist/components/DrawingTool/DrawingTool.styles.d.ts +41 -0
  18. package/dist/components/DrawingTool/DrawingTool.styles.js +54 -0
  19. package/dist/components/DrawingTool/DrawingTool.styles.js.map +1 -0
  20. package/dist/components/DrawingTool/DrawingToolProvider.d.ts +56 -0
  21. package/dist/components/DrawingTool/DrawingToolProvider.js +182 -0
  22. package/dist/components/DrawingTool/DrawingToolProvider.js.map +1 -0
  23. package/dist/components/DrawingTool/components/ColourToolbar/ColourToolbar.d.ts +21 -0
  24. package/dist/components/DrawingTool/components/ColourToolbar/ColourToolbar.js +89 -0
  25. package/dist/components/DrawingTool/components/ColourToolbar/ColourToolbar.js.map +1 -0
  26. package/dist/components/DrawingTool/components/ColourToolbar/ColourToolbar.styles.d.ts +9 -0
  27. package/dist/components/DrawingTool/components/ColourToolbar/ColourToolbar.styles.js +18 -0
  28. package/dist/components/DrawingTool/components/ColourToolbar/ColourToolbar.styles.js.map +1 -0
  29. package/dist/components/DrawingTool/components/ColourToolbar/index.d.ts +2 -0
  30. package/dist/components/DrawingTool/components/ColourToolbar/index.js +8 -0
  31. package/dist/components/DrawingTool/components/ColourToolbar/index.js.map +1 -0
  32. package/dist/components/DrawingTool/components/FileInput/FileInput.d.ts +9 -0
  33. package/dist/components/DrawingTool/components/FileInput/FileInput.js +88 -0
  34. package/dist/components/DrawingTool/components/FileInput/FileInput.js.map +1 -0
  35. package/dist/components/DrawingTool/components/FileInput/FileInput.styles.d.ts +4 -0
  36. package/dist/components/DrawingTool/components/FileInput/FileInput.styles.js +22 -0
  37. package/dist/components/DrawingTool/components/FileInput/FileInput.styles.js.map +1 -0
  38. package/dist/components/DrawingTool/components/FileInput/index.d.ts +2 -0
  39. package/dist/components/DrawingTool/components/FileInput/index.js +8 -0
  40. package/dist/components/DrawingTool/components/FileInput/index.js.map +1 -0
  41. package/dist/components/DrawingTool/components/Header/Header.d.ts +9 -0
  42. package/dist/components/DrawingTool/components/Header/Header.js +38 -0
  43. package/dist/components/DrawingTool/components/Header/Header.js.map +1 -0
  44. package/dist/components/DrawingTool/components/Header/index.d.ts +2 -0
  45. package/dist/components/DrawingTool/components/Header/index.js +8 -0
  46. package/dist/components/DrawingTool/components/Header/index.js.map +1 -0
  47. package/dist/components/DrawingTool/components/OpacityToggle/OpacityToggle.d.ts +8 -0
  48. package/dist/components/DrawingTool/components/OpacityToggle/OpacityToggle.js +31 -0
  49. package/dist/components/DrawingTool/components/OpacityToggle/OpacityToggle.js.map +1 -0
  50. package/dist/components/DrawingTool/components/OpacityToggle/OpacityToggle.styles.d.ts +9 -0
  51. package/dist/components/DrawingTool/components/OpacityToggle/OpacityToggle.styles.js +18 -0
  52. package/dist/components/DrawingTool/components/OpacityToggle/OpacityToggle.styles.js.map +1 -0
  53. package/dist/components/DrawingTool/components/OpacityToggle/index.d.ts +2 -0
  54. package/dist/components/DrawingTool/components/OpacityToggle/index.js +8 -0
  55. package/dist/components/DrawingTool/components/OpacityToggle/index.js.map +1 -0
  56. package/dist/components/DrawingTool/components/Placeable/index.d.ts +9 -0
  57. package/dist/components/DrawingTool/components/Placeable/index.js +22 -0
  58. package/dist/components/DrawingTool/components/Placeable/index.js.map +1 -0
  59. package/dist/components/DrawingTool/index.d.ts +2 -0
  60. package/dist/components/DrawingTool/lib/Sketch.d.ts +90 -0
  61. package/dist/components/DrawingTool/lib/Sketch.js +414 -0
  62. package/dist/components/DrawingTool/lib/Sketch.js.map +1 -0
  63. package/dist/components/DrawingTool/lib/SketchCut.d.ts +22 -0
  64. package/dist/components/DrawingTool/lib/SketchCut.js +98 -0
  65. package/dist/components/DrawingTool/lib/SketchCut.js.map +1 -0
  66. package/dist/components/DrawingTool/lib/SketchCutModel.d.ts +6 -0
  67. package/dist/components/DrawingTool/lib/SketchCutModel.js +20 -0
  68. package/dist/components/DrawingTool/lib/SketchCutModel.js.map +1 -0
  69. package/dist/components/DrawingTool/lib/SketchLayer.d.ts +27 -0
  70. package/dist/components/DrawingTool/lib/SketchLayer.js +59 -0
  71. package/dist/components/DrawingTool/lib/SketchLayer.js.map +1 -0
  72. package/dist/components/DrawingTool/lib/SketchModel.d.ts +65 -0
  73. package/dist/components/DrawingTool/lib/SketchModel.js +137 -0
  74. package/dist/components/DrawingTool/lib/SketchModel.js.map +1 -0
  75. package/dist/components/DrawingTool/lib/SketchStrokeModel.d.ts +33 -0
  76. package/dist/components/DrawingTool/lib/SketchStrokeModel.js +48 -0
  77. package/dist/components/DrawingTool/lib/SketchStrokeModel.js.map +1 -0
  78. package/dist/components/DrawingTool/lib/createInteractionSurface.d.ts +16 -0
  79. package/dist/components/DrawingTool/lib/createInteractionSurface.js +56 -0
  80. package/dist/components/DrawingTool/lib/createInteractionSurface.js.map +1 -0
  81. package/dist/components/DrawingTool/lib/trace.d.ts +3 -0
  82. package/dist/components/DrawingTool/lib/trace.js +24 -0
  83. package/dist/components/DrawingTool/lib/trace.js.map +1 -0
  84. package/dist/components/DrawingTool/lib/trimCanvas.d.ts +2 -0
  85. package/dist/components/DrawingTool/lib/trimCanvas.js +42 -0
  86. package/dist/components/DrawingTool/lib/trimCanvas.js.map +1 -0
  87. package/dist/components/Icon/icons.d.ts +4 -0
  88. package/dist/components/Icon/icons.js +8 -0
  89. package/dist/components/Icon/icons.js.map +1 -1
  90. package/package.json +1 -1
@@ -0,0 +1,18 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var tslib = require('tslib');
6
+ var styled = require('styled-components');
7
+
8
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
+
10
+ var styled__default = /*#__PURE__*/_interopDefaultLegacy(styled);
11
+
12
+ var Container = styled__default['default'].div(templateObject_1 || (templateObject_1 = tslib.__makeTemplateObject(["\n custor: pointer;\n background-color: #ebebeb;\n border: 6px solid #d9d9d9;\n box-shadow: 0px 4px 0px rgba(0,0,0, .5);\n border-radius: ", "px;\n width: ", ";\n padding-bottom: ", ";\n padding-right: ", ";\n display: flex;\n flex-shrink: none;\n flex-wrap: ", ";\n justify-content: center;\n align-items: center;\n position: relative;\n > div {\n transform: scale(1.2);\n &:first-child {\n position: relative;\n top: ", ";\n left: ", ";\n }\n }\n"], ["\n custor: pointer;\n background-color: #ebebeb;\n border: 6px solid #d9d9d9;\n box-shadow: 0px 4px 0px rgba(0,0,0, .5);\n border-radius: ", "px;\n width: ", ";\n padding-bottom: ", ";\n padding-right: ", ";\n display: flex;\n flex-shrink: none;\n flex-wrap: ", ";\n justify-content: center;\n align-items: center;\n position: relative;\n > div {\n transform: scale(1.2);\n &:first-child {\n position: relative;\n top: ", ";\n left: ", ";\n }\n }\n"])), function (props) { return props.buttonSize; }, function (props) { return props.orientation === 'LANDSCAPE' ? props.buttonSize + props.buttonSize * 0.2 + "px" : 'auto'; }, function (props) { return props.orientation === 'LANDSCAPE' ? '4px' : '0px'; }, function (props) { return props.orientation === 'LANDSCAPE' ? '0px' : '4px'; }, function (props) { return props.orientation === 'LANDSCAPE' ? 'wrap' : 'no-wrap'; }, function (props) { return props.orientation === 'LANDSCAPE' ? '4px' : '0px'; }, function (props) { return props.orientation === 'PORTRAIT' ? '4px' : '0px'; });
13
+ var OpacityButton = styled__default['default'].div(templateObject_2 || (templateObject_2 = tslib.__makeTemplateObject(["\n width: ", "px;\n height: ", "px;\n cursor: pointer;\n"], ["\n width: ", "px;\n height: ", "px;\n cursor: pointer;\n"])), function (props) { return props.buttonSize; }, function (props) { return props.buttonSize; });
14
+ var templateObject_1, templateObject_2;
15
+
16
+ exports.Container = Container;
17
+ exports.OpacityButton = OpacityButton;
18
+ //# sourceMappingURL=OpacityToggle.styles.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"OpacityToggle.styles.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,2 @@
1
+ import OpacityToggle from "./OpacityToggle";
2
+ export default OpacityToggle;
@@ -0,0 +1,8 @@
1
+ 'use strict';
2
+
3
+ var OpacityToggle = require('./OpacityToggle.js');
4
+
5
+
6
+
7
+ module.exports = OpacityToggle;
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;"}
@@ -0,0 +1,9 @@
1
+ /// <reference types="react" />
2
+ import { MoveableManagerInterface, Renderer } from "react-moveable";
3
+ declare const _default: {
4
+ readonly name: "placeable";
5
+ readonly props: {};
6
+ readonly events: {};
7
+ readonly render: (moveable: MoveableManagerInterface<any, any>, React: Renderer) => JSX.Element;
8
+ };
9
+ export default _default;
@@ -0,0 +1,22 @@
1
+ 'use strict';
2
+
3
+ var Icon = require('../../../Icon/Icon.js');
4
+
5
+ var Placeable = {
6
+ name: "placeable",
7
+ props: {},
8
+ events: {},
9
+ render: function (moveable, React) {
10
+ var rect = moveable.getRect();
11
+ var _a = moveable.state, pos1 = _a.pos1, pos2 = _a.pos2;
12
+ var Placeable = moveable.useCSS("div", "\n {\n position: absolute;\n left: 0px;\n top: 0px;\n will-change: transform;\n transform-origin: 0px 0px;\n pointer-events: none;\n }\n .placeable-icon {\n width: 24px;\n height: 24px;\n background: #4af;\n border-radius: 100%;\n }\n ");
13
+ return React.createElement(Placeable, { key: "placeable", className: "moveable-placeable", style: {
14
+ transform: "translate(" + (pos2[0] - (pos2[0] - pos1[0]) / 2) + "px, " + (pos2[1] - (pos2[1] - pos1[1]) / 2) + "px) rotate(" + rect.rotation + "deg) translateY(-54px) translateX(-12px)",
15
+ } },
16
+ React.createElement("div", { className: 'placeable-icon' },
17
+ React.createElement(Icon, { name: 'refresh' })));
18
+ }
19
+ };
20
+
21
+ module.exports = Placeable;
22
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,2 @@
1
+ import DrawingTool from "./DrawingTool";
2
+ export default DrawingTool;
@@ -0,0 +1,90 @@
1
+ import SketchLayer from './SketchLayer';
2
+ import { Point } from './createInteractionSurface';
3
+ import SketchModel, { SketchAction, SketchActionMergeData, SketchModelData } from './SketchModel';
4
+ export interface SketchProps {
5
+ containerEl: HTMLDivElement;
6
+ template?: string;
7
+ onChange?: Function;
8
+ sketchData?: SketchModelData;
9
+ onReady?: Function;
10
+ }
11
+ interface ExportProps {
12
+ crop?: boolean;
13
+ }
14
+ export default class Sketch {
15
+ readonly containerEl: HTMLDivElement;
16
+ readonly width: number;
17
+ readonly height: number;
18
+ readonly naturalWidth: number;
19
+ readonly naturalHeight: number;
20
+ readonly pixelRatioScale: number;
21
+ readonly model: SketchModel;
22
+ readonly drawingLayer: SketchLayer;
23
+ readonly bufferLayer: SketchLayer;
24
+ readonly cacheLayer: SketchLayer;
25
+ readonly templateLayer?: SketchLayer;
26
+ template?: string;
27
+ interactionSurface: HTMLDivElement;
28
+ isDrawing: boolean;
29
+ onChange?: Function;
30
+ sketchData?: SketchModelData;
31
+ drawUndo?: any;
32
+ drawFinished?: any;
33
+ drawAnimation?: any;
34
+ reqStroke?: any;
35
+ constructor(props: SketchProps);
36
+ setBrush({ colour, size, opacity, fill, eraser }: {
37
+ colour: any;
38
+ size: any;
39
+ opacity: any;
40
+ fill: any;
41
+ eraser: any;
42
+ }): void;
43
+ mergeImage(data: SketchActionMergeData, saveAction?: boolean, callback?: () => void): void;
44
+ export(props?: ExportProps): any;
45
+ undo(): void;
46
+ redo(): void;
47
+ canUndo(): boolean;
48
+ canRedo(): boolean;
49
+ restart(): void;
50
+ serialize(): {
51
+ colour: number[];
52
+ opacity: number;
53
+ size: number;
54
+ lastActionIndex: number;
55
+ actions: SketchAction[];
56
+ currentStroke: import("./SketchStrokeModel").SketchStrokeModelData | null;
57
+ };
58
+ deserialize(serialized: any): void;
59
+ getNumberOfActions(): number;
60
+ initDrawAnimations(): void;
61
+ startDraw(point: Point): void;
62
+ continueDraw(point: Point): boolean;
63
+ endDraw(): void;
64
+ drawExistingSketch(callback?: Function): void;
65
+ drawAction(action: SketchAction, callback?: () => void): void;
66
+ strokeAnimation(): void;
67
+ drawAnimationStroke(stroke: any): void;
68
+ endStrokeAnimation(): void;
69
+ drawUndoStroke(stroke: any): void;
70
+ drawFinishedStroke(stroke: any): void;
71
+ drawExistingStroke(stroke: any): void;
72
+ drawTransparentFillFinal(stroke: any): void;
73
+ drawStrokeFinal(stroke: any): void;
74
+ drawFillFinal(stroke: any): void;
75
+ drawFillAndStroke(stroke: any): void;
76
+ drawEraserUndoingFinal(stroke: any): void;
77
+ drawEraserUndoingFillFinal(stroke: any): void;
78
+ drawEraser(stroke: any, copy?: boolean): void;
79
+ drawEraserFillFinal(stroke: any, copy?: boolean): void;
80
+ findBoundingBox(ctx: any): {
81
+ topLeftX: number;
82
+ topLeftY: number;
83
+ bottomRightX: number;
84
+ bottomRightY: number;
85
+ width: number;
86
+ height: number;
87
+ };
88
+ getLayerToExport(): SketchLayer;
89
+ }
90
+ export {};
@@ -0,0 +1,414 @@
1
+ 'use strict';
2
+
3
+ var tslib = require('tslib');
4
+ var after = require('lodash.after');
5
+ var SketchLayer = require('./SketchLayer.js');
6
+ var createInteractionSurface = require('./createInteractionSurface.js');
7
+ var SketchModel = require('./SketchModel.js');
8
+ var trace = require('./trace.js');
9
+ var trimCanvas = require('./trimCanvas.js');
10
+
11
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
12
+
13
+ var after__default = /*#__PURE__*/_interopDefaultLegacy(after);
14
+
15
+ var setDrawingStyle = function (style, ctx) {
16
+ if (style) {
17
+ var colour = "rgba(" + style.colour[0] + ", " + style.colour[1] + ", " + style.colour[2] + ", " + style.opacity + ")";
18
+ ctx.strokeStyle = colour;
19
+ ctx.fillStyle = colour;
20
+ ctx.lineWidth = style.size;
21
+ }
22
+ ctx.lineJoin = "round";
23
+ ctx.lineCap = "round";
24
+ };
25
+ var Sketch = (function () {
26
+ function Sketch(props) {
27
+ this.isDrawing = false;
28
+ this.containerEl = props.containerEl;
29
+ this.naturalWidth = props.containerEl.offsetWidth;
30
+ this.naturalHeight = props.containerEl.offsetHeight;
31
+ this.pixelRatioScale = window.devicePixelRatio > 1.5 ? 2 : 1;
32
+ this.width = this.naturalWidth * this.pixelRatioScale;
33
+ this.height = this.naturalHeight * this.pixelRatioScale;
34
+ this.template = props.template;
35
+ this.model = new SketchModel({
36
+ pixelRatioScale: this.pixelRatioScale
37
+ });
38
+ var defaultLayerProps = {
39
+ pixelRatioScale: this.pixelRatioScale,
40
+ width: props.containerEl.offsetWidth,
41
+ height: props.containerEl.offsetHeight
42
+ };
43
+ if (this.template) {
44
+ this.templateLayer = new SketchLayer(defaultLayerProps);
45
+ this.templateLayer.drawImageToFill(this.template);
46
+ this.containerEl.appendChild(this.templateLayer.canvas);
47
+ }
48
+ this.drawingLayer = new SketchLayer(defaultLayerProps);
49
+ this.drawingLayer.canvas.style.zIndex = '1';
50
+ this.containerEl.appendChild(this.drawingLayer.canvas);
51
+ this.bufferLayer = new SketchLayer(defaultLayerProps);
52
+ this.bufferLayer.canvas.style.zIndex = '2';
53
+ this.containerEl.appendChild(this.bufferLayer.canvas);
54
+ this.cacheLayer = new SketchLayer(defaultLayerProps);
55
+ this.cacheLayer.hide();
56
+ this.containerEl.appendChild(this.cacheLayer.canvas);
57
+ this.interactionSurface = createInteractionSurface({
58
+ width: this.containerEl.offsetWidth,
59
+ height: this.containerEl.offsetHeight,
60
+ scale: this.pixelRatioScale,
61
+ onTouchStart: this.startDraw.bind(this),
62
+ onTouchMove: this.continueDraw.bind(this),
63
+ onTouchEnd: this.endDraw.bind(this),
64
+ onTouchLeave: this.endDraw.bind(this),
65
+ onTouchEnter: this.startDraw.bind(this)
66
+ });
67
+ this.interactionSurface.style.zIndex = '100';
68
+ this.containerEl.appendChild(this.interactionSurface);
69
+ this.initDrawAnimations();
70
+ if (this.bufferLayer.ctx) {
71
+ setDrawingStyle(this.model.getStyle(), this.bufferLayer.ctx);
72
+ }
73
+ if (props.onChange)
74
+ this.onChange = props.onChange;
75
+ if (props.sketchData) {
76
+ this.deserialize(props.sketchData);
77
+ this.drawExistingSketch(props.onReady);
78
+ }
79
+ else if (props.onReady) {
80
+ props.onReady();
81
+ }
82
+ }
83
+ Sketch.prototype.setBrush = function (_a) {
84
+ var colour = _a.colour, size = _a.size, opacity = _a.opacity, fill = _a.fill, eraser = _a.eraser;
85
+ if (colour !== undefined) {
86
+ this.model.colour = colour;
87
+ }
88
+ if (size !== undefined) {
89
+ this.model.size = size;
90
+ }
91
+ if (opacity !== undefined) {
92
+ this.model.opacity = opacity;
93
+ }
94
+ if (fill !== undefined) {
95
+ this.model.fill = fill;
96
+ }
97
+ if (eraser !== undefined) {
98
+ this.model.eraser = eraser;
99
+ }
100
+ };
101
+ Sketch.prototype.mergeImage = function (data, saveAction, callback) {
102
+ var _this = this;
103
+ if (saveAction === void 0) { saveAction = true; }
104
+ if (callback === void 0) { callback = function () { }; }
105
+ var doMerge = function (img) {
106
+ var scale = (data.width / img.width) * _this.pixelRatioScale;
107
+ _this.drawingLayer.ctx.globalCompositeOperation = "source-over";
108
+ _this.drawingLayer.ctx.save();
109
+ _this.drawingLayer.ctx.translate(data.origin[0] * _this.pixelRatioScale, data.origin[1] * _this.pixelRatioScale);
110
+ _this.drawingLayer.ctx.rotate(data.rotation * Math.PI / 180);
111
+ _this.drawingLayer.ctx.translate(-data.x - img.width * scale / 2, -data.y - img.height * scale / 2);
112
+ _this.drawingLayer.ctx.drawImage(img, data.x, data.y, img.width * scale, img.height * scale);
113
+ _this.drawingLayer.ctx.restore();
114
+ if (saveAction) {
115
+ _this.model.saveMergeImage(data);
116
+ if (_this.onChange) {
117
+ _this.onChange();
118
+ }
119
+ }
120
+ callback();
121
+ };
122
+ if (data.image) {
123
+ doMerge(data.image);
124
+ }
125
+ else if (data.imageSrc) {
126
+ var img_1 = new Image;
127
+ img_1.onload = function () { doMerge(img_1); };
128
+ img_1.src = data.imageSrc;
129
+ }
130
+ };
131
+ Sketch.prototype.export = function (props) {
132
+ if (this.templateLayer) {
133
+ this.templateLayer.drawImageToFit(this.drawingLayer.canvas);
134
+ }
135
+ var layerToExport = this.getLayerToExport();
136
+ var exportCanvas = (props && props.crop) ? trimCanvas(layerToExport.canvas) : layerToExport.canvas;
137
+ return exportCanvas.toDataURL();
138
+ };
139
+ Sketch.prototype.undo = function () {
140
+ if (!this.model.canUndo())
141
+ return;
142
+ this.model.lastActionIndex -= 1;
143
+ this.drawingLayer.clear();
144
+ console.log('Last action Index before undo', this.model.lastActionIndex);
145
+ for (var i = 0; i <= this.model.lastActionIndex; i += 1) {
146
+ console.log('I=', i);
147
+ if (this.model.actions[i].type === 'STROKE' && this.model.actions[i].model) {
148
+ console.log('draw stroke');
149
+ this.drawUndoStroke(this.model.actions[i].model);
150
+ }
151
+ else if (this.model.actions[i].type === 'IMAGE_MERGE' && this.model.actions[i]) {
152
+ console.log('merge image');
153
+ this.mergeImage(this.model.actions[i].data, false);
154
+ }
155
+ }
156
+ if (this.onChange)
157
+ this.onChange();
158
+ };
159
+ Sketch.prototype.redo = function () {
160
+ if (!this.model.canRedo())
161
+ return;
162
+ this.model.lastActionIndex += 1;
163
+ var action = this.model.actions[this.model.lastActionIndex];
164
+ if (action.type === 'STROKE' && action.model) {
165
+ this.drawUndoStroke(action.model);
166
+ }
167
+ else if (action.type === 'IMAGE_MERGE' && action.data) {
168
+ this.mergeImage(action.data, false);
169
+ }
170
+ if (this.onChange)
171
+ this.onChange();
172
+ };
173
+ Sketch.prototype.canUndo = function () {
174
+ return this.model.canUndo();
175
+ };
176
+ Sketch.prototype.canRedo = function () {
177
+ return this.model.canRedo();
178
+ };
179
+ Sketch.prototype.restart = function () {
180
+ this.model.reset();
181
+ this.drawingLayer.clear();
182
+ if (this.onChange) {
183
+ this.onChange();
184
+ }
185
+ };
186
+ Sketch.prototype.serialize = function () {
187
+ return this.model.serialize();
188
+ };
189
+ Sketch.prototype.deserialize = function (serialized) {
190
+ this.model.deserialize(serialized);
191
+ };
192
+ Sketch.prototype.getNumberOfActions = function () {
193
+ return this.model.lastActionIndex + 1;
194
+ };
195
+ Sketch.prototype.initDrawAnimations = function () {
196
+ this.drawUndo = {
197
+ transparentEraserFill: this.drawTransparentFillFinal,
198
+ transparentEraserStroke: this.drawStrokeFinal,
199
+ transparentColourFill: this.drawTransparentFillFinal,
200
+ transparentColourStroke: this.drawStrokeFinal,
201
+ opaqueEraserFill: this.drawEraserUndoingFillFinal,
202
+ opaqueEraserStroke: this.drawEraserUndoingFinal,
203
+ opaqueColourFill: this.drawFillFinal,
204
+ opaqueColourStroke: this.drawStrokeFinal
205
+ };
206
+ this.drawFinished = {
207
+ transparentEraserFill: this.drawTransparentFillFinal,
208
+ transparentEraserStroke: this.drawStrokeFinal,
209
+ transparentColourFill: this.drawTransparentFillFinal,
210
+ transparentColourStroke: this.drawStrokeFinal,
211
+ opaqueEraserFill: this.drawEraserFillFinal,
212
+ opaqueEraserStroke: this.drawEraser,
213
+ opaqueColourFill: this.drawFillFinal,
214
+ opaqueColourStroke: this.drawStrokeFinal
215
+ };
216
+ this.drawAnimation = {
217
+ transparentEraserFill: this.drawFillAndStroke,
218
+ transparentEraserStroke: this.drawFillAndStroke,
219
+ transparentColourFill: this.drawFillAndStroke,
220
+ transparentColourStroke: this.drawFillAndStroke,
221
+ opaqueEraserFill: this.drawEraser,
222
+ opaqueEraserStroke: this.drawEraser,
223
+ opaqueColourFill: this.drawFillAndStroke,
224
+ opaqueColourStroke: this.drawFillAndStroke
225
+ };
226
+ };
227
+ Sketch.prototype.startDraw = function (point) {
228
+ this.isDrawing = true;
229
+ this.model.initStroke(point);
230
+ if (this.model.currentStroke && this.model.currentStroke.style && this.cacheLayer.ctx && this.drawingLayer.ctx) {
231
+ if (this.model.currentStroke.style.eraser && this.model.currentStroke.style.opacity === 1.0) {
232
+ this.cacheLayer.ctx.globalCompositeOperation = "copy";
233
+ this.cacheLayer.ctx.drawImage(this.drawingLayer.ctx.canvas, 0, 0, this.width, this.height);
234
+ setDrawingStyle(this.model.currentStroke.style, this.drawingLayer.ctx);
235
+ }
236
+ }
237
+ if (this.model.currentStroke && this.bufferLayer.ctx) {
238
+ setDrawingStyle(this.model.currentStroke.style, this.bufferLayer.ctx);
239
+ }
240
+ this.strokeAnimation();
241
+ };
242
+ Sketch.prototype.continueDraw = function (point) {
243
+ if (!this.isDrawing)
244
+ return false;
245
+ this.model.continueStroke(point);
246
+ return false;
247
+ };
248
+ Sketch.prototype.endDraw = function () {
249
+ if (!(this.model.currentStroke && this.model.currentStroke.style))
250
+ return;
251
+ this.isDrawing = false;
252
+ this.endStrokeAnimation();
253
+ this.bufferLayer.clear();
254
+ this.drawFinishedStroke(this.model.currentStroke);
255
+ this.model.saveStroke();
256
+ if (this.onChange) {
257
+ this.onChange();
258
+ }
259
+ };
260
+ Sketch.prototype.drawExistingSketch = function (callback) {
261
+ if (this.model.lastActionIndex === -1) {
262
+ if (callback)
263
+ callback();
264
+ return;
265
+ }
266
+ var done = after__default['default'](Math.min(this.model.lastActionIndex, this.model.actions.length), function () {
267
+ if (callback)
268
+ callback();
269
+ });
270
+ for (var i = 0; i <= this.model.lastActionIndex; i += 1) {
271
+ this.drawAction(this.model.actions[i], done);
272
+ }
273
+ };
274
+ Sketch.prototype.drawAction = function (action, callback) {
275
+ if (callback === void 0) { callback = function () { }; }
276
+ if (action.type === 'STROKE' && action.model) {
277
+ this.drawExistingStroke(action.model);
278
+ callback();
279
+ }
280
+ else if (action.type === 'IMAGE_MERGE') {
281
+ this.mergeImage(action.data, false, callback);
282
+ }
283
+ };
284
+ Sketch.prototype.strokeAnimation = function () {
285
+ var _this = this;
286
+ this.drawAnimationStroke(this.model.currentStroke);
287
+ this.reqStroke = window.requestAnimationFrame(function () { return _this.strokeAnimation.apply(_this); });
288
+ };
289
+ Sketch.prototype.drawAnimationStroke = function (stroke) {
290
+ this.drawAnimation[stroke.style.type].apply(this, [stroke]);
291
+ };
292
+ Sketch.prototype.endStrokeAnimation = function () {
293
+ window.cancelAnimationFrame(this.reqStroke);
294
+ this.reqStroke = 0;
295
+ };
296
+ Sketch.prototype.drawUndoStroke = function (stroke) {
297
+ setDrawingStyle(stroke.style, this.drawingLayer.ctx);
298
+ this.drawUndo[stroke.style.type].apply(this, [stroke]);
299
+ };
300
+ Sketch.prototype.drawFinishedStroke = function (stroke) {
301
+ setDrawingStyle(stroke.style, this.drawingLayer.ctx);
302
+ this.drawFinished[stroke.style.type].apply(this, [stroke]);
303
+ };
304
+ Sketch.prototype.drawExistingStroke = function (stroke) {
305
+ setDrawingStyle(stroke.style, this.drawingLayer.ctx);
306
+ this.drawFinished[stroke.style.type].apply(this, [stroke, false]);
307
+ };
308
+ Sketch.prototype.drawTransparentFillFinal = function (stroke) {
309
+ this.cacheLayer.clear();
310
+ this.cacheLayer.ctx.globalCompositeOperation = "source-over";
311
+ setDrawingStyle(tslib.__assign(tslib.__assign({}, stroke.style), { opacity: 1 }), this.cacheLayer.ctx);
312
+ trace(stroke, this.cacheLayer.ctx);
313
+ this.cacheLayer.ctx.closePath();
314
+ this.cacheLayer.ctx.stroke();
315
+ this.cacheLayer.ctx.fill();
316
+ this.drawingLayer.ctx.globalCompositeOperation = "source-over";
317
+ this.drawingLayer.ctx.globalAlpha = stroke.style.opacity;
318
+ this.drawingLayer.ctx.drawImage(this.cacheLayer.ctx.canvas, 0, 0, this.width, this.height);
319
+ this.drawingLayer.ctx.globalAlpha = 1.0;
320
+ };
321
+ Sketch.prototype.drawStrokeFinal = function (stroke) {
322
+ this.drawingLayer.ctx.globalCompositeOperation = "source-over";
323
+ trace(stroke, this.drawingLayer.ctx);
324
+ this.drawingLayer.ctx.stroke();
325
+ };
326
+ Sketch.prototype.drawFillFinal = function (stroke) {
327
+ this.drawingLayer.ctx.globalCompositeOperation = "source-over";
328
+ trace(stroke, this.drawingLayer.ctx);
329
+ this.drawingLayer.ctx.closePath();
330
+ this.drawingLayer.ctx.fill();
331
+ this.drawingLayer.ctx.stroke();
332
+ };
333
+ Sketch.prototype.drawFillAndStroke = function (stroke) {
334
+ this.bufferLayer.clear();
335
+ trace(stroke, this.bufferLayer.ctx);
336
+ setDrawingStyle(tslib.__assign(tslib.__assign({}, stroke.style), { opacity: 1 }), this.bufferLayer.ctx);
337
+ this.bufferLayer.ctx.globalAlpha = stroke.style.opacity;
338
+ if (this.model.fill) {
339
+ this.bufferLayer.ctx.closePath();
340
+ this.bufferLayer.ctx.fill();
341
+ }
342
+ this.bufferLayer.ctx.stroke();
343
+ };
344
+ Sketch.prototype.drawEraserUndoingFinal = function (stroke) {
345
+ this.drawingLayer.ctx.globalCompositeOperation = "destination-out";
346
+ trace(stroke, this.drawingLayer.ctx);
347
+ this.drawingLayer.ctx.stroke();
348
+ };
349
+ Sketch.prototype.drawEraserUndoingFillFinal = function (stroke) {
350
+ this.drawingLayer.ctx.globalCompositeOperation = "destination-out";
351
+ trace(stroke, this.drawingLayer.ctx);
352
+ this.drawingLayer.ctx.closePath();
353
+ this.drawingLayer.ctx.fill();
354
+ this.drawingLayer.ctx.stroke();
355
+ };
356
+ Sketch.prototype.drawEraser = function (stroke, copy) {
357
+ if (copy === void 0) { copy = true; }
358
+ if (copy) {
359
+ this.drawingLayer.ctx.globalCompositeOperation = "copy";
360
+ this.drawingLayer.ctx.drawImage(this.cacheLayer.ctx.canvas, 0, 0, this.width, this.height);
361
+ }
362
+ this.drawingLayer.ctx.globalCompositeOperation = "destination-out";
363
+ trace(stroke, this.drawingLayer.ctx);
364
+ this.drawingLayer.ctx.stroke();
365
+ };
366
+ Sketch.prototype.drawEraserFillFinal = function (stroke, copy) {
367
+ if (copy === void 0) { copy = true; }
368
+ if (copy) {
369
+ this.drawingLayer.ctx.globalCompositeOperation = "copy";
370
+ this.drawingLayer.ctx.drawImage(this.cacheLayer.ctx.canvas, 0, 0, this.width, this.height);
371
+ }
372
+ this.drawingLayer.ctx.globalCompositeOperation = "destination-out";
373
+ trace(stroke, this.drawingLayer.ctx);
374
+ this.drawingLayer.ctx.closePath();
375
+ this.drawingLayer.ctx.fill();
376
+ this.drawingLayer.ctx.stroke();
377
+ };
378
+ Sketch.prototype.findBoundingBox = function (ctx) {
379
+ var imageData = ctx.getImageData(0, 0, this.width, this.height);
380
+ var box = {
381
+ topLeftX: this.width,
382
+ topLeftY: this.height,
383
+ bottomRightX: 0,
384
+ bottomRightY: 0,
385
+ width: 0,
386
+ height: 0
387
+ };
388
+ for (var x = 0; x < this.width; x += 1) {
389
+ for (var y = 0; y < this.height; y += 1) {
390
+ var pixelPosition = (((y * this.width) + x) * 4) + 3;
391
+ if (imageData.data[pixelPosition] > 0) {
392
+ if (x < box.topLeftX)
393
+ box.topLeftX = x;
394
+ if (y < box.topLeftY)
395
+ box.topLeftY = y;
396
+ if (x > box.bottomRightX)
397
+ box.bottomRightX = x;
398
+ if (y > box.bottomRightY)
399
+ box.bottomRightY = y;
400
+ }
401
+ }
402
+ }
403
+ box.width = box.bottomRightX - box.topLeftX;
404
+ box.height = box.bottomRightY - box.topLeftY;
405
+ return box;
406
+ };
407
+ Sketch.prototype.getLayerToExport = function () {
408
+ return this.templateLayer || this.drawingLayer;
409
+ };
410
+ return Sketch;
411
+ }());
412
+
413
+ module.exports = Sketch;
414
+ //# sourceMappingURL=Sketch.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Sketch.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,22 @@
1
+ import Sketch, { SketchProps } from "./Sketch";
2
+ import SketchCutModel from "./SketchCutModel";
3
+ import SketchLayer from "./SketchLayer";
4
+ import SketchStrokeModel from "./SketchStrokeModel";
5
+ export interface SketchCutProps extends SketchProps {
6
+ imageToCut: HTMLImageElement;
7
+ onImageCut?: () => void;
8
+ }
9
+ export default class SketchCut extends Sketch {
10
+ readonly model: SketchCutModel;
11
+ readonly imageToCut: HTMLImageElement;
12
+ cutLayer: SketchLayer;
13
+ uiLayer: SketchLayer;
14
+ onImageCut?: () => void;
15
+ constructor(props: SketchCutProps);
16
+ initDrawAnimations(): void;
17
+ drawCutStroke(stroke: any): void;
18
+ drawCutFinal(stroke: any): void;
19
+ resetCut(): void;
20
+ drawOutline(stroke: SketchStrokeModel): void;
21
+ getLayerToExport(): SketchLayer;
22
+ }