javascript - 如何使用 react 在图像中的对象上绘制矩形?

标签 javascript image facebook reactjs canvas

我正在使用 react 和 HTML5 Canvas 在图像上的面部上绘制矩形,但是 react 生命周期限制了我这样做。

因为我应该使用 refs 来引用 Canvas ,而这必须在 componentDidMount() 中完成,我无法在此函数中获取 Prop 或状态。

这是代码:

import React, { Component } from 'react'
import { render } from 'react-dom'
import {Row} from 'react-bootstrap';    
import '../App.css';

class TagImg extends Component {
  constructor(props){
    super(props);
    this.state={
      rects:[[110, 30, 70, 70], [40, 30, 70, 70]],
      ctx : ''
    };
    this.tagPerson = this.tagPerson.bind(this);
  }

  tagPerson(e){
    var x = e.offsetX,
        y = e.offsetY;

    for(var i=0;i<this.props.rects.length;i++) { // check whether:
        if(x > this.props.rects[i][0]            // mouse x between x and x + width
        && x < this.props.rects[i][0] + this.props.rects[i][2]
        && y > this.props.rects[i][1]            // mouse y between y and y + height
        && y < this.props.rects[i][1] + this.props.rects[i][3]) {
            alert('Rectangle ' + i + ' clicked');
        }
    }
  }

  componentDidMount() {
    console.log("componentDidMount");
    const ctx = this.refs.canvas.getContext('2d');
    var myImage = new Image();

    myImage.onload = function() {
      var width = myImage.naturalWidth; // this will be 300
      var height = myImage.naturalHeight; // this will be 400
      ctx.drawImage(myImage, 0, 0, 300, 200);

      ctx.beginPath();
      ctx.strokeStyle="white";
      // for(var i=0;i<this.state.rects.length;i++) {
      //   // ctx.rect(this.state.rects[i][0], // fill at (x, y) with (width, height)
      //   //          this.state.rects[i][1],
      //   //          this.state.rects[i][2],
      //   //          this.state.rects[i][3]);
      //   console.log("helloo");
      // }
      console.log(this.state.rects);

      ctx.stroke();
    }
    myImage.src = this.props.path;
  }
  render() {
    return (
      <div>
      <form id="afterUpload" action="" method="post" encType="multipart/form-data">
        <Row id="image_preview" className="row">
          <canvas ref="canvas" onClick={this.tagPerson}/>
        </Row>
      </form>
      </div>
    );
  }
}

export default TagImg;

搜索后我发现我可以使用 componentWillRecieveProps() 但不明白如何在我的情况下使用它。

最佳答案

componentDidMount在安装组件后,只会从 React 调用一次。

由于您想在用户每次点击 Canvas 时绘制一些东西,因此您必须在每次渲染时调用它的地方执行此操作,例如在 render 内部。功能本身。

像这样的东西:

import React, { Component } from 'react'
import { render } from 'react-dom'
import {Row} from 'react-bootstrap';    

class TagImg extends Component {
  constructor(props){
    super(props);
    this.state={
      rects:[[110, 30, 70, 70], [40, 30, 70, 70]]
    };
    this.tagPerson = this.tagPerson.bind(this);
  }

  tagPerson(e){
    var x = e.offsetX,
        y = e.offsetY;

    for(var i=0;i<this.props.rects.length;i++) { // check whether:
        if(x > this.props.rects[i][0]            // mouse x between x and x + width
        && x < this.props.rects[i][0] + this.props.rects[i][2]
        && y > this.props.rects[i][1]            // mouse y between y and y + height
        && y < this.props.rects[i][1] + this.props.rects[i][3]) {
            alert('Rectangle ' + i + ' clicked');
        }
    }
  }

  drawRects() {
      const ctx = this.refs.canvas.getContext('2d');
      ctx.drawImage(myImage, 0, 0, 300, 200);

      ctx.beginPath();
      ctx.strokeStyle="white";
      // for(var i=0;i<this.state.rects.length;i++) {
      //   // ctx.rect(this.state.rects[i][0], // fill at (x, y) with (width, height)
      //   //          this.state.rects[i][1],
      //   //          this.state.rects[i][2],
      //   //          this.state.rects[i][3]);
      //   console.log("helloo");
      // }
      console.log(this.state.rects);

      ctx.stroke();

  }

  componentDidMount() {
    console.log("componentDidMount");
    var myImage = new Image();
    myImage.onload = this.drawRects.bind(this);
    myImage.src = this.props.path;
  }
  render() {
    drawRects();
    return (
      <div>
      <form id="afterUpload" action="" method="post" encType="multipart/form-data">
        <Row id="image_preview" className="row">
          <canvas ref="canvas" onClick={this.tagPerson}/>
        </Row>
      </form>
      </div>
    );
  }
}

export default TagImg;

在我下载图像一次的代码中,在 componentDidMount方法。我正在运行 drawRects()每次渲染的方法,所以每次调用 setState你会有新的直 Angular

关于javascript - 如何使用 react 在图像中的对象上绘制矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43050427/

相关文章:

java - 更改 Java 应用程序的桌面图标

html - HTML img 中的奇怪像素

ios - 'FBSDKAccessTokenDidChangeNotification' 的 swift 等价物是什么

javascript - 使用条件半径函数的流程图

javascript - 使用 Ajax 在不刷新页面的情况下查询 SQL

javascript - 输入类型单选按钮已选中 else 语句在 jquery 中不起作用

javascript - 不要淡入,直到淡出完全完成

jquery - 在ajax加载的内容中预加载图像

iphone - Facebook iPhone SDK : show a progress bar while uploading an image

Javascript Facebook API POST 多部分/表单数据