V
V
Vladimir2017-06-22 18:36:14
Canvas
Vladimir, 2017-06-22 18:36:14

How to load an image into canvas?

How to load Image to full width/height of canvas in React? for example:

class PlanPage extends Component {
  constructor(props) {
    super(props);
  }

  componentWillMount() {
    this.setState({
      canvasA: {
        canvasWidth: 800,
        canvasHeight: 600
      }
    })
  }

  componentDidMount() {
    this.props.getDataMap();  // возвращает объект с полями id,..,URL где лежит картинка
    const { canvasWidth, canvasHeight } = this.state.canvasA;
    this.canvasA.width = canvasWidth;
    this.canvasA.height = canvasHeight;
  }
......
<canvas
  ref={canvasA => this.canvasA = canvasA} /> //сам канвас

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2017-06-22
@vnam92

You can access the canvas via: this.refs.canvasA, then work as usual, react should no longer appear in the question here. (i.e. call getContext, call drawImage .... )
Example (no react).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question