js实现canvas流体粒子模拟流动动画效果代码
代码语言:html
所属分类:粒子
代码描述:js实现canvas流体粒子模拟流动动画效果代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<html> <head> <meta charset="UTF-8"> <style> body { margin: 0; background: #000; } h1 { color: #ffffff; text-align: center; font-size: 20px; } canvas { position: absolute; top: 0; right: 0; bottom: 0; left: 0; margin: auto; border: 2px solid white; } </style> </head> <body > <h1>单击并拖动!</h1> <canvas id="c" width="500" height="500"></canvas><script> /* Comments were requested, here we go :) Here's the rundown: This script creates a grid of cells and a separate layer of particles that float on top of the grid. Each cell of the grid holds X and Y velocity (direction and magnitude) values and a pressure value. Whenever the user holds down and moves their mouse over the canvas, the velocity of the mouse is calculated and is used to influence the velocity and pressure in each cell that was within the defined range of the mouse coordinates. Then, the pressure change is communicated to all of the neighboring cells of those affected, adjusting their velocity and pressure, and this is repeated over and over until the change propogates to all of the cells in the path of the direction of movement. The particles are randomly placed on the canvas and move according to the velocity of the grid cells below, similar to grass seed floating on the surface of water as it's moving. Whenever the particles move off the edge of the canvas, they are "dropped" back on to the canvas in a random position. The velocity, however, is "wrapped" around to the opposite edge of the canvas. The slowing down of the movement is simulated viscosity, which is basically frictional drag in the liquid. Let's get started: -------- This is a self-invoking function. Basically, that means that it runs itself automatically. The reason for wrapping the script in this is to isolate the majority of the variables that I define inside from the global scope and only reveal specific functions and values. It looks like this: (function(argument) { alert(argument); })("Yo."); and it does the same thing as this: function thing(argument) { alert(argument); } thing("Yo."); */ (function(w) { var canvas, ctx; /* This is an associative array to hold the status of the mouse cursor Whenever the mouse is moved or pressed, there are event handlers that update the values in this array. */ var mouse = { x: 0, y: 0, px: 0, py: 0, down: false }; /* These are the variable definitions for the values that will be used throughout the rest of the script. */ var canvas_width = 500; //Needs to be a multiple of the resolution value below. var canvas_height = 500; //This too. var resolution = 10; //Width and height of each cell in the grid. var pen_size = 40; //Radius around the mouse cursor coordinates to reach when stirring var num_cols = canvas_width / resolution; //This value is the number of columns in the grid. var num_rows = canvas_height / resolution; //This is number of rows. var speck_count = 5000; //This determines how many particles will be made. var vec_cells = []; //The array that will contain the grid cells var particles = []; //The array that will contain the particles /* This is the main function. It is triggered to start the process of constructing the the grid and creating the particles, attaching event handlers, and starting the animation loop. */ function init() { //These lines get the canvas DOM element and canvas context, respectively. canvas = document.getElementById("c"); ctx = canvas.getContext("2d"); //These two set the width and height of the canvas to the defined values. canvas.width = canvas_width; canvas.height = canvas_height; /* This loop begins at zero and counts up to the defined number of particles, less one, because array elements are numbered beginning at zero. */ for (i = 0; i < speck_count; i++) { /* Th.........完整代码请登录后点击上方下载按钮下载查看
网友评论0