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 URL's 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 it's URL and the proper URL extention.
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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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.
# Original code: https://github.com/baxter/csterrain/blob/master/src/generate_terrain.coffee
#
#
# Generate realistic looking terrain using the [diamond square algorithm](http://en.wikipedia.org/wiki/Diamond-square_algorithm).
#
#### Generating a height map
# The height map is basically an array of numbers, each element represents
# a point on a map and each number represents the height of that grid.
class @HeightMap
# Initialise the height map with a `size` whose upper bound is inclusive (32 means an array of 33 items).
# Call the `reset` function to put the height_map into a state that is ready
# for terrain generation.
constructor: (size, options = {}) ->
@low_value = options.low_value ? 0
@high_value = options.high_value ? 255
@variability = options.variability ? 0.75
@mid_value = Math.floor((@low_value + @high_value) / 2)
@size = { width: size.width, height: size.height }
@reset()
@at = @get_cell
# Pop any remaining objects from the operation queue and discard them.
# Create an empty 2D array, size × size,
# Set all the corner points to the mid value.
# Push a new object to the queue for the first diamond square step.
reset: =>
@map = for y in [0..@size.height]
null for x in [0...@size.width]
@set_cell 0, 0, @mid_value
@set_cell 0, @size.height, @mid_value
# Get the value of the point at [x, y].
get_cell: (x, y) ->
x = 0 if x is @size.width
@map[y][x]
# Set the value of the point at [x, y] to be v.
set_cell: (x, y, v) ->
x = 0 if x is @size.width
@map[y][x] = v
# Set the value of the cell at [x, y] to be v, but only if it isn't already set.
# This is useful when we recurse, since some adjacent points might already
# have a value set and I'd rather preserve them.
soft_set_cell: (x, y, v) ->
x = 0 if x is @size.width
@map[y][x] ||= v
# Keep calling step() until there's nothing left in the queue.
run: ->
@diamond_square 0, 0, @size.width, @size.height, @mid_value
# The diamond square algorithm works on a particular region in 2 steps:
diamond_square: (left, top, right, bottom, base_height) ->
x_centre = Math.floor (left + right) / 2
y_centre = Math.floor (top + bottom) / 2
# * The **diamond** step populates the centre point by averaging the
# values at the four corners and adding or subtracting a random amount
# of noise.
centre_point_value = Math.floor (
(
@get_cell(left, top) +
@get_cell(right, top) +
@get_cell(left, bottom) +
@get_cell(right, bottom)
) / 4
) - (Math.floor (Math.random() - 0.5) * base_height * 2)
@soft_set_cell(x_centre, y_centre, centre_point_value)
# * The **square** step populates the North, South, East and West points
# by averaging the North West and North East values, the South East and
# South East values, etc.
@soft_set_cell(x_centre, top, Math.floor (@get_cell(left, top) + @get_cell(right, top) ) / 2 + ((Math.random() - 0.5) * base_height))
@soft_set_cell(x_centre, bottom, Math.floor (@get_cell(left, bottom) + @get_cell(right, bottom)) / 2 + ((Math.random() - 0.5) * base_height))
@soft_set_cell(left, y_centre, Math.floor (@get_cell(left, top) + @get_cell(left, bottom)) / 2 + ((Math.random() - 0.5) * base_height))
@soft_set_cell(right, y_centre, Math.floor (@get_cell(right, top) + @get_cell(right, bottom)) / 2 + ((Math.random() - 0.5) * base_height))
# Once the centre point and the four side points are populated then,
# provided there are no smaller regions left, split the current region
# into four smaller regions and perform the diamond square algorithm
# on them.
if (right - left) > 2
base_height = Math.floor base_height * Math.pow 2.0, -@variability
@diamond_square left, top, x_centre, y_centre, base_height
@diamond_square x_centre, top, right, y_centre, base_height
@diamond_square left, y_centre, x_centre, bottom, base_height
@diamond_square x_centre, y_centre, right, bottom, base_height
dsquare = new HeightMap(width: 64, height: 64)
dsquare.run()
# return an height value
generateHeight = (x, y) ->
dsquare.get_cell Math.round(x / 2), Math.round(y / 2)
# builds a tile map with the given width and height sizes
buildTileMap = (width, height) ->
tileMap = new Array(width)
for y in [0...width]
tileMap[y] = new Array(height)
for x in [0...height]
tileMap[y][x] = generateHeight(x, y)
tileMap
drawTile = (ctx, point, size) ->
ctx.beginPath()
[x1, y1] = [point.x * size.w, point.y * size.h]
[x2, y2] = [x1 + size.w, y1 + size.h]
ctx.moveTo x1, y1
ctx.lineTo x2, y1
ctx.lineTo x2, y2
ctx.lineTo x1, y2
ctx.lineTo x1, y1
ctx.closePath()
$ ->
scene = document.createElement 'canvas'
scene.width = scene.height = 512
# build a tile map.
# Tilemap size is the half of the canvas size
map = buildTileMap 128, 128
ctx = scene.getContext '2d'
tileSize =
w: scene.width / map.length
h: scene.height / map[0].length
# draw a 2px circle for each tilemap location
for y in [0...map.length]
for height, x in map[y]
ctx.fillStyle = ctx.strokeStyle = "rgb(#{height},#{height},#{height})"
drawTile(ctx, { x, y }, tileSize)
ctx.stroke()
ctx.fill()
document.body.appendChild scene
Also see: Tab Triggers