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 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.
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 esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM 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"
height="500" width="800">
</canvas>
body {
width: 100%;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #202020;
}
var c = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var TWO_PI = 2*Math.PI;
var h = canvas.height;
var w = canvas.width;
var elapsedTimer = 0;
var inc = 0;
var angle = 0;
var parms = {
x:w/2,
y:0,
width:15,
segment_height:25,
segments:15,
stem_color:"#3e8e05",
node_color:"#5cdb02",
leaf_color:"#3e8e05",
wind:0.05,
leafsf: 45,
stemlx: 61,
stemly: 35,
stemctl: -5,
min_width: 7,
width_variance: 2.0,
min_seg_height: 15,
seg_height_variance: 1.5,
min_seg_count: 13,
seg_count_variance: 3.0,
spacing: 5,
density: 30
};
var bamboos =[];
var eyes = [];
window.onload = function() {
var gui = new dat.GUI();
gui.add(parms, 'min_width', 10, 15);
gui.add(parms, 'width_variance', 1.1, 5.0);
gui.add(parms, 'min_seg_height', 15, 55);
gui.add(parms, 'seg_height_variance', 1.1, 5.0);
gui.add(parms, 'min_seg_count', 10, 45);
gui.add(parms, 'seg_count_variance', 1.1, 5.0);
gui.add(parms, 'spacing', 0, 50);
gui.add(parms, 'density', 5, 50);
gui.add(parms, 'leafsf', 10,200);
gui.add(parms, 'stemlx', 5,400);
gui.add(parms, 'stemly', 5,200);
gui.add(parms, 'stemctl', -200,-5);
gui.add(parms, 'wind', 0.01, 2.0);
gui.addColor(parms, 'stem_color');
gui.addColor(parms, 'node_color');
gui.addColor(parms, 'leaf_color');
gui.close();
};
ctx.fillStyle = "green";
ctx.strokeStyle = "green";
function init() {
generateEyes();
generateBamboos();
}
function generateEyes() {
eyes.length = 0;
for (var i=0; i < 8; i++) {
var x = randomInt(50, w-50);
var y = randomInt(50, h-50);
var size = randomNum(0.08, 0.1);
var angle = randomInt(0,20);
var color = randomInt(25, 75);
eyes.push(new Eye(x, y, size, -angle,color));
}
}
function generateBamboos() {
bamboos.length = 0;
var x = 0;
var dist = w/parms.density+parms.spacing;
for (var i=0; i<parms.density; i++) {
parms.x = randomInt(x, x+dist);
x+= dist;
parms.y = 0;
parms.width = randomInt(parms.min_width, parms.min_width * parms.width_variance);
parms.segment_height = randomInt(parms.min_seg_height,parms.min_seg_height * parms.seg_height_variance);
parms.segments = randomInt(parms.min_seg_count,parms.min_seg_count * parms.seg_count_variance);
bamboos.push(new Bamboo(ctx, parms.x, parms.y, parms.width, parms.segment_height, parms.segments, [parms.stem_color, parms.node_color, parms.leaf_color]));
}
}
function draw(timestamp) {
ctx.clearRect(0,0,w,h);
if (eyes.length > 0) {
for (var i=0; i<eyes.length; i++) {
eyes[i].draw(timestamp);
}
ctx.setTransform(1,0,0,1,0,0); //reset matrix
}
if (bamboos.length > 0) {
inc += 0.01;
angle = Math.sin(inc)/10.0 + Math.sin(inc*1.2)/20.0;
ctx.translate(0, h);
for (var j=0; j<bamboos.length; j++) {
bamboos[j].draw(angle);
}
ctx.setTransform(1,0,0,1,0,0); //reset matrix
}
else {
ctx.font = "38px sans-serif";
ctx.textAlign = "center";
ctx.fillText("Click to (re-)Generate Bamboos", w/2, h/2);
}
}
function Bamboo(ctx,x,y,width,segment_height,segments, colors) {
this.ctx = ctx;
this.x = x;
this.y = y;
this.width = width;
this.segment_height = segment_height; // height of a segment
this.segments = segments;
this.colors = colors;
this.left = !!Math.floor(Math.random() * 2);
this.branch = randomInt(1, segments-1);
this.leafScale = width/parms.leafsf;
this.branchNodes = [];
for (var i=0; i<segments/3; i++) { // just upper branches have a chance of leaf
if (Math.random() > 0.55) {
this.branchNodes[i] = !!Math.floor(Math.random() * 2); // add a leaf to sparse array
}
}
}
Bamboo.prototype.draw = function draw(angle) {
this.ctx.save();
this.ctx.translate(this.x, this.y);
this.ctx.lineWidth = this.width;
for (var i = this.segments; i > 0; i--) {
this.ctx.strokeStyle = this.colors[0];
this.ctx.lineWidth = this.width - i*0.005;
this.ctx.beginPath();
this.ctx.moveTo(0,0);
this.ctx.lineTo(0, -this.segment_height);
this.ctx.stroke();
// draw section
if (i>1) {
this.ctx.strokeStyle = this.colors[1];
this.ctx.beginPath();
this.ctx.lineWidth = this.width/5;
this.ctx.moveTo(-this.width/2, -this.segment_height);
this.ctx.lineTo(this.width/2, -this.segment_height);
this.ctx.stroke();
if (typeof this.branchNodes[i] !== 'undefined') {
//console.log("draw leaves");
this.ctx.save();
this.ctx.scale(this.leafScale, this.leafScale); // leaf method determines scale based on size of bamboo in object properties
this.drawLeaves(0,0, this.branchNodes[i]);
this.ctx.restore();
}
}
this.ctx.translate(0, -this.segment_height);
this.ctx.rotate(angle*parms.wind);
}
ctx.restore();
};
Bamboo.prototype.drawLeaves = function drawLeaves(x,y,left) {
// var lx = 100;
// var ly = 25;
var rotation = [0.0, 0.8, 1.0];
this.ctx.translate(x,y);
if (left === true) {
this.ctx.scale(-1, 1);
}
this.ctx.strokeStyle = this.colors[0];
this.ctx.fillStyle = this.colors[2];
// draw stem
this.ctx.beginPath();
this.ctx.lineWidth = 6;
this.ctx.moveTo(0,5);
this.ctx.quadraticCurveTo(0, parms.stemctl, parms.stemlx, parms.stemly);
this.ctx.stroke();
this.ctx.translate(parms.stemlx, parms.stemly); // end of branch
// draw leaves
for (var i=0; i<3; i++) {
this.ctx.rotate(rotation[i]);
this.ctx.beginPath();
this.ctx.moveTo(0,0);
this.ctx.quadraticCurveTo(50, -25, 100, 0);
this.ctx.quadraticCurveTo(37, 36, 0, 0);
this.ctx.fill();
}
};
function Eye(x, y, size, angle, color) {
this.x = x;
this.y = y;
this.color = color;
this.size = size;
this.angle = angle;
this.timer = 0;
this.interval = 0;
this.opacity = 0;
this.fadeSpeed = 0.01;
this.fadeTarget = 0;
this.fade = false;
}
Eye.prototype.draw = function draw(timestamp) {
var rotation = [0.0, 0.8, 1.0];
var sep = 20;
sep *=this.size;
if (this.fade) {
if (this.fadeTarget===1) {
if (this.opacity >= this.fadeTarget)
this.fade = false;
else
this.opacity += this.fadeSpeed;
}
else {
if (this.opacity <= this.fadeTarget)
this.fade = false;
else
this.opacity -= this.fadeSpeed;
}
}
else if (timestamp - this.timer > this.interval) {
//this.opacity = this.opacity===0 ? 1 : 0;
this.fadeTarget = this.opacity <=0 ? 1 : 0;
this.fade = true;
this.interval = randomInt(1000, 10000); // randomly toggle opacity
this.timer = timestamp;
}
ctx.fillStyle = 'hsla(0,100%,'+this.color+'%,'+this.opacity+')';
ctx.save();
ctx.setTransform(1,0,0,1,0,0); //reset matrix
//ctx.fillStyle = this.color;
ctx.translate(this.x+sep, this.y);
ctx.scale(this.size, this.size);
ctx.rotate(radians(this.angle));
ctx.beginPath();
ctx.moveTo(0,0);
ctx.quadraticCurveTo(50, -25, 100, 0);
ctx.quadraticCurveTo(37, 36, 0, 0);
ctx.fill();
ctx.setTransform(1,0,0,1,0,0); //reset matrix
ctx.translate(this.x-sep, this.y);
ctx.scale(this.size, this.size);
ctx.rotate(radians(-this.angle));
ctx.scale(-1, 1);
ctx.beginPath();
ctx.moveTo(0,0);
ctx.quadraticCurveTo(50, -25, 100, 0);
ctx.quadraticCurveTo(37, 36, 0, 0);
ctx.fill();
ctx.restore();
};
// catch left button clicks and draw a new canvas
// but allow right click to save canvas as an image without changing it
c.addEventListener("click", canvasClick, false);
function canvasClick(e) {
e = e || window.event;
if ('object' === typeof e) {
if (e.button !== 0) return; // if it's not the left mouse button, then bail
else {
init();
}
}
}
window.requestAnimationFrame(anim);
function anim(timestamp) {
// if ((timestamp - elapsedTimer) >= 100) {
draw(timestamp);
// elapsedTimer = timestamp;
// }
window.requestAnimationFrame(anim);
}
function radians(deg) {
return deg * Math.PI/180;
}
function randomInt(min,max) {
return Math.floor(Math.random()*(max-min+1)+min);
}
function randomNum(min,max) {
return Math.random() * (max - min) + min;
}
function resizeCanvas () {
var sX = window.innerWidth / canvas.width;
var sY = window.innerHeight / canvas.height;
var sFit = Math.min(sX, sY);
document.getElementById("canvas").style.transformOrigin = "0 0";
document.getElementById("canvas").style.transform = "scale(" + sFit + ")";
// center
var nX = Math.floor(((window.innerWidth - sFit * canvas.width) / 2) / sFit);
var nY = Math.floor(((window.innerHeight - sFit * canvas.height) / 2) / sFit);
document.getElementById("canvas").style.padding = nY + "px " + nX + "px";
h = canvas.height;
w = canvas.width;
}
window.addEventListener("resize", resizeCanvas);
resizeCanvas();
Also see: Tab Triggers