⼿机编程html流星代码,js代码实现流星⾬
⼀起来看流星⾬
var context;
var arr = new Array();
var starCount = 800;
var rains = new Array();
var rainCount =20;
//初始化画布及context
function init(){
//获取canvas
var stars = ElementById("stars");
windowWidth = window.innerWidth; //当前的窗⼝的⾼度
stars.width=windowWidth;
stars.height=window.innerHeight;
//获取context
context = Context("2d");
}
/
/创建⼀个星星对象
var Star = function (){
this.x = windowWidth * Math.random();//横坐标
this.y = 5000 * Math.random();//纵坐标
<=".";//⽂本
//产⽣随机颜⾊
var _r = Math.random();
if(_r<0.5){
}else{
}
}
//初始化
this.init=function(){
}
//绘制
this.draw=function(){
context.lor;
context.,this.x,this.y); }
}
//画⽉亮
function drawMoon(){
var moon = new Image();
moon.src = "./images/moon.jpg" context.drawImage(moon,-5,-10);
}
//页⾯加载的时候
init();
//画星星
for (var i=0;i
var star = new Star();
star.init();
star.draw();
arr.push(star);
}
渐变颜代码大全//画流星
for (var i=0;i
var rain = new MeteorRain();
rain.init();
rain.draw();
rains.push(rain);
}
drawMoon();//绘制⽉亮
playStars();//绘制闪动的星星
playRains();//绘制流星
}
//星星闪起来
function playStars(){
for (var n = 0; n < starCount; n++){
arr[n].getColor();
arr[n].draw();
}
setTimeout("playStars()",100);
}
/*流星⾬开始*/
var MeteorRain = function(){
this.x = -1;
this.y = -1;
this.length = -1;//长度
this.angle = 30; //倾斜⾓度
this.width = -1;//宽度
this.height = -1;//⾼度
this.speed = 1;//速度
this.offset_x = -1;//横轴移动偏移量this.offset_y = -1;//纵轴移动偏移量this.alpha = 1; //透明度
/****************初始化函数********************/ this.init = function () //初始化
{
this.alpha = 1;//透明度
//最⼩长度,最⼤长度
var x = Math.random() * 80 + 150;
this.length = il(x);
// x = Math.random()*10+30;
this.angle = 30; //流星倾斜⾓
x = Math.random()+0.5;
this.speed = il(x); //流星的速度
var cos = s(this.angle*3.14/180);
var sin = Math.sin(this.angle*3.14/180) ;
this.width = this.length*cos ; //流星所占宽度
this.height = this.length*sin ;//流星所占⾼度
this.offset_x = this.speed*cos ;
this.offset_y = this.speed*sin;
}
/**************获取随机颜⾊函数*****************/
var a = il(255-240* Math.random());
//中段颜⾊
//结束颜⾊
}
/***************重新计算流星坐标的函数******************/ untPos = function ()//
{
/
/往左下移动,x减少,y增加
this.x = this.x - this.offset_x;
this.y = this.y + this.offset_y;
}
/*****************获取随机坐标的函数*****************/
{
//横坐标200--1200
this.x = Math.random() * window.innerWidth; //窗⼝⾼度//纵坐标⼩于600
this.y = Math.random() * window.innerHeight; //窗⼝宽度}
/****绘制流星***************************/
this.draw = function () //绘制⼀个流星的函数
{
context.save();
context.beginPath();
context.lineWidth = 1; //宽度
context.globalAlpha = this.alpha; //设置透明度
//创建横向渐变颜⾊,起点坐标⾄终点坐标
var line = ateLinearGradient(this.x, this.y, this.x + this.width,
this.y - this.height);
//分段设置颜⾊
line.addColorStop(0, "white");
line.addColorStop(0.3, lor1);
line.addColorStop(0.6, lor2);
context.strokeStyle = line;
//起点
//终点
context.lineTo(this.x + this.width, this.y - this.height); context.closePath();
context.stroke();
}
/
/清空流星像素
var x = this.x+this.width-this.offset_x;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论