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. 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.
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.
If the stylesheet 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 CSS 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.
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.
<canvas id="canvas"></canvas>
* {
margin: 0; padding: 0;
}
#canvas {
display: block;
}
window.onload = function(){
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
//Lets resize the canvas to occupy the full page
var W = window.innerWidth;
var H = window.innerHeight;
canvas.width = W;
canvas.height = H;
//Some variables
var length, divergence, reduction, line_width, start_points = [];
init();
function init()
{
//filling the canvas white
ctx.fillStyle = "white";
ctx.fillRect(0, 0, W, H);
//Lets draw the trunk of the tree
//lets randomise the variables
//length of the trunk - 100-150
length = 100 + Math.round(Math.random()*50);
//angle at which branches will diverge - 10-60
divergence = 10 + Math.round(Math.random()*50);
//Every branch will be 0.75times of the previous one - 0.5-0.75
//with 2 decimal points
reduction = Math.round(50 + Math.random()*20)/100;
//width of the branch/trunk
line_width = 10;
//This is the end point of the trunk, from where branches will diverge
var trunk = {x: W/2, y: length+50, angle: 90};
//It becomes the start point for branches
start_points = []; //empty the start points on every init();
start_points.push(trunk);
//Y coordinates go positive downwards, hence they are inverted by deducting it
//from the canvas height = H
ctx.beginPath();
ctx.moveTo(trunk.x, H-50);
ctx.lineTo(trunk.x, H-trunk.y);
ctx.strokeStyle = "brown";
ctx.lineWidth = line_width;
ctx.stroke();
branches();
}
//Lets draw the branches now
function branches()
{
//reducing line_width and length
length = length * reduction;
line_width = line_width * reduction;
ctx.lineWidth = line_width;
var new_start_points = [];
ctx.beginPath();
for(var i = 0; i < start_points.length; i++)
{
var sp = start_points[i];
//2 branches will come out of every start point. Hence there will be
//2 end points. There is a difference in the divergence.
var ep1 = get_endpoint(sp.x, sp.y, sp.angle+divergence, length);
var ep2 = get_endpoint(sp.x, sp.y, sp.angle-divergence, length);
//drawing the branches now
ctx.moveTo(sp.x, H-sp.y);
ctx.lineTo(ep1.x, H-ep1.y);
ctx.moveTo(sp.x, H-sp.y);
ctx.lineTo(ep2.x, H-ep2.y);
//Time to make this function recursive to draw more branches
ep1.angle = sp.angle+divergence;
ep2.angle = sp.angle-divergence;
new_start_points.push(ep1);
new_start_points.push(ep2);
}
//Lets add some more color
if(length < 10) ctx.strokeStyle = "green";
else ctx.strokeStyle = "brown";
ctx.stroke();
start_points = new_start_points;
//recursive call - only if length is more than 2.
//Else it will fall in an long loop
if(length > 2) setTimeout(branches, 50);
else setTimeout(init, 500);
}
function get_endpoint(x, y, a, length)
{
//This function will calculate the end points based on simple vectors
//http://physics.about.com/od/mathematics/a/VectorMath.htm
//You can read about basic vectors from this link
var epx = x + length * Math.cos(a*Math.PI/180);
var epy = y + length * Math.sin(a*Math.PI/180);
return {x: epx, y: epy};
}
}
Also see: Tab Triggers