Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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 6: Programming Digital Media

//Code by Matthew A. Bardin [2020]

//we will start by changing the objects back into blank variable.
let circle1
let circle2
/*
let circle1 = {
  x: 100,
  y: 100,
  diam: 50
};


let circle2 = {
  x: 200,
  y: 300,
  diam: 50
};
*/
function setup() {
  createCanvas(400, 400);
  circle1 = new Circle();//in this line we are creating a new object within a class. new is a keyword that will generate a unique instance of this class and store it within the given variable. the name of the variable will replace the keyword 'this' later on in the code.

}

function draw() {
  background(220, 200, 0);
  circle1.appear();
  circle1.move();

/*
  fill('green');
  ellipse(circle2.x, circle2.y, circle2.diam, circle2.diam);
*/
}

// here is where we are defining the class Circle(). This can be located anywhere within your code as long as it is not in another function. This will contain two main important sections: construction and functionality.
class Circle {
  //in this section we will define everything that makes up the object that we will eventiually make. notice the keyword 'this'. 'this' is used to say that whatever object will be made, the name will replace 'this' and be used to generate an object with the properties. 

  // think of it like you are generating a template to create new objects, and you are creating the presets. However because this is not the object itself, but rather the default values to be applied to the objects later, you don't know the name to put in yet.
  constructor() {
    this.x = 25;
    this.y = 100;
    this.diam = 50;
    this.moveDir = 1;
  }

  // when we are setting up the class, we can also define how the objects will behave. We can format this like a function, however we do not need the keyword function. What we do need is the keyword 'this' 
  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('purple');
    ellipse(this.x, this.y, this.diam, this.diam);
  }
}
              
            
!
999px

Console