HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URL's 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 it's URL and the proper URL extention.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<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>
html, body {
margin: 0;
padding: 0;
}
/* 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);
}
Also see: Tab Triggers