gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:35:48 28 4
gpt4 key购买 nike

我正在使用 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com