canvas window.requestAnimationFrame

画同心圆的方法!

var canvas = document.getElementById("myCanvas");

var context = canvas.getContext("2d");

var btn = document.getElementById("btn");

var per=Math.PI/12;

var r=150;

var num=0;

//把坐标的中心移动到中点,

context.translate(200,200)

function move(){

//开始画圆

context.beginPath();

context.strokeStyle='red';

context.lineWidth=5;

context.arc(0,0,r,0,Math.PI/12)

context.stroke()

context.rotate(per)

num++;

if(num>72){

r-=20;

num=0;

console.log("hellow")

}

if(r>20){

window.requestAnimationFrame(move)

}

}

move()



三角形 原理 圆周运动

var canvas = document.getElementById("myCanvas");

var context = canvas.getContext("2d");

var btn = document.getElementById("btn");

//定义一个变量保存动画的返回值

var result = null;

//获取转动的圆的圆心的xx、yy坐标的函数,传入的参数是围绕的中心点的x、y坐标,转动的半径,每次转动的角度

function getXY(x, y, r, per) {

var xx = x + Math.cos(per) * r;

var yy = y - Math.sin(per) * r;

return [xx, yy];

}

//定义转的角度的初始值

var per = Math.PI / 12

//封装动画的函数

function move() {

//清理画布

context.clearRect(0, 0, canvas.width, canvas.height)

context.beginPath();

var res = getXY(200, 200, 150, per);

context.fillStyle = "red";

var c=20

//画外面转动的圆

context.arc(res[0], res[1], 20, 0, Math.PI * 2);

context.fill();

//增加转动的角度

per += Math.PI / 48;

//执行动画

result = window.requestAnimationFrame(move);

}

move();

//点击停止动画

btn.onclick = function() {

//阻止动画执行

window.cancelAnimationFrame(result);

}


面向对象,单个球星碰撞

var context = canvas.getContext("2d");

//圆是一个类,就是对象只有一个,就是圆

function Circle(x, y, r, speedX, speedY, color) {

//所有的属性

this.r = r;

this.x = x;

this.speedX = speedX;

this.speedY = speedY;

this.y = y;

this.color = color;

context.beginPath();

context.fillStyle = "green";

context.arc(this.x, this.y, this.r, 0, Math.PI * 2);

context.fill();

}

//在原型上写方法,

//第一个方法,画圆

Circle.prototype.draw = function() {

context.beginPath();

context.fillStyle = this.color;

context.arc(this.x, this.y, this.r, 0, Math.PI * 2);

context.fill();

}

//第二个方法,运动

Circle.prototype.move = function() {

//这里改变x递加的值,可以改变运动速度

this.x += this.speedX;

this.y += this.speedY;

if(this.x >= canvas.width - this.r || this.x <= this.r) {

this.speedX *= -1;

}

if(this.y >= canvas.height - this.r || this.y <= this.r) {

this.speedY *= -1;

}

}

function rand(min,max){

return parseInt(Math.random()*(max-min)+min+1)

}

var r=rand(10,100);

var x= rand(r,canvas.width-r);

var y=rand(r,canvas.height-r);

var color="rgb("+rand(0,255)+","+rand(0,255)+","+rand(0,255)+")"

var newCircle = new Circle(x, y,r, 2,2 ,color);

moveCircle();

//定义一个执行动画的函数

function moveCircle() {

//先清理画布

context.clearRect(0, 0, canvas.width, canvas.height);

//调用画圆的函数

newCircle.draw();

//调用运动的函数

newCircle.move();

//执行动画

window.requestAnimationFrame(moveCircle);

}



碰撞

var canvas = document.getElementById("myCanvas");

var context = canvas.getContext("2d");

//圆是一个类,就是对象只有一个,就是圆

function Circle(x, y, r, speedX, speedY, color) {

//所有的属性

this.r = r;

this.x = x;

this.speedX = speedX;

this.speedY = speedY;

this.y = y;

this.color = color;

context.beginPath();

context.fillStyle = "green";

context.arc(this.x, this.y, this.r, 0, Math.PI * 2);

context.fill();

}

//在原型上写方法,

//第一个方法,画圆

Circle.prototype.draw = function() {

context.beginPath();

context.fillStyle = this.color;

context.arc(this.x, this.y, this.r, 0, Math.PI * 2);

context.fill();

}

//第二个方法,运动

Circle.prototype.move = function() {

//这里改变x递加的值,可以改变运动速度

this.x += this.speedX;

this.y += this.speedY;

if(this.x >= canvas.width - this.r || this.x <= this.r) {

this.speedX *= -1;

}

if(this.y >= canvas.height - this.r || this.y <= this.r) {

this.speedY *= -1;

}

}

function rand(min,max){

return parseInt(Math.random()*(max-min)+min+1)

}

var r=rand(10,100);

var x= rand(r,canvas.width-r);

var y=rand(r,canvas.height-r);

var color="rgb("+rand(0,255)+","+rand(0,255)+","+rand(0,255)+")"

var newCircle = new Circle(x, y,r, 2,2 ,color);

moveCircle();

//定义一个执行动画的函数

function moveCircle() {

//先清理画布

context.clearRect(0, 0, canvas.width, canvas.height);

//调用画圆的函数

newCircle.draw();

//调用运动的函数

newCircle.move();

//执行动画

window.requestAnimationFrame(moveCircle);

}


多圆运动

var canvas = document.getElementById("myCanvas");

var context = canvas.getContext("2d");

//圆是一个类,就是对象只有一个,就是圆

function Circle(x, y, r, speedX, speedY, color) {

//所有的属性

this.r = r;

this.x = x;

this.speedX = speedX;

this.speedY = speedY;

this.y = y;

this.color = color;

context.beginPath();

context.fillStyle = "green";

context.arc(this.x, this.y, this.r, 0, Math.PI * 2);

context.fill();

}

//在原型上写方法,

//第一个方法,画圆

Circle.prototype.draw = function() {

context.beginPath();

context.fillStyle = this.color;

context.arc(this.x, this.y, this.r, 0, Math.PI * 2);

context.fill();

}

//第二个方法,运动

Circle.prototype.move = function() {

//这里改变x递加的值,可以改变运动速度

this.x += this.speedX;

this.y += this.speedY;

if(this.x >= canvas.width - this.r || this.x <= this.r) {

this.speedX *= -1;

}

if(this.y >= canvas.height - this.r || this.y <= this.r) {

this.speedY *= -1;

}

}

function rand(min, max) {

return parseInt(Math.random() * (max - min) + min + 1)

}

var arr=[];

for(i = 0; i < 100; i++) {

var r = rand(10, 50);

var x = rand(r, canvas.width - r);

var y = rand(r, canvas.height - r);

var speedx=rand(1,10);

var speedy=rand(1,10)

var color = "rgb(" + rand(0, 255) + "," + rand(0, 255) + "," + rand(0, 255) + ")"

//实例化

var newCircle = new Circle(x, y, r, speedx, speedy, color);

//插入保存的数组!

arr.push(newCircle);

}

//执行

moveCircle();

//定义一个执行动画的函数

function moveCircle() {

//先清理画布

context.clearRect(0, 0, canvas.width, canvas.height);

for(var i=0;i


自由落体

var canvas = document.getElementById("myCanvas");

var context = canvas.getContext("2d");

var y = 0;

var lastTime;

//构造函数

function Rect(x, y, w, h, speedY) {

this.x = x;

this.y = y;

this.w = w;

this.h = h;

this.speedY = speedY;

}

//原型里面加方法

Rect.prototype.draw = function() {

context.beginPath();

context.fillStyle = "red";

context.fillRect(this.x, this.y, this.w, this.h);

}

Rect.prototype.move = function() {

//涌动的时间

var nowTime=new Date();

//时间差

var t=nowTime-lastTime;

//移动后的距离!

// 自由落体公式

var distance=9.8*t*t/2/283460;

this.y += distance;

//判断停止条件

if(this.y > (canvas.height - this.h)) {

this.y = canvas.height - this.h;

}

window.requestAnimationFrame(move);

}

var newRect = new Rect(200, 0, 50, 50, 2);

newRect.draw();

//移动

function move() {

context.clearRect(0, 0, canvas.width, canvas.height);

newRect.draw();

newRect.move();

}

canvas.onclick = function() {

lastTime=new Date();

move();

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 151,688评论 1 330
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 64,559评论 1 273
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 101,749评论 0 226
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,581评论 0 191
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,741评论 3 271
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,684评论 1 192
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,122评论 2 292
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,847评论 0 182
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,441评论 0 228
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,939评论 2 232
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,333评论 1 242
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,783评论 2 236
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,275评论 3 220
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,830评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,444评论 0 180
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,553评论 2 249
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,618评论 2 249

推荐阅读更多精彩内容

  • 一:canvas简介 1.1什么是canvas? ①:canvas是HTML5提供的一种新标签 ②:HTML5 ...
    GreenHand1阅读 4,597评论 2 32
  • 一、图形的组合方式 globalAlpha是一个介于0和1之间的值(包括0和1),用于指定所有绘制的透明度。默认值...
    空谷悠阅读 1,171评论 0 0
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,158评论 0 17
  • 看了一遍申肖克的救赎 记住了一句话大概的意思是你既然已经走了那么远来了 那就再前进一点。高考的脚步越来越近 黑板上...
    _鹿的角阅读 266评论 0 1
  • 观影人与制片方中,往往存在着那种不可调和的矛盾。 简单的说,是观众不能完整地表达出自己想要什么,制片方无法推测观众...
    青柯_阅读 150评论 0 0