react-scratchingcard 1.0.0 → 1.0.2
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 +21 -3
- package/dist/index.d.ts +23 -2
- package/dist/index.js +123 -20
- package/dist/index.js.map +1 -1
- package/dist/index.modern.js +123 -20
- package/dist/index.modern.js.map +1 -1
- package/package.json +1 -1
package/dist/index.modern.js
CHANGED
|
@@ -16,6 +16,8 @@ function _setPrototypeOf(o, p) {
|
|
|
16
16
|
return _setPrototypeOf(o, p);
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
+
var SCRATCH_GRID_SIZE = 4;
|
|
20
|
+
|
|
19
21
|
var Scratch = /*#__PURE__*/function (_Component) {
|
|
20
22
|
_inheritsLoose(Scratch, _Component);
|
|
21
23
|
|
|
@@ -25,6 +27,9 @@ var Scratch = /*#__PURE__*/function (_Component) {
|
|
|
25
27
|
_this = _Component.call(this, props) || this;
|
|
26
28
|
_this.isDrawing = false;
|
|
27
29
|
_this.lastPoint = null;
|
|
30
|
+
_this.objectUrl = null;
|
|
31
|
+
_this.scratchedCells = new Set();
|
|
32
|
+
_this.scratchableCellCount = 0;
|
|
28
33
|
_this.isFinished = false;
|
|
29
34
|
|
|
30
35
|
_this.reset = function () {
|
|
@@ -33,7 +38,13 @@ var Scratch = /*#__PURE__*/function (_Component) {
|
|
|
33
38
|
|
|
34
39
|
_this.ctx.drawImage(_this.image, 0, 0, _this.props.width, _this.props.height);
|
|
35
40
|
|
|
41
|
+
_this.initializeScratchGrid();
|
|
42
|
+
|
|
36
43
|
_this.isFinished = false;
|
|
44
|
+
|
|
45
|
+
_this.setState({
|
|
46
|
+
finished: false
|
|
47
|
+
});
|
|
37
48
|
};
|
|
38
49
|
|
|
39
50
|
_this.handleMouseDown = function (e) {
|
|
@@ -63,18 +74,24 @@ var Scratch = /*#__PURE__*/function (_Component) {
|
|
|
63
74
|
|
|
64
75
|
if (_this.brushImage && _this.props.customBrush) {
|
|
65
76
|
_this.ctx.drawImage(_this.brushImage, x, y, _this.props.customBrush.width, _this.props.customBrush.height);
|
|
77
|
+
|
|
78
|
+
_this.markScratchArea(x, y, _this.props.customBrush.width, _this.props.customBrush.height);
|
|
66
79
|
} else {
|
|
67
80
|
_this.ctx.beginPath();
|
|
68
81
|
|
|
69
82
|
_this.ctx.arc(x, y, _this.props.brushSize || 20, 0, 2 * Math.PI, false);
|
|
70
83
|
|
|
71
84
|
_this.ctx.fill();
|
|
85
|
+
|
|
86
|
+
var radius = _this.props.brushSize || 20;
|
|
87
|
+
|
|
88
|
+
_this.markScratchArea(x - radius, y - radius, radius * 2, radius * 2);
|
|
72
89
|
}
|
|
73
90
|
}
|
|
74
91
|
|
|
75
92
|
_this.lastPoint = currentPoint;
|
|
76
93
|
|
|
77
|
-
_this.handlePercentage(_this.
|
|
94
|
+
_this.handlePercentage(_this.getFilledPercentage());
|
|
78
95
|
};
|
|
79
96
|
|
|
80
97
|
_this.handleMouseUp = function () {
|
|
@@ -90,24 +107,80 @@ var Scratch = /*#__PURE__*/function (_Component) {
|
|
|
90
107
|
|
|
91
108
|
var _proto = Scratch.prototype;
|
|
92
109
|
|
|
93
|
-
_proto.
|
|
110
|
+
_proto.componentDidUpdate = function componentDidUpdate(prevProps) {
|
|
111
|
+
if (prevProps.image !== this.props.image) {
|
|
112
|
+
this.loadImage();
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
_proto.componentWillUnmount = function componentWillUnmount() {
|
|
117
|
+
this.revokeObjectUrl();
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
_proto.revokeObjectUrl = function revokeObjectUrl() {
|
|
121
|
+
if (this.objectUrl) {
|
|
122
|
+
URL.revokeObjectURL(this.objectUrl);
|
|
123
|
+
this.objectUrl = null;
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
_proto.resolveImageSource = function resolveImageSource(image) {
|
|
128
|
+
this.revokeObjectUrl();
|
|
129
|
+
|
|
130
|
+
if (typeof image === 'string') {
|
|
131
|
+
return image;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (image instanceof File) {
|
|
135
|
+
this.objectUrl = URL.createObjectURL(image);
|
|
136
|
+
return this.objectUrl;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (image && typeof image.src === 'string') {
|
|
140
|
+
return image.src;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (image && typeof image["default"] === 'string') {
|
|
144
|
+
return image["default"];
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return '';
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
_proto.loadImage = function loadImage() {
|
|
94
151
|
var _this2 = this;
|
|
95
152
|
|
|
96
|
-
this.isDrawing = false;
|
|
97
|
-
this.lastPoint = null;
|
|
98
|
-
this.ctx = this.canvas.getContext('2d');
|
|
99
153
|
this.image = new Image();
|
|
100
|
-
|
|
154
|
+
|
|
155
|
+
if (this.props.imageCrossOrigin !== undefined) {
|
|
156
|
+
this.image.crossOrigin = this.props.imageCrossOrigin;
|
|
157
|
+
}
|
|
101
158
|
|
|
102
159
|
this.image.onload = function () {
|
|
160
|
+
_this2.ctx.globalCompositeOperation = 'source-over';
|
|
161
|
+
|
|
162
|
+
_this2.ctx.clearRect(0, 0, _this2.props.width, _this2.props.height);
|
|
163
|
+
|
|
103
164
|
_this2.ctx.drawImage(_this2.image, 0, 0, _this2.props.width, _this2.props.height);
|
|
104
165
|
|
|
166
|
+
_this2.initializeScratchGrid();
|
|
167
|
+
|
|
168
|
+
_this2.isFinished = false;
|
|
169
|
+
|
|
105
170
|
_this2.setState({
|
|
106
|
-
loaded: true
|
|
171
|
+
loaded: true,
|
|
172
|
+
finished: false
|
|
107
173
|
});
|
|
108
174
|
};
|
|
109
175
|
|
|
110
|
-
this.image.src = this.props.image;
|
|
176
|
+
this.image.src = this.resolveImageSource(this.props.image);
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
_proto.componentDidMount = function componentDidMount() {
|
|
180
|
+
this.isDrawing = false;
|
|
181
|
+
this.lastPoint = null;
|
|
182
|
+
this.ctx = this.canvas.getContext('2d');
|
|
183
|
+
this.loadImage();
|
|
111
184
|
|
|
112
185
|
if (this.props.customBrush) {
|
|
113
186
|
this.brushImage = new Image(this.props.customBrush.width, this.props.customBrush.height);
|
|
@@ -115,11 +188,7 @@ var Scratch = /*#__PURE__*/function (_Component) {
|
|
|
115
188
|
}
|
|
116
189
|
};
|
|
117
190
|
|
|
118
|
-
_proto.
|
|
119
|
-
if (!stride || stride < 1) {
|
|
120
|
-
stride = 1;
|
|
121
|
-
}
|
|
122
|
-
|
|
191
|
+
_proto.getCheckZone = function getCheckZone() {
|
|
123
192
|
var x = 0;
|
|
124
193
|
var y = 0;
|
|
125
194
|
var width = this.canvas.width;
|
|
@@ -132,17 +201,51 @@ var Scratch = /*#__PURE__*/function (_Component) {
|
|
|
132
201
|
height = this.props.customCheckZone.height;
|
|
133
202
|
}
|
|
134
203
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
204
|
+
return {
|
|
205
|
+
x: x,
|
|
206
|
+
y: y,
|
|
207
|
+
width: width,
|
|
208
|
+
height: height
|
|
209
|
+
};
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
_proto.initializeScratchGrid = function initializeScratchGrid() {
|
|
213
|
+
var zone = this.getCheckZone();
|
|
214
|
+
var columns = Math.ceil(zone.width / SCRATCH_GRID_SIZE);
|
|
215
|
+
var rows = Math.ceil(zone.height / SCRATCH_GRID_SIZE);
|
|
216
|
+
this.scratchedCells = new Set();
|
|
217
|
+
this.scratchableCellCount = columns * rows;
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
_proto.markScratchArea = function markScratchArea(x, y, width, height) {
|
|
221
|
+
var zone = this.getCheckZone();
|
|
222
|
+
var startX = Math.max(zone.x, x);
|
|
223
|
+
var startY = Math.max(zone.y, y);
|
|
224
|
+
var endX = Math.min(zone.x + zone.width, x + width);
|
|
225
|
+
var endY = Math.min(zone.y + zone.height, y + height);
|
|
138
226
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
227
|
+
if (startX >= endX || startY >= endY) {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
var startColumn = Math.floor((startX - zone.x) / SCRATCH_GRID_SIZE);
|
|
232
|
+
var endColumn = Math.floor((endX - zone.x - 1) / SCRATCH_GRID_SIZE);
|
|
233
|
+
var startRow = Math.floor((startY - zone.y) / SCRATCH_GRID_SIZE);
|
|
234
|
+
var endRow = Math.floor((endY - zone.y - 1) / SCRATCH_GRID_SIZE);
|
|
235
|
+
|
|
236
|
+
for (var row = startRow; row <= endRow; row++) {
|
|
237
|
+
for (var column = startColumn; column <= endColumn; column++) {
|
|
238
|
+
this.scratchedCells.add(column + ":" + row);
|
|
142
239
|
}
|
|
143
240
|
}
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
_proto.getFilledPercentage = function getFilledPercentage() {
|
|
244
|
+
if (this.scratchableCellCount === 0) {
|
|
245
|
+
return 0;
|
|
246
|
+
}
|
|
144
247
|
|
|
145
|
-
return Math.round(
|
|
248
|
+
return Math.round(this.scratchedCells.size / this.scratchableCellCount * 100);
|
|
146
249
|
};
|
|
147
250
|
|
|
148
251
|
_proto.getMouse = function getMouse(e, canvas) {
|