-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
155 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,35 @@ | ||
var x = 200; | ||
var y = 200; | ||
let boids = new p5((sketch) => { | ||
var x = 200; | ||
var y = 200; | ||
|
||
function addRandomDirection(){ | ||
let randomChoice = floor(random(4)); | ||
function addRandomDirection(){ | ||
let randomChoice = Math.floor(Math.random() * 4); | ||
|
||
switch(randomChoice){ | ||
case 0: | ||
x++; | ||
break; | ||
case 1: | ||
x--; | ||
break; | ||
case 2: | ||
y++; | ||
break; | ||
default: | ||
y--; | ||
switch(randomChoice){ | ||
case 0: | ||
x++; | ||
break; | ||
case 1: | ||
x--; | ||
break; | ||
case 2: | ||
y++; | ||
break; | ||
default: | ||
y--; | ||
} | ||
} | ||
} | ||
|
||
function setup(){ | ||
let cnv = createCanvas(400, 400); | ||
cnv.parent('BoidContainer'); | ||
sketch.setup = () => { | ||
let cnv = sketch.createCanvas(400, 400); | ||
cnv.parent('BoidContainer'); | ||
|
||
background(140, 205, 230); | ||
} | ||
sketch.background(140, 205, 230); | ||
} | ||
|
||
function draw(){ | ||
stroke(245, 175, 0); // pen colour | ||
addRandomDirection(); // change state | ||
point(x,y); // draw state | ||
} | ||
sketch.draw = () => { | ||
sketch.stroke(245, 175, 0); // pen colour | ||
addRandomDirection(); // change state | ||
sketch.point(x, y); // draw state | ||
} | ||
}); |
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 |
---|---|---|
@@ -1,12 +1,14 @@ | ||
function setup() { | ||
var cnv = createCanvas(400, 400); | ||
cnv.parent('Circles1Container'); | ||
let circles1 = new p5( (sketch) => { | ||
sketch.setup = () => { | ||
let cnv = sketch.createCanvas(400, 400); | ||
cnv.parent('Circles1Container'); | ||
|
||
background(222, 222, 222); | ||
} | ||
|
||
var circleSize = 10; | ||
|
||
function draw() { | ||
circle(mouseX, mouseY, circleSize); | ||
} | ||
sketch.background(222, 222, 222); | ||
} | ||
|
||
var circleSize = 10; | ||
|
||
sketch.draw = () => { | ||
sketch.circle(sketch.mouseX, sketch.mouseY, circleSize); | ||
} | ||
}) |
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 |
---|---|---|
@@ -1,14 +1,13 @@ | ||
console.log("this script is run!"); | ||
let circles2 = new p5( (sketch) => { | ||
sketch.setup = () => { | ||
let cnv = sketch.createCanvas(400, 400); | ||
cnv.parent('Circles2Container'); | ||
} | ||
|
||
function setup() { | ||
console.log("script is setup..."); | ||
var cnv = createCanvas(400, 400); | ||
cnv.parent('Circles2Container'); | ||
} | ||
|
||
var circleSize = 10; | ||
|
||
function draw() { | ||
background(222, 222, 222); // background drawn every frame! | ||
circle(mouseX, mouseY, circleSize); | ||
} | ||
var circleSize = 10; | ||
|
||
sketch.draw = () => { | ||
sketch.background(222, 222, 222); | ||
sketch.circle(sketch.mouseX, sketch.mouseY, circleSize); | ||
} | ||
}) |
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,31 @@ | ||
// remember unique names with let bindings! | ||
let rainbowCircles = new p5( (sk) => { | ||
sk.setup = () => { | ||
let cnv = sk.createCanvas(400, 400); | ||
cnv.parent('CirclesRainbowContainer'); | ||
sk.background(222, 222, 222); | ||
} | ||
|
||
const circleSize = 10; | ||
let redV = 134; | ||
let greenV = 213; | ||
let blueV = 21; | ||
|
||
const rShift = 13; | ||
const gShift = 5; | ||
const bShift = 27; | ||
|
||
sk.draw = () => { | ||
// update rgb values | ||
redV = (redV + rShift) % 256; | ||
greenV = (greenV + gShift) % 256; | ||
blueV = (blueV + bShift) % 256; | ||
|
||
// update the colours | ||
sk.stroke(redV, greenV, blueV); | ||
sk.fill(redV, greenV, blueV); | ||
|
||
// draw | ||
sk.circle(sk.mouseX, sk.mouseY, circleSize); | ||
} | ||
}) |
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,40 @@ | ||
/* | ||
IGNORE THIS FILE; I was just experimenting with scoping lol, and may refer to this document while programming | ||
Guide: https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/ | ||
var variables can be redeclared | ||
*/ | ||
|
||
var a = 5; | ||
var a = 6; | ||
|
||
/* | ||
var variables are function scoped - if it's defined in a function, then it can be used in the function (even if it was defined many blocks deep) | ||
Also hoisted, with undefined | ||
*/ | ||
|
||
function doSomething(){ | ||
var a = 8; | ||
console.log("Inside function: " + a); // 8 | ||
} | ||
|
||
doSomething(); | ||
|
||
console.log("After function: " + a); // 6 - note that this is *different* because vars are function scoped; the variable 'a' in the function is different from the global 'a' | ||
|
||
function changeA(){ | ||
a = 13; | ||
console.log("Inside changing function: " + a); // 13 | ||
} | ||
changeA(); | ||
|
||
console.log("Outside changing function: " + a); // 13 - expected since function changed the global var 'a' | ||
|
||
/* | ||
Okay so obviously this has issues because "var" allows you to redeclare a global variable | ||
"let" is a variable that is block-scoped and ALSO not able to be redeclared | ||
you CAN "redeclare" a let variable in another scope though -> they're treated as different variables, so there's no actual redeclaration | ||
Hoisted with REFERENCE ERROR | ||
*/ |