Forked from aerrity/bouncing-circles-es6-classes.js
Created
January 20, 2021 07:18
-
-
Save Tunji545/c48d0db4215d13e374d21db09eafcbcb to your computer and use it in GitHub Desktop.
Revisions
-
aerrity revised this gist
Nov 15, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,3 @@ P5.js example demonstrating [ES6 classes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes), inheritance and [method chaining](https://schier.co/blog/2013/11/14/method-chaining-in-javascript.html)  -
aerrity created this gist
Nov 15, 2017 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,78 @@ // ---------------------------- // Parent class (or superclass) // ---------------------------- class Circle { constructor(x = 50, y = 50, r = 100, col = '#f00') { this.x = x; this.y = y; this.r = r; this.col = col; this.vx = random(-5,5); this.vy = random(-5,5); } move() { this.x += this.vx; this.y += this.vy; return this; } draw() { fill(this.col); ellipse(this.x,this.y,this.r); return this; // allows method chaining } } // ---------------------------- // Child class (or subclass) // ---------------------------- class BouncingCircle extends Circle { constructor(x = 50, y = 50, r = 50, col = '#0f0') { super(x, y, r, col); } move() { this.x += this.vx; this.y += this.vy; // reverse direction ('bounce') if(!this.onScreen()) { this.vx *= -1; this.vy *= -1; } return this; // allows method chaining } onScreen() { if(this.x - (this.r / 2) < 0 || this.x + (this.r / 2) > width || this.y - (this.r / 2) < 0 || this.y + (this.r / 2) > height) { return false; } return true; }; } // ------------------------------- // Code to use the above 'classes' // ------------------------------- let circles = []; function setup() { createCanvas(400,400); background(0); } function draw() { background(0); for(let i = 0; i < circles.length; i++) { circles[i].move().draw(); // method chaining } } function mousePressed() { // clicking on the left half of the screen creates bouncing circles if(mouseX > width / 2) { circles.push( new BouncingCircle(mouseX, mouseY) ); } else { circles.push( new Circle(mouseX, mouseY) ); } } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,28 @@ <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>p5.js playground</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.14/p5.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.14/addons/p5.dom.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.14/addons/p5.sound.min.js"></script> <script src="script.js"></script> <style> body { margin:0; padding:0; overflow: hidden; } canvas { margin:auto; } </style> </head> <body> </body> </html> This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ P5.js example demonstrating ES6 classes, inheritance and method chaining 