react-scratchingcard 1.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Dopey2
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,160 @@
1
+ # react-scratchingcard
2
+
3
+ > A scratchcard component for React
4
+
5
+ [![NPM](https://img.shields.io/npm/v/react-scratchingcard.svg)](https://www.npmjs.com/package/react-scratchingcard) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
6
+
7
+
8
+ ##### Original repo by Aleksik (not maintained)
9
+ https://github.com/aleksik/react-scratchcard
10
+
11
+ ##### Improvement ✨
12
+ - Resize the image using width and height props (in the original repo, the image was croped)
13
+ - Smooth fade out animation on scratch complete
14
+ - Add type definition (ts)
15
+ - Change brush size through props
16
+ - Use custom brush through props
17
+ - Define a custom check zone through props
18
+
19
+ ## Demo
20
+
21
+ ![scratchcard-demo](https://user-images.githubusercontent.com/22329040/140519100-b6ee86e3-0009-4ab6-bcd0-c7fefdb8720d.gif)
22
+
23
+ ## Install
24
+
25
+ ```bash
26
+ npm install --save react-scratchingcard
27
+ ```
28
+ or
29
+ ```bash
30
+ yarn add react-scratchingcard
31
+ ```
32
+
33
+
34
+ ## Usage
35
+
36
+ ```tsx
37
+ import React, { useRef } from 'react';
38
+ import ScratchCard from 'react-scratchcard-v2';
39
+
40
+ import * as IMG from './img.jpg';
41
+
42
+ const App = () => {
43
+
44
+ const ref = useRef<ScratchCard>(null);
45
+
46
+ const onClickReset = () => {
47
+ ref.current && ref.current.reset();
48
+ }
49
+
50
+ return (
51
+ <div>
52
+ <button onClick={onClickReset}>Reset</button>
53
+ <ScratchCard
54
+ width={320}
55
+ height={226}
56
+ image={IMG}
57
+ finishPercent={80}
58
+ onComplete={() => console.log('complete')}
59
+ >
60
+ <div style={{
61
+ display: 'flex',
62
+ width: '100%',
63
+ height: '100%',
64
+ alignItems: 'center',
65
+ justifyContent: 'center'
66
+ }}
67
+ >
68
+ <h1>Scratch card</h1>
69
+ </div>
70
+ </ScratchCard>
71
+ </div>
72
+ );
73
+ };
74
+ ```
75
+
76
+ ### Custom brush
77
+
78
+ ```tsx
79
+ const App = () => {
80
+ return (
81
+ <div>
82
+ <ScratchCard
83
+ width={320}
84
+ height={226}
85
+ image={IMG}
86
+ finishPercent={80}
87
+ customBrush={{
88
+ image: require('./brush.img'),
89
+ width: 15,
90
+ height: 15
91
+ }}
92
+ >
93
+ <h1>Scratch card</h1>
94
+ </ScratchCard>
95
+ </div>
96
+ );
97
+ };
98
+ ```
99
+
100
+ or you can use CUSTOM_BRUSH_PRESET object
101
+
102
+ ```tsx
103
+ import { CUSTOM_BRUSH_PRESET } from 'react-scratchcard-v2';
104
+
105
+ const App = () => {
106
+ return (
107
+ <div>
108
+ <ScratchCard
109
+ width={320}
110
+ height={226}
111
+ image={IMG}
112
+ finishPercent={80}
113
+ customBrush={CUSTOM_BRUSH_PRESET}
114
+ >
115
+ <h1>Scratch card</h1>
116
+ </ScratchCard>
117
+ </div>
118
+ )
119
+ }
120
+ ```
121
+
122
+
123
+ ## Type
124
+
125
+ ### Props
126
+
127
+ | **name** | **type** | **default** |
128
+ |-------------------|-----------------|-------------|
129
+ | width | number | |
130
+ | height | number | |
131
+ | image | File or Base64 | |
132
+ | finishPercent | ?number | 70 |
133
+ | brushSize | ?number | 20 |
134
+ | fadeOutOnComplete | ?boolean | true |
135
+ | onComplete | ?callback | |
136
+ | customBrush | ?CustomBrush | |
137
+ | customCheckZone | ?CustomCheckZone| |
138
+
139
+ ### CustomBrush
140
+
141
+ | **name** | **type** |
142
+ |----------|----------------|
143
+ | width | number |
144
+ | height | number |
145
+ | image | File or Base64 |
146
+
147
+ ### CustomCheckZone
148
+
149
+ | **name** | **type** |
150
+ |----------|----------------|
151
+ | x | number |
152
+ | y | number |
153
+ | width | number |
154
+ | height | number |
155
+
156
+
157
+
158
+ ## License
159
+
160
+ MIT © [exaland](https://github.com/exaland)
@@ -0,0 +1,58 @@
1
+ import React, { Component } from 'react';
2
+ declare type Point = {
3
+ x: number;
4
+ y: number;
5
+ };
6
+ declare type CustomBrush = {
7
+ image: any;
8
+ width: number;
9
+ height: number;
10
+ };
11
+ declare type CustomCheckZone = {
12
+ x: number;
13
+ y: number;
14
+ width: number;
15
+ height: number;
16
+ };
17
+ interface Props {
18
+ width: number;
19
+ height: number;
20
+ image: any;
21
+ finishPercent?: number;
22
+ onComplete?: () => void;
23
+ brushSize?: number;
24
+ fadeOutOnComplete?: boolean;
25
+ children?: any;
26
+ customBrush?: CustomBrush;
27
+ customCheckZone?: CustomCheckZone;
28
+ }
29
+ interface State {
30
+ loaded: boolean;
31
+ finished: boolean;
32
+ }
33
+ declare class Scratch extends Component<Props, State> {
34
+ isDrawing: boolean;
35
+ lastPoint: Point | null;
36
+ ctx: CanvasRenderingContext2D;
37
+ canvas: HTMLCanvasElement;
38
+ brushImage?: any;
39
+ image: HTMLImageElement;
40
+ isFinished: boolean;
41
+ constructor(props: Props);
42
+ componentDidMount(): void;
43
+ reset: () => void;
44
+ getFilledInPixels(stride: number): number;
45
+ getMouse(e: any, canvas: HTMLCanvasElement): {
46
+ x: number;
47
+ y: number;
48
+ };
49
+ distanceBetween(point1: Point | null, point2: Point | null): number;
50
+ angleBetween(point1: Point | null, point2: Point | null): number;
51
+ handlePercentage(filledInPixels?: number): void;
52
+ handleMouseDown: (e: any) => void;
53
+ handleMouseMove: (e: any) => void;
54
+ handleMouseUp: () => void;
55
+ render(): React.JSX.Element;
56
+ }
57
+ export default Scratch;
58
+ export declare const CUSTOM_BRUSH_PRESET: CustomBrush;