微信小程序 wxapp画布 canvas :最近学习微信小程序的知识,这里记录下小程序 waxpp画布canvas 的知识:
canvas
hidden | Boolean | false | 设置画布的显示/隐藏,hidden值为true表示隐藏,值为false表示显示 |
canvas-id | String | canvas组件的唯一标识符 | |
binderror | EventHandle | 当发生错误时触发error事件,detail = { errMsg: 'something wrong' } |
注:
1.canvas标签默认宽度300px、高度225px
2.同一页面中的canvas-id不可重复,如果使用一个已经出现过的canvas-id,该canvas标签对应的画布将被隐藏并不再正常工作
示例代码:下载
<> <canvas canvas-id="firstCanvas"></canvas> <> <canvas canvas-id="secondCanvas"></canvas> <> <canvas canvas-id="secondCanvas" binderror="canvasIdErrorCallback"></canvas> // canvas.js
Page({ canvasIdErrorCallback: function (e) { console.error(e.detail.errMsg); }, onReady: function (e) { //使用wx.createContext获取绘图上下文context var context = wx.createContext(); context.setStrokeStyle("#00ff00"); context.setLineWidth(5); context.rect(0,0,200,200); context.stroke() context.setStrokeStyle ("#ff0000") ; context.setLineWidth (2) context.moveTo(160,100) context.arc(100,100,60,0,2*Math.PI,true); context.moveTo(140,100); context.arc(100,100,40,0,Math.PI,false); context.moveTo(85,80); context.arc(80,80,5,0,2*Math.PI,true); context.moveTo(125,80); context.arc(120,80,5,0,2*Math.PI,true); context.stroke(); //调用wx.drawCanvas,通过canvasId指定在哪张画布上绘制,通过actions指定绘制行为 wx.drawCanvas({ canvasId: 'firstCanvas', actions: context.getActions() //获取绘图动作数组 }); } });
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!