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

              
                <div id="app">
  <div id="toolbar">
    <div class="tool selected" data-tool-id="pen">Pen</div>
    <div class="tool" data-tool-id="brush">Brush</div>
    <div class="tool" data-tool-id="eraser">Eraser</div>
    <div class="debug">Dump to console</div>
  </div>
  
  <canvas id="appCanvas" width="500" height="300"></canvas>

</div>
              
            
!

CSS

              
                #app
  width: 500px
  
  border: 1px solid #000
  
  #toolbar
    background: #333
    
    .tool
      display: inline-block
      padding: 5px
      cursor: pointer
      color: #fff
      font-size: 22px
      
      &.selected
        font-weight: bold
        
    .debug
      float: right
      right: 0px
      color: #f00
      padding: 5px
      cursor: pointer
      font-size: 20px
        

              
            
!

JS

              
                class Pen
  drawing: false
  color: '#000'
  strokeWidth: 2
  startX: null
  startY: null
  
  mousedown: =>
    console.log 'mousedown', @constructor.name
    @drawing = true

  mousemove: (evt) =>
    if @startX and @drawing is true
      console.log 'sup', @constructor.name
      @shape.graphics.beginStroke(@color)
        .setStrokeStyle(@strokeWidth, 'round')
        .moveTo(@startX, @startY)
        .lineTo(evt.stageX, evt.stageY)

      @app.stage.update()

    @startX = evt.stageX
    @startY = evt.stageY

  mouseup: =>
    console.log 'mouseup', @constructor.name
    @drawing = false
  
  constructor: (@app = {}) ->
    @events =
      mousemove: null
      mousedown: null
      mouseup: null
      
    @shape = new createjs.Shape()
    @app.stage.addChild @shape
    
  init: ->
    console.log "Activating #{@constructor.name}"
    @app.stage.mouseMoveOutside = true
    
    for stageEvent of @events
      console.log 'stageEvent', stageEvent
      @events[stageEvent] = @app.stage.on("stage#{stageEvent}", this[stageEvent], this)
    
    @app.stage.update()

  stop: ->
    console.log 'stop', @constructor.name
    for stageEvent, listener of @events
      console.log 'nuking', stageEvent
      @app.stage.off "stage#{stageEvent}", listener
      @events[stageEvent] = null
    
    @app.stage.update()

class Brush extends Pen
  strokeWidth: 10
  
class Eraser extends Pen
  strokeWidth: 10
  color: '#fff'

  
class Rectangle extends Pen

class App
  selected_tool: null

  constructor: (@stage) ->
    @tools =
        pen: new Pen(this)
        brush: new Brush(this)
        rect: new Rectangle(this)
        eraser: new Eraser(this)
        
    @activate 'pen'
    @stage.update()

    console.log 'app initialized'

  activate: (tool) ->
    @deactivate @selected_tool
    @selected_tool = tool
    @tools[tool].init()

  deactivate: (tool) ->
    @tools[tool]?.stop?()
    
  dump: =>
    console.log @stage.toDataURL()


toolbar = $ '#toolbar'
stage = new createjs.Stage 'appCanvas'
app = new App stage

toolbar.find('.tool').click (e) ->
  toolbar.find('.tool.selected').removeClass 'selected'
  $el = $(e.target).addClass 'selected'
  app.activate $el.data 'toolId'
  
toolbar.find('.debug').click app.dump
              
            
!
999px

Console