Skip to content

Commit 53c379d

Browse files
committed
renaming and arranging example files to sync all 4 languages
1 parent 2b86c8e commit 53c379d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+26880
-18
lines changed

package-lock.json

+25,533-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/assets/js/reference.js

+8-8
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* @name Trig Wheels and Pie Chart
3+
* @frame 400,400
4+
* @description contributed by <a href="https://www.rit.edu/directory/wmhics-w-michelle-harris">
5+
<b>Prof WM Harris,</b></a> <b>How</b> to create
6+
a trig color wheel and a visualization of a population age data as a
7+
pie chart.<br/>
8+
Functions are
9+
created for the canvas setup, trig color wheel, drawslice, and pie
10+
chart. The size of the slices are determined as well as their color
11+
range. The pie chart is separated by definitive color per value
12+
whereas the trig color wheel has a fixed slice amount with a range
13+
color fill.
14+
*/
15+
16+
function setup() {
17+
createCanvas(400, 400);
18+
colorMode(HSB);
19+
angleMode(DEGREES);
20+
21+
//vars for color wheel center point
22+
let x = width / 2;
23+
let y = height / 2 + 100;
24+
colorWheel(x, y, 100); //slide 11
25+
26+
noStroke();
27+
pieChartPop(200, 100); //slide 12
28+
}
29+
30+
//**** slide 12 pie chart trig demo
31+
function pieChartPop(x, y) {
32+
let [total, child, young, adult, senior, elder] = [577, 103, 69,
33+
122, 170, 113
34+
];
35+
let startValue = 0;
36+
let range = 0;
37+
38+
//child slice
39+
range = child / total;
40+
drawSlice("blue", x, y, 200, startValue, startValue + range);
41+
startValue += range;
42+
//young slice
43+
range = young / total;
44+
drawSlice("orange", x, y, 200, startValue, startValue + range);
45+
startValue += range;
46+
//adult slice
47+
range = adult / total;
48+
drawSlice("green", x, y, 200, startValue, startValue + range);
49+
startValue += range;
50+
//senior slice
51+
range = senior / total;
52+
drawSlice("tan", x, y, 200, startValue, startValue + range);
53+
startValue += range;
54+
//elder slice
55+
range = elder / total;
56+
drawSlice("pink", x, y, 200, startValue, startValue + range);
57+
startValue += range;
58+
59+
}
60+
61+
/**
62+
* drawSlice - draw colored arc based on angle percentages. slide 13
63+
* Adjust angles so that 0% starts at top (actually -90).
64+
* @param {color} fColor - fill color
65+
* @param {number} x - center x
66+
* @param {number} y - center y
67+
* @param {number} d - diameter
68+
* @param {float} percent1 - starting percentage
69+
* @param {float} percent2 - ending percentage
70+
*/
71+
function drawSlice(fColor, x, y, d, percent1, percent2) {
72+
fill(fColor);
73+
arc(x, y, d, d, -90 + percent1 * 360, -90 + percent2 * 360);
74+
}
75+
76+
//**** slide 11 trig demo
77+
function colorWheel(x, y, rad) {
78+
strokeWeight(10);
79+
strokeCap(SQUARE);
80+
81+
//Iterate 360 degrees of lines, +10deg per turn
82+
for (let a = 0; a < 360; a += 10) {
83+
stroke(a, 150, 200); //hue based on a
84+
//radius is 100, angle is a degrees
85+
line(x, y, x + rad * cos(a),
86+
y + rad * sin(a));
87+
}
88+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* @name Walk Over 2dArray
3+
* @frame 400,400
4+
* @description contributed by <a href="https://www.rit.edu/directory/wmhics-w-michelle-harris">
5+
<b>Prof WM Harris,</b></a> How to display 2D array contents on the canvas
6+
using regular for and for-of loops in multiple different ways.<br/>
7+
A function is created for the canvas, the 2D
8+
array (Friend Array) is initialized and walked over using nested
9+
loops in different ways. Variables x and y are used to place the
10+
array item on the canvas in the form of 2D array.
11+
The final nested loop is used to initialize 2D
12+
array (Fish Array) with random Integers (fish ages).
13+
*/
14+
15+
16+
//"use strict"; //catch some common coding errors
17+
18+
19+
/**
20+
* setup :
21+
*/
22+
function setup() {
23+
createCanvas(400, 600);
24+
//create 2D array, slide 4
25+
let friendArray = [
26+
["Nona", "mac & cheese", "orange", "Eid al-fitr"],
27+
["Marylin", "ice cream", "blue", "Halloween"],
28+
["Rashaad", "garbage plates", "turquoise", "Christmas"],
29+
["Ava", "sushi", "pink", "New Years"]
30+
];
31+
friendArray.push(["Xavier", "Louisiana creole", "red", "their birthday"]);
32+
33+
//walking 2D array, slide 6
34+
let y = 20; // Start row based on text size of 20
35+
for (let f = 0; f < friendArray.length; f++) { // outer array
36+
let x = 10; // Start item in this row
37+
for (let t = 0; t < friendArray[f].length; t++) { //inner
38+
text(friendArray[f][t], x, y);
39+
x += textWidth(friendArray[f][t]) + 20; //place next item
40+
}
41+
y += 28; // place next row
42+
}
43+
44+
//walking 2D array, variation on slide 6
45+
//with embedded arithmetic for y
46+
//
47+
for (let f = 0; f < friendArray.length; f++) { // outer array
48+
let x = 10; // Start item in this row
49+
for (let t = 0; t < friendArray[f].length; t++) { //inner
50+
//y is v-padding + LCV * v-spacing
51+
text(friendArray[f][t], x, 200 + f * 28);
52+
x += textWidth(friendArray[f][t]) + 20; //place next item
53+
}
54+
}
55+
56+
//walking 2D array, slide 7
57+
//need to use x and y variables to manage canvas placement
58+
y = 400;
59+
for (let friend of friendArray) {
60+
let x = 10; // Start item in this row
61+
console.log("x and y", x, y);
62+
console.log("friend:", friend);
63+
for (let item of friend) {
64+
console.log("item & x:", item, x);
65+
text(item, x, y);
66+
x += textWidth(item) + 20; //place next item
67+
}
68+
y += 28; // place next row
69+
}
70+
71+
//slide 9, creating 2D array: schools of fish ages
72+
console.log("\n *** Fish ages in 2D ***");
73+
const schools = [];
74+
//4 schools of fish
75+
for (let t = 0; t < 4; t++) {
76+
schools[t] = []; //initialize this school
77+
console.log("schools[t]?", t, schools[t]);
78+
79+
// Add 10 randomized ages to the array
80+
for (let a = 0; a < 10; a++) {
81+
schools[t].push(round(random(1, 5)));
82+
}
83+
}
84+
console.log(schools);
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* @name Conditional Shapes
3+
* @frame 400,400
4+
* @description contributed by <a href="https://www.rit.edu/directory/wmhics-w-michelle-harris">
5+
<b>Prof WM Harris,</b></a> How
6+
to draw different shapes mid canvas depending on the mouse position.<br/>
7+
<b>Functions</b>
8+
are created for the main canvas set up with the markers on the left and
9+
right hand sides. One is also created for the location of the mouse
10+
regarding the canvas and the markers. If the mouse is within the
11+
outer left hand beige rectangle, then the shape of circle is drawn
12+
down the center of the canvas. If the mouse is within the outer right
13+
hand beige rectangle, then the shape of square is drawn down the
14+
center of the canvas.
15+
*/
16+
function setup() {
17+
createCanvas(400, 400);
18+
strokeWeight(3);
19+
//center squares to match circles
20+
rectMode(CENTER);
21+
22+
//draw rects to mark far sides
23+
noStroke();
24+
fill("beige");
25+
rect(5, height / 2, 10, height);
26+
rect(width - 5, height / 2, 10, height);
27+
fill("orange");
28+
stroke("brown");
29+
30+
}
31+
32+
function draw() {
33+
point(mouseX, mouseY);
34+
35+
//if (test) {doThis; }
36+
//test: mouseX on far left of canvas
37+
//doThis: draw a circle at mouseY
38+
if (mouseX < 10) {
39+
circle(width / 2, mouseY, 20);
40+
}
41+
42+
//test: mouseX on far right of canvas
43+
//doThis: draw a square at mouseY
44+
if (mouseX > width - 10) {
45+
square(width / 2, mouseY, 20);
46+
}
47+
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* @name Car Instances
3+
* @frame 400,400
4+
* @description contributed by <a href="https://www.rit.edu/directory/wmhics-w-michelle-harris">
5+
<b>Prof WM Harris,</b></a> <b>How </b>to create three instances of Car Class and
6+
invoke class methods.<br/>
7+
A function is created for the canvas setup, and
8+
3 car instances are initialized with different colors and canvas
9+
positions. The speed of each car is set by passing value to the
10+
instance’s start method. A second function calls class methods to
11+
display and move the cars.
12+
*/
13+
class Car {
14+
/* Constructor expects parameters for
15+
fill color, x and y coordinates that
16+
will be used to initialize class properties.
17+
*/
18+
constructor(cColor, x, y) {
19+
this.color = cColor;
20+
this.doors = 4;
21+
this.isConvertible = false;
22+
this.x = x;
23+
this.y = y;
24+
this.speed = 0;
25+
}
26+
27+
start(speed) { // method expects parameter!
28+
this.speed = speed;
29+
}
30+
31+
display() { // method!
32+
fill(this.color);
33+
rect(this.x, this.y, 20, 10);
34+
}
35+
36+
move() { // method!
37+
this.x += this.speed;
38+
// Wrap x around boundaries
39+
if (this.x < -20) {
40+
this.x = width;
41+
} else if (this.x > width) {
42+
this.x = -20;
43+
}
44+
}
45+
} //end class Car
46+
47+
let rav4;
48+
let charger;
49+
let nova;
50+
51+
function setup() {
52+
createCanvas(200, 400);
53+
/* Construct the 3 Cars */
54+
//constructor expects cColor, x, y
55+
rav4 = new Car("silver", 100, 300);
56+
charger = new Car("gold", 0, 200);
57+
nova = new Car("blue", 200, 100);
58+
nova.doors = 2; //update nova's doors property
59+
60+
console.log("rav4", rav4);
61+
console.log("charger", charger);
62+
console.log("nova", nova);
63+
64+
//call start methods of Car instances
65+
//the start method expects a number for speed
66+
rav4.start(2.3);
67+
charger.start(-4);
68+
nova.start(random(-1, 1));
69+
}
70+
71+
function draw() {
72+
background("beige");
73+
74+
//display and move all 3 Cars
75+
rav4.display();
76+
charger.display();
77+
nova.display();
78+
79+
rav4.move();
80+
charger.move();
81+
nova.move();
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* @name Comments and Statements
3+
* @description Statements are the elements that make up programs. The ";" (semi-colon) symbol is used to end statements. It is called the "statement * terminator". Comments are used for making notes to help people better understand programs. A comment begins with two forward slashes ("//"). (ported from https://processing.org/examples/statementscomments.html)
4+
*/
5+
// The createCanvas function is a statement that tells the computer
6+
// how large to make the window.
7+
// Each function statement has zero or more parameters.
8+
// Parameters are data passed into the function
9+
// and are used as values for telling the computer what to do.
10+
function setup() {
11+
createCanvas(710, 400);
12+
}
13+
14+
// The background function is a statement that tells the computer
15+
// which color (or gray value) to make the background of the display window
16+
function draw() {
17+
background(204, 153, 0);
18+
}
19+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* @name Parametric Equations
3+
* @description A parametric equation is where x and y
4+
* coordinates are both written in terms of another letter. This is
5+
* called a parameter and is usually given in the letter t or θ.
6+
* The inspiration was taken from the YouTube channel of Alexander Miller.
7+
*/
8+
9+
function setup(){
10+
createCanvas(720,400);
11+
}
12+
13+
// the parameter at which x and y depends is usually taken as either t or symbol of theta
14+
let t = 0;
15+
function draw(){
16+
background('#fff');
17+
translate(width/2,height/2);
18+
stroke('#0f0f0f');
19+
strokeWeight(1.5);
20+
//loop for adding 100 lines
21+
for(let i = 0;i<100;i++){
22+
line(x1(t+i),y1(t+i),x2(t+i)+20,y2(t+i)+20);
23+
}
24+
t+=0.15;
25+
}
26+
// function to change initial x co-ordinate of the line
27+
function x1(t){
28+
return sin(t/10)*125+sin(t/20)*125+sin(t/30)*125;
29+
}
30+
31+
// function to change initial y co-ordinate of the line
32+
function y1(t){
33+
return cos(t/10)*125+cos(t/20)*125+cos(t/30)*125;
34+
}
35+
36+
// function to change final x co-ordinate of the line
37+
function x2(t){
38+
return sin(t/15)*125+sin(t/25)*125+sin(t/35)*125;
39+
}
40+
41+
// function to change final y co-ordinate of the line
42+
function y2(t){
43+
return cos(t/15)*125+cos(t/25)*125+cos(t/35)*125;
44+
}

0 commit comments

Comments
 (0)