Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. class MyRect extends Component<Props, State> {
  2.  
  3. state = {
  4. width: window.innerWidth,
  5. height: window.innerHeight
  6. }
  7.  
  8.  
  9. componentDidMount() {
  10. this.updateCanvas()
  11. window.addEventListener("resize", this.resizeCanvas)
  12. }
  13.  
  14. componentWillUnmount() {
  15. window.removeEventListener('resize', this.resizeCanvas);
  16. }
  17.  
  18. updateCanvas() {
  19. const canvas = document.getElementById('canvass') as HTMLCanvasElement
  20. const ctx: CanvasRenderingContext2D = canvas.getContext('2d')
  21. for (let x = 0; x < this.state.width; x += 32) {
  22. for (let y = 0; y < this.state.height; y += 32) {
  23. ctx.fillRect(x, y, 3, 3)
  24. }
  25. }
  26. }
  27. resizeCanvas = () => {
  28. this.setState({
  29. width: window.innerWidth,
  30. height: window.innerHeight
  31. })
  32. this.updateCanvas()
  33. }
  34.  
  35. render() {
  36. const { classes } = this.props
  37. return (
  38. <div className={classes.canvas}>
  39. <canvas id="canvass" ref="canvas" width={this.state.width + 'px'} height={this.state.height + 'px'}/>
  40. </div>
  41. )
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement