var canvas = document.getElementById('canvas');
var cxt = canvas.getContext('2d');

var timer;
var iStop = false;
var rotateSpeed = 0;
var endLines = [{'deg':0},{'deg':90},{'deg':180},{'deg':270}];
var runSpeed = 0;
var runLines = [{'x1':300,'y1':600, 'x2':300, 'y2':500, 'speed':0}];

function draw(){
cxt.save();
cxt.translate(300, 300);
cxt.rotate(rotateSpeed*Math.PI/180);
cxt.beginPath();
cxt.arc(0, 0, 50, 0, 360*Math.PI/180, false);
cxt.stroke();
cxt.closePath();

for(var i=0; i<endLines.length; i++){
cxt.save();
cxt.rotate(endLines[i].deg*Math.PI/180);
cxt.beginPath();
cxt.moveTo(50, 0);
cxt.lineTo(150, 0);
cxt.stroke();
cxt.closePath();
cxt.restore();
}
cxt.restore();

for(var j=0; j<runLines.length; j++){
cxt.save();
cxt.beginPath();
cxt.moveTo(runLines[j].x1, runLines[j].y1);
cxt.lineTo(runLines[j].x2, runLines[j].y2);
cxt.stroke();
cxt.closePath();
cxt.restore();
}

var _runLines = [];
for(var k=0; k<runLines.length; k++){
if(runLines[k].y2 < 450){
for(var m=0; m<endLines.length; m++){
if((endLines[m].deg + rotateSpeed)%360 == 90){
iStop = true;
}
}
}
if(runLines[k].y2 == 350){
endLines.push({'deg':90-rotateSpeed});
}else{
runLines[k].y1 -= runLines[k].speed;
runLines[k].y2 -= runLines[k].speed;
_runLines.push(runLines[k]);
}
}
runLines = _runLines;

if((++rotateSpeed) == 360){
rotateSpeed = 0;
}

}

function erase(){
cxt.clearRect(0, 0, canvas.width, canvas.height);
}

document.onmousedown = function(){
runLines.push({'x1':300,'y1':600, 'x2':300, 'y2':500, 'speed':10});
};

window.requestAnimationFrame =
window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame;

window.cancelRequestAnimationFrame =
window.cancelRequestAnimationFrame ||
window.mozCancelRequestAnimationFrame ||
window.webkitCancelRequestAnimationFrame ||
window.msCancelRequestAnimationFrame;

function animate() {
erase();
draw();
if(iStop){
cancelRequestAnimationFrame(timer);
}else{
timer = requestAnimationFrame(animate);
}
}

animate();

canvas 实现微信小游戏的更多相关文章

  1. 微信小游戏开发Canvas资源汇总

    Demo: 微信小程序demo组件:股票分时图 微信小程序小组件:仿直播点赞气泡效果,基于Canvas 优质demo推荐:二维码生成器:使用canvas与纯JS版二维码生成 微信小程序学习用完整dem ...

  2. 微信小游戏canvas操作

    这几天在做项目的时候,想在游戏画面之前,在Canvas上面画上一张背景图,代码如下     let ctx = canvas.getContext('2d')    export default cl ...

  3. 微信小游戏排行榜页滚动查看排行榜(canvas指定区域溢出滚动,懒渲染)

    在微信小游戏中,好友排名数据是能在关系数据域操作,整个关系数据域只会返回一个最终的sharedCanvas,并且这个canvas不能调用toDataURL()方法,所以要展示好友排行榜的话只能在关系数 ...

  4. 【转】利用 three.js 开发微信小游戏的尝试

    前言 这是一次利用 three.js 开发微信小游戏的尝试,并不能算作是教程,只能算是一篇笔记吧. 微信 WeChat 6.6.1 开始引入了微信小游戏,初期上线了一批质量相当不错的小游戏.我在查阅各 ...

  5. 微信小游戏开发之四:使用three.js引擎

    一.前言 微信小游戏中最魔性的'跳一跳'就是基于three.js 引擎开发的 源码放到github上了:GitHub地址   请自行下载. 二.下载 three.min.js 打开页面,复制代码到本地 ...

  6. 使用Laya引擎开发微信小游戏(上)

    本文由云+社区发表 使用一个简单的游戏开发示例,由浅入深,介绍了如何用Laya引擎开发微信小游戏. 作者:马晓东,腾讯前端高级工程师. 微信小游戏的推出也快一年时间了,在IEG的游戏运营活动中,也出现 ...

  7. three.js 微信小游戏

    最近在 https://classroom.udacity.com/courses/cs291 学习了一些 3D 引擎和 three.js 的知识 把 three.js 弄到微信小游戏里,先随便跑一跑 ...

  8. .Net Core ORM选择之路,哪个才适合你 通用查询类封装之Mongodb篇 Snowflake(雪花算法)的JavaScript实现 【开发记录】如何在B/S项目中使用中国天气的实时天气功能 【开发记录】微信小游戏开发入门——俄罗斯方块

    .Net Core ORM选择之路,哪个才适合你   因为老板的一句话公司项目需要迁移到.Net Core ,但是以前同事用的ORM不支持.Net Core 开发过程也遇到了各种坑,插入条数多了也特别 ...

  9. Egret5.2.2 微信小游戏行的示例排行榜

    Egret5.2.2版本发布微信小游戏后,在开放数据域有一个默认排行榜.这个文件夹代码+图大小就22kb. 排行榜的效果就是示范用的,很丑...带翻页. 代码如下,基本就是使用canvas渲染了一个排 ...

随机推荐

  1. vue 定时器的问题

    在项目中,我们经常会使用到定时器setInterval(),可是很多时候我们会发现,即使我退出当前页面,定时器依然在工作,非常消耗内存,所以我们要进行手动清理: 将定时器保存在变量中,退出页面时清除变 ...

  2. 获取标签as3.0

    import flash.utils.Timer; import flash.events.TimerEvent; var time:Timer=new Timer(25); time.addEven ...

  3. nohup top -p 22452 -b >>jiu.log &

    解释一下: 1. nohup \$order & 后台执行 2. nohup \$order >>$file & 后台执行,并输入指定文件 3. top -p $num 使 ...

  4. perl-基础

    1.关系运算符 数字: == != < <= > >= 字符串: eq ne lt le  gt   ge 2.循环 循环:while(){}   for(){}   last ...

  5. PAT1135(红黑书的判定)

    There is a kind of balanced binary search tree named red-black tree in the data structure. It has th ...

  6. 164. Maximum Gap (Array; sort)

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  7. 今天清理C盘空间,发现Unity的一个问题

    C:\Users\Acer\AppData\LocalLow\Unity\Caches\GiCache AppData目录下,Unity占用了大量C盘空间,大概有5,6个G

  8. struct和union,enum分析

    空结构体占用的内存多大? struct d { }; int main() { struct d d1; struct d d2; printf("%d,%0x\n",sizeof ...

  9. 删除sslvpn用户

    config user localedit xinghen unset two-factornextend config user groupedit vpngroup unselect member ...

  10. java调用dll

    @参考文章1,@参考文章2 根据上篇博客(参考文章2)java生成的dll测试 1,新建java项目,新建WebContent,子目录建WEB-INF\lib,加进jna-3.4.0.jar 新建ja ...