I can't draw rectangle using canvas as child component


Riddle

I need to draw a rectangle using canvas. Canvas must be in child component. Just like the updateCanvas function. Is it possible to do so?

I am trying to draw a rectangle in the parent component and everything works fine, but in this case I need to put the canvas in the child component.

// Parent Component

import React, { Component } from "react";
import PropTypes from "prop-types";
import Shape from "../../components/Shape";
import "./groupShapes.css";
export default class GroupShapes extends Component {
  constructor(props) {
    super(props);
    // this.state = {
    //   reactanglesOptions: [0, 1, 2]
    // };
    this.canvas = React.createRef();
  }
  componentDidMount() {
    this.updateCanvas();
  }

  updateCanvas = () => {
    const ctx = this.canvas.getContext("2d");
    ctx.fillRect(0, 0, 100, 100);
  };

  render() {
    // const { reactanglesOptions } = this.state;
    return (
      <div className="groupContainer">
        <Shape ref={this.canvas} />
      </div>
    );
  }

  static propTypes = {
    prop: PropTypes
  };
}

// Child Component
import React, { Component } from "react";

export default class Shape extends Component {
  render() {
    const { ref } = this.props;
    return (
      <div>
        <canvas ref={ref} width={300} height={300} />
      </div>
    );
  }
}

expected results. drawn rectangle.

Mohammad Guda

In react documentation it says if you use ref.current through it

<canvas ref={ref.current} width={300} height={300} />

Related


Can't draw square on canvas using setInterval?

User 10870573 Hey guys, I'm trying to use setInterval to draw a square on the canvas every 70ms. Interval is set to call function draw()this function, which contains the fillRect() method. But I can't get it to work any ideas? Oh, I tried clearRect() but it di

I can't draw solid rectangle

shark coding I am trying to draw a screen with a filled box in the middle. I am using pygame.Surface.fill()this box, but it gives this error: TypeError: descriptor 'fill' for 'pygame.Surface' object is not applicable to 'tuple' object Here is my code: my_color

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

I can't draw solid rectangle

shark coding I am trying to draw a screen with a filled box in the middle. I am using pygame.Surface.fill()this box, but it gives this error: TypeError: descriptor 'fill' for 'pygame.Surface' object is not applicable to 'tuple' object Here is my code: my_color

Can't draw square on canvas using setInterval?

User 10870573 Hey guys, I'm trying to use setInterval to draw a square on the canvas every 70ms. Interval is set to call function draw()this function, which contains the fillRect() method. But I can't get it to work any ideas? Oh, I tried clearRect() but it di

Why can't I draw a square on the canvas?

Lords Hey, I'm making a snake game in JS, and now all I want to do is draw a snake in the center of the canvas. I've set the canvas size to the board size, so everything scales correctly, but nothing shows up. Any help would be greatly appreciated :) //declare

I can't draw rectangle using canvas as child component

Riddle I need to draw a rectangle using canvas. Canvas must be in child component. Just like the updateCanvas function. Is it possible to do so? I am trying to draw a rectangle in the parent component and everything works fine, but in this case I need to put t

Can't draw rectangle on JPanel

Jonah I'm currently trying to learn more about swing painting and am creating a little game but seem to be stuck. I'm trying to add several rectangles (enemies) to an ArrayList and then draw them in a paintComponent()method , but that doesn't seem to work. Thi

Tkinter won't draw rectangle on Canvas

Cadel Watson I'm trying to initialize a grid of blue rectangles with a user-specified size. However, the rectangle is not drawn on the initialized canvas. I am trying to store them in a matrix for later use. My code is as follows: import Tkinter import sys fro

I can't draw solid rectangle

shark coding I am trying to draw a screen with a filled box in the middle. I am using pygame.Surface.fill()this box, but it gives this error: TypeError: descriptor 'fill' for 'pygame.Surface' object is not applicable to 'tuple' object Here is my code: my_color

Draw rounded rectangle using RectF and canvas?

TokTok123 I am trying to draw a rounded rectangle using RectF and canvas.drawRoundRect(). Please see my code below: package com.example.test; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.graphics.Canvas

Can't draw proper circle using canvas

Xiajin In any case, I can't get a proper circle to draw. I am always oval. Here is sample code to show you what I mean: function canvasClicked(number) { var c = "canvas" + number; var canvas = document.getElementById(c); var context = canvas.getContext("

Can I draw on canvas using Selenium Python?

PhD I'm trying to run automated tests and benchmarks for an HTML5 canvas application. I've setup Selenium with python-unittest and ChromeDriver (though I'm open to alternatives). Canvas apps are whiteboards where lines are drawn by clicking the mouse and dragg

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

Can't rotate rectangle in canvas

Rajib I am new to HTML5 canvas. I'm trying to build a game area using an HTML canvas, where someone can drive around a car inside the Canvas area, like if I press the left arrow key, the car will turn to the left, and if the right arrow key is pressed, the car

I can't draw solid rectangle

shark coding I am trying to draw a screen with a filled box in the middle. I am using pygame.Surface.fill()this box, but it gives this error: TypeError: descriptor 'fill' for 'pygame.Surface' object is not applicable to 'tuple' object Here is my code: my_color

Why can't I draw a square on the canvas?

Lords Hey, I'm making a snake game in JS, and now all I want to do is draw a snake in the center of the canvas. I've set the canvas size to the board size, so everything scales correctly, but nothing shows up. Any help would be greatly appreciated :) //declare

Can't draw rectangle on JPanel

Jonah I'm currently trying to learn more about swing painting and am creating a little game but seem to be stuck. I'm trying to add several rectangles (enemies) to an ArrayList and then draw them in a paintComponent()method , but that doesn't seem to work. Thi

I can't draw solid rectangle

shark coding I am trying to draw a screen with a filled box in the middle. I am using pygame.Surface.fill()this box, but it gives this error: TypeError: descriptor 'fill' for 'pygame.Surface' object is not applicable to 'tuple' object Here is my code: my_color

I can't draw solid rectangle

shark coding I am trying to draw a screen with a filled box in the middle. I am using pygame.Surface.fill()this box, but it gives this error: TypeError: descriptor 'fill' for 'pygame.Surface' object is not applicable to 'tuple' object Here is my code: my_color

Tkinter won't draw rectangle on Canvas

Cadel Watson I'm trying to initialize a grid of blue rectangles with a user-specified size. However, the rectangle is not drawn on the initialized canvas. I am trying to store them in a matrix for later use. My code is as follows: import Tkinter import sys fro

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

I can't draw solid rectangle

shark coding I am trying to draw a screen with a filled box in the middle. I am using pygame.Surface.fill()this box, but it gives this error: TypeError: descriptor 'fill' for 'pygame.Surface' object is not applicable to 'tuple' object Here is my code: my_color