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></canvas>
<p>Mouse down = explosions</p>
<!-- Social -->
<div class="twitter social-icon">
<a href="https://twitter.com/Christopher4Lis" target="_blank"></a>
<i class="fa fa-twitter fa-lg"></i>
</div>
<div class="youtube social-icon">
<a href="https://www.youtube.com/channel/UC9Yp2yz6-pwhQuPlIDV_mjA" target="_blank"></a>
<i class="fa fa-youtube fa-lg"></i>
</div>
body {
background: #212121;
margin: 0;
overflow: hidden;
}
@keyframes fadeIn {
from {opacity: 0;}
to {opacity: 1;}
}
@keyframes chill {
from {opacity: 1;}
to {opacity: 1;}
}
@keyframes fadeOut {
from {opacity: 1;}
to {opacity: 0;}
}
p {
position: absolute;
top: 10px;
left: 12px;
color: white;
opacity: 0;
font-family: "Source Sans Pro";
animation: fadeIn 2s ease-out, chill 2s 2s, fadeOut 2s 4s;
}
// Twitter
.twitter {
&:hover {
a {
transform: rotate(-45deg) scale(1.05);
}
i {
color: lighten(#00ACED, 10%);
}
}
a {
bottom: -40px;
right: -75px;
transform: rotate(-45deg);
}
i {
bottom: 7px;
right: 7px;
color: #00ACED;
}
}
.social-icon {
a {
position: absolute;
background: white;
color: white;
box-shadow: -1px -1px 20px 0px rgba(0,0,0,0.30);
display: inline-block;
width: 150px;
height: 80px;
transform-origin: 50% 50%;
transition: .15s ease-out;
}
i {
position: absolute;
pointer-events: none;
z-index: 1000;
transition: .15s ease-out;
}
}
// YouTube
.youtube {
&:hover {
a {
transform: rotate(45deg) scale(1.05);
}
i {
color: lighten(#E62117, 10%);
}
}
a {
bottom: -40px;
left: -75px;
transform: rotate(45deg);
}
i {
bottom: 7px;
left: 7px;
color: #E62117;
}
}
var canvas = document.querySelector('canvas');
var c = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var mouse = {
x: window.innerWidth / 2,
y: window.innerHeight / 2
};
var isMouseDown = false;
window.addEventListener("mousemove", function(event) {
mouse.x = event.clientX;
mouse.y = event.clientY;
});
window.addEventListener("resize", function() {
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
initializeVariables();
});
window.addEventListener("mousedown", function() {
isMouseDown = true;
});
window.addEventListener("mouseup", function() {
isMouseDown = false;
});
canvas.addEventListener("touchstart", function() {
isMouseDown = true;
});
canvas.addEventListener("touchmove", function(event) {
event.preventDefault();
mouse.x = event.touches[0].pageX;
mouse.y = event.touches[0].pageY;
});
canvas.addEventListener("touchend", function() {
isMouseDown = false;
});
function Cannon(x, y, width, height, color) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.angle = 0;
this.color = color;
this.update = function() {
desiredAngle = Math.atan2(mouse.y - this.y, mouse.x - this.x);
this.angle = desiredAngle;
this.draw();
};
this.draw = function() {
c.save();
c.translate(this.x, this.y);
c.rotate(this.angle);
c.beginPath();
c.fillStyle = this.color;
c.shadowColor = this.color;
c.shadowBlur = 3;
c.shadowOffsetX = 0;
c.shadowOffsetY = 0;
c.fillRect(0, -this.height / 2, this.width, height);
c.closePath();
c.restore();
};
}
function Cannonball(x, y, dx, dy, radius, color, cannon, particleColors) {
this.x = x;
this.y = y;
this.dx = dx;
this.dy = -dy;
this.radius = radius;
this.color = color;
this.particleColors = particleColors;
this.source = cannon;
this.timeToLive = canvas.height / (canvas.height + 800);
this.init = function() {
// Initialize the cannonballs start coordinates (from muzzle of cannon)
this.x = Math.cos(this.source.angle) * this.source.width;
this.y = Math.sin(this.source.angle) * this.source.width;
// Translate relative to canvas positioning
this.x = this.x + (canvas.width / 2);
this.y = this.y + (canvas.height);
// Determine whether the cannonball should be
// fired to the left or right of the cannon
if (mouse.x - canvas.width / 2 < 0) {
this.dx = -this.dx;
}
this.dy = Math.sin(this.source.angle) * 8;
this.dx = Math.cos(this.source.angle) * 8;
};
this.update = function() {
if (this.y + this.radius + this.dy > canvas.height) {
this.dy = -this.dy;
} else {
this.dy += gravity;
}
this.x += this.dx;
this.y += this.dy;
this.draw();
this.timeToLive -= 0.01;
};
this.draw = function() {
c.save();
c.beginPath();
c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
c.shadowColor = this.color;
c.shadowBlur = 5;
c.shadowOffsetX = 0;
c.shadowOffsetY = 0;
c.fillStyle = this.color;
c.fill();
c.closePath();
c.restore();
};
this.init();
}
function Particle(x, y, dx, dy, radius, color) {
this.x = x;
this.y = y;
this.dx = dx;
this.dy = -dy;
this.radius = 5;
this.color = color;
this.timeToLive = 1;
// this.mass = 0.2;
this.update = function() {
if (this.y + this.radius + this.dy > canvas.height) {
this.dy = -this.dy;
}
if (this.x + this.radius + this.dx > canvas.width || this.x - this.radius + this.dx < 0) {
this.dx = -this.dx;
}
// this.dy += gravity * this.mass;
this.x += this.dx;
this.y += this.dy;
this.draw();
this.timeToLive -= 0.01;
};
this.draw = function() {
c.save();
c.beginPath();
c.arc(this.x, this.y, 2, 0, Math.PI * 2, false);
c.shadowColor = this.color;
c.shadowBlur = 10;
c.shadowOffsetX = 0;
c.shadowOffsetY = 0;
c.fillStyle = this.color;
c.fill();
c.closePath();
c.restore();
};
}
function Explosion(cannonball) {
this.particles = [];
this.rings = [];
this.source = cannonball;
this.init = function() {
for (var i = 0; i < 10; i++) {
var dx = (Math.random() * 6) - 3;
var dy = (Math.random() * 6) - 3;
// var hue = (255 / 5) * i;
// var color = "hsl(" + hue + ", 100%, 50%)";
var randomColorIndex = Math.floor(Math.random() * this.source.particleColors.length);
var randomParticleColor = this.source.particleColors[randomColorIndex];
this.particles.push(new Particle(this.source.x, this.source.y, dx, dy, 1, randomParticleColor));
}
// Create ring once explosion is instantiated
// this.rings.push(new Ring(this.source, "blue"));
};
this.init();
this.update = function() {
for (var i = 0; i < this.particles.length; i++) {
this.particles[i].update();
// Remove particles from scene one time to live is up
if (this.particles[i].timeToLive < 0) {
this.particles.splice(i, 1);
}
}
// Render rings
for (var j = 0; j < this.rings.length; j++) {
this.rings[j].update();
// Remove rings from scene one time to live is up
if (this.rings[j].timeToLive < 0) {
this.rings.splice(i, 1);
}
}
};
}
var gravity = 0.08;
var desiredAngle = 0;
var cannon, cannonballs, explosions, colors;
function initializeVariables() {
cannon = new Cannon(canvas.width / 2, canvas.height, 20, 10, "white");
cannonballs = [];
explosions = [];
colors = [
// Red / Orange
{
cannonballColor: "#fff",
particleColors: [
"#ff4747",
"#00ceed",
"#fff",
]
}
];
}
initializeVariables();
var timer = 0;
var isIntroComplete = false;
var introTimer = 0;
function animate() {
window.requestAnimationFrame(animate);
c.fillStyle = "rgba(18, 18, 18, 0.2)";
c.fillRect(0, 0, canvas.width, canvas.height);
cannon.update();
if (isIntroComplete === false) {
introTimer += 1;
if (introTimer % 3 === 0) {
var randomColor = Math.floor(Math.random() * colors.length);
var color = colors[randomColor];
cannonballs.push(new Cannonball(canvas.width / 2, canvas.height / 2, 2, 2, 4, color.cannonballColor, cannon, color.particleColors));
}
if (introTimer > 30) {
isIntroComplete = true;
}
}
// Render cannonballs
for (var i = 0; i < cannonballs.length; i++) {
cannonballs[i].update();
if (cannonballs[i].timeToLive <= 0) {
// Create particle explosion after time to live expires
explosions.push(new Explosion(cannonballs[i]));
cannonballs.splice(i, 1);
}
}
// Render explosions
for (var j = 0; j < explosions.length; j++) {
//Do something
explosions[j].update();
// Remove explosions from scene once all associated particles are removed
if (explosions[j].particles.length <= 0) {
explosions.splice(j, 1);
}
}
if (isMouseDown === true) {
timer += 1;
if (timer % 3 === 0) {
var randomParticleColorIndex = Math.floor(Math.random() * colors.length);
var randomColors = colors[randomParticleColorIndex];
cannonballs.push(new Cannonball(mouse.x, mouse.y, 2, 2, 4, randomColors.cannonballColor, cannon, randomColors.particleColors));
}
}
}
animate();
Also see: Tab Triggers