-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #463 from nature-of-code/notion-update-docs
[Notion] Update particle screenshots for Chapter 4
- Loading branch information
Showing
27 changed files
with
313 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
content/examples/04_particles/4_1_single_particle/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.1/p5.min.js"></script> | ||
<meta charset="utf-8"> | ||
<title>NOC_4_01_SingleParticle</title> | ||
<link rel="stylesheet" type="text/css" href="style.css"> | ||
</head> | ||
<body> | ||
<script src="particle.js"></script> | ||
<script src="sketch.js"></script> | ||
</body> | ||
</html> |
42 changes: 42 additions & 0 deletions
42
content/examples/04_particles/4_1_single_particle/particle.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// The Nature of Code | ||
// Daniel Shiffman | ||
// http://natureofcode.com | ||
|
||
// Simple Particle System | ||
|
||
// A simple Particle class | ||
|
||
class Particle { | ||
|
||
constructor(x,y) { | ||
this.position = createVector(x, y); | ||
//{!1 .offset-top} For demonstration purposes the Particle has a random velocity. | ||
this.velocity = createVector(random(-1, 1), random(-2, 0)); | ||
this.acceleration = createVector(0, 0); | ||
this.lifespan = 255.0; | ||
} | ||
|
||
update() { | ||
this.velocity.add(this.acceleration); | ||
this.position.add(this.velocity); | ||
this.lifespan -= 2.0; | ||
this.acceleration.mult(0); | ||
} | ||
|
||
|
||
show() { | ||
stroke(0, this.lifespan); | ||
fill(0, this.lifespan); | ||
circle(this.position.x, this.position.y, 8); | ||
} | ||
|
||
//{!3} Keeping the same physics model as with previous chapters | ||
applyForce(force) { | ||
this.acceleration.add(force); | ||
} | ||
|
||
//{!3} Is the Particle alive or dead? | ||
isDead() { | ||
return (this.lifespan < 0); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions
28
content/examples/04_particles/4_1_single_particle/sketch.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// The Nature of Code | ||
// Daniel Shiffman | ||
// http://natureofcode.com | ||
|
||
let particle; | ||
|
||
function setup() { | ||
createCanvas(640, 240); | ||
particle = new Particle(width / 2, 10); | ||
} | ||
|
||
function draw() { | ||
background(255); | ||
// Operating the single Particle | ||
particle.update(); | ||
particle.show(); | ||
|
||
// Applying a gravity force | ||
let gravity = createVector(0, 0.1); | ||
particle.applyForce(gravity); | ||
|
||
// Checking the particle's state and making a new particle | ||
if (particle.isDead()) { | ||
particle = new Particle(width / 2, 20); | ||
console.log("Particle dead!"); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
html, body { | ||
margin: 0; | ||
padding: 0; | ||
} | ||
canvas { | ||
display: block; | ||
} |
13 changes: 13 additions & 0 deletions
13
content/examples/04_particles/4_2_array_particles/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.1/p5.min.js"></script> | ||
<meta charset="utf-8"> | ||
<title>NOC_4_02_ArrayParticles</title> | ||
<link rel="stylesheet" type="text/css" href="style.css"> | ||
</head> | ||
<body> | ||
<script src="particle.js"></script> | ||
<script src="sketch.js"></script> | ||
</body> | ||
</html> |
48 changes: 48 additions & 0 deletions
48
content/examples/04_particles/4_2_array_particles/particle.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// The Nature of Code | ||
// Daniel Shiffman | ||
// http://natureofcode.com | ||
|
||
// Simple Particle System | ||
|
||
// A simple Particle class | ||
|
||
class Particle { | ||
constructor(x, y) { | ||
this.position = createVector(x, y); | ||
this.acceleration = createVector(0, 0); | ||
this.velocity = createVector(random(-1, 1), random(-1, 0)); | ||
this.lifespan = 255.0; | ||
} | ||
|
||
run() { | ||
let gravity = createVector(0, 0.05); | ||
this.applyForce(gravity); | ||
this.update(); | ||
this.show(); | ||
} | ||
|
||
applyForce(force) { | ||
this.acceleration.add(force); | ||
} | ||
|
||
// Method to update position | ||
update() { | ||
this.velocity.add(this.acceleration); | ||
this.position.add(this.velocity); | ||
this.lifespan -= 2; | ||
this.acceleration.mult(0); | ||
} | ||
|
||
// Method to display | ||
show() { | ||
stroke(0, this.lifespan); | ||
strokeWeight(2); | ||
fill(127, this.lifespan); | ||
circle(this.position.x, this.position.y, 8); | ||
} | ||
|
||
// Is the particle still useful? | ||
isDead() { | ||
return (this.lifespan < 0.0); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions
25
content/examples/04_particles/4_2_array_particles/sketch.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// The Nature of Code | ||
// Daniel Shiffman | ||
// http://natureofcode.com | ||
|
||
let particles = []; | ||
|
||
function setup() { | ||
createCanvas(640, 240); | ||
} | ||
|
||
function draw() { | ||
background(255); | ||
particles.push(new Particle(width / 2, 20)); | ||
|
||
// Looping through backwards to delete | ||
for (let i = particles.length - 1; i >= 0; i--) { | ||
let particle = particles[i]; | ||
particle.run(); | ||
if (particle.isDead()) { | ||
//remove the particle | ||
particles.splice(i, 1); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
html, body { | ||
margin: 0; | ||
padding: 0; | ||
} | ||
canvas { | ||
display: block; | ||
} |
Binary file modified
BIN
-123 KB
(29%)
content/examples/04_particles/4_3_particle_emitter/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,4 @@ function draw() { | |
background(255); | ||
emitter.addParticle(); | ||
emitter.run(); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-173 KB
(30%)
content/examples/04_particles/4_4_multiple_emitters/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions
33
content/examples/04_particles/4_6_particle_system_forces/emitter.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// The Nature of Code | ||
// Daniel Shiffman | ||
// http://natureofcode.com | ||
|
||
class Emitter { | ||
|
||
constructor(x, y) { | ||
this.origin = createVector(x, y); | ||
this.particles = []; | ||
} | ||
|
||
addParticle() { | ||
this.particles.push(new Particle(this.origin.x, this.origin.y)); | ||
} | ||
|
||
applyForce(force) { | ||
//{!3} Using a for of loop to apply the force to all particles | ||
for (let particle of this.particles) { | ||
particle.applyForce(force); | ||
} | ||
} | ||
|
||
run() { | ||
//{!7} Can’t use the enhanced loop because checking for particles to delete. | ||
for (let i = this.particles.length - 1; i >= 0; i--) { | ||
const particle = this.particles[i]; | ||
particle.run(); | ||
if (particle.isDead()) { | ||
this.particles.splice(i, 1); | ||
} | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
content/examples/04_particles/4_6_particle_system_forces/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.1/p5.min.js"></script> | ||
<meta charset="utf-8"> | ||
<title>NOC_4_06_ParticlesSystemForces</title> | ||
<link rel="stylesheet" type="text/css" href="style.css"> | ||
</head> | ||
<body> | ||
<script src="particle.js"></script> | ||
<script src="emitter.js"></script> | ||
<script src="sketch.js"></script> | ||
</body> | ||
</html> |
49 changes: 49 additions & 0 deletions
49
content/examples/04_particles/4_6_particle_system_forces/particle.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// The Nature of Code | ||
// Daniel Shiffman | ||
// http://natureofcode.com | ||
|
||
// Simple Particle System | ||
|
||
// A simple Particle class | ||
|
||
class Particle { | ||
constructor(x, y) { | ||
this.position = createVector(x, y); | ||
this.acceleration = createVector(0, 0.0); | ||
this.velocity = createVector(random(-1, 1), random(-2, 0)); | ||
this.lifespan = 255.0; | ||
this.mass = 1; // Let's do something better here! | ||
} | ||
|
||
run() { | ||
this.update(); | ||
this.show(); | ||
} | ||
|
||
applyForce(force) { | ||
let f = force.copy(); | ||
f.div(this.mass); | ||
this.acceleration.add(f); | ||
} | ||
|
||
// Method to update position | ||
update() { | ||
this.velocity.add(this.acceleration); | ||
this.position.add(this.velocity); | ||
this.acceleration.mult(0); | ||
this.lifespan -= 2.0; | ||
} | ||
|
||
// Method to display | ||
show() { | ||
stroke(0, this.lifespan); | ||
strokeWeight(2); | ||
fill(127, this.lifespan); | ||
circle(this.position.x, this.position.y, 8); | ||
} | ||
|
||
// Is the particle still useful? | ||
isDead() { | ||
return this.lifespan < 0.0; | ||
} | ||
} |
Binary file added
BIN
+43.4 KB
content/examples/04_particles/4_6_particle_system_forces/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions
21
content/examples/04_particles/4_6_particle_system_forces/sketch.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// The Nature of Code | ||
// Daniel Shiffman | ||
// http://natureofcode.com | ||
|
||
let emitter; | ||
|
||
function setup() { | ||
createCanvas(640, 240); | ||
emitter = new Emitter(width / 2, 50); | ||
} | ||
|
||
function draw() { | ||
background(255); | ||
|
||
// Apply gravity force to all Particles | ||
let gravity = createVector(0, 0.1); | ||
emitter.applyForce(gravity); | ||
|
||
emitter.addParticle(); | ||
emitter.run(); | ||
} |
7 changes: 7 additions & 0 deletions
7
content/examples/04_particles/4_6_particle_system_forces/style.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
html, body { | ||
margin: 0; | ||
padding: 0; | ||
} | ||
canvas { | ||
display: block; | ||
} |
Binary file modified
BIN
-207 KB
(24%)
.../examples/04_particles/example_4_7_particle_system_with_repeller/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-114 KB
(33%)
...s/04_particles/noc_4_05_particle_system_inheritance_polymorphism/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.