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.
- package/README.md +3 -1
- package/dist/index.d.ts +11 -1
- package/dist/index.js +65 -14
- package/dist/index.js.map +1 -1
- package/dist/index.modern.js +65 -14
- package/dist/index.modern.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -129,7 +129,7 @@ const App = () => {
|
|
|
129
129
|
| width | number | |
|
|
130
130
|
| height | number | |
|
|
131
131
|
| image | File \| string \| {src?: string, default?: string} | |
|
|
132
|
-
| imageCrossOrigin | ?'' \| 'anonymous' \| 'use-credentials' |
|
|
132
|
+
| imageCrossOrigin | ?'' \| 'anonymous' \| 'use-credentials' | |
|
|
133
133
|
| finishPercent | ?number | 70 |
|
|
134
134
|
| brushSize | ?number | 20 |
|
|
135
135
|
| fadeOutOnComplete | ?boolean | true |
|
|
@@ -152,6 +152,8 @@ const App = () => {
|
|
|
152
152
|
|
|
153
153
|
Note: pour calculer le pourcentage gratté (`finishPercent`) avec une image distante, le serveur distant doit autoriser CORS.
|
|
154
154
|
|
|
155
|
+
Le composant n'impose plus CORS par defaut pour une URL distante. `imageCrossOrigin` est utile seulement si ton serveur renvoie deja les en-tetes CORS voulus.
|
|
156
|
+
|
|
155
157
|
### CustomBrush
|
|
156
158
|
|
|
157
159
|
| **name** | **type** |
|
package/dist/index.d.ts
CHANGED
|
@@ -43,6 +43,8 @@ declare class Scratch extends Component<Props, State> {
|
|
|
43
43
|
brushImage?: any;
|
|
44
44
|
image: HTMLImageElement;
|
|
45
45
|
objectUrl: string | null;
|
|
46
|
+
scratchedCells: Set<string>;
|
|
47
|
+
scratchableCellCount: number;
|
|
46
48
|
isFinished: boolean;
|
|
47
49
|
constructor(props: Props);
|
|
48
50
|
componentDidUpdate(prevProps: Props): void;
|
|
@@ -52,7 +54,15 @@ declare class Scratch extends Component<Props, State> {
|
|
|
52
54
|
loadImage(): void;
|
|
53
55
|
componentDidMount(): void;
|
|
54
56
|
reset: () => void;
|
|
55
|
-
|
|
57
|
+
getCheckZone(): {
|
|
58
|
+
x: number;
|
|
59
|
+
y: number;
|
|
60
|
+
width: number;
|
|
61
|
+
height: number;
|
|
62
|
+
};
|
|
63
|
+
initializeScratchGrid(): void;
|
|
64
|
+
markScratchArea(x: number, y: number, width: number, height: number): void;
|
|
65
|
+
getFilledPercentage(): number;
|
|
56
66
|
getMouse(e: any, canvas: HTMLCanvasElement): {
|
|
57
67
|
x: number;
|
|
58
68
|
y: number;
|
package/dist/index.js
CHANGED
|
@@ -19,6 +19,8 @@ function _setPrototypeOf(o, p) {
|
|
|
19
19
|
return _setPrototypeOf(o, p);
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
var SCRATCH_GRID_SIZE = 4;
|
|
23
|
+
|
|
22
24
|
var Scratch = /*#__PURE__*/function (_Component) {
|
|
23
25
|
_inheritsLoose(Scratch, _Component);
|
|
24
26
|
|
|
@@ -29,6 +31,8 @@ var Scratch = /*#__PURE__*/function (_Component) {
|
|
|
29
31
|
_this.isDrawing = false;
|
|
30
32
|
_this.lastPoint = null;
|
|
31
33
|
_this.objectUrl = null;
|
|
34
|
+
_this.scratchedCells = new Set();
|
|
35
|
+
_this.scratchableCellCount = 0;
|
|
32
36
|
_this.isFinished = false;
|
|
33
37
|
|
|
34
38
|
_this.reset = function () {
|
|
@@ -37,7 +41,13 @@ var Scratch = /*#__PURE__*/function (_Component) {
|
|
|
37
41
|
|
|
38
42
|
_this.ctx.drawImage(_this.image, 0, 0, _this.props.width, _this.props.height);
|
|
39
43
|
|
|
44
|
+
_this.initializeScratchGrid();
|
|
45
|
+
|
|
40
46
|
_this.isFinished = false;
|
|
47
|
+
|
|
48
|
+
_this.setState({
|
|
49
|
+
finished: false
|
|
50
|
+
});
|
|
41
51
|
};
|
|
42
52
|
|
|
43
53
|
_this.handleMouseDown = function (e) {
|
|
@@ -67,18 +77,24 @@ var Scratch = /*#__PURE__*/function (_Component) {
|
|
|
67
77
|
|
|
68
78
|
if (_this.brushImage && _this.props.customBrush) {
|
|
69
79
|
_this.ctx.drawImage(_this.brushImage, x, y, _this.props.customBrush.width, _this.props.customBrush.height);
|
|
80
|
+
|
|
81
|
+
_this.markScratchArea(x, y, _this.props.customBrush.width, _this.props.customBrush.height);
|
|
70
82
|
} else {
|
|
71
83
|
_this.ctx.beginPath();
|
|
72
84
|
|
|
73
85
|
_this.ctx.arc(x, y, _this.props.brushSize || 20, 0, 2 * Math.PI, false);
|
|
74
86
|
|
|
75
87
|
_this.ctx.fill();
|
|
88
|
+
|
|
89
|
+
var radius = _this.props.brushSize || 20;
|
|
90
|
+
|
|
91
|
+
_this.markScratchArea(x - radius, y - radius, radius * 2, radius * 2);
|
|
76
92
|
}
|
|
77
93
|
}
|
|
78
94
|
|
|
79
95
|
_this.lastPoint = currentPoint;
|
|
80
96
|
|
|
81
|
-
_this.handlePercentage(_this.
|
|
97
|
+
_this.handlePercentage(_this.getFilledPercentage());
|
|
82
98
|
};
|
|
83
99
|
|
|
84
100
|
_this.handleMouseUp = function () {
|
|
@@ -138,7 +154,10 @@ var Scratch = /*#__PURE__*/function (_Component) {
|
|
|
138
154
|
var _this2 = this;
|
|
139
155
|
|
|
140
156
|
this.image = new Image();
|
|
141
|
-
|
|
157
|
+
|
|
158
|
+
if (this.props.imageCrossOrigin !== undefined) {
|
|
159
|
+
this.image.crossOrigin = this.props.imageCrossOrigin;
|
|
160
|
+
}
|
|
142
161
|
|
|
143
162
|
this.image.onload = function () {
|
|
144
163
|
_this2.ctx.globalCompositeOperation = 'source-over';
|
|
@@ -147,6 +166,8 @@ var Scratch = /*#__PURE__*/function (_Component) {
|
|
|
147
166
|
|
|
148
167
|
_this2.ctx.drawImage(_this2.image, 0, 0, _this2.props.width, _this2.props.height);
|
|
149
168
|
|
|
169
|
+
_this2.initializeScratchGrid();
|
|
170
|
+
|
|
150
171
|
_this2.isFinished = false;
|
|
151
172
|
|
|
152
173
|
_this2.setState({
|
|
@@ -170,11 +191,7 @@ var Scratch = /*#__PURE__*/function (_Component) {
|
|
|
170
191
|
}
|
|
171
192
|
};
|
|
172
193
|
|
|
173
|
-
_proto.
|
|
174
|
-
if (!stride || stride < 1) {
|
|
175
|
-
stride = 1;
|
|
176
|
-
}
|
|
177
|
-
|
|
194
|
+
_proto.getCheckZone = function getCheckZone() {
|
|
178
195
|
var x = 0;
|
|
179
196
|
var y = 0;
|
|
180
197
|
var width = this.canvas.width;
|
|
@@ -187,17 +204,51 @@ var Scratch = /*#__PURE__*/function (_Component) {
|
|
|
187
204
|
height = this.props.customCheckZone.height;
|
|
188
205
|
}
|
|
189
206
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
207
|
+
return {
|
|
208
|
+
x: x,
|
|
209
|
+
y: y,
|
|
210
|
+
width: width,
|
|
211
|
+
height: height
|
|
212
|
+
};
|
|
213
|
+
};
|
|
193
214
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
215
|
+
_proto.initializeScratchGrid = function initializeScratchGrid() {
|
|
216
|
+
var zone = this.getCheckZone();
|
|
217
|
+
var columns = Math.ceil(zone.width / SCRATCH_GRID_SIZE);
|
|
218
|
+
var rows = Math.ceil(zone.height / SCRATCH_GRID_SIZE);
|
|
219
|
+
this.scratchedCells = new Set();
|
|
220
|
+
this.scratchableCellCount = columns * rows;
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
_proto.markScratchArea = function markScratchArea(x, y, width, height) {
|
|
224
|
+
var zone = this.getCheckZone();
|
|
225
|
+
var startX = Math.max(zone.x, x);
|
|
226
|
+
var startY = Math.max(zone.y, y);
|
|
227
|
+
var endX = Math.min(zone.x + zone.width, x + width);
|
|
228
|
+
var endY = Math.min(zone.y + zone.height, y + height);
|
|
229
|
+
|
|
230
|
+
if (startX >= endX || startY >= endY) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
var startColumn = Math.floor((startX - zone.x) / SCRATCH_GRID_SIZE);
|
|
235
|
+
var endColumn = Math.floor((endX - zone.x - 1) / SCRATCH_GRID_SIZE);
|
|
236
|
+
var startRow = Math.floor((startY - zone.y) / SCRATCH_GRID_SIZE);
|
|
237
|
+
var endRow = Math.floor((endY - zone.y - 1) / SCRATCH_GRID_SIZE);
|
|
238
|
+
|
|
239
|
+
for (var row = startRow; row <= endRow; row++) {
|
|
240
|
+
for (var column = startColumn; column <= endColumn; column++) {
|
|
241
|
+
this.scratchedCells.add(column + ":" + row);
|
|
197
242
|
}
|
|
198
243
|
}
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
_proto.getFilledPercentage = function getFilledPercentage() {
|
|
247
|
+
if (this.scratchableCellCount === 0) {
|
|
248
|
+
return 0;
|
|
249
|
+
}
|
|
199
250
|
|
|
200
|
-
return Math.round(
|
|
251
|
+
return Math.round(this.scratchedCells.size / this.scratchableCellCount * 100);
|
|
201
252
|
};
|
|
202
253
|
|
|
203
254
|
_proto.getMouse = function getMouse(e, canvas) {
|