java喷泉编码_Java干货分享使用JS实现简单喷泉效果

Java干货分享使用JS实现简单喷泉效果,最近,在教学生使用JS的基本操作,为了练习JS的基本作用,特地写了一个喷泉效果,代码如下:

页面代码:

body{

margin: 0px;

}

Particle.js代码如下:

window.onload = function(){

// 创建一个画布对象

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

// 设置大小和颜色

canvas.width = window.innerWidth;

canvas.height = window.innerHeight;

canvas.style.backgroundColor = "#333333";

// 将画布放置到body里

document.body.appendChild(canvas);

// 得到画笔

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

// 定义一个存放所有粒子的数组

var particles = [ ];

// 调用显示粒子

showParticle();

// 创建并显示粒子的方法

function showParticle(){

// 循环操作

setInterval(function(){

// 清空画布

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

// 创建粒子

var p = new Particle(canvas.width * 0.5, canvas.height * 0.5);

// 将粒子装入存放粒子的数组

particles.push(p);

// 循环更新所有粒子的位置

for (var i = 0;i

// 更新位置

particles[i].updateData();

}

}, 50);

}

function Particle(x, y){

// 原坐标

this.x = x;

this.y = y;

// 初始出现的改变的y的值

this.yVal = -5;

// 改变的x的值

this.xVal = Math.random() * 8 - 4;

// 定义一个下降的重力加速度

this.g = 0.1;

// 更新位置

this.updateData = function(){

// X值的变化

this.x = this.x + this.xVal;

// Y值的变化

this.y = this.y + this.yVal;

// 每次改变Y值速度的变化

this.yVal = this.yVal + this.g;

// 生成一个随机颜色

context.fillStyle = "#" + Math.floor(Math.random() * 0xffffff).toString(16);

// 将更新位置后的圆绘制出来

this.draw();

};

// 绘图的方法

this.draw = function(){

// 开始画图

context.beginPath();

// 画圆

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

// 结束画图

context.closePath();

// 填充

context.fill();

};

}

};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值