微信小程序游戏-----动画
2019-03-24 08:53
459 查看
[code]setInterval() setTimeout() requestAnimationFrame() clearInterval() clearTimeout() cancelAnimationFrame()
[code]var canvas =wx.createCanvas() var ctx =canvas.getContext("2d") ctx.fillStyle ="red" ctx.fillRect(0,0,100,100) ctx.clearRect(0,0,canvas.width,canvas.height) //清除画布 ctx.fillRect(0,20,100,100) ctx.clearRect(0,0,canvas.width,canvas.height) ctx.fillRect(0,150,100,100) var num =0 var timer=setInterval(function(){ num++ ctx.clearRect(0,0,canvas.width,canvas.height) ctx.fillRect(0,num++,100,100) if(num>150){ clearInterval(timer) } },20)
[code]//用延时器做动画 var canvas =wx.createCanvas() var ctx =canvas.getContext("2d") ctx.fillStyle ="red" //延迟定时器去做 ctx.fillRect(0,0,100,100) setTimeout(() => { ctx.clearRect(0,0,canvas.width,canvas.height) ctx.fillRect(0,150,100,100) }, 500);
[code]var canvas =wx.createCanvas() var ctx =canvas.getContext("2d") ctx.fillStyle ="red" VAR TIMER =NULL //延迟定时器去做 ctx.fillRect(0,0,100,100) var num =0; loop() function loop(){ num ++ TIMER= setTimeout(() => { ctx.clearRect(0,0,canvas.width,canvas.height) ctx.fillRect(0,num,100,100) if(num<150){ clearTimeout(TIMER) } }, 20); }
[code]var canvas = wx.createCanvas(); var ctx = canvas.getContext("2d"); ctx.fillStyle = "red"; //requestAnimationFrame var num = 0 loop() function loop(){ num++ requestAnimationFrame(function(){ ctx.clearRect(0,0,canvas.width,canvas.height) ctx.fillRect(0,num,100,100) loop() }) }
[code] //碰撞检测 var canvas = wx.createCanvas(); var ctx = canvas.getContext("2d"); var img = wx.createImage(); var firstX = 0; var firstY = 0; var imgW = 100; var imgH = 100; var isShow = false; img.src = "./source/2.jpg"; img.onload = function() { ctx.drawImage(img, firstX, firstY, imgW, imgH); }; wx.onTouchStart(function(e) { var touch = e.changedTouches[0]; var clientX = touch.clientX; var clienty = touch.clientY; if ( firstX < clientX && clientX < firstX + imgW && (firstY < clienty && clienty < firstY + imgH) ) { isShow = true; } }); wx.onTouchMove(function(e) { if (isShow) { ctx.clearRect(0, 0, canvas.width, canvas.height); //清除画布 var touch = e.changedTouches[0]; var clientX = touch.clientX; var clienty = touch.clientY; if((clientX-imgW/2)<0){ firstX =0 }else if((clientX-imgW/2)>(canvas.width-imgW)){ firstX = canvas.width-imgW }else{ firstX = clientX-imgW/2 } if((clienty-imgH/2)<0){ firstY =0 }else if((clienty-imgH/2)>(canvas.height-imgH)){ firstY =(canvas.height-imgH) }else{ firstY = clienty-imgH/2 } ctx.drawImage(img,firstX,firstY,imgW,imgH); } }); wx.onTouchEnd(function(){ isShow = false })
相关文章推荐
- 微信小程序自定义对话框弹出和隐藏动画
- 微信小程序动画技巧
- 微信小程序例子——控制导航条加载动画
- 微信小程序开发之录音机 音频播放 动画 (真机可用)
- 微信小程序CSS3动画下拉菜单效果
- 微信小程序开发之animation循环动画实现的让云朵飘效果
- 微信小程序----scroll-view组件(MUI索引列表)滚动动画
- 微信小程序开发之麦克风动画 帧动画 放大 淡出
- 微信小程序(游戏)----五子棋(AI篇)
- 微信小程序 持续绘图 动画
- 微信小程序之自定义模态弹窗(带动画)实例
- Java动画 重力弹球 如鹏游戏引擎 精灵 设计一个小球加速落地又减速弹起并反复直到停止的Java程序
- 微信小程序开发之动画 Animation 放大 透明度
- 微信小程序开发效果:animation心跳动画
- 微信小程序~动画应用
- 微信小程序(游戏)----五子棋(棋盘,重置,对弈)
- [微信小程序]手指触摸动画效果(完整代码附效果图)
- 微信小程序scroll-view组件实现滚动动画
- 微信小程序 循环动画
- [微信小程序]物流信息样式加动画效果(源代码附效果图)