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="c"></canvas>
@import "compass/css3";
body {
background: #efefef;
overflow: hidden;
}
#c {
width: 100%;
height: 1080px;
}
#-------------------------------------------------------
# CONFIGURATION - Feel free to mess with these values
#-------------------------------------------------------
config =
#RENDERING SETTINGS
triangles: 30 # number of triangles to render
speed: 2 # speed of animation ... approximately pixels per update
#TRIANGLE COLORS
redMin: 100 # minimum red value (0 - 255)
redMax: 140 # maximum red value
greenMin: 160 # minimum green value
greenMax: 200 # maximum green value
blueMin: 130 # minimum blue value
blueMax: 170 # maximum blue value
opacityMin: .05 # minimum opacity (0 - 1)
opacityMax: .2 # maximum opacity
# Clamps a numeric value between a minimum and maximum (inclusively)
clamp = (value, min, max) -> Math.min(Math.max(value, min), max)
# Gets a random number between min (inclusive) and max (exclusive)
rand = (min = 0, max = 1) -> Math.random() * (max - min) + min
# requestAnimationFrame compatibility/fallback wrapper
# a la Paul Irish: http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/
window.requestAnimFrame =
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
(callback, element) -> window.setTimeout callback, 1000 / 60
#-------------------------------------------------------
# VECTOR - A point on the canvas or a bearing
#-------------------------------------------------------
class Vector
x: 0
y: 0
constructor: (@x = 0, @y = 0) ->
# Generates a random vector within the bounds
@random: (bounds) =>
x = rand bounds.minX(), bounds.maxX()
y = rand bounds.minY(), bounds.maxY()
new @ x, y
# Detects whether or not this vertex is out of the bounds
outOfBounds: (bounds) ->
bounds.minX() >= @x ||
bounds.maxX() <= @x ||
bounds.minY() >= @y ||
bounds.maxY() <= @y
# Clamps the vector to a bounds
clamp: (bounds) ->
@x = clamp @x, bounds.minX(), bounds.maxX()
@y = clamp @y, bounds.minY(), bounds.maxY()
@
# Adds another vector to this one
addVector: (vector) ->
@x += vector.x
@y += vector.y
@
# performs a scalar product against the vector
scalarProduct: (scalar) ->
@x *= scalar
@y *= scalar
@
# clamps and reflects a vector if it's out of bounds
reflectAndClamp: (bounds, bearing) ->
if not @outOfBounds bounds then return @
if @x <= bounds.minX() or @x >= bounds.maxX() then bearing.x *= -1
if @y <= bounds.minY() or @y >= bounds.maxY() then bearing.y *= -1
@clamp(bounds)
@
#-------------------------------------------------------
# BOUND - A rectangular boundary for drawing
#-------------------------------------------------------
class Bounds
topLeft: undefined
bottomRight: undefined
constructor: (@topLeft = new Vector, @bottomRight = new Vector) ->
window.bounds = @
minX: -> @topLeft.x
maxX: -> @bottomRight.x
minY: -> @topLeft.y
maxY: -> @bottomRight.y
#-------------------------------------------------------
# TRIANGLE - Polygon to be drawn onto the canvas
#-------------------------------------------------------
class Triangle
red: undefined
green: undefined
blue: undefined
opacity: undefined
bounds: undefined
vertices: undefined
bearings: undefined
bearingBounds: new Bounds(new Vector(-1, -1), new Vector(1, 1))
# creates a random triangle within the bounds
constructor: (@bounds = new Bounds) ->
@red = Math.floor rand config.redMin, config.redMax
@green = Math.floor rand config.greenMin, config.greenMax
@blue = Math.floor rand config.blueMin, config.blueMax
@opacity = rand config.opacityMin, config.opacityMax
@vertices = []
@bearings = []
for [0...3]
@vertices.push Vector.random(@bounds)
@bearings.push Vector.random(@bearingBounds).scalarProduct config.speed
# updates the position of each of the triangle vertices by its bearing
update: ->
for i in [0...3]
vertex = @vertices[i]
bearing = @bearings[i]
vertex.addVector bearing
vertex.reflectAndClamp @bounds, bearing
# renders the triangle in the canvas context
# includes an optional partialStep for extrapolating lag position
render: (context, partialStep = 0) ->
points = []
for i in [0...3]
bearing = @bearings[i]
vertex = @vertices[i]
points.push
x: parseInt(vertex.x + partialStep * bearing.x)
y: parseInt(vertex.y + partialStep * bearing.y)
context.fillStyle = "rgba(#{@red},#{@green},#{@blue},#{@opacity})"
context.beginPath()
context.moveTo points[0].x, points[0].y
context.lineTo(points[i].x, points[i].y) for i in [2..0]
context.closePath()
context.fill()
@
#-------------------------------------------------------
# CANVAS - The animation logic and wrapper for the canvas
#-------------------------------------------------------
class Canvas
updateStep: 1000/60
el: undefined
context: undefined
triangles: undefined
previous: undefined
lag: undefined
constructor: (@el) ->
@context = @el.getContext('2d')
@triangles = []
@previous = new Date().getTime()
@lag = 0.0
# add a Triangle to the stack to be animated
addTriangle: (triangle) -> @triangles.push triangle
# updates all the triangles on the stack
update: ->
triangle.update() for triangle in @triangles
return
# renders all the triangles on the stack
render: (partial) ->
@context.clearRect 0, 0, @el.width, @el.height
triangle.render(@context, partial) for triangle in @triangles
return
# run at each animation frame
# inspired by Bob Nystrom: http://gameprogrammingpatterns.com/
loop: ->
current = new Date().getTime()
@lag += current - @previous
@previous = current
while @lag >= @updateStep
@update()
@lag -= @updateStep
@render @lag / @updateStep
# start the animation loop using requestAnimationFrame wrapper
start: ->
window.requestAnimFrame => @start()
@loop()
#-------------------------------------------------------
# THE MAGIC
#-------------------------------------------------------
# grab the canvas element
el = document.getElementById 'c'
$el = $(el)
# gimme that full screen
el.width = $el.innerWidth()
el.height = $el.innerHeight()
# get the vectors to define the bounds
# giving a bit of wiggle room for overflow (makes for a prettier pattern)
topLeft = new Vector()
topLeft.x -= el.width * .25
topLeft.y -= el.height * .25
bottomRight = new Vector el.width, el.height
bottomRight.x += el.width * .25
bottomRight.y += el.height * .25
# make the bounds for rendering the triangles
bounds = new Bounds topLeft, bottomRight
# create the canvas object with the canvas element
canvas = new Canvas el
# add in the triangles
canvas.addTriangle new Triangle bounds for [0...config.triangles]
# profit!
canvas.start()
Also see: Tab Triggers