Skip to content

Commit 667e00c

Browse files
committed
Merge branch 'feature/refresh-canvas-background' of https://github.com/Code-Crash/react-canvas-draw into Code-Crash-feature/refresh-canvas-background
2 parents eb34e41 + b5882c5 commit 667e00c

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
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/

demo/src/index.js

Lines changed: 36 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 2 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,13 @@ 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>This will refresh the background in every two seconds.</p>
103+
<CanvasDraw
104+
brushColor="rgba(155,12,60,0.3)"
105+
imgSrc={this.state.backgroundImg}
106+
/>
72107
<h2>Hide UI</h2>
73108
<p>To hide the UI elements, set the `hideInterface` prop. You can also hide the grid with the `hideGrid` prop.</p>
74109
<CanvasDraw hideInterface hideGrid />

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,10 @@ export default class CanvasDraw extends PureComponent {
339339
if (!this.props.enablePanAndZoom) {
340340
this.coordSystem.resetView();
341341
}
342+
343+
if (prevProps.imgSrc !== this.props.imgSrc) {
344+
this.drawImage();
345+
}
342346
}
343347

344348
componentWillUnmount = () => {

0 commit comments

Comments
 (0)