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.
<input type="text" name = "title" placeholder="new node" id="node-title" />
<input type="button" value="add node" id="add-node" />
<label for="edge-mode">
<input type="checkbox" id="edge-mode" />edge
</label>
<div id="container"></div>
#container {
border: 1px solid #000;
width: 600px;
height: 350px;
}
label, input[type='checkbox'] {
cursor: pointer;
}
class Node
constructor: (@title, @xpos, @ypos, @width, @height, @id) ->
centerX: -> @xpos + @width/2
centerY: -> @ypos + @height/2
show: -> "title:#{@title} :#{@xpos} y:#{@ypos} width:#{@width} height:#{@height}"
class Edge
constructor: (@from, @to, @id) ->
@startx: 0
@starty: 0
@endx: 0
@endy: 0
show: ->
"from:#{@from}(#{@startx}, #{@starty}) to:#{@to}(#{@endx}, #{@endy})"
@Nodes = []
@Edges = []
class EdgeAdding
@enabled: false
@adding: false
@startx: 0
@starty: 0
@endx: 0
@endy: 0
@from: ''
@to: ''
started: ->
@adding is true
start: ->
@adding = true
end: ->
@adding = false
fromAttr: (edgeId)->
edge = new Edge(@from, @to, edgeId)
edge.startx = @startx
edge.starty = @starty
edge.endx = @endx
edge.endy = @endy
edge
@edgeAdding = new EdgeAdding()
@dragContext =
nodeId:0
node: null
froms: []
tos: []
class Sequence
constructor: (@nodeSeq, @edgeSeq) ->
getNodeSeq: ->
@nodeSeq++
getEdgeSeq: ->
@edgeSeq++
@sequence = new Sequence(0, 0)
class Graph
addNode: (title) ->
id = sequence.getNodeSeq()
node = new Node title, konvaStage.randomX(), konvaStage.randomY(), 0, 0, id
shape = konvaFactory.createShape(node, id)
node.width = shape.width()
node.height = shape.height()
console.log node.show()
konvaStage.registerShape shape
Nodes.push node
id
addEdge: ->
edge = edgeAdding.fromAttr(sequence.getEdgeSeq())
this.addEdgeInternal edge
addEdgeByIds: (fromId, toId) ->
edgeId = sequence.getEdgeSeq()
edge = new Edge(fromId, toId, edgeId)
fromNode = _.find Nodes, (node) -> node.id == fromId
toNode = _.find Nodes, (node) -> node.id == toId
edge.startx = fromNode.centerX()
edge.starty = fromNode.centerY()
edge.endx = toNode.centerX()
edge.endy = toNode.centerY()
this.addEdgeInternal edge
addEdgeInternal: (newEdge) ->
edge = _.find Edges, (e) ->
e.from is newEdge.from && e.to is newEdge.to || e.from is newEdge.to && e.to is newEdge.from
if !edge?
Edges.push newEdge
console.log newEdge.show()
konvaStage.registerLine konvaFactory.createLine newEdge
else
console.log "edge already exists.."
moveNode: ->
id = dragContext.nodeId
x = dragContext.node.xpos
y = dragContext.node.ypos
node = _.find Nodes, (node) -> node.id == id
node.xpos = x
node.ypos = y
moveEdges: ->
x = dragContext.node.centerX()
y = dragContext.node.centerY()
_.each dragContext.froms, (edge) ->
edge.startx = x
edge.starty = y
_.each dragContext.tos, (edge) ->
edge.endx = x
edge.endy = y
getNode: (id) ->
_.find Nodes, (node) -> node.id == id
findEdgesFrom: (nodeId) ->
_.filter Edges, (edge) -> edge.from == nodeId
findEdgesTo: (nodeId) ->
_.filter Edges, (edge) -> edge.to == nodeId
@graph = new Graph
class KonvaStage
constructor: ->
@stage = new Konva.Stage({
container : container
width : 600
height : 350
})
@layer = new Konva.Layer()
.on 'mouseup tap', (event) ->
layerEdgeAction(event)
@stage.add @layer
randomX: ->
parseInt Math.random()*(@stage.getWidth() / 2)
randomY: ->
parseInt Math.random()*(@stage.getHeight() / 2)
registerShape: (label) ->
@layer.add label
applyTweenTo label.getTag()
applyTweenTo label.getText()
@layer.draw()
registerLine: (line) ->
@layer.add line
line.moveToBottom()
@layer.draw()
applyTweenTo = (node) ->
node.tween = new Konva.Tween({
node: node
scaleX: 1.2
scaleY: 1.2
easing: Konva.Easings.EaseInOut
duration: 0.5
})
dragEdges: ->
l = @layer
n = dragContext.node
_.each dragContext.froms, (from) ->
edgeId = from.id
line = l.find("##{edgeId}")[0]
if line
points = line.attrs.points
line.points [n.centerX(), n.centerY(), points[2], points[3]]
l.draw()
_.each dragContext.tos, (to) ->
edgeId = to.id
line = l.find("##{edgeId}")[0]
if line
points = line.attrs.points
line.points [points[0], points[1], n.centerX(), n.centerY()]
l.draw()
layerEdgeAction = (event) ->
if !edgeAdding.enabled
return
if !edgeAdding.started()
edgeAdding.start()
else
edgeAdding.end()
if edgeAdding.from is edgeAdding.to
return
graph.addEdge()
edgeAdding.enabled = false
$("#edge-mode").prop("checked", false)
@konvaStage = new KonvaStage
class KonvaFactory
createShape: (graphNode, id) ->
new Konva.Label({
x: graphNode.xpos
y: graphNode.ypos
width: 100
height: 50
draggable: true
id: id
name: graphNode.title
})
.on 'dragstart', () ->
dragStartAction(@)
.on 'dragmove', () ->
dragMoveAction(@)
.on 'dragend', () ->
dragEndAction(@)
.on 'mouseover touchstart', () ->
document.body.style.cursor = 'pointer'
if edgeAdding.enabled
@getTag().tween.play()
@getText().tween.play()
.on 'mouseout touchend', () ->
document.body.style.cursor = 'default'
if edgeAdding.enabled
@getTag().tween.reverse()
@getText().tween.reverse()
.on 'mouseup tap', (event) ->
labelEdgeAction(event, @)
.add new Konva.Tag({
fill: ((length) ->
n = parseInt Math.random() * Math.floor(5)
if n == 0
'#CEF6CE' # 緑
else if n == 1
'#F5A9A9' # ピンク
else if n == 2
'#F6CEF5' # 紫
else if n == 3
'#F2F5A9' # 黄
else
'#FFFFFF' # 白
)(graphNode.title.length)
stroke: 'black'
strokeWidth: 4
})
.add new Konva.Text({
text: graphNode.title
fontSize: 14
padding: 8
fill: 'black'
})
createLine: (edge) ->
line = new Konva.Line({
points: [edge.startx, edge.starty, edge.endx, edge.endy]
stroke: 'black'
strokeWidth: 4
id: edge.id
name: 'test'
})
labelEdgeAction = (event, shape) ->
if edgeAdding.enabled
if !edgeAdding.started()
edgeAdding.from = shape.getId()
edgeAdding.startx = shape.x() + shape.width()/2
edgeAdding.starty = shape.y() + shape.height()/2
else if edgeAdding.started()
edgeAdding.to = shape.getId()
edgeAdding.endx = shape.x() + shape.width()/2
edgeAdding.endy = shape.y() + shape.height()/2
dragStartAction = (shape) ->
dragContext.nodeId = shape.getId()
node = graph.getNode shape.getId()
console.log "drag began. - #{node.title}"
if node
node.xpos = shape.x()
node.ypos = shape.y()
dragContext.node = node
dragContext.froms = graph.findEdgesFrom shape.getId()
dragContext.tos = graph.findEdgesTo shape.getId()
dragMoveAction = (shape) ->
dragContext.node.xpos = shape.x()
dragContext.node.ypos = shape.y()
konvaStage.dragEdges()
dragEndAction = (shape) ->
console.log 'drag End.'
#console.log dragContext
dragContext.node.xpos = shape.x()
dragContext.node.ypos = shape.y()
graph.moveNode()
konvaStage.dragEdges()
graph.moveEdges()
@konvaFactory = new KonvaFactory
append = () ->
title = $('#node-title').val()
if title != ''
graph.addNode title
$('#node-title').val ''
$('#add-node').click -> append()
$('#node-title').keypress (key)->
if key.charCode == 13
append()
$("#edge-mode").click ->
edgeAdding.enabled = $("#edge-mode").prop("checked")
console.log "edge mode:" + $("#edge-mode").prop("checked")
funa = graph.addNode 'ふなっしー'
hyaha = graph.addNode 'ヒャッハー ヒャッハー'
shiru = graph.addNode '梨汁プシャー'
graph.addEdgeByIds funa, hyaha
graph.addEdgeByIds funa, shiru
Also see: Tab Triggers