【发布时间】:2019-08-12 18:12:29
【问题描述】:
我正在尝试在 react 组件中创建一个非常简单的画布。但是画布没有以正确的颜色显示。
import React, { Component } from 'react'
export default class Home extends Component {
constructor(props) {
super(props)
this.canvasRef = React.createRef();
}
componentDidMount() {
const canvas = this.canvasRef.current;
const ctx = canvas.getContext('2d')
ctx.fillRect(0, 0, canvas.width, canvas.height)
ctx.fillStyle = 'blue'
}
render() {
return (
<div>
Welcome to Home Page!
<div className="clearfix"></div>
<canvas ref={this.canvasRef} />
</div>
)
}
}
我刚刚看到一个默认的 150*300 默认画布填充黑色。我想我把它设计成蓝色。为什么会这样?
【问题讨论】:
标签: reactjs canvas html5-canvas