Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

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.

+ add another resource

Packages

Add Packages

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.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <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>

              
            
!

CSS

              
                #container {
  border: 1px solid #000;
  width: 600px;
  height: 350px;
}

label, input[type='checkbox'] {
    cursor: pointer;
}

              
            
!

JS

              
                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

              
            
!
999px

Console