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

              
                <canvas class="" id="cubes" style="">
    This is a canvas. Sorry your browser does not support this tag
</canvas>

              
            
!

CSS

              
                body {
  background: #3E4777;
  background-image: 
    radial-gradient(center, rgba(white,0.1), transparent 75%);
  overflow: hidden;
  margin: 0;
}
              
            
!

JS

              
                class CubesL
  constructor: () ->
    @rowCount = 17
    @perRow = 10
    @totalItemCount = @rowCount * @perRow
    @itemRotationD = 360 / @perRow
    @itemRotationR = @rotateRadians(@itemRotationD)

    @setStage()

    @setLighting()

    @setActors()

    @animate()
    @move()
    @render()

  setActors: =>
    @parent = new THREE.Object3D()
    @parent.rotation.x = @rotateRadians -90
    @scene.add( @parent )

    @addCubes()
    @positionCubes()

  move: =>
    for i in [0..@totalItemCount - 1]
      @basicTimeline = anime.timeline
        loop: true

      @basicTimeline
        .add({
          targets: @cubes[i].rotation,
          x: [@rotateRadians(0), @rotateRadians(0)]
          y: [@rotateRadians(0), @rotateRadians(90)]
          z: [@rotateRadians(0), @rotateRadians(90)]
          easing: 'easeInOutCubic'
          duration: 2000
        })
        .add({
          targets: @cubes[i].rotation,
          x: [@rotateRadians(0), @rotateRadians(90)]
          y: [@rotateRadians(90), @rotateRadians(180)]
          z: [@rotateRadians(90), @rotateRadians(90)]
          easing: 'easeInOutCubic'
          duration: 2000
        })
        .add({
          targets: @cubes[i].rotation,
          x: [@rotateRadians(90), @rotateRadians(180)]
          y: [@rotateRadians(180), @rotateRadians(180)]
          z: [@rotateRadians(90), @rotateRadians(180)]
          easing: 'easeInOutCubic'
          duration: 2000
        })

  addCubes: =>
    @cubes = []
    @cubeParents = []

    @addCube(i, Math.ceil(i/@perRow)) for i in [1..@totalItemCount]

  positionCubes: =>
    @offsetY = 13

    @positionCube(i-1, Math.ceil(i/@perRow)) for i in [1..@totalItemCount]

  positionCube: (i, row) =>
    # Angle for sin/cos curve positioning
    @angle = @itemRotationR * i

    # Offset every other row so cubes aren't in one single column
    @rowOffset = 0.3

    unless row % 2 == 0
      @cubeParents[i].rotation.y = @rotateRadians((i * (@itemRotationD)) + 45)
      @x = 4 * Math.sin(@angle) * -((i / 10) * 0.1)
      @z = 4 * Math.cos(@angle) * -((i / 10) * 0.1)
    else
      @cubeParents[i].rotation.y = @rotateRadians((i * (@itemRotationD)) + 65)
      @x = 4 * Math.sin(@angle + @rowOffset) * -((i / 10) * 0.1)
      @z = 4 * Math.cos(@angle + @rowOffset) * -((i / 10) * 0.1)

    # Positioning cubes in levels
    if i % 10 == 0
      @offsetY -= 1

    @cubeParents[i].rotation.z = @rotateRadians 10
    @cubeParents[i].position.x = @x
    @cubeParents[i].position.y = @offsetY
    @cubeParents[i].position.z = @z

  addCube: (i, row) =>
    # Reduce cube size
    @sizeModifier = 0.4 + (@rowCount / 340) * (row / 1.75)

    @sizeModifier = 1 + (@rowCount / 340) * (row / 1.75)

    # Cube initialisation
    @cubeParent = new THREE.Object3D()
    @geometry = new THREE.BoxGeometry( 1.5 * @sizeModifier, 1.5 * @sizeModifier, 1.5 * @sizeModifier )
    @material = new THREE.MeshBasicMaterial( { color: "#ffffff" } )
    @material.vertexColors = THREE.FaceColors
    @geometry.faces[7].color.set("#FD0000")
    @geometry.faces[6].color.set("#FD0000")
    @geometry.faces[8].color.set("#ffffff")
    @geometry.faces[9].color.set("#ffffff")
    @geometry.faces[0].color.set("#000000")
    @geometry.faces[1].color.set("#000000")
    @geometry.faces[2].color.set("#000000")
    @geometry.faces[3].color.set("#000000")
    @geometry.faces[4].color.set("#FD0000")
    @geometry.faces[5].color.set("#FD0000")
    @geometry.faces[10].color.set("#ffffff")
    @geometry.faces[11].color.set("#ffffff")
    @cube = new THREE.Mesh( @geometry, @material )

    @cubeParent.add( @cube )
    @parent.add( @cubeParent )

    # Push to arrays (used to target for animation)
    @cubes.push @cube
    @cubeParents.push @cubeParent

  setLighting: =>
    @ambientLight = new THREE.AmbientLight("#ffffff", 0.5)
    @scene.add(@ambientLight)

  setStage: =>
    @renderer = new THREE.WebGLRenderer({canvas: document.getElementById("cubes"), antialias: true})
    @renderer.setClearColor("#4e4d70")
    @renderer.setPixelRatio(window.devicePixelRatio)
    @renderer.setSize(window.innerWidth, window.innerHeight)

    @scene = new THREE.Scene()
    @camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000)
    @camera.position.z = 10

  rotateRadians: (deg) => return deg * (Math.PI / 180)

  animate: =>
    requestAnimationFrame( @animate )
    @render()

  render: =>
    @renderer.render(@scene, @camera)

cubes = new CubesL

              
            
!
999px

Console