Pen Settings

HTML

CSS

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

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

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.

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

              
                .icon.cardboard
              
            
!

CSS

              
                body { margin: 0; }
canvas { width: 100%; height: 100% }

.icon {
  position: absolute;
  top: 5px;
  left: 5px;
  z-index: 100;
  display: inline-block;
  width: 80px;
  height: 80px;
}

.icon.cardboard {
}

.icon.lcd {
}
              
            
!

JS

              
                gears = 
  one: 
    color: 'red'
    x: 0
    y: 0
    teeth: 12
  two: 
    color: 'teal'
    x: 8
    y: -14
    teeth: 10
    parent: 'one'
  three: 
    color: 'yellow'
    x: 15.7
    y: 4
    teeth: 10
    parent: 'one'
  four: 
    color: 'green'
    x: 3
    y: 14.5
    teeth: 8
    parent: 'one'
  five: 
    color: 'blue'
    x: -13
    y: 1
    teeth: 5
    parent: 'one'  
  six: 
    color: 'magenta'
    x: 13.5
    y: 28.5
    teeth: 16
    parent: 'four'   
  seven: 
    color: 'orange'
    x: -22
    y: 8.5
    teeth: 10
    parent: 'five'
  eight: 
    color: 'white'
    x: -15.3
    y: 20.5
    parent: 'seven'
    teeth: 8

 
THREE.invert_geometry = (g) ->
  for face in g.faces
    temp = face.a
    face.a = face.c
    face.c = temp

  g.computeFaceNormals()
  g.computeVertexNormals()

  for faceVertexUv in g.faceVertexUvs[0]
    temp = faceVertexUv[0]
    faceVertexUv[0] = faceVertexUv[2]
    faceVertexUv[2] = temp
  g
    
class WasherGeometry
  constructor: (outerRadius, innerRadius, height, radiusSegments, heightSegments) ->
    @outerRadius = outerRadius
    @innerRadius = innerRadius
    @height = height
    @radiusSegments = radiusSegments
    @heightSegments = heightSegments
    
    @geometry = new THREE.Geometry
    @addCylinders()
    @addRings()
    return @geometry
    
  flipMatrix: ->
    new THREE.Matrix4().makeRotationX(Math.PI)
    
  shiftMatrix: ->
    new THREE.Matrix4().setPosition new THREE.Vector3( 0, 0, @height/2 )
    
  cylinder: (radius) ->
    new THREE.CylinderGeometry radius, radius, @height, @radiusSegments, @heightSegments, true
        
  addCylinders: ->   
    @geometry.merge THREE.invert_geometry( @cylinder(@innerRadius) ) 
    @geometry.merge @cylinder(@outerRadius) 
    @geometry.applyMatrix new THREE.Matrix4().makeRotationX(Math.PI/2)
    
  addRings: () ->
    ring = new THREE.RingGeometry( @innerRadius, @outerRadius, @radiusSegments, 1 )
    ring.applyMatrix(@shiftMatrix())
    @geometry.merge ring 
    flipped = ring.clone()
    flipped.applyMatrix(@flipMatrix())
    @geometry.merge flipped
    
THREE.WasherGeometry = WasherGeometry
  
class Gear
  constructor: (teeth,color,x,y,parent=false) ->
    @teeth = teeth
    @parent = parent
    @x = x
    @y = y
    @speed = 2
    @width = 3
    @hole_radius = 2.5
    @color = color
    @object = new THREE.Object3D()
    @material = new THREE.MeshPhongMaterial
      color: color
      ambient: color
      shininess: 50
      #wireframe: true
    washer = new WasherGeometry(@radius(),@hole_radius,@width,@teeth*4, 1 )
        
    mesh = new THREE.Mesh washer, @material
    @object.add mesh
    
    @addTooth(t) for t in [0...@teeth]
      
    @object.position.x = @x
    @object.position.y = @y
    
  direction: ->
    if @parent then @parent.direction()*-1 else 1
      
      
  rootSpeed: ->
    @root_speed ?= if @parent then @parent.rootSpeed() else @speed
    
  ratio: ->
    #this is the ratio to the parent
    if @parent
      @parent.teeth/@teeth
    else
      1
      
  ratioToRoot: ->
    #this is the ratio all the way down to the root parent
    @ratio_to_root ?= if @parent then @parent.ratioToRoot()*@ratio() else @ratio()
      
  offsetAngle: ->
    #this lines up the teeth between all the gears
    @offset_angle ?= if @parent
      rx = @x-@parent.x
      ry = @y-@parent.y 
      tan = Math.atan2(ry,rx)
      Math.PI + tan - tan * @ratio() * -1 - @parent.offsetAngle() * @ratio() + @toothOffset()
    else
      0
      
  toothOffset: ->
    @toothRad(1)/2
      
  radius: ->
    @teeth*2/Math.PI
  toothDeg: (t) ->
    360/@teeth*t
  toothX: (t) ->
    Math.cos(@toothRad(t))*(@radius()+0.80)
  toothY: (t) ->
    Math.sin(@toothRad(t))*(@radius()+0.80)
  toothRad: (t) ->
    @toothDeg(t)*Math.PI/180
    
  addTooth: (t) ->
    geometry = new THREE.BoxGeometry 2.2, 2, @width
    mesh = new THREE.Mesh geometry, @material
    mesh.rotation.z = @toothRad(t)    
    mesh.position.x = @toothX(t)
    mesh.position.y = @toothY(t)
    @object.add mesh 
    
  rotate: (t) ->
    @object.rotation.z = @rootSpeed() *  t /2000 * @ratioToRoot() * @direction() + @offsetAngle()
    
class GearScene
  constructor: (gear_defs) ->  
    @scene = new THREE.Scene()
    @camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 )
    @speed = 0.4
    @lastframe = 0
    @renderSetup()
     
    @buildGears(gear_defs)
    
    @scene.add new THREE.AmbientLight( 0x303030 )
    
    @light = new THREE.DirectionalLight( 0xffffff, 1 );
  
    @light.position.z = 30
    @light.position.y = 25
    @light.position.x = 25
    @scene.add( @light )
    
    @camera.position.z = 45
    @camera.position.x = -20
    @camera.position.y = -18
    @camera.lookAt @scene.position 
    @camera.position.y = -15
    window.addEventListener 'resize', @windowResize   
    @render()
    $('.icon').on 'click', @changeView
    
  
  buildGears: (gear_defs) ->
    @gear_group = new THREE.Object3D()
    @gears = {}
    for key,def of gear_defs
      @gears[key] = new Gear def.teeth, def.color, def.x, def.y, @gears[def.parent]
      @gear_group.add @gears[key].object
    @scene.add @gear_group
    
  changeView: =>
    $('.icon').toggleClass('cardboard').toggleClass('lcd')
    
    if $('.icon').hasClass('lcd') 
      @view = @effect 
      @fullScreen()
    else
      @view = @renderer
    
    @windowResize()
    
  fullScreen: ->
    elem = @renderer.domElement
    if elem.requestFullscreen
      elem.requestFullscreen()
    else if elem.msRequestFullscreen
      elem.msRequestFullscreen()
    else if elem.mozRequestFullScreen
      elem.mozRequestFullScreen()
    else if elem.webkitRequestFullscreen
      elem.webkitRequestFullscreen()    
      
  renderSetup: ->
    @renderer = new THREE.WebGLRenderer
      antialias: true
    document.body.appendChild( @renderer.domElement ) 
    #@effect = new THREE.StereoEffect( @renderer );
    #@effect.eyeSeparation = 0.5;
    @view = @renderer
    @view.setSize( window.innerWidth, window.innerHeight );
    
  windowResize: =>
    @view.setSize( window.innerWidth, window.innerHeight )
    
    @camera.aspect = window.innerWidth / window.innerHeight
    @camera.updateProjectionMatrix()

  rotate: (t) ->
    for key,gear of @gears
      gear.rotate(t)
    @gear_group.rotation.y = @speed *  t / 2000
    
  render: (t=0) =>
    requestAnimationFrame( @render ) 
    @rotate(t)
    @view.render( @scene, @camera )


new GearScene(gears)

              
            
!
999px

Console