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 {
overflow: hidden;
}
body {
margin: 0px;
background: black;
}
// Thanks to http://archive.ncsa.illinois.edu/Classes/MATH198/townsend/math.html
// for an introduction the maths behind this simulation
//////////////////////////////////////////////////////////////////////
// Framework
var nextEvent
var automata
function determine() {
nextEvent = undefined
automata.forEach(a => {
let event = a.determine()
if (event && (!nextEvent || event.dt < nextEvent.dt)) {
nextEvent = event
}
})
}
function calcAutomata() {
let t = spf
while (!nextEvent || nextEvent.dt <= t) {
automata.forEach(a => {
a.advance(nextEvent.dt)
})
t -= nextEvent.dt
nextEvent.notify()
determine()
}
automata.forEach(a => {
a.advance(t)
})
nextEvent.dt -= t
}
function drawAutomata() {
automata.forEach(a => {
point(a.pos.x, a.pos.y)
})
}
//////////////////////////////////////////////////////////////////////
// Simulation
const epsilon = 1E-13
const radius = 30
const fps = 60
const spf = 1 / fps
class CollisionVEdge {
constructor(dt, ball) {
this.dt = dt
this.ball = ball
}
notify() {
this.ball.handleCollisionVEdge()
}
}
class CollisionHEdge {
constructor(dt, ball) {
this.dt = dt
this.ball = ball
}
notify() {
this.ball.handleCollisionHEdge()
}
}
class CollisionBalls {
constructor(dt, ball1, ball2) {
this.dt = dt
this.ball1 = ball1
this.ball2 = ball2
}
notify() {
let copy = this.ball1.copy()
this.ball1.handleCollisionBall(this.ball2)
this.ball2.handleCollisionBall(copy)
}
}
class Ball {
constructor(pos, vel) {
this.pos = pos
this.vel = vel
}
copy() {
return new Ball(this.pos.copy(), this.vel.copy())
}
determine() {
let nextCollision
[
[this.pos.x, this.vel.x, CollisionVEdge],
[width - this.pos.x, -this.vel.x, CollisionVEdge],
[this.pos.y, this.vel.y, CollisionHEdge],
[height - this.pos.y, -this.vel.y, CollisionHEdge]
].forEach(([d, v, C]) => {
if (v < 0) {
// Object is heading towards this edge
if (d <= radius) {
// Object has already hit this edge
nextCollision = new C(0, this)
} else {
let dt = (radius - d) / v
if (dt < epsilon) dt = 0
if (!nextCollision || dt < nextCollision.dt) {
nextCollision = new C(dt, this)
}
}
}
})
let foundThis = false
automata.forEach(b2 => {
if (!foundThis) {
if (b2 === this) {
foundThis = true
}
return
}
let rp = this.pos.copy().sub(b2.pos)
let rv = this.vel.copy().sub(b2.vel)
let a = rv.dot(rv)
let b = 2 * rp.dot(rv)
let c = rp.dot(rp)
if (b < 0) {
// Objects are heading towards each other
if (c <= 4 * radius * radius) {
// Objects have already collided
nextCollision = new CollisionBalls(0, this, b2)
} else {
let dt = (-b - Math.sqrt(b * b - 4 * a * (c - 4 * radius * radius))) / (2 * a)
if (dt < epsilon) dt = 0
if (!nextCollision || dt < nextCollision.dt) {
nextCollision = new CollisionBalls(dt, this, b2)
}
}
}
})
return nextCollision
}
advance(dt) {
let dp = this.vel.copy().mult(dt)
this.pos.add(dp)
}
handleCollisionVEdge() {
this.vel.x = -this.vel.x
}
handleCollisionHEdge() {
this.vel.y = -this.vel.y
}
handleCollisionBall(ball2) {
let n = this.pos.copy().sub(ball2.pos).normalize()
let v2 = ball2.vel
let v2n = n.copy().mult(v2.dot(n))
let nn = n.copy().mult(-1)
let v1n = nn.mult(this.vel.dot(nn))
this.vel.sub(v1n).add(v2n)
}
}
function initNBalls(n) {
automata = [];
for (let i = 0; i < n; i++) {
automata.push(new Ball(
createVector(width * 0.5, height * 0.5),
createVector((Math.random() - 0.5) * 1200, (Math.random() - 0.5) * 1200)
))
}
}
function init3Balls() {
automata = [];
automata.push(new Ball(
createVector(width * 0.5 - 300, height * 0.5),
createVector(300, 0)
))
automata.push(new Ball(
createVector(width * 0.5 + 300, height * 0.5),
createVector(-300, 0)
))
automata.push(new Ball(
createVector(width * 0.5, height * 0.5),
createVector(0, 0)
))
}
function init4Balls() {
automata = [];
automata.push(new Ball(
createVector(width * 0.5 - 300, height * 0.5),
createVector(300, 0)
))
automata.push(new Ball(
createVector(width * 0.5 + 300, height * 0.5),
createVector(-300, 0)
))
automata.push(new Ball(
createVector(width * 0.5, height * 0.5 - 300),
createVector(0, 300)
))
automata.push(new Ball(
createVector(width * 0.5, height * 0.5 + 300),
createVector(0, -300)
))
}
function initNewton(n) {
automata = [];
for (let i = 0; i < 6; i++) {
automata.push(new Ball(
createVector((i - 2.5) * 2 * radius + width * 0.5, height * 0.5),
createVector(0, 0)
))
}
for (let i = 0; i < n; i++) {
automata[i].pos.x -= 100
automata[i].vel.x = 1200
}
}
function setup() {
createCanvas(windowWidth, windowHeight)
background(0)
stroke(255,50,0)
strokeWeight(2 * radius)
noFill()
frameRate(fps)
initNBalls(10)
//init3Balls()
//init4Balls()
//initNewton(1)
//automata[2].pos.y += 1E-13
determine()
//noLoop()
}
function draw() {
calcAutomata()
background(0)
drawAutomata()
}
Also see: Tab Triggers