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

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

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.

            
              <div></div>
<!--

Canvas Parallax Mountains
-----------------------
Move your mouse to change speed and move up and down.

-->
            
          
!
            
              canvas {
  background: 
    linear-gradient(
      hsl(200, 50%, 80%) 0%, 
      hsl(200, 30%, 95%) 75%)
  ;
  display: block;
}

div {
  background: url(https://jackrugile.com/images/misc/skyline-texture.png);
  height: 100%;
  left: 0;
  position: fixed;
  top: 0;
  width: 100%;
}
            
          
!
            
              sketch = Sketch.create()
sketch.mouse.x = sketch.width / 10
sketch.mouse.y = sketch.height
mountainRanges = []
dt = 1

#
# MOUNTAINS
#
  
Mountain = ( config ) ->
  this.reset( config )

Mountain.prototype.reset = (config) ->
  this.layer = config.layer
  this.x = config.x
  this.y = config.y
  this.width = config.width
  this.height = config.height
  this.color = config.color  

#
# MOUNTAIN RANGE
#

MountainRange = (config) -> 
  this.x = 0
  this.mountains = []
  this.layer = config.layer
  this.width =
    min: config.width.min
    max: config.width.max
  this.height =
    min: config.height.min
    max: config.height.max
  this.speed = config.speed
  this.color = config.color
  this.populate()
  return this
  
MountainRange.prototype.populate = ->
  totalWidth = 0
  while totalWidth <= sketch.width + ( this.width.max * 4 )
    newWidth = round ( random( this.width.min, this.width.max ) )
    newHeight = round ( random( this.height.min, this.height.max ) )
    this.mountains.push( new Mountain(
      layer: this.layer
      x: if this.mountains.length == 0 then 0 else ( this.mountains[ this.mountains.length - 1 ].x + this.mountains[ this.mountains.length - 1 ].width )
      y: sketch.height - newHeight
      width: newWidth
      height: newHeight
      color: this.color
    ) )
    totalWidth += newWidth

MountainRange.prototype.update = ->
  this.x -= ( sketch.mouse.x * this.speed ) * dt
      
  firstMountain = this.mountains[ 0 ]
  if firstMountain.width + firstMountain.x + this.x < -this.width.max
    newWidth = round ( random( this.width.min, this.width.max ) )
    newHeight = round ( random( this.height.min, this.height.max ) )
    lastMountain = this.mountains[ this.mountains.length - 1 ]    
    firstMountain.reset(
      layer: this.layer
      x: lastMountain.x + lastMountain.width
      y: sketch.height - newHeight
      width: newWidth
      height: newHeight
      color: this.color
    )    
    this.mountains.push( this.mountains.shift() )
  
MountainRange.prototype.render = ->
  sketch.save()
  sketch.translate( this.x, ( sketch.height - sketch.mouse.y ) / 20 * this.layer )     
  sketch.beginPath()
  pointCount = this.mountains.length
  sketch.moveTo(this.mountains[0].x, this.mountains[0].y)  
  for i in [0..(pointCount-2)] by 1
    c = (this.mountains[i].x + this.mountains[i + 1].x) / 2
    d = (this.mountains[i].y + this.mountains[i + 1].y) / 2
    sketch.quadraticCurveTo(this.mountains[i].x, this.mountains[i].y, c, d)
  sketch.lineTo(sketch.width - this.x, sketch.height)
  sketch.lineTo(0 - this.x, sketch.height)  
  sketch.closePath()
  sketch.fillStyle = this.color
  sketch.fill()    
  sketch.restore()

#
# SETUP
#
  
sketch.setup = ->    
  i = 5
  while i--
    mountainRanges.push( new MountainRange(
      layer: i + 1
      width:
        min: ( i + 1 ) * 50
        max: ( i + 1 ) * 70
      height:
        min: 200 - ( ( i ) * 40 )
        max: 300 - ( ( i ) * 40 )
      speed: ( i + 1 ) * .003
      color: 'hsl( 120, ' + ( ( ( i + 1 ) * 1 ) + 10 ) + '%, ' + ( 75 - ( i * 13 ) ) + '% )'
    ) )
    
#
# CLEAR
#
  
sketch.clear = ->
  sketch.clearRect( 0, 0, sketch.width, sketch.height )

#
# UPDATE
#
  
sketch.update = ->
  dt = if sketch.dt < .1 then .1 else sketch.dt / 16
  dt = if dt > 5 then 5 else dt
  i = mountainRanges.length
  mountainRanges[ i ].update( i ) while i--
  
#
# DRAW
#
  
sketch.draw = ->
  i = mountainRanges.length
  mountainRanges[ i ].render( i ) while i--

#
# Mousemove Fix
#  
    
$( window ).on 'mousemove', (e) ->
  sketch.mouse.x = e.pageX
  sketch.mouse.y = e.pageY
            
          
!
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