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.
<html>
<head>
<title>Canvas Balloons</title>
</head>
<body>
<p>
left and right - wind direction<br/>
up key - climb<br/>
down key - fall<br/>
enter - reset climb and fall
</p>
<canvas id="mainCanvas"></canvas>
<script src="balloon.js"></script>
</body>
</html>
* {
margin: 0;
padding: 0;
}
html, body {
width:100%;
height:100%;
background-color:#463E41;
}
canvas {
margin: 0 auto;
display: block;
border-left: 2px solid #463E41;
border-right: 2px solid #463E41;
border-bottom: 2px solid #463E41;
display:block;
}
p {
position: absolute;
top: 14px;
left: 8px;
font-family: Helvetica, Arial, sans-serif;
color: #aaa;
}
// twitter @msvaljek
// https://msvaljek.blogspot.com/2013/03/canvas-balloons.html
var ballonHeight = 75; //pixel
var ballonWidth = 50; //pixel
var ballonFlameLighter = 60; //percent
var ballonFlame = 'yellow';
var flameSize = 3; //pixel
var gondolaSize = 10; //pixel
var gondolaColor = '#150517';
var ballonFallRate = 0.05;
var ballonClimbRate = 0.1;
var ballonClimbRateBase = 60;//animFrame
var ballonClimbRateRandom = 500;//variable animFrame
var ballonCount = 5; //count
var windSpeed = 0.5;
var redrawClearMargin = 3;
var collisionMarginSensitivity = 2;
var starIntenistyGrades = 1;
var starIntensityStep = -30; // percent
var starCount = 200;
var canvasBackground = '#463E41';
var canvas = document.getElementById('mainCanvas');
canvas.style.background = canvasBackground;
canvas.width = window.innerWidth - 4;
canvas.height = window.innerHeight - 3;
var ctx = canvas.getContext('2d');
ctx.lineWidth = 0;
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function( callback ) {
window.setTimeout(callback, 1000 / 60);
};
})();
function randomInt(max) {
return Math.floor(Math.random() * max);
}
function shadeColor(color, percent) {
var num = parseInt(color.slice(1),16), amt = Math.round(2.55 * percent), R = (num >> 16) + amt, B = (num >> 8 & 0x00FF) + amt, G = (num & 0x0000FF) + amt;
return "#" + (0x1000000 + (R<255?R<1?0:R:255)*0x10000 + (B<255?B<1?0:B:255)*0x100 + (G<255?G<1?0:G:255)).toString(16).slice(1);
}
function getBallonColor() {
var r = randomInt(50).toString(16);
var g = randomInt(50).toString(16);
var b = randomInt(50).toString(16);
r = r.length < 2 ? '0' + r : r;
g = g.length < 2 ? '0' + g : g;
b = b.length < 2 ? '0' + b : b;
return '#' + r + g + b;
}
function checkCollision(obj1, obj2) {
return !(
(obj1.y + obj1.height < obj2.y) ||
(obj1.y > obj2.y + obj2.height) ||
(obj1.x > obj2.x + obj2.width) ||
(obj1.x + obj1.width < obj2.x)
);
}
var Star = function (x, y){
this.x = x;
this.y = y;
this.color = '#FFFFFF';
this.blinkCounter = 0;
this.blinkRate = 60 + randomInt(100);
this.intensity = Math.floor(Math.random() * starIntenistyGrades);
};
Star.prototype.draw = function () {
this.blinkCounter = (this.blinkCounter + 1) % this.blinkRate;
if (this.blinkCounter === 0) {
this.intensity = (this.intensity + 1) % (starIntenistyGrades + 1);
}
ctx.fillStyle = shadeColor(this.color, starIntensityStep * this.intensity);
ctx.beginPath();
ctx.arc(this.x, this.y, 1, 0, 2 * Math.PI, true);
ctx.fill();
};
var Ballon = function (x, y, id) {
this.x = x;
this.y = y;
this.dy = 0;
this.dx = 0;
this.id = id;
this.height = ballonHeight;
this.width = ballonWidth;
this.radius = (this.height > this.width ? this.width : this.height) / 2;
this.boxHeight = gondolaSize;
this.color = getBallonColor();
this.climbInterval = ballonClimbRateBase + randomInt(ballonClimbRateRandom);
this.climbCounter = 0;
this.climbing = Math.random() > 0.5;
this.responsiveness = Math.random();
};
Ballon.prototype.clear = function () {
ctx.clearRect(this.x - 1, this.y - 1, this.width + 1, this.height + 1);
};
Ballon.prototype.checkCollision = function (dx, dy) {
var collisionFlag = false;
var testBallon = {
x: this.x + dx - collisionMarginSensitivity * redrawClearMargin,
y: this.y + dy - collisionMarginSensitivity * redrawClearMargin,
width: ballonWidth + 2 * collisionMarginSensitivity * redrawClearMargin,
height: ballonHeight + 2 * collisionMarginSensitivity * redrawClearMargin
};
for (var j = 0; j < ballons.length && !collisionFlag; j ++) {
if (j !== this.id) {
collisionFlag = checkCollision(testBallon, ballons[j]);
}
}
return collisionFlag;
},
Ballon.prototype.move = function () {
var newX = this.x;
var newY = this.y;
this.climbCounter = (this.climbCounter + 1) % this.climbInterval;
if (this.climbCounter === 0) {
this.climbing = !this.climbing;
}
this.dy = this.responsiveness * (this.climbing ? -1 * ballonClimbRate : ballonFallRate);
var localWindSpeed = this.responsiveness * windSpeed;
if (!this.checkCollision(localWindSpeed, this.dy)) {
newX += localWindSpeed;
newY += this.dy;
} else if (!this.checkCollision(localWindSpeed, 0)) {
newX += localWindSpeed;
} else if (!this.checkCollision(0, this.dy)) {
newY += this.dy;
} else if (this.climbing && !this.checkCollision(0, ballonFallRate)) {
newY += ballonFallRate;
this.climbing = false;
} else if (!this.checkCollision(0, ballonFallRate)) {
newY += ballonFallRate;
} else {
for (var k = 0; k < this.height; k++) {
var factor = (this.climbing ? -1 : 1) * k;
if (!this.checkCollision(0, factor)) {
newY += factor;
break;
}
}
}
if (newX > 0 && newX + this.width < canvas.width - redrawClearMargin) {
this.x = newX;
}
if (newY > 0 && newY + this.height < canvas.height - redrawClearMargin) {
this.y = newY;
}
};
Ballon.prototype.draw = function () {
this.drawShapes(redrawClearMargin);
this.move();
this.drawShapes(0);
};
Ballon.prototype.drawShapes = function (clearColor) {
var ballonColor = this.color;
if (this.climbing) {
var linGrad = ctx.createLinearGradient(0, this.y, 0, this.y + this.height);
linGrad.addColorStop(0, ballonColor);
linGrad.addColorStop(1, shadeColor(ballonColor, ballonFlameLighter));
ballonColor = linGrad;
}
ctx.fillStyle = clearColor > 1 ? canvasBackground : ballonColor;
// top ballon circle
ctx.beginPath();
ctx.arc(this.x + this.width / 2, this.y + this.radius, this.radius + clearColor, Math.PI, 0, false);
ctx.fill();
// triangle
ctx.beginPath();
ctx.moveTo(this.x - clearColor, this.y + this.radius - clearColor - 1);
ctx.lineTo(this.x - clearColor + this.width / 2, this.y + this.height - this.boxHeight + clearColor);
ctx.lineTo(this.x - clearColor + this.width / 2 + clearColor + 1, this.y + this.height - this.boxHeight + clearColor);
ctx.lineTo(this.x + clearColor + this.width, this.y + this.radius - clearColor - 1);
ctx.lineTo(this.x - clearColor, this.y + this.radius - clearColor - 1);
ctx.fill();
// flame
if (this.climbing) {
ctx.fillStyle = clearColor > 1 ? canvasBackground : ballonFlame;
ctx.beginPath();
ctx.arc(this.x + this.width / 2, this.y + this.height - this.boxHeight, flameSize, 0, 2 * Math.PI, true);
ctx.fill();
}
// gondol
ctx.fillStyle = clearColor > 1 ? canvasBackground : gondolaColor;
ctx.fillRect(this.x + this.width / 4 + this.width / 8 - clearColor, this.y + this.height - this.boxHeight - clearColor, this.width / 4 + 2 *clearColor, this.boxHeight + 2 * clearColor);
};
var stars = [];
for (var i = 0; i < starCount; i++) {
stars[i] = new Star(randomInt(canvas.width), randomInt(canvas.height));
}
function generateTestBallon() {
return {
x: randomInt(canvas.width - ballonWidth),
y: randomInt(canvas.height - ballonHeight),
width : ballonWidth + 2 * collisionMarginSensitivity * redrawClearMargin,
height : ballonHeight + 2 * collisionMarginSensitivity * redrawClearMargin
};
}
var ballons = [];
var collisionFlag = false;
var testBallon = {};
for (var i = 0; i < ballonCount; i++) {
do {
collisionFlag = false;
testBallon = generateTestBallon();
for (var j = 0; j < ballons.length && !collisionFlag; j ++) {
collisionFlag = checkCollision(testBallon, ballons[j]);
}
} while (collisionFlag);
ballons[i] = new Ballon(testBallon.x, testBallon.y, i);
}
document.onkeydown = function(e) {
if(e.keyCode == 37) {
windSpeed -= 0.05;
} else if(e.keyCode == 39) {
windSpeed += 0.05;
} else if(e.keyCode == 38) {
ballonClimbRate += 0.03;
} else if(e.keyCode == 40) {
ballonFallRate += 0.03;
} else if (e.keyCode == 13) {
ballonFallRate = 0.05;
ballonClimbRate = 0.1;
windSpeed = 0;
}
return false;
};
(function animloop(){
requestAnimFrame(animloop);
for (var i = 0; i < stars.length; i++) {
stars[i].draw();
}
for (i = 0; i < ballons.length; i++) {
ballons[i].draw();
}
})();
Also see: Tab Triggers