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

              
                <p id="loading">Loading...</p>
<main>  
  <svg id="svg" viewBox="0 0 1200 870" preserveAspectRatio="xMinYMin meet" style="enable-background:new 0 0 1200 870;">    
    <defs>
      <path id="path" d="M 331 162 C 359.8 129.7 399.1 107.2 441.3 97.6 C 483.5 88.1 528.4 91.5 569 106.5 C 609.6 121.4 645.8 147.8 673.1 181.4 C 700.5 214.9 719 255.4 727.4 297.8 C 744.2 382.7 719.1 473.6 666.4 542.2 C 613.7 610.8 535.1 657.6 451 678 C 408.1 688.4 363.4 692.3 319.6 687.1 C 275.7 681.9 232.7 667.4 196 643 C 159.2 618.6 128.8 584.1 111.3 543.5 C 93.8 503 89.7 456.4 102 414 C 110.9 383.3 128.2 355.1 151.5 333.2 C 174.7 311.3 203.8 295.6 234.8 288.1 C 265.9 280.7 298.9 281.3 329.6 289.9 C 360.4 298.4 389 314.8 412 337 C 449.9 373.5 472.2 426.5 468.6 479 C 466.8 505.3 458.7 531.1 444.8 553.5 C 430.9 575.8 411.2 594.6 388 607 C 355.9 624.2 317.5 628.9 282 621 C 254.9 615 229.2 601.7 209.7 581.9 C 190.2 562.1 177.2 535.7 175 508 C 173.5 488.9 177 469.6 184.7 452.1 C 192.3 434.6 204.1 418.9 218.4 406.3 C 247.2 381.1 285.8 368.5 324 368 C 365.7 367.4 406.7 380.5 443.5 400.1 C 480.3 419.7 513.3 445.7 545.2 472.6 C 577.1 499.4 608.2 527.4 642.2 551.5 C 676.2 575.6 713.5 596 754 606 C 801.2 617.6 851.9 614.4 897.6 597.6 C 943.2 580.8 983.7 550.5 1013.2 511.8 C 1042.8 473.2 1061.4 426.3 1067 378 C 1072.7 329.7 1065.5 280 1047 235 C 1026.5 185.1 992 140.9 947.8 110 C 903.7 79 849.9 61.6 796 62 C 753.9 62.3 711.8 73.5 675.8 95.1 C 639.7 116.7 609.8 148.9 592 187 C 572.6 228.5 567.9 276.6 577.8 321.4 C 587.6 366.1 611.7 407.5 645 439 C 674.9 467.2 711.4 487.4 748.3 505.3 C 785.3 523.3 823.3 539.4 858.3 561 C 893.2 582.7 925.4 610.4 945.4 646.4 C 955.3 664.4 962.1 684.2 964.1 704.7 C 966.1 725.1 963.2 746.2 955 765 C 947.2 782.8 934.7 798.4 919.6 810.6 C 904.5 822.8 886.9 831.8 868.4 837.8 C 831.5 849.7 791.7 849.8 753 846 C 648.1 835.7 545.9 796.8 462 733 C 398.4 684.6 345.4 621.9 310.9 549.9 C 276.5 477.8 260.8 396.5 269 317 C 274.8 260.7 293.3 204.2 331 162" />
    </defs>    
  </svg>
  <canvas id="view"></canvas>
</main>
              
            
!

CSS

              
                html, body {
  height: 100%;
}

body {
  overflow: hidden;
  background: black;
  color: #aaa;
}

main {
  position: relative;
  visibility: hidden;
  transform-origin: left top;
  height: 100%;
  width: 100%;
}

svg, canvas {
  position: absolute;
  width: 100%;
  height: 100%;
  max-height: 870px;
}

path {
  fill: none;
  stroke: #555;
  stroke-width: 2;
}

#loading {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

              
            
!

JS

              
                console.clear();

gsap.registerPlugin(MotionPathPlugin);

let baseURL = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/";

let w = window.innerWidth;
let h = window.innerHeight;

let loader = new PIXI.loaders.Loader(baseURL)
  .add("fighter-02.json?v=4")
  .add("trail", "particle01.png?v=4")
  .load((loader, assets) => new Demo(assets));

//
// DEMO
// ===========================================================================
class Demo {
  constructor(assets) {

    this.last = performance.now();
    this.time = 0;
    this.size = { x: 1200, y: 870 };

    this.renderer = PIXI.autoDetectRenderer(this.size.x, this.size.y, {
      view: document.querySelector("#view"),
      backgroundColor: 0x000000,
    });

    this.stage = new PIXI.Container();
    this.fighter = new Fighter(this.stage, assets);

    window.addEventListener("resize", () => this.resize());

    gsap.ticker.add(() => this.render());

    this.resize();

    gsap.set("#loading", { autoAlpha: 0 });
    gsap.to("main", { autoAlpha: 1, duration: 0.5 });
  }

  render() {
    let current = performance.now();
    this.time = current - this.last;
    this.last = current;
    this.fighter.update(this.time);
    this.renderer.render(this.stage);
  }

  resize() {
    w = window.innerWidth;
    h = window.innerHeight;

    let ratio = Math.min(w / this.size.x, h / this.size.y);

    this.stage.scale.set(ratio);
    this.renderer.resize(w, h);
  }
}

//
// MOTION PATH
// ===========================================================================
class MotionPath {
  constructor(path) {
    let d = document.querySelector(path).getAttribute("d");
    let rawPath = MotionPathPlugin.stringToRawPath(d)[0];
    let values = this.values = [];
        
    for (let i = 0; i < rawPath.length; i += 2) {
      values.push({
        x: rawPath[i],
        y: rawPath[i + 1]
      });
    }

    this.lines = [];
    this.speeds = [];
    this.steps = 1000;

    this.speed = {
      now: 1,
      min: 0.5,
      max: 2.5,
      ratio: 0.5,
      step: 0.015,
    };

    this.bezier = {
      autoRotate: Math.PI / 2,
      path: this.values,
      type: "cubic",
      useRadians: true,
    };

    this.init();
  }

  init() {
    let speed = this.speed;
    let lines = this.lines;

    let obj = { x: 0, y: 0, rotation: 0 };
    
    let tween = gsap.to(obj, {
      paused: true,
      duration: 1,
      ease: "none",
      motionPath: {
        path: this.values,
        autoRotate: true,
        type: "cubic"
      }
    });

    // Loop through it twice to blend the start and end speeds together
    for (let loops = 0; loops < 2; loops++) {
      for (let i = 0; i <= this.steps; i++) {
        tween.progress(i * 0.001);

        let rot = obj.rotation % 90;
        let sign = rot < 0 ? -1 : 1;
        let mult = Math.abs(rot / 90);
        let diff = speed.ratio + speed.step * sign * mult || 0;

        speed.ratio = gsap.utils.clamp(0, 1, diff);

        if (!loops) continue;

        let color = lerpColor(speed.ratio);

        lines.push({ color, x: obj.x, y: obj.y });

        speed.now = gsap.utils.interpolate(speed.min, speed.max, speed.ratio);

        this.speeds.push(speed.now);
      }

      speed.now = gsap.utils.interpolate(speed.min, speed.max, speed.ratio);
    }

    this.plotSpeed();
  }

  plotSpeed() {
    let graphics = new PIXI.Graphics();

    let size = this.lines.length;
    let prev = this.values[0];

    for (let i = 0; i < size; i++) {
      let line = this.lines[i];

      graphics.beginFill(line.color);
      graphics.lineStyle(4, line.color);
      graphics.drawPolygon(prev.x, prev.y, line.x, line.y, prev.x, prev.y);
      graphics.endFill();

      prev = line;
    }

    let box = graphics.getBounds();

    graphics.lineStyle(0);
    graphics.beginFill(0x000000, 0);
    graphics.drawRect(0, 0, box.x + box.width, box.y + box.height);

    this.texture = graphics.generateTexture();
    this.sprite = new PIXI.Sprite(this.texture);

    graphics.destroy();
  }
}

//
// FIGHTER
// ===========================================================================
class Fighter {
  constructor(parent, assets) {
    this.stallSpeed = 0.55;

    this.sprite = new FighterSprite();

    this.spriteContainer = new PIXI.Container();
    this.emitterContainer = new PIXI.Container();

    this.emitter = new Emitter(this.emitterContainer, assets.trail.texture);
    this.motionPath = new MotionPath("#path");

    this.speeds = this.motionPath.speeds;

    this.bezierTween = gsap.to(this.spriteContainer, {
      duration: 5,
      paused: true,
      ease: "none",
      motionPath: this.motionPath.bezier
    });

    this.progressTween = gsap.to(this.bezierTween, {
      duration: 5,
      progress: 1,
      repeat: -1,
      ease: "none",
      onStart: this.setSpeed,
      onUpdate: this.setSpeed,
      callbackScope: this
    });

    this.spriteContainer.addChild(this.sprite);

    parent.addChild(this.motionPath.sprite);
    parent.addChild(this.emitterContainer);
    parent.addChild(this.spriteContainer);

    this.emitter.emit = true;
  }

  get x() {
    return this.spriteContainer.x;
  }
  get y() {
    return this.spriteContainer.y;
  }

  setSpeed() {
    let index = Math.round(this.progressTween.progress() * 1000);
    let speed = this.speeds[index];

    gsap.to(this.progressTween, { timeScale: speed, duration: 0.2 });

    if (speed < this.stallSpeed && !this.sprite.inRoll) {
      this.sprite.roll();
    }
  }

  update(time) {
    this.emitter.spawn(this.x, this.y, time * 0.001);
  }
}

//
// FIGHTER SPRITE
// ===========================================================================
class FighterSprite extends PIXI.Sprite {
  constructor() {
    super();

    this.total = 30;
    this.frames = new Array(this.total);

    for (let i = 0; i < this.total; i++) {
      var frame = `rollSequence00${i < 10 ? "0" + i : i}.png`;
      this.frames[i] = PIXI.Texture.fromFrame(frame);
    }

    this.anchor.set(0.5, 0.6);
    this.pivot.set(0.5);

    this.frame = 0;
    this.inRoll = false;
  }

  get frame() {
    return this._frame;
  }
  set frame(n) {
    n = n % this.total;
    this._frame = n;
    this.texture = this.frames[n];
  }

  roll() {
    this.inRoll = true;

    gsap.to(this, {
      duration: 1,
      frame: this.total,
      ease: "none",
      snap: "frame",
      onComplete: () => this.inRoll = false
    });
  }
}

//
// EMITTER
// ===========================================================================
class Emitter {
  constructor(container, texture) {
    this.container = container;

    this.emitter = new cloudkid.Emitter(this.container, [texture], {
      alpha: { start: 0.8, end: 0.1 },
      scale: { start: 1, end: 0.3, minimumScaleMultiplier: 1 },
      color: { start: "#e3f9ff", end: "#0ec8f8" },
      speed: { start: 0, end: 0 },
      acceleration: { x: 0, y: 0 },
      startRotation: { min: 0, max: 0 },
      rotationSpeed: { min: 0, max: 0 },
      lifetime: { min: 0.4, max: 0.4 },
      blendMode: "normal",
      frequency: 0.008,
      emitterLifetime: -1,
      maxParticles: 1000,
      pos: { x: 0, y: 0 },
      addAtBack: false,
      spawnType: "point",
    });
  }

  set emit(flag) {
    this.emitter.emit = flag;
  }

  spawn(x, y, time) {
    this.emitter.updateSpawnPos(x, y);
    this.emitter.update(time);
  }
}

//
// LERP COLOR
// ===========================================================================
function lerpColor(progress = 0) {
  let color1 = [255, 0, 0];
  let color2 = [0, 255, 0];
  let rgb = [];

  for (let i = 0; i < 3; i++) {
    let rgb1 = Math.pow(color1[i], 2);
    let rgb2 = Math.pow(color2[i], 2);

    rgb[i] = Math.round(Math.sqrt(gsap.utils.interpolate(rgb1, rgb2, progress)));
  }

  return (1 << 24) + (rgb[0] << 16) + (rgb[1] << 8) + rgb[2];
}


              
            
!
999px

Console