Skip to content

Instantly share code, notes, and snippets.

@viebel
Last active November 9, 2017 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save viebel/3556cbd89ab268a82344bb06180132bc to your computer and use it in GitHub Desktop.
Save viebel/3556cbd89ab268a82344bb06180132bc to your computer and use it in GitHub Desktop.
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