我正试着爬上画布:
class DungeonMap extends Component {
constructor(props) {
super(props);
}
componentDidMount() {
const canvas = this.refs.canvas;
const ctx = canvas.getContext('2d');
ctx.scale(2, 2)
this.setState({})
}
render() {
console.log("render")
return (
<div className="DungeonMap">
<canvas ref="canvas" style={canvasStyle}/>
</div>
);
}
}代码编译,但画布缩放没有应用,我尝试了不同的值进行缩放。
正如您所看到的,我也尝试使用this.setState({})强制重呈现,但是画布仍然没有缩放。
我确信componentDidMount()是正确使用的方法,因为我必须确保在加载了Cannot read property 'getContext' of null, using canvas之后应用了缩放。
如何将缩放应用于画布?
发布于 2018-11-30 01:26:45
尝试在width元素上设置<canvas>和height属性:
class DungeonMap extends Component {
constructor(props) {
super(props);
}
componentDidMount() {
const canvas = this.refs.canvas;
// to scale the <canvas> element itself
canvas.width = 500;
canvas.height = 500;
// to scale the drawings, use the context
const ctx = canvas.getContext('2d');
ctx.scale(2, 2);
}
render() {
console.log("render")
return (
<div className="DungeonMap">
<canvas ref="canvas" style={canvasStyle}/>
</div>
);
}
};顺便问一句:我不太确定,但是你的JSX中的<canvas>元素不应该有一个结束标记吗?或者JSX处理的方式有那么不同?我自己也没这么多地使用过JSX。
https://stackoverflow.com/questions/53549401
复制相似问题