1 Star 0 Fork 0

溪的那边/html

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
下雪.html 7.52 KB
一键复制 编辑 原始数据 按行查看 历史
溪的那边 提交于 2018-06-10 14:44 . html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>下雪</title>
<style>
body {
background:aqua;
}
</style>
</head>
<body>
<button id="start-snowFall">开始<tton>
<button id="stop-snowFall">停止<tton>
<button id="pause-snowFall">暂停<tton>
<button id="resume-snowFall">回复<tton>
<script>
window.onload = function () {
//绑定事件
var snow = new snowFall({ maxFlake: 200 });
var start = document.getElementById('start-snowFall'),
stop = document.getElementById('stop-snowFall'),
pause = document.getElementById('pause-snowFall'),
resume = document.getElementById('resume-snowFall');
start.onclick = function () {
snow.start();
}
//停止
stop.onclick = function () {
snow.stop();
}
//暂停
pause.onclick = function () {
snow.pause();
}
//回复
resume.onclick = function () {
snow.resume();
}
}
//控制对象封装
function snowFall(snow) {
//可配置属性
snow = snow || {};
this.maxFlake = snow.maxFlake || 200; //最多骗术
this.flakeSize = snow.flakeSize || 10;// 雪花形状
this.fallSpeed = snow.fallSpeed || 2;//坠落速度
this.status = 0; //0-初始化、1-开始下雪 、2--停止下雪 、3--暂停下雪、4--继续下雨
}
//兼容写法
requestAnimationFrame = window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame ||
window.oRequestAnimationFrame ||
function (callback) { setTimeout(callback, 1000 / 60) };
cancelAnimationFrame = window.cancelAnimationFrame ||
window.mozCancelAnimationFrame ||
window.webkitCancelAnimationFrame ||
window.msCancelAnimationFrame ||
window.oCancelAnimationFrame;
snowFall.prototype.start = function () {
if (this.status == 1 || this.status == 4)
return false;
this.status = 1;
//创建画布
snowCanvas.apply(this);
//创建雪花形状
createFlakes.apply(this);
//画学
drawSnow.apply(this);
}
//停止下雪
snowFall.prototype.stop = function () {
if (this.status == 2 || this.status == 0 || !this.canvas) {
return false;
}
//定制动画循环
this.pause();
this.status = 2;
//删除画布
this.canvas.parentNode.removeChild(this.canvas);
this.canvas = null;
};
//暂停下雪
snowFall.prototype.pause = function () {
if (this.status == 3) {
return false;
}
this.status = 3;
cancelAnimationFrame(this.loop);
}
//继续下雪
snowFall.prototype.resume = function () {
if (this.status == 3 && this.canvas) {
this.status = 4;
//动画的计时控制
this.loop = requestAnimationFrame(function () {
drawSnow.apply(that);
});
}
}
//创建画布
function snowCanvas() {
//添加Dom节点
var snowcanvas = document.createElement('canvas');
snowcanvas.id = 'snowfall';
snowcanvas.width = window.innerWidth;
snowcanvas.height = window.innerHeight;
snowcanvas.setAttribute('style', 'position:fixed;top:0;left:0;z-index:2999;pointer-events:none;');
document.getElementsByTagName('body')[0].appendChild(snowcanvas);
this.canvas = snowcanvas;
this.ctx = snowcanvas.getContext('2d');
window.onresize = function () {
snowcanvas.width = window.innerWidth;
snowcanvas.height = window.innerHeight;
}
}
//创建雪花
function createFlakes() {
var maxFlake = this.maxFlake,
flakes = this.flakes = [],
canvas = this.canvas;
for (var i = 0; i < maxFlake; i++) {
flakes.push(new flakeMove(canvas.width, canvas.height, this.flakeSize, this.fallSpeed));
}
}
function flakeMove(canvasWidth, canvasHeight, flakeSize, fallSpeed) {
this.x = Math.floor(Math.random() * canvasWidth); //x坐标
this.y = Math.floor(Math.random() * canvasHeight); //y坐标
this.size = Math.random() * flakeSize + 2; //形状
this.masSize = flakeSize; //最大形状
this.speed = Math.random() * 1 + fallSpeed; //坠落速度
this.fallSpeed = fallSpeed; //坠落速度
this.velY = this.speed; //y方向速度
this.velX = 0; //x方向速度
this.stepSize = Math.random() / 30; //步长
this.step = 0; //步数
}
//重置当前雪花的位置
flakeMove.prototype.update = function () {
var x = this.x,
y = this.y;
//左右摆动(余弦)
this.velX *= 0.98;
if (this.velY <= this.speed) {
this.velY = this.speed;
}
this.velX += Math.cos(this.step += 0.05) * this.stepSize;
this.y += this.velY;
this.x += this.velX;
//飞出边界处理
if (this.x >= canvas.width || this.x <= 0 || this.y >=canvas.height || this.y <= 0) {
this.reset(canvas.width,canvas.height);
}
}
flakeMove.prototype.reset = function (width, height) {
this.x = Math.floor(Math.random() * width);
this.y = 0;
this.size = Math.random() * this.masSize + 2;
this.speed = Math.random() * 1 + this.fallSpeed;
this.velY = this.speed;
this.velX = 0;
}
//渲染雪花--随机形状
flakeMove.prototype.render = function (ctx) {
var snowFlake = ctx.createRadialGradient(this.x, this.y, 0, this.x, this.y, this.size);
snowFlake.addColorStop(0, 'rgba(255,255,255,0.9)');
snowFlake.addColorStop(0.5, 'rgba(255,255,255,0.5)');
snowFlake.addColorStop(1, 'rgba(255,255,255,0)');
ctx.save();
ctx.fillStyle = snowFlake;
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.fill();
ctx.restore();
}
//滑雪
function drawSnow() {
var maxFlake = this.maxFlake,
flakes = this.flakes;
ctx = this.ctx,
canvas = this.canvas,
that = this;
//清空
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (var i = 0; i < maxFlake; i++) {
flakes[i].update();
flakes[i].render(ctx);
}
//一折一帧的画
this.loop = requestAnimationFrame(function () {
drawSnow.apply(that);
});
}
</script>
</body>
</html>
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
HTML
1
https://gitee.com/wgc942/html.git
[email protected]:wgc942/html.git
wgc942
html
html
master

搜索帮助