js实现鼠标控制方向的canvas下雨粒子效果代码
代码语言:html
所属分类:粒子
代码描述:js实现鼠标控制方向的canvas下雨粒子效果代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html > <head> <meta charset="UTF-8"> <title></title> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> html, body, canvas, #instructions { position: absolute; width: 100%; height: 100%; } body { /* FF3.6-15 */ background: -webkit-linear-gradient(top, #080d28 0%, #261300 100%); /* Chrome10-25,Safari5.1-6 */ background: linear-gradient(to bottom, #080d28 0%, #261300 100%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */ } #instructions { opacity: 1; -webkit-transition: opacity 2s; transition: opacity 2s; background-repeat: no-repeat; background-position: center center; background-size: contain; background-image: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB2ZXJzaW9uPSIxLjEiIGlkPSJMYXllcl8xIiB4PSIwcHgiIHk9IjBweCIgd2lkdGg9IjI1NnB4IiBoZWlnaHQ9IjI1NnB4IiB2aWV3Qm94PSIwIDAgMjU2IDI1NiIgc3R5bGU9ImVuYWJsZS1iYWNrZ3JvdW5kOm5ldyAwIDAgMjU2IDI1NjsiIHhtbDpzcGFjZT0icHJlc2VydmUiPjxwb2x5Z29uIHN0eWxlPSJmaWxsOiNGRkZGRkY7IiBwb2ludHM9IjIyLjE0LDQ4IDM3LjA0NSwyMi4xODMgNTEuOTUxLDQ4Ii8+PHBvbHlnb24gc3R5bGU9ImZpbGw6I0ZGRkZGRjsiIHBvaW50cz0iNTEuOTUxLDE4NiAzNy4wNDUsMjExLjgxNiAyMi4xNCwxODYiLz48cG9seWdvbiBzdHlsZT0iZmlsbDojRkZGRkZGOyIgcG9pbnRzPSI3MCwyMzMuODEyIDQ0LjE4NCwyMTguOTA2IDcwLDIwNC4w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maWxsOiNGRkZGRkY7IGZvbnQtZmFtaWx5OidBcmlhbC1Cb2xkTVQnOyBmb250LXNpemU6MTY7Ij5SQUlOIElOVEVOU0lUWTwvdGV4dD48L3N2Zz4=); } @media (max-height: 400px) { #instructions { display: none; } } </style> </head> <body> <div id="instructions"></div> <canvas id="canvas">Canvas is not supported in your browser</canvas> <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/dat.gui-min.js"></script> <script > // Falling rain simulation using 2D canvas // - vanilla JS, no frameworks // - framerate independent physics // - slow-mo / fast-forward support via demo.speed // - supports high-DPI screens // - falling rain particles are drawn as vector lines // - splash particles are lazily pre-rendered so gradients aren't computed each frame // - all particles make use of object pooling to further boost performance // initialize document.addEventListener("DOMContentLoaded", function() { demo.init(); window.addEventListener('resize', demo.resize); }); // demo namespace var demo = { // CUSTOMIZABLE PROPERTIES // - physics speed multiplier: allows slowing down or speeding up simulation speed: 1, // - color of particles color: { r: '80', g: '175', b: '255', a: '0.5' }, // END CUSTOMIZATION // whether demo is running started: false, // canvas and associated context references canvas: null, ctx: null, // viewport dimensions (DIPs) width: 0, height: 0, // devicePixelRatio alias (should only be used for rendering, physics shouldn't care) dpr: window.devicePixelRatio || 1, // time since last drop drop_time: 0, // ideal time between drops (changed with mouse/finger) drop_delay: 25, // wind applied to rain (changed with mouse/finger) wind: 4, // color of rain (set in init) rain_color: null, rain_color_clear: null, // rain particles rain: [], rain_pool: [], // rain droplet (splash) particles drops: [], drop_pool: [] }; // demo initialization (should only run once) demo.init = function() { if (!demo.started) { demo.started = true; demo.canvas = document.getElementById('canvas'); demo.ctx = demo.canvas.getContext('2d'); var c = demo.color; demo.rain_color = 'rgba(' + c.r + ',' + c.g + ',' + c.b + ',' + c.a + ')'; demo.rain_color_clear = 'rgba(' + c.r + ',' + c.g + ',' + c.b + ',0)'; demo.resize(); Ticker.addListener(demo.step); // const gui = new dat.GUI(); gui.add(demo, 'speed', 0.2, 2); // fade out instructions after a few seconds var instructions = document.getElementById('instructions'); setTimeout(function() { instructions.style.opacity = 0; setTimeout(function(){ instructions.parentNode.removeChild(instructions); }, 2000); }, 4000); } } // (re)size canvas (clears all particles) demo.resize = function() { // localize common references var rain = demo.rain; var drops = demo.drops; // recycle particles for (var i = rain.length - 1; i >= 0; i--) { rain.pop().recycle(); } for (var i = drops.length - 1; i >= 0; i--) { drops.pop().recycle(); } // resize demo.width = window.innerWidth; demo.height = window.innerHeight; demo.canvas.width = demo.width * demo.dpr; demo.canvas.height = demo.height * demo.dpr; } demo.step = function(time, lag) { // localize common references var demo = window.demo; var speed = demo.speed; var width = demo.width; var height = demo.height; var wind = demo.wind; var rain = demo.rain; var rain_pool = demo.rain_pool; var drops = demo.drops; var drop_pool = demo.drop_pool; // multiplier for physics var multiplier = speed * lag; // spawn drops demo.drop_time += time * speed; while (demo.drop_time > demo.drop_delay) { de.........完整代码请登录后点击上方下载按钮下载查看
网友评论0