react-scratchingcard 1.0.0 → 1.0.1

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.
@@ -25,6 +25,7 @@ var Scratch = /*#__PURE__*/function (_Component) {
25
25
  _this = _Component.call(this, props) || this;
26
26
  _this.isDrawing = false;
27
27
  _this.lastPoint = null;
28
+ _this.objectUrl = null;
28
29
  _this.isFinished = false;
29
30
 
30
31
  _this.reset = function () {
@@ -90,24 +91,75 @@ var Scratch = /*#__PURE__*/function (_Component) {
90
91
 
91
92
  var _proto = Scratch.prototype;
92
93
 
93
- _proto.componentDidMount = function componentDidMount() {
94
+ _proto.componentDidUpdate = function componentDidUpdate(prevProps) {
95
+ if (prevProps.image !== this.props.image) {
96
+ this.loadImage();
97
+ }
98
+ };
99
+
100
+ _proto.componentWillUnmount = function componentWillUnmount() {
101
+ this.revokeObjectUrl();
102
+ };
103
+
104
+ _proto.revokeObjectUrl = function revokeObjectUrl() {
105
+ if (this.objectUrl) {
106
+ URL.revokeObjectURL(this.objectUrl);
107
+ this.objectUrl = null;
108
+ }
109
+ };
110
+
111
+ _proto.resolveImageSource = function resolveImageSource(image) {
112
+ this.revokeObjectUrl();
113
+
114
+ if (typeof image === 'string') {
115
+ return image;
116
+ }
117
+
118
+ if (image instanceof File) {
119
+ this.objectUrl = URL.createObjectURL(image);
120
+ return this.objectUrl;
121
+ }
122
+
123
+ if (image && typeof image.src === 'string') {
124
+ return image.src;
125
+ }
126
+
127
+ if (image && typeof image["default"] === 'string') {
128
+ return image["default"];
129
+ }
130
+
131
+ return '';
132
+ };
133
+
134
+ _proto.loadImage = function loadImage() {
94
135
  var _this2 = this;
95
136
 
96
- this.isDrawing = false;
97
- this.lastPoint = null;
98
- this.ctx = this.canvas.getContext('2d');
99
137
  this.image = new Image();
100
- this.image.crossOrigin = 'Anonymous';
138
+ this.image.crossOrigin = this.props.imageCrossOrigin === undefined ? 'anonymous' : this.props.imageCrossOrigin;
101
139
 
102
140
  this.image.onload = function () {
141
+ _this2.ctx.globalCompositeOperation = 'source-over';
142
+
143
+ _this2.ctx.clearRect(0, 0, _this2.props.width, _this2.props.height);
144
+
103
145
  _this2.ctx.drawImage(_this2.image, 0, 0, _this2.props.width, _this2.props.height);
104
146
 
147
+ _this2.isFinished = false;
148
+
105
149
  _this2.setState({
106
- loaded: true
150
+ loaded: true,
151
+ finished: false
107
152
  });
108
153
  };
109
154
 
110
- this.image.src = this.props.image;
155
+ this.image.src = this.resolveImageSource(this.props.image);
156
+ };
157
+
158
+ _proto.componentDidMount = function componentDidMount() {
159
+ this.isDrawing = false;
160
+ this.lastPoint = null;
161
+ this.ctx = this.canvas.getContext('2d');
162
+ this.loadImage();
111
163
 
112
164
  if (this.props.customBrush) {
113
165
  this.brushImage = new Image(this.props.customBrush.width, this.props.customBrush.height);