[图片] 这个加id="myCanvas"就可以了, 我遇到的问题是开发者工具里能获取canvas,但真机调试不行,不知道咋回事儿
canvas 获取失败?[代码]export [代码][代码]default[代码] [代码]class ProgressPanel extends Component {[代码][代码] [代码] [代码] [代码][代码]componentDidMount () {[代码][代码] [代码][代码]console.log([代码][代码]'test componentDidMount'[代码][代码])[代码][代码] [代码][代码]const query = wx.createSelectorQuery().[代码][代码]in[代码][代码]([代码][代码]this[代码][代码])[代码][代码] [代码][代码]query.select([代码][代码]'#myCanvas'[代码][代码])[代码][代码] [代码][代码].fields({ node: [代码][代码]true[代码][代码], size: [代码][代码]true[代码] [代码]})[代码][代码] [代码][代码].exec((res) => {[代码][代码] [代码][代码]console.log([代码][代码]'test ProgressPanel res 没执行到这里'[代码][代码], res)[代码][代码] [代码][代码]const canvas = res[0].node[代码][代码] [代码][代码]const ctx = canvas.getContext([代码][代码]'2d'[代码][代码])[代码][代码] [代码] [代码] [代码][代码]const dpr = wx.getSystemInfoSync().pixelRatio[代码][代码] [代码][代码]canvas.width = res[0].width * dpr[代码][代码] [代码][代码]canvas.height = res[0].height * dpr[代码][代码] [代码][代码]ctx.scale(dpr, dpr)[代码][代码] [代码] [代码] [代码][代码]ctx.fillRect(0, 0, 100, 100)[代码][代码] [代码][代码]})[代码][代码] [代码][代码]}[代码] [代码] [代码][代码]public render() {[代码][代码] [代码][代码]return[代码] [代码]([代码][代码] [代码][代码]<View className=[代码][代码]"panel"[代码][代码]>[代码][代码] [代码][代码]<View className=[代码][代码]"panel-background"[代码][代码]>[代码][代码] [代码][代码]<View id=[代码][代码]"canvas_container"[代码] [代码]className=[代码][代码]"panel-background-gray"[代码][代码]>[代码][代码] [代码][代码]<canvas style=[代码][代码]'width: 300px; height: 200px;'[代码] [代码]canvas-id=[代码][代码]'myCanvas'[代码] [代码]/>[代码][代码] [代码][代码]</View>[代码] [代码] [代码][代码]</View>[代码] [代码] [代码][代码]</View>[代码][代码] [代码][代码])[代码] [代码] [代码][代码]}[代码][代码] [代码] [代码]}[代码] 获取失败 没有拿到canvas
2020-09-29