Skip to content

Commit 6026d2c

Browse files
committed
feat: add example simulation in ch-3, with interactivity
- add code the intended interactivity as radio buttons - change the initial label text in ch-2 normal walker simulation - add required lines for the simulation to work in ch-3 content files
1 parent f9e3b3c commit 6026d2c

File tree

5 files changed

+139
-5
lines changed

5 files changed

+139
-5
lines changed

src/assets/cssStyle/generic.css

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,13 @@ img {
8686

8787
/* Style all divs inside interactive panel */
8888
.interactive-panel div {
89-
margin-bottom: 15px;
89+
margin: 5px 0;
9090
display: flex;
9191
flex-direction: column;
9292
}
9393

9494
.interactive-panel span {
95-
margin-bottom: 5px;
95+
margin-bottom: 3px;
9696
font-size: 14px;
9797
color: white;
9898
}
@@ -106,11 +106,22 @@ img {
106106
margin-top: 5px;
107107
}
108108

109+
.interactive-panel div > label{
110+
display: flex;
111+
align-items: center;
112+
margin: 3px 0;
113+
}
114+
115+
.interactive-panel input[type="radio"] {
116+
margin: 3px 3px;
117+
}
118+
109119
.interactive-panel label {
110120
font-size: 14px;
111121
color: white;
112122
}
113123

124+
114125
/* Style for pause and play buttons */
115126
.control-buttons {
116127
grid-column: 1;

src/assets/scripts/chap2_02.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ let normalWalkerScript = function(p) {
5656

5757
stdDevSlider = p.createSlider(p.PI/60, p.PI/6, p.PI/8, 0);
5858
stdDevSlider.parent(container);
59-
label = p.createSpan('Std Dev: ' + stdDevSlider.value().toFixed(3));
59+
label = p.createSpan('Std Dev: ' + p.degrees(stdDevSlider.value()).toFixed(3) + '°');
6060
label.parent(container);
6161

6262
stdDevSlider.input(() => {

src/assets/scripts/chap3_01.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
let OGPerlinScript = function (p) {
2+
class Walker {
3+
4+
xOffset = 0;
5+
yOffset = 1000;
6+
magOffset = 2000;
7+
constructor(x, y, r) {
8+
this.x = x;
9+
this.y = y;
10+
this.r = r;
11+
this.D = this.r * 2;
12+
}
13+
14+
display() {
15+
p.stroke(0);
16+
p.fill(100);
17+
p.circle(this.x, this.y, this.D);
18+
}
19+
20+
update(affecting_params = 'direction', chk_edges = true) {
21+
let noiseX = p.noise(this.xOffset);
22+
let noiseY = p.noise(this.yOffset);
23+
const behaviors = {
24+
direction: () => {
25+
this.x += (noiseX - 0.5) * 8;
26+
this.y += (noiseY - 0.5) * 8;
27+
},
28+
location: () => {
29+
this.x = noiseX * p.width;
30+
this.y = noiseY * p.height;
31+
},
32+
step: () => {
33+
let vel_mag = p.noise(this.magOffset) * (this.r * 1.5);
34+
this.x += (noiseX - 0.5) * 2 * vel_mag;
35+
this.y += (noiseY - 0.5) * 2 * vel_mag;
36+
this.magOffset += 0.01;
37+
}
38+
};
39+
40+
behaviors[affecting_params]();
41+
42+
this.xOffset += 0.01;
43+
this.yOffset += 0.01;
44+
45+
if (chk_edges){ this.checkEdges(); }
46+
}
47+
48+
checkEdges() {
49+
if (this.x > p.width + this.r) {
50+
this.x = this.x - (p.width + this.r);
51+
} else if (this.x < -this.r) {
52+
this.x = p.width + (this.x + this.r);
53+
}
54+
if (this.y > p.height + this.r) {
55+
this.y = this.y - (p.height + this.r);
56+
} else if (this.y < -this.r) {
57+
this.y = p.height + (this.y + this.r);
58+
}
59+
}
60+
}
61+
62+
let w = new Walker(p.width / 2, p.height / 2, 8);
63+
let paramsRadio, chosenParam;
64+
65+
p.initializeSketch = function () {
66+
p.background(255);
67+
68+
chosenParam = paramsRadio.value();
69+
if (chosenParam !== 'location'){
70+
w.x = p.width / 2;
71+
w.y = p.height / 2;
72+
}
73+
else (w.update('location'));
74+
75+
p.redraw();
76+
}
77+
78+
79+
p.setup = function () {
80+
p.noLoop();
81+
82+
let canvas = p.createCanvas(850, 550);
83+
canvas.parent('sec1');
84+
85+
let container = p.select('#interactive-controls-sec1');
86+
paramsRadio = p.createRadio();
87+
paramsRadio.option('direction', 'Direction');
88+
paramsRadio.option('location', 'Location');
89+
paramsRadio.option('step', 'Step Size + Direction');
90+
paramsRadio.selected('direction');
91+
paramsRadio.parent(container);
92+
93+
resetLabel = p.createSpan('Reset to see effect');
94+
resetLabel.parent(container);
95+
resetLabel.style('display', 'flex');
96+
97+
p.initializeSketch();
98+
}
99+
100+
p.draw = function () {
101+
w.display();
102+
w.update(chosenParam);
103+
}
104+
}
105+
106+
let sec1Sketch = new p5(OGPerlinScript, 'sec1');

src/chapter3/_section3_1.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
---
22
tags: 'chap3'
3+
script: '/assets/scripts/chap3_01.js'
4+
id: 'sec1'
35
permalink: false
46
---
57

@@ -107,7 +109,7 @@ Applied parameters:
107109
* Velocity (both magnitude and direction)
108110

109111
range:
110-
- mag $ \in (0,$ diameter $/2)$,
112+
- mag $\in (0,$ diameter $/2)$,
111113
- direction $\in (-1,1)$
112114
```js
113115
/* Code for a 2D RW that is manupilated according to perlin noise.
@@ -175,7 +177,10 @@ function draw() {
175177
w.step('direction');
176178
}
177179
```
178-
A simulation of a similar implementation of the Perlin Noise walker is added to the 4^th^ chapter, in section [4.3.4]()
180+
181+
{% include "simulation-grid.html" %}
182+
183+
A simulation of a similar implementation of the Perlin Noise walker is added to the 4^th^ chapter, in section [4.3.4](../chapter4/#434-comparing-implementations-of-random-motion-behaviours)
179184
180185
## Notes:
181186

src/chapter3/index.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@
99
<h1>Chapter 3</h1>
1010
<h1>Perlin Noise and Smooth Movers</h1>
1111
<hr>
12+
<!-- Shared utility function for pausing and resuming any sketch -->
13+
<script>
14+
let pauseButtonToggle = function (button, sketch) {
15+
if (sketch.isLooping()) {
16+
button.innerText = "Play";
17+
sketch.noLoop();
18+
} else {
19+
button.innerText = "Pause";
20+
sketch.loop();
21+
}
22+
}
23+
</script>
1224

1325
{% for section in collections.chap3 %}
1426
{{ section.templateContent | safe }}

0 commit comments

Comments
 (0)