Skip to content

Commit 96b11e6

Browse files
committed
build: add ch1 and ch5 content and scripts
- change the code taken from draft (or Honours-project-codes) to work in website - change ch0 index file to fix some formats - add linkify and break options to mardown-it library
1 parent 2337804 commit 96b11e6

File tree

14 files changed

+1067
-14
lines changed

14 files changed

+1067
-14
lines changed

.eleventy.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ export default function(eleventyConfig) {
1010
eleventyConfig.addLayoutAlias('base', 'layouts/base.html');
1111

1212
// Add Markdown-it plugins
13-
eleventyConfig.amendLibrary("md", (mdLib) => mdLib.use(markdownItSup));
13+
eleventyConfig.amendLibrary("md", (mdLib) => {
14+
mdLib.set({
15+
linkify: true,
16+
breaks: true
17+
});
18+
19+
mdLib.use(markdownItSup);
20+
});
1421

1522
// Add MathJax plugin
1623
eleventyConfig.addPlugin(mathjaxPlugin);

src/assets/scripts/chap1_01.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
let simpleMoverScript = function(p) {
2+
let x_coord, y_coord, timeTaken, speed;
3+
let timeSlider, timeLabel, resetLabel;
4+
5+
p.initializeSketch = function() {
6+
x_coord = p.width/2;
7+
y_coord = p.height/2;
8+
timeTaken = timeSlider.value();
9+
speed = 250/timeTaken;
10+
11+
p.fill(100);
12+
13+
p.redraw();
14+
};
15+
16+
p.setup = function() {
17+
p.noLoop();
18+
19+
let cnv = p.createCanvas(600, 400);
20+
cnv.parent('sec1');
21+
22+
let container = document.getElementById('interactive-controls-sec1');
23+
24+
timeSlider = p.createSlider(60, 300, 90, 1);
25+
timeSlider.parent(container);
26+
timeLabel = p.createSpan('Time (frames): ' + timeSlider.value())
27+
timeLabel.parent(container);
28+
29+
resetLabel = p.createSpan('Reset to see effect');
30+
resetLabel.parent(container);
31+
resetLabel.style('display', 'flex');
32+
33+
timeSlider.input(() =>{
34+
timeLabel.html('Time (frames): ' + timeSlider.value());
35+
})
36+
37+
p.initializeSketch();
38+
};
39+
40+
p.draw = function() {
41+
if (x_coord <= p.width/2 + 250) {
42+
p.background(255);
43+
x_coord += speed;
44+
p.circle(x_coord, y_coord, 20);
45+
} else {
46+
p.initializeSketch();
47+
}
48+
};
49+
};
50+
51+
let sec1Sketch = new p5(simpleMoverScript, 'sec1');
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
// A simple p5js script 'simple_script.js'
21
let bouncingBallScript = function (p) {
32
class Mover {
43
constructor(x, y, m) {
54
this.pos = p.createVector(x, y);
65
this.vel = p.createVector(3, -2);
7-
this.acc = p.createVector(); // Creates a vector of default values (0,0)
6+
this.acc = p.createVector();
87
this.mass = m;
98
this.r = m * 0.875;
109
this.D = this.r * 2;
@@ -14,8 +13,6 @@ let bouncingBallScript = function (p) {
1413
this.vel.add(this.acc);
1514
this.pos.add(this.vel);
1615
this.acc.mult(0);
17-
// bounceEdges() is called after the reset, since the effect of friction
18-
// applied in bounceEdges() is only used in the next frame
1916
this.bounceEdges();
2017
}
2118

@@ -24,8 +21,6 @@ let bouncingBallScript = function (p) {
2421
}
2522

2623
applyForce(force) {
27-
// Since we may not want to directly affect the vector that is passed to us
28-
// we have to create a copy apply second law to the copied vector
2924
let f = p5.Vector.div(force, this.mass);
3025
this.acc.add(f);
3126
}
@@ -71,10 +66,10 @@ let bouncingBallScript = function (p) {
7166
p.noLoop();
7267

7368
let cnv = p.createCanvas(500, 400);
74-
cnv.parent('test1');
69+
cnv.parent('sec2');
7570

7671
p.fill(100);
77-
container = document.getElementById('interactive-controls');
72+
container = document.getElementById('interactive-controls-sec2');
7873

7974
gravityMagSlider = p.createSlider(0, 1, 0.1, 0.05);
8075
gravityMagSlider.parent(container);
@@ -102,4 +97,4 @@ let bouncingBallScript = function (p) {
10297
}
10398
}
10499

105-
let mySketch = new p5(bouncingBallScript, 'test1');
100+
let sec2Sketch = new p5(bouncingBallScript, 'sec2');

src/assets/scripts/chap2_04.js

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

8383
p.mouseDragged = function() {
8484
rWalkers.addWalker(p.mouseX, p.mouseY, 15);
85-
if (!isLooping()) p.redraw();
85+
if (!p.isLooping()) p.redraw();
8686
};
8787
};
8888

src/assets/scripts/chap5_01.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
multiMouseSeekersScript = function (p) {
3+
class Seekers {
4+
constructor(maxCapacity = 40) {
5+
this.seekers = [];
6+
this.maxCapacity = maxCapacity;
7+
}
8+
9+
addSeeker(x, y, r) {
10+
this.seekers.push(new Seeker(p, x, y, r));
11+
}
12+
13+
run(target) {
14+
for (let i = this.seekers.length - 1; i >= 0; i--) {
15+
this.seekers[i].display();
16+
this.seekers[i].applyBehaviours(0.5, 0.5, target, false, this.seekers);
17+
this.seekers[i].update(true);
18+
if (this.seekers[i].isDead()) this.seekers.splice(i, 1);
19+
}
20+
}
21+
}
22+
let seekers = new Seekers();
23+
let mouse = p.createVector();
24+
25+
p.initializeSketch = function() {
26+
seekers.seekers = [];
27+
for (let i = 0; i < seekers.maxCapacity; i++) {
28+
seekers.addSeeker(
29+
p.random(p.width * 0.25, p.width * 0.75),
30+
p.random(p.height * 0.25, p.height * 0.75),
31+
p.random([9,10,11])
32+
);
33+
}
34+
mouse.set(p.mouseX, p.mouseY);
35+
p.redraw();
36+
};
37+
38+
p.setup = function() {
39+
p.noLoop();
40+
41+
let cnv = p.createCanvas(700, 500);
42+
cnv.parent('sec1');
43+
44+
p.initializeSketch();
45+
};
46+
47+
p.draw = function() {
48+
p.background(220);
49+
seekers.run(mouse);
50+
mouse.set(p.mouseX, p.mouseY);
51+
};
52+
}
53+
54+
let sec1Sketch = new p5(multiMouseSeekersScript, 'sec1');

src/assets/scripts/chap5_02.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
let FlockerScript = function (p) {
2+
class Flock {
3+
constructor() {
4+
this.flock = [];
5+
this.maxCapacity = 50;
6+
this.curIndex = this.maxCapacity - 1;
7+
}
8+
9+
addMover(x, y, r) {
10+
this.flock.push(new AutonMover(p, x, y, r));
11+
}
12+
13+
applyBehaviours(mover = this.flock[0], sepW = sepWeight, cohereW = cohereWeight, alignW = alignWeight) {
14+
let sepForce = p5.Vector.mult(mover.separate(this.flock), sepW);
15+
let alignForce = p5.Vector.mult(mover.align(this.flock), alignW);
16+
let cohereForce = p5.Vector.mult(mover.cohere(this.flock), cohereW);
17+
mover.applyForce(p5.Vector.add(sepForce, p5.Vector.add(cohereForce, alignForce)));
18+
}
19+
20+
run(chk_edges = true) {
21+
for (; this.curIndex > 0; this.curIndex--) {
22+
this.flock[this.curIndex].display();
23+
this.applyBehaviours(this.flock[this.curIndex]);
24+
this.flock[this.curIndex].update(chk_edges);
25+
if (this.flock[this.curIndex].isDead()) {
26+
this.flock.splice(this.curIndex, 1);
27+
}
28+
}
29+
this.curIndex = this.maxCapacity - 1;
30+
}
31+
}
32+
33+
let flock= new Flock();
34+
let sepSlider, alignSlider, cohereSlider;
35+
let sepWeight = 1, alignWeight = 0.5, cohereWeight = 0.5;
36+
37+
p.initializeSketch = function () {
38+
flock.flock = [];
39+
for (let i = 0; i < flock.maxCapacity; i++) {
40+
flock.addMover(
41+
p.random(p.width * 0.3, p.width * 0.7),
42+
p.random(p.height * 0.3, p.height * 0.7),
43+
10
44+
);
45+
}
46+
47+
p.redraw();
48+
};
49+
50+
p.setup = function () {
51+
p.noLoop();
52+
53+
let cnv = p.createCanvas(850, 600);
54+
cnv.parent('sec2');
55+
let container = document.getElementById('interactive-controls-sec2');
56+
57+
sepSlider = p.createSlider(0.5, 2.5, 1, 0.25);
58+
sepSlider.parent(container);
59+
sepLabel = p.createSpan('Separation: ' + sepSlider.value())
60+
sepLabel.parent(container);
61+
62+
alignSlider = p.createSlider(0.5, 2.5, 0.5, 0.25);
63+
alignSlider.parent(container);
64+
alignLabel = p.createSpan('Align:' + alignSlider.value());
65+
alignLabel.parent(container);
66+
67+
cohereSlider = p.createSlider(0.5, 2.5, 0.5, 0.25);
68+
cohereSlider.parent(container);
69+
cohereLabel = p.createSpan('Cohere:' + cohereSlider.value());
70+
cohereLabel.parent(container);
71+
72+
sepSlider.input(() => {
73+
sepWeight = sepSlider.value();
74+
sepLabel.html('Separation: ' + sepWeight);
75+
});
76+
77+
alignSlider.input(() => {
78+
alignWeight = alignSlider.value();
79+
alignLabel.html('Align: ' + alignWeight);
80+
});
81+
82+
cohereSlider.input(() => {
83+
cohereWeight = cohereSlider.value();
84+
cohereLabel.html('Cohere: ' + cohereWeight);
85+
});
86+
87+
p.initializeSketch();
88+
};
89+
90+
p.draw = function () {
91+
p.background(220);
92+
flock.run();
93+
};
94+
95+
};
96+
97+
let sec2Sketch = new p5(FlockerScript, 'sec2');

0 commit comments

Comments
 (0)