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.10.2/p5.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.10.2/addons/p5.sound.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

              
                /* PDM Course: Graphics Unit
This version by Matthew A. Bardin[2021]
    This is a version of the Paint App Project that utilizes a class of objects to generate all of the color boxes, an dmethods to call funcitons and change the color. This is just one of several potential solutions.
*/

// a list of all the variables used in this code.
let black, red, blue, green, yellow, orange, violet, white, currentColor;

function setup() {
  createCanvas(600, 600); //size of the canvas to draw on
  background(255);// be sure to set the background to white, and move the line to setup();
  
  black = new colorBox(0, 0, 50, 50, "black"); //making an object for each color box. This could potentially be done with a for loop
  red = new colorBox(0, 50, 50, 50, "red");
  green = new colorBox(0, 100, 50, 50, "green");
  blue = new colorBox(0, 150, 50, 50, "blue");
  yellow = new colorBox(0, 200, 50, 50, "yellow");
  orange = new colorBox(0, 250, 50, 50, "orange");
  violet = new colorBox(0, 300, 50, 50, "violet");
  white = new colorBox(0, 350, 50, 50, "white");
  
  currentColor = 0; //setting the default starting color
  
  instructions(); //makes the text instructions appear on the screen
}

function draw() {
  //calls a function to begin drawing the line on the canvas. doesn't need to be a function.
  if (mouseIsPressed) {
    drawing();
  }
//calling the methods for each object. Need them to appear on the canvas and behave properly when clicked on. without groups, this can take a lot of space in the code.
  black.appear();
  black.onMousePressed();
  red.appear();
  red.onMousePressed();
  green.appear();
  green.onMousePressed();
  blue.appear();
  blue.onMousePressed();
  yellow.appear();
  yellow.onMousePressed();
  orange.appear();
  orange.onMousePressed();
  violet.appear();
  violet.onMousePressed();
  white.appear();
  white.onMousePressed();
}
//class with all of the color box info
class colorBox {
  constructor(x, y, w, h, color) { //we don't technically need all of these controls for the color boxes. However it might be nice to have the flexibilty. The bare minimum of adjustable parameters are the y-position and the color. everything else (x-position, width, and height) can be hard coded in this project
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.color = color;
  }

  appear() { //tells the objects how to appear. without this called in draw() nothing will appear on the canvas
    push();
    if (this.color != "white") { //test in order to put the outline only around the white color box
      noStroke();
    } else {
      stroke(0);
      strokeWeight(1);
    }

    fill(this.color);
    rect(this.x, this.y, this.w, this.h);
    pop();
  }

  onMousePressed() { //what happens when the object is clicked on? in this case it tests to see if the mouse is over the boxes and then runs the color selection function if true. This doesn't need to be a seperate function, and can be something locally coded, but serves as a good reminder of how custom functions can work.
    if (mouseIsPressed) {
      if (mouseX < 51) {
        colorChange();
      }
    }
  }
}

function colorChange() { //the function that changes the color of the line being drawn
  if (mouseY > 0 && mouseY < 50) { //test to see which object the mouse is lined up with
    currentColor = "black"; //changes the vairable responcible for the color of the line. this line needs to match the same value as this.color
  } else if (mouseY > 50 && mouseY < 100) {
    currentColor = "red";
  } else if (mouseY > 100 && mouseY < 150) {
    currentColor = "green";
  } else if (mouseY > 150 && mouseY < 200) {
    currentColor = "blue";
  } else if (mouseY > 200 && mouseY < 250) {
    currentColor = "yellow";
  } else if (mouseY > 250 && mouseY < 300) {
    currentColor = "orange";
  } else if (mouseY > 300 && mouseY < 350) {
    currentColor = "violet";
  } else if (mouseY > 350 && mouseY < 400) {
    currentColor = "white";
  }
}

function drawing() { //function that does the drawing
  if (mouseX > 50) { //parameter to keep from drawing on the color selector boxes
    stroke(currentColor); //sets drawing color
    strokeWeight(3); //thichkess of drawing line
    line(pmouseX, pmouseY, mouseX, mouseY); //draws a line that follows the mouse
  }
}

function instructions(){ //seperate function containing everything with the on-screen instructions.
  fill("grey");
  stroke(0);
  rect(0, height - 20, width, 20);
  textAlign(CENTER);
  fill(0);
  text("Click and drag the mouse to paint! Choose your color by clicking on the boxes to the left!", width / 2, height - 8);
}
              
            
!
999px

Console