react-scratchingcard 1.0.1 → 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.
@@ -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
 
@@ -26,6 +28,8 @@ var Scratch = /*#__PURE__*/function (_Component) {
26
28
  _this.isDrawing = false;
27
29
  _this.lastPoint = null;
28
30
  _this.objectUrl = null;
31
+ _this.scratchedCells = new Set();
32
+ _this.scratchableCellCount = 0;
29
33
  _this.isFinished = false;
30
34
 
31
35
  _this.reset = function () {
@@ -34,7 +38,13 @@ var Scratch = /*#__PURE__*/function (_Component) {
34
38
 
35
39
  _this.ctx.drawImage(_this.image, 0, 0, _this.props.width, _this.props.height);
36
40
 
41
+ _this.initializeScratchGrid();
42
+
37
43
  _this.isFinished = false;
44
+
45
+ _this.setState({
46
+ finished: false
47
+ });
38
48
  };
39
49
 
40
50
  _this.handleMouseDown = function (e) {
@@ -64,18 +74,24 @@ var Scratch = /*#__PURE__*/function (_Component) {
64
74
 
65
75
  if (_this.brushImage && _this.props.customBrush) {
66
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);
67
79
  } else {
68
80
  _this.ctx.beginPath();
69
81
 
70
82
  _this.ctx.arc(x, y, _this.props.brushSize || 20, 0, 2 * Math.PI, false);
71
83
 
72
84
  _this.ctx.fill();
85
+
86
+ var radius = _this.props.brushSize || 20;
87
+
88
+ _this.markScratchArea(x - radius, y - radius, radius * 2, radius * 2);
73
89
  }
74
90
  }
75
91
 
76
92
  _this.lastPoint = currentPoint;
77
93
 
78
- _this.handlePercentage(_this.getFilledInPixels(32));
94
+ _this.handlePercentage(_this.getFilledPercentage());
79
95
  };
80
96
 
81
97
  _this.handleMouseUp = function () {
@@ -135,7 +151,10 @@ var Scratch = /*#__PURE__*/function (_Component) {
135
151
  var _this2 = this;
136
152
 
137
153
  this.image = new Image();
138
- this.image.crossOrigin = this.props.imageCrossOrigin === undefined ? 'anonymous' : this.props.imageCrossOrigin;
154
+
155
+ if (this.props.imageCrossOrigin !== undefined) {
156
+ this.image.crossOrigin = this.props.imageCrossOrigin;
157
+ }
139
158
 
140
159
  this.image.onload = function () {
141
160
  _this2.ctx.globalCompositeOperation = 'source-over';
@@ -144,6 +163,8 @@ var Scratch = /*#__PURE__*/function (_Component) {
144
163
 
145
164
  _this2.ctx.drawImage(_this2.image, 0, 0, _this2.props.width, _this2.props.height);
146
165
 
166
+ _this2.initializeScratchGrid();
167
+
147
168
  _this2.isFinished = false;
148
169
 
149
170
  _this2.setState({
@@ -167,11 +188,7 @@ var Scratch = /*#__PURE__*/function (_Component) {
167
188
  }
168
189
  };
169
190
 
170
- _proto.getFilledInPixels = function getFilledInPixels(stride) {
171
- if (!stride || stride < 1) {
172
- stride = 1;
173
- }
174
-
191
+ _proto.getCheckZone = function getCheckZone() {
175
192
  var x = 0;
176
193
  var y = 0;
177
194
  var width = this.canvas.width;
@@ -184,17 +201,51 @@ var Scratch = /*#__PURE__*/function (_Component) {
184
201
  height = this.props.customCheckZone.height;
185
202
  }
186
203
 
187
- var pixels = this.ctx.getImageData(x, y, width, height);
188
- var total = pixels.data.length / stride;
189
- var count = 0;
204
+ return {
205
+ x: x,
206
+ y: y,
207
+ width: width,
208
+ height: height
209
+ };
210
+ };
190
211
 
191
- for (var i = 0; i < pixels.data.length; i += stride) {
192
- if (parseInt(pixels.data[i], 10) === 0) {
193
- count++;
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);
226
+
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);
194
239
  }
195
240
  }
241
+ };
242
+
243
+ _proto.getFilledPercentage = function getFilledPercentage() {
244
+ if (this.scratchableCellCount === 0) {
245
+ return 0;
246
+ }
196
247
 
197
- return Math.round(count / total * 100);
248
+ return Math.round(this.scratchedCells.size / this.scratchableCellCount * 100);
198
249
  };
199
250
 
200
251
  _proto.getMouse = function getMouse(e, canvas) {