Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
<html>
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/p5.min.js"></script>
     <script src="https://cdn.jsdelivr.net/gh/molleindustria/p5.play/lib/p5.play.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/addons/p5.dom.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tone/13.8.12/Tone.min.js"></script>
    <link rel="stylesheet" type="text/css" href="style.css">
    <meta charset="utf-8" />

  </head>
  <body>
    <script src="sketch.js"></script>
  </body>
</html>

              
            
!

CSS

              
                html, body {
  margin: 0;
  padding: 0;
}
              
            
!

JS

              
                //Objects and Classes 7: Programming Digital Media

//Code by Matthew A. Bardin [2020]

//If you experimented with the previous code you will have noticed that we couldn't get multiple cicrles to appear on the canvas at once and behave independently. That will be rectified in this example. In order to achieve this we will have to add arguments to our class, similarly to when we create custom functions.

let circle1;
let circle2;
let circles = []
function setup() {
  createCanvas(400, 400);

  // when we make our new objects, we can list out the arguments in the parentheses like when using a function, but that is not all we have to do. We also need to tell our class what to do with all of this new information that we are giving it.
  circle1 = new Circle(100, 100, 100, "red"); //x, y, diameter, color
  circle2 = new Circle(200, 300, 50, "green");
  
  for(let i = 0; i < 30; i++) {
    circles[i] = new Circle(i * 10, i * 15, i * 2, "red")
  }
}

function draw() {
  background(220);
  
  circles.forEach(circle => {
    circle.appear(); 
    circle.move();
  })
//   circle1.appear();
//   circle1.move();

//   circle2.appear();
//   circle2.move();
}

class Circle {
  // in the class we will pass the value fromt the arguments inside of setup() to the this. variables. This will allow us to create different objects using the same class. This is set up just like when aking functions. In this instance we are passing in 4 values, the x, y, diameter, and color of the object.
  constructor(x, y, diam, shade) {
    this.x = x;
    this.y = y;
    this.diam = diam;
    this.shade = shade;
    this.moveDir = 1;
  }

  move() {
    this.x = this.x + this.moveDir;

    if (this.x >= width - 25) {
      this.moveDir = -1;
    } else if (this.x <= 25) {
      this.moveDir = 1;
    }
  }

  appear() {
    fill(this.shade);
    ellipse(this.x, this.y, this.diam, this.diam);
  }
}

//Lastly, try to go in and create 2 more unique objects within this class. Give them unique values for the arguments so that the size, starting location, and colors are all different.

              
            
!
999px

Console