Skip to content

Commit 74900b0

Browse files
committed
Added examples
1 parent da5c8ca commit 74900b0

307 files changed

Lines changed: 6861 additions & 107 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class Ball {
2+
constructor(tempX, tempY, tempW) {
3+
this.x = tempX;
4+
this.y = tempY;
5+
this.w = tempW;
6+
this.speed = 0;
7+
this.gravity = 0.1;
8+
this.life = 255;
9+
}
10+
move() {
11+
this.speed += this.gravity;
12+
this.y += this.speed;
13+
if (this.y > height) {
14+
this.speed *= -0.8;
15+
this.y = height;
16+
}
17+
}
18+
finished() {
19+
this.life--;
20+
return this.life < 0;
21+
}
22+
display() {
23+
fill(0, this.life);
24+
ellipse(this.x, this.y, this.w, this.w);
25+
}
26+
}
27+
28+
let balls = [];
29+
let ballWidth = 48;
30+
31+
function setup() {
32+
createCanvas(640, 360);
33+
noStroke();
34+
balls.push(new Ball(width / 2, 0, ballWidth));
35+
}
36+
37+
function draw() {
38+
background(255);
39+
for (let i = balls.length - 1; i >= 0; i--) {
40+
let b = balls[i];
41+
b.move();
42+
b.display();
43+
if (b.finished()) {
44+
balls.splice(i, 1);
45+
}
46+
}
47+
}
48+
49+
function mousePressed() {
50+
balls.push(new Ball(mouseX, mouseY, ballWidth));
51+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
let lottery;
2+
let results;
3+
let ticket;
4+
5+
function showList(list, x, y) {
6+
for (let i = 0; i < list.size(); i++) {
7+
let val = list.get(i);
8+
stroke(255);
9+
noFill();
10+
ellipse(x + i * 32, y, 24, 24);
11+
textAlign(CENTER);
12+
fill(255);
13+
text(val, x + i * 32, y + 6);
14+
}
15+
}
16+
17+
function setup() {
18+
createCanvas(640, 360);
19+
frameRate(30);
20+
lottery = new IntList();
21+
results = new IntList();
22+
ticket = new IntList();
23+
for (let i = 0; i < 20; i++) {
24+
lottery.append(i);
25+
}
26+
for (let i = 0; i < 5; i++) {
27+
let index = int(random(lottery.size()));
28+
ticket.append(lottery.get(index));
29+
}
30+
}
31+
32+
function draw() {
33+
background(51);
34+
lottery.shuffle();
35+
showList(lottery, 16, 48);
36+
showList(results, 16, 100);
37+
showList(ticket, 16, 140);
38+
for (let i = 0; i < results.size(); i++) {
39+
if (results.get(i) === ticket.get(i)) {
40+
fill(0, 255, 0, 100);
41+
} else {
42+
fill(255, 0, 0, 100);
43+
}
44+
ellipse(16 + i * 32, 140, 24, 24);
45+
}
46+
if (frameCount % 30 === 0) {
47+
if (results.size() < 5) {
48+
let val = lottery.get(0);
49+
lottery.remove(0);
50+
results.append(val);
51+
} else {
52+
for (let i = 0; i < results.size(); i++) {
53+
lottery.append(results.get(i));
54+
}
55+
results.clear();
56+
}
57+
}
58+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* Loading JSON Data
3+
* Translated to C++ Mode from Processing Java by Daniel Shiffman.
4+
*/
5+
6+
class Bubble {
7+
constructor(x, y, diameter, name) {
8+
this.x = x;
9+
this.y = y;
10+
this.diameter = diameter;
11+
this.name = name;
12+
this.over = false;
13+
}
14+
15+
rollover(px, py) {
16+
this.over = dist(px, py, this.x, this.y) < this.diameter / 2;
17+
}
18+
19+
display() {
20+
stroke(0);
21+
strokeWeight(2);
22+
noFill();
23+
ellipse(this.x, this.y, this.diameter, this.diameter);
24+
if (this.over) {
25+
fill(0);
26+
textAlign(CENTER);
27+
text(this.name, this.x, this.y + this.diameter / 2 + 20);
28+
}
29+
}
30+
}
31+
32+
let bubbles = [];
33+
let json;
34+
35+
function loadData() {
36+
json = loadJSONObject("data/data.json");
37+
38+
if (json === null || !json.hasKey("bubbles")) {
39+
println("Failed to load data.json");
40+
return;
41+
}
42+
43+
let bubbleData = json.getJSONArray("bubbles");
44+
bubbles = [];
45+
46+
for (let i = 0; i < bubbleData.size(); i++) {
47+
let b = bubbleData.getJSONObject(i);
48+
if (!b.hasKey("position")) continue;
49+
let pos = b.getJSONObject("position");
50+
let x = pos.getFloat("x");
51+
let y = pos.getFloat("y");
52+
let diameter = b.getFloat("diameter");
53+
let label = b.getString("label");
54+
bubbles.push(new Bubble(x, y, diameter, label));
55+
}
56+
57+
println("Loaded " + bubbles.length + " bubbles");
58+
}
59+
60+
function setup() {
61+
size(640, 360);
62+
loadData();
63+
}
64+
65+
function draw() {
66+
background(255);
67+
for (let i = 0; i < bubbles.length; i++) {
68+
let b = bubbles[i];
69+
b.display();
70+
b.rollover(mouseX, mouseY);
71+
}
72+
textAlign(LEFT);
73+
fill(0);
74+
text("Click to add bubbles.", 10, height - 10);
75+
}
76+
77+
function mousePressed() {
78+
let pos = new JSONObject();
79+
pos.setFloat("x", mouseX);
80+
pos.setFloat("y", mouseY);
81+
82+
let newBubble = new JSONObject();
83+
newBubble.setJSONObject("position", pos);
84+
newBubble.setFloat("diameter", random(40, 80));
85+
newBubble.setString("label", "New label");
86+
87+
let bubbleData = json.getJSONArray("bubbles");
88+
bubbleData.append(newBubble);
89+
90+
if (bubbleData.size() > 10) {
91+
bubbleData.remove(0);
92+
}
93+
94+
saveJSONObject(json, "data/data.json");
95+
loadData();
96+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* Loading Tabular Data
3+
* Translated to C++ Mode from Processing Java by Daniel Shiffman.
4+
*
5+
* Loads bubble data from a CSV file and allows adding
6+
* new bubbles by clicking.
7+
*
8+
* CSV format (data/data.csv):
9+
* x,y,diameter,name
10+
* 160,103,43.19838,Happy
11+
* 372,137,52.42526,Sad
12+
* 273,235,61.14072,Joyous
13+
* 121,179,44.758068,Melancholy
14+
*/
15+
16+
class Bubble {
17+
constructor(x, y, diameter, name) {
18+
this.x = x;
19+
this.y = y;
20+
this.diameter = diameter;
21+
this.name = name;
22+
this.over = false;
23+
}
24+
25+
rollover(px, py) {
26+
this.over = dist(px, py, this.x, this.y) < this.diameter / 2;
27+
}
28+
29+
display() {
30+
stroke(0);
31+
strokeWeight(2);
32+
noFill();
33+
ellipse(this.x, this.y, this.diameter, this.diameter);
34+
if (this.over) {
35+
fill(0);
36+
textAlign(CENTER);
37+
text(this.name, this.x, this.y + this.diameter / 2 + 20);
38+
}
39+
}
40+
}
41+
42+
let bubbles = [];
43+
let table;
44+
45+
function loadData() {
46+
table = loadTable("data/data.csv", "header");
47+
48+
if (table === null) {
49+
println("Failed to load data.csv");
50+
return;
51+
}
52+
53+
bubbles = [];
54+
for (let i = 0; i < table.getRowCount(); i++) {
55+
let x = table.getNum(i, "x");
56+
let y = table.getNum(i, "y");
57+
let diameter = table.getNum(i, "diameter");
58+
let name = table.getString(i, "name");
59+
bubbles.push(new Bubble(x, y, diameter, name));
60+
}
61+
62+
println("Loaded " + bubbles.length + " bubbles");
63+
}
64+
65+
function setup() {
66+
size(640, 360);
67+
loadData();
68+
}
69+
70+
function draw() {
71+
background(255);
72+
for (let i = 0; i < bubbles.length; i++) {
73+
let b = bubbles[i];
74+
b.display();
75+
b.rollover(mouseX, mouseY);
76+
}
77+
textAlign(LEFT);
78+
fill(0);
79+
text("Click to add bubbles.", 10, height - 10);
80+
}
81+
82+
function mousePressed() {
83+
if (table === null) return;
84+
85+
let row = table.addRow();
86+
row.setNum("x", mouseX);
87+
row.setNum("y", mouseY);
88+
row.setNum("diameter", random(40, 80));
89+
row.setString("name", "Blah");
90+
91+
if (table.getRowCount() > 10) {
92+
table.removeRow(0);
93+
}
94+
95+
saveTable(table, "data/data.csv");
96+
loadData();
97+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Animated Sprite (Shifty + Teddy)
3+
* by James Paterson.
4+
* Translated to JavaScript Mode.
5+
*
6+
* Press the mouse button to change animations.
7+
*/
8+
let animation1 = null;
9+
let animation2 = null;
10+
let xpos;
11+
let ypos;
12+
let drag = 30.0;
13+
14+
function setup() {
15+
createCanvas(640, 360);
16+
background(255, 204, 0);
17+
frameRate(24);
18+
animation1 = new Animation("PT_Shifty_", 38);
19+
animation2 = new Animation("PT_Teddy_", 60);
20+
ypos = height * 0.25;
21+
}
22+
23+
function draw() {
24+
let dx = mouseX - xpos;
25+
xpos = xpos + dx / drag;
26+
if (mouseIsPressed) {
27+
background(153, 153, 0);
28+
animation1.display(xpos - animation1.getWidth() / 2, ypos);
29+
} else {
30+
background(255, 204, 0);
31+
animation2.display(xpos - animation2.getWidth() / 2, ypos);
32+
}
33+
}
34+
35+
class Animation {
36+
constructor(prefix, count) {
37+
this.imageCount = count;
38+
this.images = [];
39+
this.frame = 0;
40+
for (let i = 0; i < count; i++) {
41+
let filename = prefix + nf(i, 4) + ".gif";
42+
this.images.push(loadImage(filename));
43+
}
44+
}
45+
46+
display(x, y) {
47+
this.frame = (this.frame + 1) % this.imageCount;
48+
let img = this.images[this.frame];
49+
if (img) image(img, x, y);
50+
}
51+
52+
getWidth() {
53+
let img = this.images[0];
54+
if (img) return img.width;
55+
return 0;
56+
}
57+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Sequential
3+
* by James Paterson.
4+
* Translated to JavaScript Mode.
5+
*
6+
* Displaying a sequence of images creates the illusion of motion.
7+
* Twelve images are loaded and each is displayed individually in a loop.
8+
*/
9+
let numFrames = 12;
10+
let currentFrame = 0;
11+
let images = [];
12+
13+
function setup() {
14+
createCanvas(640, 360);
15+
frameRate(24);
16+
images = [];
17+
for (let i = 0; i < numFrames; i++) {
18+
let name = "PT_anim" + nf(i, 4) + ".gif";
19+
images.push(loadImage(name));
20+
}
21+
}
22+
23+
function draw() {
24+
background(0);
25+
currentFrame = (currentFrame + 1) % numFrames;
26+
let offset = 0;
27+
for (let x = -100; x < width; x += images[0].width) {
28+
image(images[(currentFrame + offset) % numFrames], x, -20);
29+
offset += 2;
30+
image(images[(currentFrame + offset) % numFrames], x, height / 2);
31+
offset += 2;
32+
}
33+
}

0 commit comments

Comments
 (0)