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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                
              
            
!

CSS

              
                html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  background: linear-gradient(#14161d, #030032);
}

              
            
!

JS

              
                /************************************
  This codepen is part of the svg.js
  advent calendar. You can find all
  the pens at twitter: @svg_js
*************************************/

const width = 1000 //  window.innerWidth
const height = 400 // window.innerHeight - 10
const GRAVITY = 9.81 / 1000000// m/s²

const canvas = SVG()
  .addTo('body')
  .size(window.innerWidth, window.innerHeight-10)
  .viewbox(0, 0, width, height)

const wrapper = canvas.group().flip('y', height/2)

const rockets = canvas.group()
const filter = canvas.filter()
// Turbulence and color filter
var temp = filter.turbulence(0.15, 3)
  .colorMatrix('hueRotate', 0)

// We animate the hueRotate value
temp.animate(500)
  .ease('-')
  .attr('values', 360)
  .loop()

// Use the created bump map to pixelate the blur
filter.displacementMap(filter.source, temp, 10, 'R', 'G')

// Values are factors to 100%. So 1.4 is 140%
filter.move(-5, -5).size(11, 11)

const {sin, cos} = Math
const vecFromLength = (angle, len) =>
  [len * cos(angle), len * sin(angle)]

class GravityControlled extends SVG.G {
  constructor (options = {}, sparkleFilter) {
    super(SVG.create('g'))
    this.animateRocket = this.animateRocket.bind(this)

    const r = options.r || (Math.random() * 5 + 5)
    const x = options.x || (Math.random() * (width - 400) + 200)
    const y = options.y || (Math.random() * 50)

    this.p0 = new SVG.Point(x, y)
    this.angle = options.angle != null ? options.angle : (90 + Math.random() * 46 - 23)
    const vel = options.v != null ? options.v : ((200 + 50 * Math.random()) / 1000)

    const [vx0, vy0] = vecFromLength(this.angle / 180 * Math.PI, vel)
    this.vx0 = vx0 + (options.vx0 || 0)
    this.vy0 = vy0 + (options.vy0 || 0)

    this.filter = sparkleFilter

    this.color = options.color || new SVG.Color(Math.random() * 360, 100, 50, 'hsl')

    this.circle({r})
      .center(x, y)
      .fill(this.color)
      .filterWith(sparkleFilter)
  }

  animateRocket (now) {
    const t = now - this.time

    const {vx0, vy0} = this
    const {x: x0, y: y0} = this.p0
    const ax = 0
    const ay = -GRAVITY * 10

    const x = x0 + vx0 * t + ax / 2 * t ** 2
    const y = y0 + vy0 * t + ay / 2 * t ** 2

    this.x = x
    this.y = y

    this.children()[0].center(x, y)
    this.frame = SVG.Animator.frame(this.animateRocket)
  }

  start () {
    this.time = performance.now()
    this.frame = SVG.Animator.frame(this.animateRocket)
    SVG.Animator.timeout(this.explode, 2000 + 2000 * Math.random())
    return this
  }
}

class Sparkle extends GravityControlled {
  constructor(options, sparkleFilter) {
    super(options, sparkleFilter)
    this.explode = this.explode.bind(this)

    const c = new SVG.Color(
      this.color.h,
      this.color.s,
      this.color.l + Math.sign(Math.random() - 0.5) * (20 + Math.random() * 20),
      'hsl'
    )

    this.children()[0].animate(100 + Math.random() * 50 - 25, Math.random()*50).fill(c).loop(500, true)
  }

  explode () {
    SVG.Animator.cancelFrame(this.frame)
    this.remove()
  }
}

class Rocket extends GravityControlled {
  constructor(sparkleFilter) {
    super({}, sparkleFilter)
    this.explode = this.explode.bind(this)
  }

  explode () {
    SVG.Animator.cancelFrame(this.frame)

    const v = 100 / 1000
    const n = 16

    const ax = 0
    const ay = -GRAVITY * 10
    const now = performance.now()
    const currVx = this.vx0 + ax * (now - this.time)
    const currVy = this.vy0 + ay * (now - this.time)

    for (var i = n; i--;) {
      let angle = 360/n * i
      const sparkle = new Sparkle({
        r: 5,
        x: this.x,
        y: this.y,
        angle, v,
        vx0: currVx,
        vy0: currVy,
        color: new SVG.Color(this.color)
      }, this.filter).addTo(this.parent()).start()

    }

    this.remove()
  }
}


const startRocket = () => {
  new Rocket(filter).addTo(wrapper).start()
}

// Every 100ms we decide if we want to create
// a new star and after which time
setInterval(() => {
  if (Math.random() < 0.2) {
    setTimeout(() => {
      startRocket()
    }, Math.random() * 1000)
  }
  if (Math.random() < 0.2) {
    setTimeout(() => {
      startRocket()
    }, Math.random() * 1000)
  }
  if (Math.random() < 0.2) {
    setTimeout(() => {
      startRocket()
    }, Math.random() * 1000)
  }
}, 500)

startRocket()
startRocket()
startRocket()

              
            
!
999px

Console