分享一个水珠自动下落的插件,下载地址:https://github.com/foreverjiangting/rainyday.js
下面来看看如何使用它?添加下面代码即可运行它。
实现效果如下:
代码如下:
<!DOCTYPE HTML> <html> <head> <style> </style> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <script type="text/javascript"> function runImage() { var image=document.getElementById("img"); image.src="https://www.jb51.net/article/4.jpg"; image.onload=function() { //设定一个rain对象 var engine=new RainyDay({image:this,}); //调用rain函数 engine.rain([[4,6,8000]]);//设定雨滴大小4,6 数量为8000 ||也可为 engine.rain([[6,8000]]),此时水珠的大小较小 engine.rain([[3,3,0.88],[5,5,0.9],[6,2,1]],100);//设定雨滴重复时间 } // image.crossOrigin="jt"; 加载跨域图片 } </script> </head> <body > <div> <img src="https://www.jb51.net/article/4.jpg" alt="点击图片"> </div> <script type="text/javascript" src="https://www.jb51.net/rainy.js"></script> </body> </html>
下面来研究下rainy.js部分代码,源代码见上面的github里面的:
RainyDay.prototype.rain = function(presets, speed) { // 准备canvas 进行下落映射 if (this.reflection !== this.REFLECTION_NONE) { this.prepareReflections(); } this.animateDrops(); // 动画 this.presets = presets; this.PRIVATE_GRAVITY_FORCE_FACTOR_Y = (this.options.fps * 0.001) / 25; this.PRIVATE_GRAVITY_FORCE_FACTOR_X = ((Math.PI / 2) - this.options.gravityAngle) * (this.options.fps * 0.001) / 50; // 准备下落的模型 if (this.options.enableCollisions) { // 计算最大的下落水珠圆角 var maxDropRadius = 0; for (var i = 0; i < presets.length; i++) { if (presets[i][0] + presets[i][1] > maxDropRadius) { maxDropRadius = Math.floor(presets[i][0] + presets[i][1]); } } if (maxDropRadius > 0) { // 初始化下落的模型 var mwi = Math.ceil(this.canvas.width / maxDropRadius); var mhi = Math.ceil(this.canvas.height / maxDropRadius); this.matrix = new CollisionMatrix(mwi, mhi, maxDropRadius); } else { this.options.enableCollisions = false; } } for (var i = 0; i < presets.length; i++) { if (!presets[i][3]) { presets[i][3] = -1; } } var lastExecutionTime = 0; this.addDropCallback = function() { var timestamp = new Date().getTime(); if (timestamp - lastExecutionTime < speed) { return; } lastExecutionTime = timestamp; var context = this.canvas.getContext('2d'); context.clearRect(0, 0, this.canvas.width, this.canvas.height); context.drawImage(this.background, 0, 0, this.canvas.width, this.canvas.height); // 选择匹配的模型 var preset; for (var i = 0; i < presets.length; i++) { if (presets[i][2] > 1 || presets[i][3] === -1) { if (presets[i][3] !== 0) { presets[i][3]--; for (var y = 0; y < presets[i][2]; ++y) { this.putDrop(new Drop(this, Math.random() * this.canvas.width, Math.random() * this.canvas.height, presets[i][0], presets[i][1])); } } } else if (Math.random() < presets[i][2]) { preset = presets[i]; break; } } if (preset) { this.putDrop(new Drop(this, Math.random() * this.canvas.width, Math.random() * this.canvas.height, preset[0], preset[1])); } context.save(); context.globalAlpha = this.opacity; context.drawImage(this.glass, 0, 0, this.canvas.width, this.canvas.height); context.restore(); } .bind(this); };
这里我想提到关于跨域资源的问题,image.crossOrigin="jt"; 加载跨域图片。刚开始我用的是跨域的图片,但出现问题了,然后我就使用本地的图片加载,即 src="https://www.jb51.net/article/4.jpg" ,就没问题了。其实如果你要使用跨域的图片,只要加上image.crossOrigin="jt"; 这句代码就可以了。
(前提是服务器开了允许的权限)
这里涉及到CORS的问题,我们来看下: CORS全称是"跨域资源共享"(Cross-origin resource sharing)。它允许浏览器向跨源服务器
发出xmlhttprequest请求从而克服了AJAX只能同源使用的限制。
我们还用上面的列子来看问题:
<!DOCTYPE HTML> <html> <head> <style> </style> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <script type="text/javascript"> function runImage() { var image=document.getElementById("img"); image.onload=function() { //设定一个rain对象 var engine=new RainyDay({image:this,}); engine.rain([[4,6,8000]]);//设定雨滴大小4,6 数量为8000 engine.rain([[3,3,0.88],[5,5,0.9],[6,2,1]],100);//设定雨滴重复时间 } // image.crossOrigin="jt"; //跨域图片 image.src="https://img0.imgtn.bdimg.com/it/u=938096994,3074232342&fm=21&gp=0.jpg"; } </script> </head> <body > <div> <img src="https://img0.imgtn.bdimg.com/it/u=938096994,3074232342&fm=21&gp=0.jpg" alt="点击图片"> </div> <script type="text/javascript" src="https://www.jb51.net/rainy.js"></script> </body> </html>
我们来看下调试控制台里面的信息: