Last active
November 9, 2017 16:21
-
-
Save viebel/3556cbd89ab268a82344bb06180132bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyCanvas extends React.Component { | |
constructor(props) { | |
super(props) | |
this.state = {value: ''} | |
this.handleClick = this.handleClick.bind(this) | |
} | |
componentDidMount() { | |
this.render() | |
} | |
handleClick(event) { | |
const rect = this.canvas.getBoundingClientRect(); | |
var x = event.clientX - rect.left; | |
var y = event.clientY - rect.top; | |
const ctx = this.canvas.getContext('2d') | |
ctx.fillStyle = 'white' | |
ctx.fillRect(x, y, 3, 3) | |
} | |
render() { | |
return ( | |
<div> | |
<canvas | |
onClick={this.handleClick} | |
style={{ | |
backgroundColor: this.props.bgColor | |
}} | |
width={400} | |
height={300} | |
ref={element => this.canvas = element}/> | |
</div> | |
) | |
} | |
} | |
window.MyCanvas = MyCanvas |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment