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.
<svg id='board'>
<g id='lines'></g>
<g id='circles'></g>
</svg>
<div id='buttons'>
<button id='regenerate'>Regenerate Graph</button>
<button id='bfs'>Start BFS</button>
</div>
body {
padding: 0;
margin: 0;
border: none;
background-color: black;
}
circle {
fill: white;
cursor: pointer;
}
circle:hover {
fill:pink;
}
#buttons {
opacity: 0;
position:absolute;
left: 10px;
bottom: 10px;
}
# VIEW SETUP STUFF
OUTER_MARGIN = 10.0
SVG_ELEMENT = d3.select '#board'
SVG_CIRCLES = SVG_ELEMENT.select('#circles')
SVG_LINES = SVG_ELEMENT.select('#lines')
windowWidth = window.innerWidth
windowHeight = window.innerHeight
svgWidth = windowWidth - 2 * OUTER_MARGIN
svgHeight = windowHeight - 2 * OUTER_MARGIN
px = (x) -> String(x) + 'px'
SVG_ELEMENT.attr 'width', px svgWidth
.attr 'height', px svgHeight
.style 'margin-left', px OUTER_MARGIN
.style 'margin-top', px OUTER_MARGIN
# GOODIES
class Queue
constructor: () ->
@arr = []
@p1 = 0
@p2 = 0
return
push: (a) ->
@p2 += 1
@arr.push(a)
return
squish: () ->
@arr.splice(0, @p1)
@p2 = @p2 - @p1
@p1 = 0
return
front: () -> @arr[@p1]
pop: () ->
if @isEmpty()
return
x = @arr[@p1]
@arr[@p1] =
@p1 += 1
if @isEmpty()
@arr = []
@p1 = 0
@p2 = 0
else if (@p2 - @p1) < @p2 / 2
@squish()
x
isEmpty: () -> @p1 == @p2
diag = (w, h) -> Math.sqrt(w * w + h * h)
ranbt = (a, b) -> a + Math.random() * (b - a)
ranIntbt = (a, b) -> parseInt(ranbt a, b)
colorOpen = (color, factor) ->
rrr = 255 * factor
res = 'black'
if color == 'red'
res = "rgb(255, #{rrr}, #{rrr})"
else if color == 'green'
res = "rgb(#{rrr}, 255, #{rrr})"
else if color == 'blue' || color == 'green'
res = "rgb(#{rrr}, #{rrr}, 255)"
res
class Point
constructor: (@x, @y) ->
return
distanceTo: (a) -> Math.sqrt((@x - a.x) * (@x - a.x) + (@y - a.y) * (@y - a.y))
randomPointInAnnulus: (r1, r2) ->
r = ranbt(r1, r2)
a = ranbt(0, 2 * Math.PI)
new Point(@x + r * Math.cos(a), @y + r * Math.sin(a))
randomPoint = (x1, y1, x2, y2) -> new Point(ranbt(x1, x2), ranbt(y1, y2))
# LOGIC STUFF
class PoissonDisk
constructor: (@boardWidth,
@boardHeight,
@minDistance,
@candidatesSize,
@newPointCallback,
@newLineCallback,
@doneCallback) ->
@cellSize = @minDistance / Math.SQRT2
@cellsWide = Math.ceil @boardWidth / @cellSize
@cellsHigh = Math.ceil @boardHeight / @cellSize
@allPoints = []
@grid = []
for i in [0..@cellsHigh - 1]
tarr = []
for j in [0..@cellsWide - 1]
tarr.push -1
@grid.push tarr
return
sample: () ->
fp = new Point(ranbt(50, @boardWidth - 50), ranbt(50, @boardHeight - 50))
@grid[parseInt(fp.y / @cellSize)][parseInt(fp.x / @cellSize)] = 0
@allPoints.push fp
@newPointCallback fp.x, fp.y, 0
q = new Queue()
q.push(0)
_candidatesSize = @candidatesSize
_newPointCallback = @newPointCallback
_newLineCallback = @newLineCallback
_doneCallback = @doneCallback
_grid = @grid
_cellSize = @cellSize
_cellsWide = @cellsWide
_cellsHigh = @cellsHigh
_allPoints = @allPoints
_minDistance = @minDistance
_boardWidth = @boardWidth
_boardHeight = @boardHeight
testPoint = (point) ->
row = parseInt point.y / _cellSize
col = parseInt point.x / _cellSize
res =
goodClose: []
goodCloseId: []
isBad: false
if point.x < 50 or point.x > _boardWidth - 50 or
point.y < 50 or point.y > _boardHeight - 50
res.isBad = true
return res
for i in [-5..5]
for j in [-5..5]
prow = row + i
pcol = col + j
if prow < 0 or pcol < 0 or prow >= _cellsHigh or pcol >= _cellsWide
continue
if _grid[prow][pcol] > -1
ppid = _grid[prow][pcol]
pp = _allPoints[ppid]
if point.distanceTo(pp) < _minDistance
res.isBad = true
return res
if point.distanceTo(pp) < 2 * _minDistance
res.goodClose.push pp
res.goodCloseId.push ppid
return res
doWork = () ->
found = false
ppid = q.front()
pp = _allPoints[ppid]
for i in [1.._candidatesSize]
gp = pp.randomPointInAnnulus(_minDistance, 2 * _minDistance)
ttt = testPoint(gp)
if !ttt.isBad
row = parseInt gp.y / _cellSize
col = parseInt gp.x / _cellSize
_grid[row][col] = _allPoints.length
gpid = _allPoints.length
q.push gpid
_allPoints.push gp
_newPointCallback gp.x, gp.y, gpid
found = true
for ppwid in ttt.goodCloseId
ppw = _allPoints[ppwid]
_newLineCallback ppw.x, ppw.y, gp.x, gp.y, ppwid, gpid
if found == false
q.pop()
if !q.isEmpty()
setTimeout doWork, 0
else
setTimeout _doneCallback, 0
setTimeout doWork, 0
return
# CALL STUFF
graph = []
myNewPointCallback = (x, y, _id) ->
domref = SVG_CIRCLES.append 'circle'
domref.attr 'cx', x
.attr 'cy', y
.attr 'r', diag(svgWidth, svgHeight) / 180 * 5
.style 'opacity', 0.0
.transition()
.duration 250
.style 'opacity', 1.0
.attr 'r', diag(svgWidth, svgHeight) / 180
node =
ref : domref
id : _id
edges : []
graph.push node
alll = () ->
setTimeout () ->
doBFS(_id)
return
, 0
return
domref.on('click', alll)
return
myNewLineCallback = (x1, y1, x2, y2, id1, id2) ->
edge = SVG_LINES.append 'line'
edge.attr 'x1', x1
.attr 'y1', y1
.attr 'x2', x1
.attr 'y2', y1
.attr 'stroke-width', 1
.attr 'stroke', 'white'
.transition()
.duration 500
.attr 'x2', x2
.attr 'y2', y2
n1ton2 =
ref : edge
end : id2
n2ton1 =
ref : edge
end : id1
graph[id1].edges.push n1ton2
graph[id2].edges.push n2ton1
active = true
myDoneCallback = () ->
active = true
d3.select('#regenerate').attr('disabled', null)
d3.select('#bfs').attr('disabled', null)
doWork = () ->
graph = []
if !active
return
active = false
d3.select('#regenerate').attr('disabled', 'disabled')
d3.select('#bfs').attr('disabled', 'disabled')
SVG_CIRCLES.selectAll('*').remove()
SVG_LINES .selectAll('*').remove()
a = new PoissonDisk svgWidth,
svgHeight,
# minDistance, candidateSize
diag(svgWidth, svgHeight) / 30, 30,
myNewPointCallback,
myNewLineCallback,
myDoneCallback
a.sample()
d3.select('#regenerate').on('click', doWork)
doWork()
cl = 0
cls = ['red', 'blue', 'green']
doBFS = (startNodeId) ->
d3.select('#regenerate').attr('disabled', 'disabled')
d3.select('#bfs').attr('disabled', 'disabled')
cl = (cl + 1) % 3
startNode = graph[startNodeId]
d3.selectAll('line').transition().style('stroke-width', 1)
.style('stroke', 'lightgrey')
visited = []
deep = []
for x in graph
visited.push false
deep.push -1
visited[startNodeId] = true
deep[startNodeId] = 0
q = new Queue()
q.push(startNodeId)
startNode.ref.style('fill', colorOpen(cls[cl], 0))
startNode.ref.attr('r', diag(svgWidth, svgHeight) / 180 * 3)
bfsssDone = () ->
d3.select('#regenerate').attr('disabled', null)
d3.select('#bfs').attr('disabled', null)
return
bfsss = () ->
if q.isEmpty()
setTimeout bfsssDone, 0
return
xId = q.pop()
xNode = graph[xId]
for edge in xNode.edges
yId = edge.end
yNode = graph[yId]
if !visited[yId]
q.push yId
visited[yId] = true
deep[yId] = deep[xId] + 1
aug = Math.max(3 - deep[yId] / 5, 1)
cll = Math.min((3 - aug) / 3, 1)
fill = colorOpen(cls[cl], cll)
console.log fill
yNode.ref.transition().style('fill', fill)
.attr('r', diag(svgWidth, svgHeight) / 180 * aug)
edge.ref.transition().style('stroke-width', aug * diag(svgWidth, svgHeight) / 500)
.style('stroke', fill)
setTimeout bfsss, 0
setTimeout bfsss, 0
return
d3.select('#bfs').on('click', () ->
startNodeId = ranIntbt(0, graph.length - 1)
doBFS(startNodeId)
)
Also see: Tab Triggers