Toggle navigation
在线编辑器
在线代码
文本比较
jQuery下载
前端库
在线手册
登录/注册
下载代码
html
css
js
分享到微信朋友圈
X
html
css
body { padding: 0; margin: 0; display: flex; width: 100vw; height: 100vh; justify-content: center; align-items: center; } canvas { vertical-align: top; }
JavaScript
var flock; var gif = []; var index = 1; var totalHogs = 1; var HogCount = Math.floor(Math.random() * (50 - 30 + 1)) + 30; function windowResized(){ resizeCanvas(windowWidth, windowHeight); } function preload() { for(var i = 1; i < 31; i++){ gif[i] = loadImage("https://s3-us-west-2.amazonaws.com/s.cdpn.io/383755/pig" + i + ".png"); } } function setup() { createCanvas(windowWidth, windowHeight); flock = new Flock(); // Add an initial set of Hogs into the system for (var i = 0; i < HogCount; i++) { var b = new Hog(width/2,height/2); flock.addHog(b); } } function draw() { background(140,104,78); totalHogs++; if(totalHogs >= 101){ totalHogs = 1; } index++; if(index >= 31){ index = 1; } flock.run(); } // Flock object manages the array of all the Hogs function Flock() { // An array for all the Hogs this.Hogs = []; // Initialize the array } Flock.prototype.run = function() { for (var i = 0; i < this.Hogs.length; i++) { this.Hogs[i].run(this.Hogs); // Passing the entire list of Hogs to each Hog individually } } Flock.prototype.addHog = function(b) { this.Hogs.push(b); } // Hog class // Methods for Separation, Cohesion, Alignment added function Hog(x,y) { this.acceleration = createVector(0,0); this.velocity = createVector(random(-1,1),random(-1,1)); this.position = createVector(x,y); this.r = 3.0; this.maxspeed = 5; // Maximum speed this.maxforce = 0.05; // Maximum steering force } Hog.prototype.run = function(Hogs) { this.flock(Hogs); this.update(); this.borders(); this.render(); } Hog.prototype.applyForce = function(force) { // We could add mass here if we want A = F / M this.acceleration.add(force); } // We accumulate a new acceleration each time based on three rules Hog.prototype.flock = function(Hogs) { var sep = this.separate(Hogs); // Separation var ali = this.align(Hogs); // Alignment var coh = this.cohesion(Hogs); // Cohesion // Arbitrarily weight these forces sep.mult(1.5); ali.mult(1.0); coh.mult(1.0); // Add the force vectors to acceleration this.applyForce(sep); this.applyForce(ali); this.applyForce(coh); } // Method to update location Hog.prototype.update = function() { // Update velocity this.velocity.add(this.acceleration); // Limit speed this.velocity.limit(this.maxspeed); this.position.add(this.velocity); // Reset accelertion to 0 each cycle this.acceleration.mult(0); } // A method that calculates and applies a steering force towards a target // STEER = DESIRED MINUS VELOCITY Hog.prototype.seek = function(target) { var desired = p5.Vector.sub(target,this.position); // A vector pointing from the location to the target // Normalize desired and scale to maximum speed desired.normalize(); desired.mult(this.maxspeed); // Steering = Desired minus Velocity var steer = p5.Vector.sub(desired,this.velocity); steer.limit(this.maxforce); // Limit to maximum steering force return steer; } Hog.prototype.render = function() { var theta = this.velocity.heading() + radians(90); push(); translate(this.position.x,this.position.y); rotate((theta) - 0); image(gif[index], 0, 0, 25, 35); pop(); } // Wraparound Hog.prototype.borders = function() { if (this.position.x < -this.r) this.position.x = width +this.r; if (this.position.y < -this.r) this.position.y = height+this.r; if (this.position.x > width +this.r) this.position.x = -this.r; if (this.position.y > height+this.r) this.position.y = -this.r; } // Separation // Method checks for nearby Hogs and steers away Hog.prototype.separate = function(Hogs) { var desiredseparation = 55.0; var steer = createVector(0,0); var count = 0; // For every Hog in the system, check if it's too close for (var i = 0; i < Hogs.length; i++) { var d = p5.Vector.dist(this.position,Hogs[i].position); // If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself) if ((d > 0) && (d < desiredseparation)) { // Calculate vector pointing away from neighbor var diff = p5.Vector.sub(this.position,Hogs[i].position); diff.normalize(); diff.div(d); // Weight by distance steer.add(diff); count++; // Keep track of how many } } // Average -- divide by how many if (count > 0) { steer.div(count); } // As long as the vector is greater than 0 if (steer.mag() > 0) { // Implement Reynolds: Steering = Desired - Velocity steer.normalize(); steer.mult(this.maxspeed); steer.sub(this.velocity); steer.limit(this.maxforce); } return steer; } // Alignment // For every nearby Hog in the system, calculate the average velocity Hog.prototype.align = function(Hogs) { var neighbordist = 50; var sum = createVector(0,0); var count = 0; for (var i = 0; i < Hogs.length; i++) { var d = p5.Vector.dist(this.position,Hogs[i].position); if ((d > 0) && (d < neighbordist)) { sum.add(Hogs[i].velocity); count++; } } if (count > 0) { sum.div(count); sum.normalize(); sum.mult(this.maxspeed); var steer = p5.Vector.sub(sum,this.velocity); steer.limit(this.maxforce); return steer; } else { return createVector(0,0); } } // Cohesion // For the average location (i.e. center) of all nearby Hogs, calculate steering vector towards that location Hog.prototype.cohesion = function(Hogs) { var neighbordist = 50; var sum = createVector(0,0); // Start with empty vector to accumulate all locations var count = 0; for (var i = 0; i < Hogs.length; i++) { var d = p5.Vector.dist(this.position,Hogs[i].position); if ((d > 0) && (d < neighbordist)) { sum.add(Hogs[i].position); // Add location count++; } } if (count > 0) { sum.div(count); return this.seek(sum); // Steer towards the location } else { return createVector(0,0); } }
粒子
时间
文字
hover
canvas
3d
游戏
音乐
火焰
水波
轮播图
鼠标跟随
动画
css
加载动画
导航
菜单
按钮
滑块
tab
弹出层
统计图
svg
×
Close
在线代码下载提示
开通在线代码永久免费下载,需支付20jQ币
开通后,在线代码模块中所有代码可终身免费下!
您已开通在线代码永久免费下载,关闭提示框后,点下载代码可直接下载!
您已经开通过在线代码永久免费下载
对不起,您的jQ币不足!可通过发布资源 或
直接充值获取jQ币
取消
开通下载
<!doctype html> <html> <head> <meta charset="utf-8"> <title>canvas野猪奔跑动画-jq22.com</title> <script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script> <style>
</style> </head> <body>
<script>
</script>
</body> </html>
2012-2021 jQuery插件库版权所有
jquery插件
|
jq22工具库
|
网页技术
|
广告合作
|
在线反馈
|
版权声明
沪ICP备13043785号-1
浙公网安备 33041102000314号