特效描述:利用H5实现 Canvas 五彩纸屑飘落 动画特效。利用H5实现Canvas五彩纸屑飘落动画特效

代码结构

1. HTML代码

"use strict";

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Progress = function () {

function Progress() {

var param = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];

_classCallCheck(this, Progress);

this.timestamp = null;

this.duration = param.duration || Progress.CONST.DURATION;

this.progress = 0;

this.delta = 0;

this.progress = 0;

this.isLoop = !!param.isLoop;

this.reset();

}

Progress.prototype.reset = function reset() {

this.timestamp = null;

};

Progress.prototype.start = function start(now) {

this.timestamp = now;

};

Progress.prototype.tick = function tick(now) {

if (this.timestamp) {

this.delta = now - this.timestamp;

this.progress = Math.min(this.delta / this.duration, 1);

if (this.progress >= 1 && this.isLoop) {

this.start(now);

}

return this.progress;

} else {

return 0;

}

};

_createClass(Progress, null, [{

key: "CONST",

get: function get() {

return {

DURATION: 1000

};

}

}]);

return Progress;

}();

var Confetti = function () {

function Confetti(param) {

_classCallCheck(this, Confetti);

this.parent = param.elm || document.body;

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

this.ctx = this.canvas.getContext("2d");

this.width = param.width || this.parent.offsetWidth;

this.height = param.height || this.parent.offsetHeight;

this.length = param.length || Confetti.CONST.PAPER_LENGTH;

this.yRange = param.yRange || this.height * 2;

this.progress = new Progress({

duration: param.duration,

isLoop: true

});

this.rotationRange = typeof param.rotationLength === "number" ? param.rotationRange : 10;

this.speedRange = typeof param.speedRange === "number" ? param.speedRange : 10;

this.sprites = [];

this.canvas.style.cssText = ["display: block", "position: absolute", "top: 0", "left: 0", "pointer-events: none"].join(";");

this.render = this.render.bind(this);

this.build();

this.parent.append(this.canvas);

this.progress.start(performance.now());

requestAnimationFrame(this.render);

}

Confetti.prototype.build = function build() {

for (var i = 0; i < this.length; ++i) {

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

ctx = canvas.getContext("2d");

canvas.width = Confetti.CONST.SPRITE_WIDTH;

canvas.height = Confetti.CONST.SPRITE_HEIGHT;

canvas.position = {

initX: Math.random() * this.width,

initY: -canvas.height - Math.random() * this.yRange

};

canvas.rotation = this.rotationRange / 2 - Math.random() * this.rotationRange;

canvas.speed = this.speedRange / 2 + Math.random() * (this.speedRange / 2);

ctx.save();

ctx.fillStyle = Confetti.CONST.COLORS[Math.random() * Confetti.CONST.COLORS.length | 0];

ctx.fillRect(0, 0, canvas.width, canvas.height);

ctx.restore();

this.sprites.push(canvas);

}

};

Confetti.prototype.render = function render(now) {

var progress = this.progress.tick(now);

this.canvas.width = this.width;

this.canvas.height = this.height;

for (var i = 0; i < this.length; ++i) {

this.ctx.save();

this.ctx.translate(this.sprites[i].position.initX + this.sprites[i].rotation * Confetti.CONST.ROTATION_RATE * progress, this.sprites[i].position.initY + progress * (this.height + this.yRange));

this.ctx.rotate(this.sprites[i].rotation);

this.ctx.drawImage(this.sprites[i], -Confetti.CONST.SPRITE_WIDTH * Math.abs(Math.sin(progress * Math.PI * 2 * this.sprites[i].speed)) / 2, -Confetti.CONST.SPRITE_HEIGHT / 2, Confetti.CONST.SPRITE_WIDTH * Math.abs(Math.sin(progress * Math.PI * 2 * this.sprites[i].speed)), Confetti.CONST.SPRITE_HEIGHT);

this.ctx.restore();

}

requestAnimationFrame(this.render);

};

_createClass(Confetti, null, [{

key: "CONST",

get: function get() {

return {

SPRITE_WIDTH: 9,

SPRITE_HEIGHT: 16,

PAPER_LENGTH: 100,

DURATION: 8000,

ROTATION_RATE: 50,

COLORS: ["#EF5350", "#EC407A", "#AB47BC", "#7E57C2", "#5C6BC0", "#42A5F5", "#29B6F6", "#26C6DA", "#26A69A", "#66BB6A", "#9CCC65", "#D4E157", "#FFEE58", "#FFCA28", "#FFA726", "#FF7043", "#8D6E63", "#BDBDBD", "#78909C"]

};

}

}]);

return Confetti;

}();

(function () {

var DURATION = 8000,

LENGTH = 120;

new Confetti({

width: window.innerWidth,

height: window.innerHeight,

length: LENGTH,

duration: DURATION

});

setTimeout(function () {

new Confetti({

width: window.innerWidth,

height: window.innerHeight,

length: LENGTH,

duration: DURATION

});

}, DURATION / 2);

})();

android 五彩纸屑动画,利用H5实现Canvas五彩纸屑飘落动画特效相关推荐

  1. html画布敲碎的视频,html5 canvas碎纸屑空中飘落动画特效

    特效描述:html5 canvas碎纸屑 空中飘落动画特效.canvas绘制空中飘落的纸屑动画特效. 代码结构 1. HTML代码 "use strict"; var _creat ...

  2. HTML5 canvas绘制雪花飘落动画(需求分析、知识点、程序编写分布详解)

    HTML5 canvas绘制雪花飘落动画(需求分析.知识点.程序编写分布详解) 原文:HTML5 canvas绘制雪花飘落动画(需求分析.知识点.程序编写分布详解) 看到网上很多展示html5雪花飞动 ...

  3. html动画转换为gif,h5使用canvas输出为gif动画图片

    使用h5的canvas提供的接口很容易实现一些动画,如果能把这些动画实现成gif动图就更好啦,还真有大神实现啦这个功能下面说明使用方法 开源库地址 首先引入需要的js文件gif.js在页面里自己创建一 ...

  4. 利用H5的canvas画一个时钟

    学习了一周的canvas了,看了一些资料和视频,跟着视频也画了一些统计图和圆,矩形,线之类的,跟着视频也做了一个钟表,话不多说,直接上代码 canvas上套一个div盒子,作为钟表的背景 <di ...

  5. android 矢量粒子动画,android 手摸手教你用 Canvas 实现简单粒子动画

    Article Attributes name format description 中文解释 pv_host_text string set left host text 设置左边主文案 pv_ho ...

  6. 如何用html5 canvas制作子画面动画,如何用HTML5 Canvas制作子画面动画

    用户提问 为了用DOM做2D游戏,你基本上要动态地调整元素风格,以便在页面上移动它.虽然有些时候DOM修改是很好的,但这一次我将重点介绍使用HTML5 Canvas来制作图像,因为对于现代浏览器,它是 ...

  7. canvas动画科技园_构建canvas动画框架(一)

    最近一直在做canvas动画效果,发现canvas这个东西做动画不是不可以.相对于flash,它太底层.如果有给力的编辑器或者给力的框架的话,它就能发挥出更大的威力. 于是决定自己写一个简单一点的动画 ...

  8. android 五彩纸屑动画,HTML5-Canvas五彩纸屑飘落动画特效

    HTML5-Canvas五彩纸屑飘落动画特效 代码片段: var _createClass = function () { function defineProperties(target, prop ...

  9. html5绘制草,利用html5实现canvas海底水草动画特效

    特效描述:利用html5实现 canvas海底水草动画特效.利用html5实现canvas海底水草动画特效 代码结构 1. HTML代码 var canvas, ctx, width, height, ...

最新文章

  1. python基础语法合集-Python基础语法合集.zip
  2. DeepLearning索引
  3. Hibernate事务管理
  4. mongodb查询分页优化(二)
  5. 前端框架TopJUI使用心得
  6. Linux调试智能卡环境搭建(二),其中包含Linux编译链接动态库相关
  7. 学习帮——提高智商、改善记忆力的120种绝佳方法!
  8. c语言程序中TMOD,keil 中用c 语言写的代码 error C231: 'TMOD': redefinition
  9. vue项目中如何解决跨域问题
  10. [Bilingual]Klein四元群的四个例子Four examples of Klein four-group
  11. 图形学基础知识:走样和反走样,频域和滤波
  12. BAT脚本清理注册表信息
  13. 三次样条拟合(附完整代码)
  14. 使用计算机的场所和用途,天天在使用计算机,很多东西我竟然不知道!
  15. 浏览器内核和Standards模式与Quirks模式
  16. 免费的 AI 动作捕捉工具 #Rokoko Video
  17. Linux删除当前日志的前1万行
  18. php 图片透明,PHP_功能强大的PHP图片处理类(水印、透明度、旋转),非常强大的php图片处理类,可 - phpStudy...
  19. excel技巧——时间复制粘贴后变成数字
  20. 网络连接变成小地球,提示无法访问internet

热门文章

  1. 米老鼠背后的男人:一文带你认识字节跳动新COO Kevin Mayer
  2. 排位赛一 B MooBuzz
  3. 全球及中国自拍环形灯行业市场需求及未来竞争动向展望报告2022-2027年
  4. WIN10+Ubuntu18.04双系统安装记录20191228
  5. android休眠 wifi唤醒,Android深度睡眠和唤醒锁
  6. 2022-07-16 Android app获取屏幕分辨率
  7. 关于开发人员聘用–房间里的大象
  8. RTKLIB rtkpost
  9. 开源流程引擎camunda需要扩展哪些功能
  10. Error: Unable to access jarfile /www/wwwroot/xxxx.jar--server.port=5635 问题解决