简体   繁体   English

如何在React中的画布上加载图像?

[英]How to load Image on canvas in React?

How to upload Image to the full width/height of the canvas in React? 如何在React中将图像上传到画布的整个宽度/高度? for example: 例如:

 class PlanPage extends Component { constructor(props) { super(props); } componentWillMount() { this.setState({ canvasA: { canvasWidth: 800, canvasHeight: 600 } }) } componentDidMount() { this.props.getDataMap(); //return object which has the fields eg id,... and field URL which specifies where image is const { canvasWidth, canvasHeight } = this.state.canvasA; this.canvasA.width = canvasWidth; this.canvasA.height = canvasHeight; } ...... <canvas ref={canvasA => this.canvasA = canvasA} /> //canvas 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> 

will appreciated any help. 将不胜感激。

You can try something like this 您可以尝试这样的事情

componentDidMount() {
  const context = this.canvasA.getContext('2d');

  const image = new Image();
  image.src = "whereever-you-image-url-live.jpg";
  image.onload = () => {
    context.drawImage(image, 0, 0, this.canvasA.width, this.canvasA.height);
  };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM