我正在使用 React 和 HTML5 Canvas 在图像上的面上绘制矩形,但 React 生命周期限制了我这样做。
因为我应该使用 refs 来引用画布,并且这必须在 componentDidMount() 中完成,并且我无法在此函数内获取道具或状态。
这是代码:
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 …Run Code Online (Sandbox Code Playgroud)