cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation


Save Automatically?

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.

    <script src="http://wellcaffeinated.net/PhysicsJS/examples/physicsjs-full.js"></script>
    <canvas id="viewport"></canvas>

              html, body
  background: #eee
  border: 1px solid black
  width: 250px
  height: 250px
  display: block
  margin: 50px auto
              Physics (world) ->
  # create a canvas renderer
  renderer = Physics.renderer 'canvas',
    el: 'viewport'
    width: 250
    height: 250
    #debug: true
  # load the ChallengePost Logo
  cpLogo = new Image()
  cpLogo.src = 'http://i.pb.lc/image/0T2I3x1L1u0w/cplogo.png'
  # num bodies to create
  nbLogos = 5
  rect = [
    { x: 0, y: 18 }
    { x: 18, y: 0 }
    { x: -0, y: -18 }
    { x: -18, y: -0 }
  # create bodies stored in array
  logos = for i in [1..nbLogos]
    Physics.body 'convex-polygon',
      vertices: rect
      restitution: 0.95
      mass: 0.5
      x: Math.random() * 220
      y: Math.random() * 100 + 50
      vx: i * .01
      vy: (nbLogos - i + 1) * 0.01
      mass: i * .25
      view: cpLogo
  # add the bodies to the world  
  world.add logos
  # render the world on every step
  world.subscribe 'step', ->
  bounds = Physics.aabb(0, 0, 250, 250)
  world.add [
    Physics.behavior 'constant-acceleration'
    Physics.behavior 'edge-collision-detection',
      aabb: bounds
      restitution: 1.05
    Physics.behavior 'body-impulse-response'
    Physics.behavior 'sweep-prune'
    Physics.behavior 'body-collision-detection'
  # subscribe to an animation loop and step the world every time
  Physics.util.ticker.subscribe ( time, dt ) ->
  # start the animation loop

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.


Loading ..................