Skip to content
This repository was archived by the owner on Aug 1, 2022. It is now read-only.

Commit 308f55b

Browse files
committed
Added a support for refresh the background image
1 parent 1d7652d commit 308f55b

File tree

4 files changed

+52
-6
lines changed

4 files changed

+52
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
/node_modules
66
/umd
77
npm-debug.log*
8+
.vscode/

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ These are the defaultProps of CanvasDraw. You can pass along any of these props
5858
imgSrc: "",
5959
saveData: null,
6060
immediateLoading: false,
61-
hideInterface: false
61+
hideInterface: false,
62+
refreshBackgroundImage: false,
6263
};
6364
```
6465

demo/src/index.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,43 @@ class Demo extends Component {
1010
width: 400,
1111
height: 400,
1212
brushRadius: 10,
13-
lazyRadius: 12
13+
lazyRadius: 12,
14+
backgroundImg: "https://upload.wikimedia.org/wikipedia/commons/a/a1/Nepalese_Mhapuja_Mandala.jpg",
15+
imgs: [
16+
"https://upload.wikimedia.org/wikipedia/commons/a/a1/Nepalese_Mhapuja_Mandala.jpg",
17+
"https://i.imgur.com/a0CGGVC.jpg"
18+
]
1419
};
20+
1521
componentDidMount() {
1622
// let's change the color randomly every 2 seconds. fun!
1723
window.setInterval(() => {
1824
this.setState({
1925
color: "#" + Math.floor(Math.random() * 16777215).toString(16)
2026
});
2127
}, 2000);
28+
29+
// let's change the background image every 15 seconds. fun!
30+
window.setInterval(() => {
31+
if (
32+
this.state.imgs &&
33+
this.state.imgs.length &&
34+
this.state.backgroundImg
35+
) {
36+
let img = '';
37+
let imgs = this.state.imgs;
38+
for (let i = 0; i < imgs.length; i++) {
39+
if (this.state.backgroundImg !== imgs[i]) {
40+
img = imgs[i];
41+
}
42+
}
43+
44+
this.setState({
45+
backgroundImg: img,
46+
});
47+
}
48+
}, 2000);
49+
2250
}
2351
render() {
2452
return (
@@ -69,6 +97,15 @@ class Demo extends Component {
6997
brushColor="rgba(155,12,60,0.3)"
7098
imgSrc="https://upload.wikimedia.org/wikipedia/commons/a/a1/Nepalese_Mhapuja_Mandala.jpg"
7199
/>
100+
101+
<h2>Refreshable Background Image</h2>
102+
<p>You can also set the `refreshBackgroundImage` prop to true is you want to enable background refreshable.</p>
103+
<p>This will refresh the background in every two seconds.</p>
104+
<CanvasDraw
105+
brushColor="rgba(155,12,60,0.3)"
106+
refreshBackgroundImage={true}
107+
imgSrc={this.state.backgroundImg}
108+
/>
72109
<h2>Hide UI</h2>
73110
<p>To hide the UI elements, set the `hideInterface` prop. You can also hide the grid with the `hideGrid` prop.</p>
74111
<CanvasDraw hideInterface hideGrid />

src/index.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ export default class extends PureComponent {
6060
imgSrc: PropTypes.string,
6161
saveData: PropTypes.string,
6262
immediateLoading: PropTypes.bool,
63-
hideInterface: PropTypes.bool
63+
hideInterface: PropTypes.bool,
64+
refreshBackgroundImage: PropTypes.bool,
6465
};
6566

6667
static defaultProps = {
@@ -79,7 +80,8 @@ export default class extends PureComponent {
7980
imgSrc: "",
8081
saveData: "",
8182
immediateLoading: false,
82-
hideInterface: false
83+
hideInterface: false,
84+
refreshBackgroundImage: false
8385
};
8486

8587
constructor(props) {
@@ -155,6 +157,12 @@ export default class extends PureComponent {
155157
// Signal this.loop function that values changed
156158
this.valuesChanged = true;
157159
}
160+
161+
// Refresh the Background Canvas if refreshBackgroundImage is true
162+
if (this.props.refreshBackgroundImage) {
163+
this.drawImage();
164+
}
165+
158166
}
159167

160168
componentWillUnmount = () => {
@@ -166,11 +174,11 @@ export default class extends PureComponent {
166174

167175
// Load the image
168176
this.image = new Image();
177+
this.image.src = this.props.imgSrc;
169178

170179
// Draw the image once loaded
171180
this.image.onload = () =>
172181
drawImage({ ctx: this.ctx.grid, img: this.image });
173-
this.image.src = this.props.imgSrc;
174182
};
175183

176184
undo = () => {
@@ -321,7 +329,6 @@ export default class extends PureComponent {
321329
this.setCanvasSize(this.canvas.grid, width, height);
322330

323331
this.drawGrid(this.ctx.grid);
324-
this.drawImage();
325332
this.loop({ once: true });
326333
}
327334
this.loadSaveData(saveData, true);

0 commit comments

Comments
 (0)