当前位置: 首页 > news >正文

【JavaScript】explode动画

这是一个js实现的粒子聚合文字或图片的动画特效

部分程序如下

        n.container = n.container[0] || n.container;   /*有且仅有一个container*/

        var width = n.params.width;
        var height = n.params.height;
        var wWidth = document.body.clientWidth;
        var wHeight = document.body.clientHeight;

        n.container.width = width;
        n.container.height = height;
        var ctx = n.container.getContext('2d');
        var c = document.createElement('canvas');
        var ct = c.getContext('2d');   /*用于绘制图片或文字*/
        var items = [];
        var picture = null;
        var requestId = null;
        var total = 0;
        var getRandom = function(max, min) {
            min = arguments[1] || 0;
            return Math.floor(Math.random() * (max - min + 1) + min); /*取min和max之间的随机数*/
        };

        function cutSlice() {/*cutSlice()方法,实现粒子动画,让其往最终位置运动*/
            ctx.clearRect(0, 0, n.container.width, n.container.height);
            for(var i = 0; i < c.width * c.height; i++) {
                var item = items[i];
                var targetX = item.targetX;
                var targetY = item.targetY;
                var currentX = item.currentX;
                var currentY = item.currentY;
                var ax = false;
                var ay = false;
                if(!item.isLock) {
                    if(Math.abs(targetX - currentX) <= .5) {
                        item.currentX = targetX;
                        ax = true;
                    } else {
                        item.currentX += (targetX - currentX) * item.ax;
                    };
                    if(Math.abs(targetY - currentY) <= .5) {
                        item.currentY = targetY;
                        ay = true;
                    } else {
                        item.currentY += (targetY - currentY) * item.ay;
                    };
                    if(ax && ay) {/*只有ax和ay同时到达终点时,total才会减1*/
                        total--;
                        item.isLock = true;
                    }
                };
                var ix = item.currentX;
                var iy = item.currentY;
                ctx.putImageData(item.data, ix, iy); /*putImageData() 方法将图像数据(从指定的 ImageData 对象)放回画布上。*/
            };
            if(total > 0) {
                requestId = requestAnimationFrame(cutSlice); /*不用设置间隔,反复调用*/
            } else {
                cancelAnimationFrame(requestId);

            };
        }

        function Item(data, targetX, targetY, currentX, currentY) {/*创建一个Item构造函数,用来放置每一个粒子*/
            this.data = data;
            this.targetX = targetX; /*聚合的最终位置*/
            this.targetY = targetY;
            this.currentX = currentX;/*当前位置*/
            this.currentY = currentY;
            this.ax = .13 - Math.random() * .06;  /*ax和ay分别表示运动速度*/
            this.ay = .16 - Math.random() * .08;
        }

        function drawCanvas() {
            if(n.params.type == 2) {  /*针对图片*/
                picture = new Image();
                picture.crossOrigin = "";
                picture.onload = function() {
                    var pw = picture.width;
                    var ph = picture.height;
                    c.width = pw;   /*设置canvas的宽度*/
                    c.height = ph;
                    ct.drawImage(picture, 0, 0, pw, ph, 0, 0, pw, ph);  /*把图像中的某个区域绘制到上下文中,源图像(起点和宽高),上下文中的起点和宽高*/
                    draw(pw, ph);
                };
                picture.src = n.params.img;
            } else {  /*针对文字*/
                var word = n.params.text;
                ct.font = '60px Arial';   /*这里指定用于测文本宽度*/
                var w = ct.measureText(word).width;   /*测文本宽度*/
                var h = 100;
                c.width = w;
                c.height = h;
                ct.fillStyle = 'deepskyblue';
                ct.font = '60px Arial';   /*这里指定用于绘制文本,应与之前设置保持一致*/
//                ct.textAlign = 'center';
                ct.textBaseline = 'middle';
                ct.fillText(word, 0, 50);  /*绘制文本,这里为什么没有直接绘制上去?而要调用draw???*/
                draw(w, h);
            }

            function draw(pw, ph) {/*draw 方法用来分解粒子,先分成cols 列和rows 行,每一个粒子高度都为1,然后用
getImageData() 来获取ImageData对象,然后创建新的Item实例,然后添加到items数组中。*/
                var w = 1;
                var h = 1;
                var cols = Math.floor(pw / w);/*图片或文字的宽度高度*/
                var rows = Math.floor(ph / h);
                for(var i = 0; i < c.width * c.height; i++) {
                    var x = Math.floor(i % cols); /*通过xy找到每一行的所有元素(0,0)(1,0)...(0,1)(1,1)(2,1)*/
                    var y = Math.floor(i / cols);
                    var data = ct.getImageData(x * w, y * h, w, h);/* 文字也能获取??拷贝!取得原始图像数据,要取得取数据的画面区域的xy坐标以及该区域的像素宽度和高度,这里每次取1*1像素*/
                    var vx = getRandom(300, -300);
                    var vy = getRandom(500, -500); /*扩大范围,使图片周围也有粒子*/
                    var item = new Item(data, x, y, x + vx, y + vy);
                    items.push(item);
                };
                total = items.length;
                cutSlice();
            }
        }

 

转载于:https://www.cnblogs.com/yujihang/p/7002993.html

相关文章:

  • 花了 4 个月整理了 50 篇 Android 干货文章
  • centos7 双网卡双ip内外网设置最小化安装
  • 移动端web app开发备忘
  • 人生最重要的三种能力,不是读书能学来的
  • 解密浏览器缓存机制
  • OGG运维优化脚本(五)-信息修改类--批量注释
  • 拥抱PBO(基于项目的组织)聚焦核心价值创造
  • awk条件语句
  • ES5新特性
  • MySQL用命令行复制表,查看表结构
  • java并发之TimeUnit理解
  • spring定时任务
  • 读《十亿美金的教训》看中国传奇人物
  • TCP端口状态说明ESTABLISHED、TIME_WAIT
  • http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException 异常
  • [ JavaScript ] 数据结构与算法 —— 链表
  • 2017-08-04 前端日报
  • Angular 响应式表单之下拉框
  • axios 和 cookie 的那些事
  • CentOS7 安装JDK
  • express.js的介绍及使用
  • JAVA并发编程--1.基础概念
  • nodejs:开发并发布一个nodejs包
  • Octave 入门
  • SOFAMosn配置模型
  • Spark VS Hadoop:两大大数据分析系统深度解读
  • Synchronized 关键字使用、底层原理、JDK1.6 之后的底层优化以及 和ReenTrantLock 的对比...
  • thinkphp5.1 easywechat4 微信第三方开放平台
  • 初探 Vue 生命周期和钩子函数
  • 汉诺塔算法
  • 适配iPhoneX、iPhoneXs、iPhoneXs Max、iPhoneXr 屏幕尺寸及安全区域
  • 消息队列系列二(IOT中消息队列的应用)
  • 一些基于React、Vue、Node.js、MongoDB技术栈的实践项目
  • puppet连载22:define用法
  • Spring第一个helloWorld
  • #NOIP 2014#day.2 T1 无限网络发射器选址
  • #stm32整理(一)flash读写
  • #鸿蒙生态创新中心#揭幕仪式在深圳湾科技生态园举行
  • %3cli%3e连接html页面,html+canvas实现屏幕截取
  • (day18) leetcode 204.计数质数
  • (echarts)echarts使用时重新加载数据之前的数据存留在图上的问题
  • (html转换)StringEscapeUtils类的转义与反转义方法
  • (八)Flink Join 连接
  • (六)c52学习之旅-独立按键
  • (算法)硬币问题
  • (一)、python程序--模拟电脑鼠走迷宫
  • (已解决)什么是vue导航守卫
  • ***详解账号泄露:全球约1亿用户已泄露
  • .JPG图片,各种压缩率下的文件尺寸
  • .NET CORE 3.1 集成JWT鉴权和授权2
  • .NET Core WebAPI中使用swagger版本控制,添加注释
  • .net framwork4.6操作MySQL报错Character set ‘utf8mb3‘ is not supported 解决方法
  • .net mvc 获取url中controller和action
  • .NET MVC之AOP
  • .Net 基于MiniExcel的导入功能接口示例