我正在尝试在 React 中使用画布。下面的代码应该给我一个黑色的 800x800 画布,但由于某种原因它忽略了我的尺寸并显示了一个 300x150 的画布。
import React, {Component} from "react";
class Sample extends Component {
state = {
canvasWidth: 800,
canvasHeight: 800
}
canvasRef = React.createRef();
componentDidMount() {
const canvas = this.canvasRef.current;
const context = canvas.getContext('2d');
context.fillRect(0, 0, this.state.canvasWidth, this.state.canvasHeight);
}
render() {
return (
<div>
<canvas ref={this.canvasRef} />
</div>
);
}
}
export default Sample
Run Code Online (Sandbox Code Playgroud) 在python3中:
>>> abs(-5) == 5
Run Code Online (Sandbox Code Playgroud)
和
>>> abs(5) == 5
Run Code Online (Sandbox Code Playgroud)
但
>> abs(5+0j) == 5.0
Run Code Online (Sandbox Code Playgroud)