微信小程序中setInterval的使用
转:http://blog.****.net/siwen1990
看了下小程序的画布功能,简单的使用了一下,用蹩脚的逻辑做了个 “弹啊弹,弹走鱼尾纹的小球”,一起来看下吧。过程不重要主要是画布的使用哦。(本来想传gif的来着,后来发现不会传,就传个图片吧,想看的自己下载下来玩呦)
先上图,后上代码了:
js:
- var winWidth = 0
- var winHeight = 0
- var diameter = 10
- var time = 0
- Page({
- data:{
- numX:1,
- numY:1
- },
- xy:{
- //小球的xy坐标
- x:10,
- y:10
- },
- onLoad:function(options){
- //进来先获取手机的屏幕宽度和高度
- wx.getSystemInfo({
- success: function(res) {
- console.log(res)
- winHeight = res.windowHeight;
- winWidth = res.windowWidth;
- }
- })
- },
- onReady:function(){
- //循环滚动小球
- for(var i=0;i<1;i++){
- //随机一个滚动的速度
- time = (1+Math.random()*10)
- setInterval(this.move,time);
- console.log(time)
- }
- },
- move(){
- //x
- if(this.data.numX == 1){
- this.xy.x++
- }else{
- this.xy.x--
- }
- //判断x轴的状态
- if(this.xy.x == winWidth-diameter){
- this.data.numX=2
- }
- if(this.xy.x == diameter){
- this.data.numX=1
- }
- //y
- if(this.data.numY == 1){
- this.xy.y++
- }else{
- this.xy.y--
- }
- //判断y轴的状态
- if(this.xy.y == 400-diameter){
- this.data.numY=2
- }
- if(this.xy.y == diameter){
- this.data.numY=1
- }
- //画图
- this.ballMove(this.xy.x,this.xy.y);
- },
- ballMove(x,y){
- // 使用 wx.createContext 获取绘图上下文 context
- var context = wx.createContext()
- // context.setShadow(0,1,6,'#000000')//阴影效果
- context.setFillStyle("#FF4500")//球的颜色
- context.setLineWidth(2)
- context.arc(x, y, diameter, 0, 2 * Math.PI, true)
- context.fill()
- // 调用 wx.drawCanvas,通过 canvasId 指定在哪张画布上绘制,通过 actions 指定绘制行为
- wx.drawCanvas({
- canvasId: 'ball',
- actions: context.getActions() // 获取绘图动作数组
- })
- }
- })
wxml:
- <view>
- <canvas canvas-id="ball"></canvas>
- </view>
相关推荐
- 提取wux-weapp的微信小程序筛选框:FilterBar
- 微信小程序版本管理使用以及冲突文件project.config.json问题
- 微信小程序开发流程的两种方法科普
- Hbuilder编译微信小程序提示工具的服务端口已关闭。要使用命令行调用工具,请打开工具 -> 设置 -> 安全设置,将服务端口开启
- 微信小程序input使用
- 微信小程序之audio的Src资源路径查询
- 微信小程序+SpringBoot+mybatis+MySQL实现简单的登录
- 摆脱手机、即用即走,全球首款运行微信小程序的机器人来了
- 浅谈微信小程序的发展
- 我是怎么在 3 天内零成本开发 AI 微信小程序的
- 乐高加快中国授权专卖店开店步伐,相继落户烟台、昆明等地 | 知消图集
- 逆袭之路,普通二本的八年开发码农如何进阿里拿年薪百万