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="myCanvas" width="600" height="400"></canvas>
<p>This demo is part of the <a href="https://getbutterfly.com/canvas-an-awesome-introduction/"><b>Canvas</b>: An Awesome Introduction</a> introductory series.</p>
body {
height: 100%;
margin: 128px;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
canvas {
border: 4px dashed #ff0000; /* A red dashed border */
}
p {
font-family: "Times New Roman", "Times", serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
font-smoothing: antialiased;
text-rendering: optimizeLegibility;
font-smooth: always;
font-variant: historical-ligatures;
font-feature-settings: "hlig" 1, "dlig" 1, "onum" 1, "zero" 1, "swsh" 1, "kern" 1, "medi" 1;
font-size: 20px;
color: #1B1464;
letter-spacing: -0.004em;
}
window.requestAnimFrame = ((callback) => {
return (
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function (callback) {
window.setTimeout(callback, 1000 / 60);
}
);
})();
function randomFromTo(from, to) {
return Math.floor(Math.random() * (to - from + 1) + from);
}
function initBalls() {
balls = [];
let colors = ["#6F1E51", "#833471", "#B53471", "#ED4C67"];
for (let n = 0; n <= 8; n++) {
for (let i = 0; i <= 18; i++) {
balls.push(new Ball((i * 3 * 10) + 30, (n * 30) + 50 + 30, 0, 0, colors[randomFromTo(0, 8)]));
}
}
return balls;
}
function getMousePos(canvas, evt) {
let obj = canvas;
let top = 0;
let left = 0;
while (obj.tagName !== 'BODY') {
top += obj.offsetTop;
left += obj.offsetLeft;
obj = obj.offsetParent;
}
// Return relative mouse position
let mouseX = evt.clientX - left + window.pageXOffset;
let mouseY = evt.clientY - top + window.pageYOffset;
return {
x: mouseX,
y: mouseY
};
}
function updateBalls(canvas, balls, timeDiff, mousePos) {
let context = canvas.getContext('2d');
let collisionDamper = 0.3;
let floorFriction = 0.0005 * timeDiff;
let mouseForceMultiplier = 1 * timeDiff;
let restoreForce = 0.002 * timeDiff;
for (let n = 0; n < balls.length; n++) {
var ball = balls[n];
// Set ball position based on velocity
ball.y += ball.vy;
ball.x += ball.vx;
// Restore forces
if (ball.x > ball.origX) {
ball.vx -= restoreForce;
} else {
ball.vx += restoreForce;
}
if (ball.y > ball.origY) {
ball.vy -= restoreForce;
} else {
ball.vy += restoreForce;
}
// Mouse forces
let mouseX = mousePos.x;
let mouseY = mousePos.y;
let distX = ball.x - mouseX;
let distY = ball.y - mouseY;
let radius = Math.sqrt(Math.pow(distX, 2) + Math.pow(distY, 2));
let totalDist = Math.abs(distX) + Math.abs(distY);
let forceX =
(Math.abs(distX) / totalDist) * (1 / radius) * mouseForceMultiplier;
let forceY =
(Math.abs(distY) / totalDist) * (1 / radius) * mouseForceMultiplier;
if (distX > 0) {
// Mouse is left of ball
ball.vx += forceX;
} else {
ball.vx -= forceX;
}
if (distY > 0) {
// Mouse is on top of ball
ball.vy += forceY;
} else {
ball.vy -= forceY;
}
// Floor friction
if (ball.vx > 0) {
ball.vx -= floorFriction;
} else if (ball.vx < 0) {
ball.vx += floorFriction;
}
if (ball.vy > 0) {
ball.vy -= floorFriction;
} else if (ball.vy < 0) {
ball.vy += floorFriction;
}
// Floor condition
if (ball.y > canvas.height - ball.radius) {
ball.y = canvas.height - ball.radius - 2;
ball.vy *= -1;
ball.vy *= 1 - collisionDamper;
}
// Ceiling condition
if (ball.y < ball.radius) {
ball.y = ball.radius + 2;
ball.vy *= -1;
ball.vy *= 1 - collisionDamper;
}
// Right wall condition
if (ball.x > canvas.width - ball.radius) {
ball.x = canvas.width - ball.radius - 2;
ball.vx *= -1;
ball.vx *= 1 - collisionDamper;
}
// Left wall condition
if (ball.x < ball.radius) {
ball.x = ball.radius + 2;
ball.vx *= -1;
ball.vx *= 1 - collisionDamper;
}
}
}
function Ball(x, y, vx, vy, color) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.color = color;
this.origX = x;
this.origY = y;
this.radius = 10;
}
function animate(canvas, balls, lastTime, mousePos) {
let context = canvas.getContext('2d');
// Update
let date = new Date();
let time = date.getTime();
let timeDiff = time - lastTime;
updateBalls(canvas, balls, timeDiff, mousePos);
lastTime = time;
// Clear
context.clearRect(0, 0, canvas.width, canvas.height);
// Render
for (let n = 0; n < balls.length; n++) {
let ball = balls[n];
context.beginPath();
context.arc(ball.x, ball.y, ball.radius, 0, 2 * Math.PI, false);
context.fillStyle = ball.color;
context.fill();
}
// Request new frame
requestAnimFrame(() => {
animate(canvas, balls, lastTime, mousePos);
});
}
let canvas = document.getElementById('myCanvas');
var balls = initBalls();
let date = new Date();
let time = date.getTime();
// Set mouse position really far away so the mouse forces are nearly obsolete
var mousePos = {
x: 9999,
y: 9999
};
canvas.addEventListener('mousemove', (evt) => {
let pos = getMousePos(canvas, evt);
mousePos.x = pos.x;
mousePos.y = pos.y;
});
canvas.addEventListener("mouseout", (evt) => {
mousePos.x = 9999;
mousePos.y = 9999;
});
animate(canvas, balls, time, mousePos);
Also see: Tab Triggers