cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

            
              html, body, canvas
  margin: 0
  padding: 0
  overflow: hidden
  position: absolute
  transform-origin: top left
  width: 100%
  height: 100%
  left: 0
  top: 0
  
.debug
  position: fixed
  color: black
  background: white
  top: 0
  left: 0
  padding: 5px
  font: 10px sans-serif
  z-index: 10
  display: none
            
          
!
            
              class SubClass
  
  constructor: ( parent, data ) ->
    @.parent = parent
    if @.parent.root? then @.root = @.parent.root
    else @.root = @.parent
    @.init? data

    
    
class Colors extends SubClass
  
  init: ->
    @.background = 0x8eb4c2
    @.container = 0xfffdf8
    @.balls = [ 0xfa0f1e, 0x28ebb8, 0xffe740, 0x27aff2 ]



class Stage extends SubClass
  
  init: ->
    @.addListeners()
    @.build()
    @.onResize()
    
  addListeners: ->
    window.addEventListener "resize", @.onResize
    @.dpi = window.devicePixelRatio
    
  build: ->
    @.scene = new THREE.Scene()
    @.renderer = new THREE.WebGLRenderer()
    @.scene.fog = new THREE.Fog @.root.colors.background , 0 , 25
    @.renderer.setClearColor @.root.colors.background
    @.renderer.shadowMap.enabled = true
    @.renderer.shadowMap.type = THREE.PCFShadowMap
    document.body.appendChild @.renderer.domElement
    
  onResize: =>
    @.root.width = window.innerWidth * @.dpi
    @.root.height = window.innerHeight * @.dpi
    @.renderer.domElement.style.transform = "scale(#{1/@.dpi})"
    @.renderer.setSize @.root.width, @.root.height
    
  add: ( obj ) =>
    @.scene.add obj
    
  remove: ( obj ) =>
    obj.geometry.dispose()
    obj.material.dispose()
    @.scene.remove obj
    
  loop: =>
    @.renderer.render @.scene, @.parent.camera.main
    
    
    
class Camera extends SubClass
    
    init: ->
      @.build()
      @.addListeners()
      @.onResize()
      
    build: ->
      ratio = @.root.width / @.root.height
      color = 0xFFFFFF
      @.main = new THREE.PerspectiveCamera 35, ratio, 1, 1000
      @.ambient = new THREE.HemisphereLight color, color, 1.1
      @.spot = new THREE.DirectionalLight color , 0.15
      @.spot.castShadow = true
      @.spot.shadow.darkness = 1
      @.spot.shadow.cameraNear = 0
      @.spot.shadow.cameraFar = 1500
      @.spot.shadow.cameraLeft = -900
      @.spot.shadow.cameraRight = 900
      @.spot.shadow.cameraTop = 900
      @.spot.shadow.cameraBottom = -900
      @.spot.shadow.mapWidth = 1024
      @.spot.shadow.mapHeight = 1024
      @.spot.position.x = 9
      @.spot.position.y = 9
      @.spot.position.z = -15
      @.parent.stage.scene.add @.spot
      @.root.stage.scene.add @.ambient
    
    addListeners: ->
      window.addEventListener "resize", @.onResize
      
    onResize: =>
      @.main.aspect = @.root.width / @.root.height
      @.main.updateProjectionMatrix()
      @.main.position.x = @.main.position.y = 0
      @.main.position.z = -3.6
      @.main.lookAt x: 0, y: 0, z: 1
      
    
    
class Container extends SubClass
  
  pieces: [
    "floor", 
    "top", 
    "right", 
    "bottom", 
    "left" 
  ]
  
  init: ->
    @.build()
    @.addListeners()
    @.onResize()
    
  build: ->
    geometry = new THREE.PlaneGeometry 2, 2, 1, 1
    material = new THREE.MeshLambertMaterial
      color: @.root.colors.container
      side: THREE.DoubleSide
    for piece in @.pieces
      @[piece] = new THREE.Mesh geometry, material
      @[piece].castShadow = true
      @[piece].recieveShadow = true
      @[piece].collision = new CANNON.Body
        mass: 0
        position: new CANNON.Vec3( 0, 0, 10 )
        shape: new CANNON.Plane()
      material: @.root.physics.groundMaterial
      @.root.stage.add @[piece]
      @.root.physics.world.addBody @[piece].collision
      
    @.cover = new CANNON.Body
      mass: 0
      position: new CANNON.Vec3( 0, 0, 3 )
      shape: new CANNON.Plane()
      material: @.root.physics.material
    @.root.physics.world.addBody @.cover
      
  addListeners: ->
    window.addEventListener "resize", @.onResize
    
  loop: =>
    vertices = [ "x", "y", "z" ]
    for piece in @.pieces
      for vertex in vertices
        @[piece].collision?.position[vertex] = @[piece].position[vertex]
    
  onResize: =>
    for piece in @.pieces
      @[piece].position.x = @[piece].position.y = 0
      @[piece].position.z = 10
      
      fov = 35 * Math.PI / 180
      height = 2 * Math.tan( fov / 2 ) * 10
      aspect = @.root.width / @.root.height
      width = height * aspect
      
      if piece is "floor"
        @[piece].position.z = 7.5
        @[piece].scale.y = height * 0.666
        @[piece].scale.x = width * 0.666
        
        vector = new CANNON.Vec3( 1, 0, 0 )
        angle = 180 * ( Math.PI / 180 )
        @[piece].collision.quaternion.setFromAxisAngle( vector, angle )
        
      if piece is "left" or piece is "right"
        @[piece].rotation.y = 90 * (Math.PI / 180)
        @[piece].position.y = 0
        @[piece].scale.y = height * 0.666 * 2
        @[piece].scale.x = 10
        
        vector = new CANNON.Vec3( 0, 1, 0 )
        angle = -90 * ( Math.PI / 180 )
        
        if piece is "left"
          @[piece].position.x = width * 0.666 * 0.5
        else 
          angle = -angle
          @[piece].position.x = width * -0.666 * 0.5
        @[piece].collision.quaternion.setFromAxisAngle( vector, angle )
        
      if piece is "top" or piece is "bottom"
        @[piece].rotation.x = 90 * (Math.PI / 180)
        @[piece].position.x = 0
        @[piece].scale.x = width * 0.666 * 2
        @[piece].scale.y = 10
        
        vector = new CANNON.Vec3( 1, 0, 0 )
        angle = -90 * ( Math.PI / 180 )
        
        if piece is "top"
          @[piece].position.y = height * 0.666 * 0.5
          angle = -angle
        else 
          @[piece].position.y = height * -0.666 * 0.5
        @[piece].collision.quaternion.setFromAxisAngle( vector, angle )
    
    

    
class Ball extends SubClass
  
  init: ->
    @.build()
    
  build: ->
    scale = 0.15 + Math.random() * 0.3
    geometry = new THREE.SphereGeometry scale, 12, 12
    material = new THREE.MeshLambertMaterial
      vertexColors: THREE.FaceColors
      color: @.root.colors.balls[ Math.floor( Math.random() * @.root.colors.balls.length )]
    @.piece = new THREE.Mesh geometry, material
    @.piece.castShadow = true
    @.piece.recieveShadow = false
    position = 
      x: Math.random() * 8 - 4
      y: Math.random() * 8 - 4
      z: 4 + Math.random() * 5
    @.piece.collision = new CANNON.Body
      mass: scale * 20
      shape: new CANNON.Sphere( scale )
      position: new CANNON.Vec3( position.x, position.y, position.z )
      material: @.root.physics.material
    @.root.stage.add @.piece
    @.root.physics.world.addBody @.piece.collision
    
  loop: =>
    vertices = [ "x", "y", "z" ]
    for vertex in vertices
      @.piece.position[vertex] = @.piece.collision.position[vertex]
    
    
    
class Environment extends SubClass
  
  init: ->
    @.container = new Container @
    @.balls = []
    total = 25
    while total > 0
      @.balls.push new Ball @
      total--
    
  loop: =>
    @.container.loop()
    for ball in @.balls
      ball.loop()
      
      
      
class Physics extends SubClass
  
  init: ->
    @.build()
    @.addListeners()
    
  build: ->
    @.ios = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream
    @.world = new CANNON.World()
    @.world.broadphase = new CANNON.NaiveBroadphase()
    @.world.gravity.set( 0, 0, 0 )
    @.world.solver.iterations = 20
    @.world.solver.tolerance = 0
    @.material = new CANNON.Material()
    material = new CANNON.ContactMaterial @.material, @.material
    material.restitution = 0.7
    material.friction = 0.1
    material.contactEquationStiffness = 100000000
    material.contactEquationRegularizationTime = 3
    @.world.addContactMaterial material
    @.world.defaultContactMaterial = material
    @.past = new Date().getTime()
    @.debug = document.querySelector ".debug"
    
  addListeners: ->
    window.addEventListener "devicemotion", @.onDeviceMotion
    window.addEventListener "mousemove", @.onMouseMove
    
  onDeviceMotion: ( e ) =>
    x = 0
    y = -9.86
    z = 0
    ox = 0
    oy = 0
    oz = -3.5
    mult = 1
    # iphones are upside down :(
    if @.ios then mult = -1
    acceleration = e.accelerationIncludingGravity
    if acceleration.x isnt null then x = acceleration.x * mult
    if acceleration.y isnt null then y = -acceleration.y * mult
    if acceleration.z isnt null then z = acceleration.z * mult
    @.world.gravity.set x, y, z
    @.root.camera.main.position.x = ox
    @.root.camera.main.position.y = oy
    @.root.camera.main.position.z = oz
    # @.debug.innerHTML = "x: #{x}<br />y: #{y}<br />z: #{z}"
    
  onMouseMove: ( e ) =>
    x = ( e.clientX / window.innerWidth ) - 0.5
    y = ( e.clientY / window.innerHeight ) - 0.5
    @.world.gravity.set -x * 9, -y * 9, 0
    @.root.camera.spot.position.x = x * 5
    @.root.camera.spot.position.y = y * 5
    @.root.camera.main.position.x = x * 0.1
    @.root.camera.main.position.y = y * 0.1
    
  loop: =>
    present = new Date().getTime()
    delta = present - @.past
    @.past = present
    @.world.step 1 / 60, delta, 3
      
      

class App
  
  constructor: ->
    @.colors = new Colors @
    @.stage = new Stage @
    @.camera = new Camera @
    @.physics = new Physics @
    @.environment = new Environment @
    @.loop()
      
  loop: =>
    requestAnimationFrame @.loop
    tasks = [ "physics", "environment", "stage" ]
    for task in tasks
      @[task].loop?()
          
          
          
new App()
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console