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

              
                <div></div>
<!--

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

-->
              
            
!

CSS

              
                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%;
}
              
            
!

JS

              
                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

Console