Skip to content

Commit a7de77b

Browse files
committed
add composite Objects example
1 parent c7c1ddc commit a7de77b

File tree

3 files changed

+288
-0
lines changed

3 files changed

+288
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/* @name Composite Objects
2+
* @description An object can include several other objects.
3+
* Creating such composite objects is a good way to use the principles
4+
* of modularity and build higher levels of abstraction within a program.
5+
*/
6+
let er1, er2;
7+
8+
function setup() {
9+
createCanvas(640, 360);
10+
er1 = new EggRing(width*0.45, height*0.5, 0.1, 120);
11+
er2 = new EggRing(width*0.65, height*0.8, 0.05, 180);
12+
}
13+
14+
function draw() {
15+
background(0);
16+
er1.transmit();
17+
er2.transmit();
18+
}
19+
20+
Egg = function(xpos, ypos, t, s) {
21+
this.x = xpos;
22+
this.y = ypos;
23+
this.tilt = t;
24+
this.scalar = s / 100.0;
25+
this.angle = 0.0;
26+
27+
this.wobble = function() {
28+
this.tilt = cos(this.angle) / 8;
29+
this.angle += 0.1;
30+
}
31+
32+
this.display = function() {
33+
noStroke();
34+
fill(255);
35+
push();
36+
translate(this.x, this.y);
37+
rotate(this.tilt);
38+
scale(this.scalar);
39+
beginShape();
40+
vertex(0, -100);
41+
bezierVertex(25, -100, 40, -65, 40, -40);
42+
bezierVertex(40, -15, 25, 0, 0, 0);
43+
bezierVertex(-25, 0, -40, -15, -40, -40);
44+
bezierVertex(-40, -65, -25, -100, 0, -100);
45+
endShape();
46+
pop();
47+
}
48+
}
49+
50+
class Ring {
51+
start(xpos, ypos) {
52+
this.x = xpos;
53+
this.y = ypos;
54+
this.on = true;
55+
this.diameter = 1;
56+
}
57+
58+
grow() {
59+
if (this.on == true) {
60+
this.diameter += 0.5;
61+
if (this.diameter > width*2) {
62+
this.diameter = 0.0;
63+
}
64+
}
65+
}
66+
67+
display() {
68+
if (this.on == true) {
69+
noFill();
70+
strokeWeight(4);
71+
stroke(155, 153);
72+
ellipse(this.x, this.y, this.diameter, this.diameter);
73+
}
74+
}
75+
}
76+
77+
EggRing = function(x, y, t, sp) {
78+
let ovoid;
79+
let circle = new Ring();
80+
this.x = x;
81+
this.y = y;
82+
this.t = t;
83+
this.sp = sp;
84+
ovoid = new Egg(this.x, this.y, this.t, this.sp);
85+
circle.start(this.x, this.y - this.sp/2);
86+
87+
this.transmit = function() {
88+
ovoid.wobble();
89+
ovoid.display();
90+
circle.grow();
91+
circle.display();
92+
if (circle.on == false) {
93+
circle.on = true;
94+
}
95+
}
96+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/* @name Composite Objects
2+
* @description An object can include several other objects.
3+
* Creating such composite objects is a good way to use the principles
4+
* of modularity and build higher levels of abstraction within a program.
5+
*/
6+
let er1, er2;
7+
8+
function setup() {
9+
createCanvas(640, 360);
10+
er1 = new EggRing(width*0.45, height*0.5, 0.1, 120);
11+
er2 = new EggRing(width*0.65, height*0.8, 0.05, 180);
12+
}
13+
14+
function draw() {
15+
background(0);
16+
er1.transmit();
17+
er2.transmit();
18+
}
19+
20+
Egg = function(xpos, ypos, t, s) {
21+
this.x = xpos;
22+
this.y = ypos;
23+
this.tilt = t;
24+
this.scalar = s / 100.0;
25+
this.angle = 0.0;
26+
27+
this.wobble = function() {
28+
this.tilt = cos(this.angle) / 8;
29+
this.angle += 0.1;
30+
}
31+
32+
this.display = function() {
33+
noStroke();
34+
fill(255);
35+
push();
36+
translate(this.x, this.y);
37+
rotate(this.tilt);
38+
scale(this.scalar);
39+
beginShape();
40+
vertex(0, -100);
41+
bezierVertex(25, -100, 40, -65, 40, -40);
42+
bezierVertex(40, -15, 25, 0, 0, 0);
43+
bezierVertex(-25, 0, -40, -15, -40, -40);
44+
bezierVertex(-40, -65, -25, -100, 0, -100);
45+
endShape();
46+
pop();
47+
}
48+
}
49+
50+
class Ring {
51+
start(xpos, ypos) {
52+
this.x = xpos;
53+
this.y = ypos;
54+
this.on = true;
55+
this.diameter = 1;
56+
}
57+
58+
grow() {
59+
if (this.on == true) {
60+
this.diameter += 0.5;
61+
if (this.diameter > width*2) {
62+
this.diameter = 0.0;
63+
}
64+
}
65+
}
66+
67+
display() {
68+
if (this.on == true) {
69+
noFill();
70+
strokeWeight(4);
71+
stroke(155, 153);
72+
ellipse(this.x, this.y, this.diameter, this.diameter);
73+
}
74+
}
75+
}
76+
77+
EggRing = function(x, y, t, sp) {
78+
let ovoid;
79+
let circle = new Ring();
80+
this.x = x;
81+
this.y = y;
82+
this.t = t;
83+
this.sp = sp;
84+
ovoid = new Egg(this.x, this.y, this.t, this.sp);
85+
circle.start(this.x, this.y - this.sp/2);
86+
87+
this.transmit = function() {
88+
ovoid.wobble();
89+
ovoid.display();
90+
circle.grow();
91+
circle.display();
92+
if (circle.on == false) {
93+
circle.on = true;
94+
}
95+
}
96+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/* @name Composite Objects
2+
* @description An object can include several other objects.
3+
* Creating such composite objects is a good way to use the principles
4+
* of modularity and build higher levels of abstraction within a program.
5+
*/
6+
let er1, er2;
7+
8+
function setup() {
9+
createCanvas(640, 360);
10+
er1 = new EggRing(width*0.45, height*0.5, 0.1, 120);
11+
er2 = new EggRing(width*0.65, height*0.8, 0.05, 180);
12+
}
13+
14+
function draw() {
15+
background(0);
16+
er1.transmit();
17+
er2.transmit();
18+
}
19+
20+
Egg = function(xpos, ypos, t, s) {
21+
this.x = xpos;
22+
this.y = ypos;
23+
this.tilt = t;
24+
this.scalar = s / 100.0;
25+
this.angle = 0.0;
26+
27+
this.wobble = function() {
28+
this.tilt = cos(this.angle) / 8;
29+
this.angle += 0.1;
30+
}
31+
32+
this.display = function() {
33+
noStroke();
34+
fill(255);
35+
push();
36+
translate(this.x, this.y);
37+
rotate(this.tilt);
38+
scale(this.scalar);
39+
beginShape();
40+
vertex(0, -100);
41+
bezierVertex(25, -100, 40, -65, 40, -40);
42+
bezierVertex(40, -15, 25, 0, 0, 0);
43+
bezierVertex(-25, 0, -40, -15, -40, -40);
44+
bezierVertex(-40, -65, -25, -100, 0, -100);
45+
endShape();
46+
pop();
47+
}
48+
}
49+
50+
class Ring {
51+
start(xpos, ypos) {
52+
this.x = xpos;
53+
this.y = ypos;
54+
this.on = true;
55+
this.diameter = 1;
56+
}
57+
58+
grow() {
59+
if (this.on == true) {
60+
this.diameter += 0.5;
61+
if (this.diameter > width*2) {
62+
this.diameter = 0.0;
63+
}
64+
}
65+
}
66+
67+
display() {
68+
if (this.on == true) {
69+
noFill();
70+
strokeWeight(4);
71+
stroke(155, 153);
72+
ellipse(this.x, this.y, this.diameter, this.diameter);
73+
}
74+
}
75+
}
76+
77+
EggRing = function(x, y, t, sp) {
78+
let ovoid;
79+
let circle = new Ring();
80+
this.x = x;
81+
this.y = y;
82+
this.t = t;
83+
this.sp = sp;
84+
ovoid = new Egg(this.x, this.y, this.t, this.sp);
85+
circle.start(this.x, this.y - this.sp/2);
86+
87+
this.transmit = function() {
88+
ovoid.wobble();
89+
ovoid.display();
90+
circle.grow();
91+
circle.display();
92+
if (circle.on == false) {
93+
circle.on = true;
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)