Clean up canvas particle sim guide code examples (#30742)
* docs(guide): clean up canvas particle sim code examples * Use ES6 syntax * Format code for easy readability * Normalize single vs. double quotes * Adjust spacing * Add link to inverse-square law wikipedia page for additional information * docs(guide): use arrow functions in particle sim examples
This commit is contained in:
@ -8,11 +8,11 @@ In this guide, we're going to build a basic particle simulation in Canvas using
|
|||||||
We will want to set up an array of particles with accelerations and velocities. We will create 100 particles at random points on the canvas.
|
We will want to set up an array of particles with accelerations and velocities. We will create 100 particles at random points on the canvas.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
canvas = document.getElementById("canvas");
|
const canvas = document.getElementById('canvas');
|
||||||
ctx = canvas.getContext('2d');
|
const ctx = canvas.getContext('2d');
|
||||||
|
|
||||||
var particles = [];
|
const particles = [];
|
||||||
for(var i=0; i<100; i++) {
|
for (let i=0; i<100; i++) {
|
||||||
particles.push(
|
particles.push(
|
||||||
{
|
{
|
||||||
x: Math.random() * canvas.width,
|
x: Math.random() * canvas.width,
|
||||||
@ -29,9 +29,9 @@ for(var i=0; i<100; i++) {
|
|||||||
In our draw loop, we render these particles.
|
In our draw loop, we render these particles.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function draw() {
|
const draw = () => {
|
||||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||||
for(var i=0; i<particles.length; i++) {
|
for (let i=0; i<particles.length; i++) {
|
||||||
// update state
|
// update state
|
||||||
|
|
||||||
// render state
|
// render state
|
||||||
@ -41,15 +41,15 @@ function draw() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
window.requestAnimationFrame(draw);
|
window.requestAnimationFrame(draw);
|
||||||
}
|
};
|
||||||
window.requestAnimationFrame(draw);
|
window.requestAnimationFrame(draw);
|
||||||
```
|
```
|
||||||
|
|
||||||
Now, all we need to do is update velocity and acceleration each frame. We will add the acceleration to the velocity and add the velocity to the position.
|
Now, all we need to do is update velocity and acceleration each frame. We will add the acceleration to the velocity and add the velocity to the position.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function draw() {
|
const draw = () => {
|
||||||
for(var i=0; i<particles.length; i++) {
|
for (let i=0; i<particles.length; i++) {
|
||||||
// update state
|
// update state
|
||||||
particles[i].x += particles[i].vx;
|
particles[i].x += particles[i].vx;
|
||||||
particles[i].y += particles[i].vy;
|
particles[i].y += particles[i].vy;
|
||||||
@ -60,30 +60,28 @@ function draw() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
window.requestAnimationFrame(draw);
|
window.requestAnimationFrame(draw);
|
||||||
}
|
};
|
||||||
window.requestAnimationFrame(draw);
|
window.requestAnimationFrame(draw);
|
||||||
```
|
```
|
||||||
|
|
||||||
That's it! All we need to do to is create a force somewhere. Let's do it with a click listener.
|
That's it! All we need to do to is create a force somewhere. Let's do it with a click listener.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
canvas.addEventListener("click", function(e) {
|
canvas.addEventListener('click', e => {
|
||||||
var clickX = e.clientX-canvas.offsetLeft;
|
const clickX = e.clientX - canvas.offsetLeft;
|
||||||
var clickY = e.clientY-canvas.offsetTop;
|
const clickY = e.clientY - canvas.offsetTop;
|
||||||
for(var i=0; i<particles.length; i++) {
|
for (let i=0; i<particles.length; i++) {
|
||||||
var delx = particles[i].x-clickX;
|
const delx = particles[i].x - clickX;
|
||||||
var dely = particles[i].y-clickY;
|
const dely = particles[i].y - clickY;
|
||||||
var magnitudeSquared = Math.pow(delx, 2)+Math.pow(dely, 2);
|
const magnitudeSquared = Math.pow(delx, 2) + Math.pow(dely, 2);
|
||||||
|
|
||||||
|
|
||||||
particles[i].ax = 0.1*delx/magnitudeSquared;
|
particles[i].ax = 0.1*delx/magnitudeSquared;
|
||||||
particles[i].ay = 0.1*dely/magnitudeSquared;
|
particles[i].ay = 0.1*dely/magnitudeSquared;
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
This adds force via the inverse-square law. See [this page](#placeholder) for more details.
|
This adds force via the [inverse-square law](https://en.wikipedia.org/wiki/Inverse-square_law).
|
||||||
|
|
||||||
That's all! The final codepen:
|
That's all! The final codepen:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user