球动画设计HTML5,html5 canvas炫彩运动小球动画特效

这篇博客介绍了如何使用HTML5的Canvas API和面向对象编程实现一个炫彩小球运动的动画特效。代码示例展示了如何创建随机颜色和速度的小球,并在Canvas画布上进行动态移动,形成视觉效果丰富的动画。
摘要由CSDN通过智能技术生成

特效描述:html5 canvas 炫彩运动 小球动画特效。html5用canvas加面向对象 制作的运动小球动画特效

代码结构

1. HTML代码

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

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

var maxWidth=canvas.width;

var maxHeight=canvas.height;

var colors = ["#33B5E5", "#0099CC", "#AA66CC", "#9933CC", "#99CC00", "#669900", "#FFBB33", "#FF8800", "#FF4444", "#CC0000"]

//随机数

function random(min,max){

return Math.floor(Math.random()*(max-min)+min)

}

//构造函数

function Ball(){

this.a=true;

this.b=true;

this.r=random(10,30);

this.ballColor={color: colors[Math.floor(Math.random() * colors.length)]}

this.vx=random(30,maxWidth-30);

this.vy=random(30,maxHeight-30);

this.ispeed=random(1,10);

this.ispeed2=random(1,10);

}

// 面向对象

Ball.prototype.moveBall=function(){

context.beginPath();

if (this.a) {

this.vx += this.ispeed;

if (this.vx>=maxWidth-this.r) {

this.a = false;

}

} else {

this.vx -= this.ispeed;

if (this.vx <= this.r) {

this.a = true;

}

}

if (this.b) {

this.vy+= this.ispeed2;

if (this.vy >= maxHeight-this.r) {

this.b = false;

}

} else {

this.vy -= this.ispeed2;

if (this.vy <= this.r) {

this.b = true;

}

}

context.fillStyle=this.ballColor.color;

context.arc(this.vx,this.vy,this.r,0,Math.PI*2,false);

context.fill();

}

var Aball=[];

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

Aball[i]=new Ball();

}

setInterval(function(){

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

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

Aball[i].moveBall();

}

},30)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值