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 CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              <canvas id="stage"></canvas>
<div id="info">Move your mouse around :)</div>

<script id="bulgeVert" type="x-shader/x-vertex">

  attribute vec2 aVertexPosition;
  attribute vec2 aTextureCoord;

  uniform mat3 projectionMatrix;
  varying vec2 vTextureCoord;

  void main(void) {
    gl_Position = vec4((projectionMatrix * vec3(aVertexPosition, 1.0)).xy, 0.0, 1.0);
    vTextureCoord = aTextureCoord;
  }
</script>

<script id="bulgeFrag" type="x-shader/x-vertex">

  varying vec2 vTextureCoord;
  uniform sampler2D uSampler;
  uniform float radius;
  uniform float strength;
  uniform vec2 center;
  uniform vec4 dimensions;
  uniform vec4 filterArea;

  vec2 mapCoord(vec2 coord) {
    coord *= filterArea.xy;
    coord += filterArea.zw;
    return coord;
  }

  vec2 unmapCoord(vec2 coord) {
    coord -= filterArea.zw;
    coord /= filterArea.xy;
    return coord;
  }

  vec2 bulge(vec2 coord) {

    coord -= center;

    float distance = length(coord);

    if (distance < radius) {
      float percent = distance / radius;
      if (strength > 0.0) {
        coord *= mix(1.0, smoothstep(0.0, radius / distance, percent), strength * 0.75);
      } else {
        coord *= mix(1.0, pow(percent, 1.0 + strength * 0.75) * radius / distance, 1.0 - percent);
      }
    }

    coord += center;

    return coord;
  }

  void main(void) {

    vec2 coord = mapCoord(vTextureCoord);  
    coord = bulge(coord);   
    vec2 clampedCoord = clamp(coord, vec2(0.0), filterArea.xy);  
    coord = unmapCoord(coord);

    gl_FragColor = texture2D(uSampler, coord);
    if (coord != clampedCoord) {
      gl_FragColor.a *= max(0.0, 1.0 - length(coord - clampedCoord));
    }
  }
</script>

<script id="marbleFrag" type="x-shader/x-vertex">

  precision mediump float;
  uniform vec2  resolution;
  uniform float time;
  uniform vec2  mouse;
  uniform float alpha;
  uniform float fluid_speed;
  uniform float color_intensity;

  uniform vec2 size;
  uniform vec4 dimensions;
  uniform vec4 filterArea;

  const int   complexity   = 40;   // More points of color.
  const float mouse_factor = 25.0; // Makes it more/less jumpy.
  const float mouse_offset = 5.0;  // Drives complexity in the amount of curls/cuves.  Zero is a single whirlpool.

  const float Pi = 3.14159;

  float sinApprox(float x) {
    x = Pi + (2.0 * Pi) * floor(x / (2.0 * Pi)) - x;
    return (4.0 / Pi) * x - (4.0 / Pi / Pi) * x * abs(x);
  }

  float cosApprox(float x) {
    return sinApprox(x + 0.5 * Pi);
  }

  void main(void) {
    vec2 p = (2.0 * gl_FragCoord.xy - resolution) / max(resolution.x, resolution.y);
    for(int i = 1; i < complexity; i++) {
      vec2 newp = p;
      newp.x += 0.6 / float(i) * sin(float(i) * p.y + time / fluid_speed + 0.3 * float(i)) + mouse.y / mouse_factor + mouse_offset;
      newp.y += 0.6 / float(i) * sin(float(i) * p.x + time / fluid_speed + 0.3 * float(i+10)) - mouse.x / mouse_factor + mouse_offset;
      p = newp;
    }
    vec3 col = vec3(color_intensity * sin(3.0 * p.x) + color_intensity,color_intensity * sin(3.0 * p.y) + color_intensity,color_intensity * sin(p.x + p.y) + color_intensity);
    gl_FragColor = vec4(col, alpha);
  }
</script>


            
          
!
            
              body {
  background: #000;
}

#stage {
  position: fixed;
}

#info {
  position: fixed;
  pointer-events: none;
  padding: 12px;
  color: #ddd;
  font-size: 16px;
  user-select: none;
  font-weight: bold;  
  text-shadow:
    -1px -1px 0 rgba(0,0,0,0.5),
    1px -1px 0 rgba(0,0,0,0.5),
    -1px 1px 0 rgba(0,0,0,0.5),
    1px 1px 0 rgba(0,0,0,0.5);
}
            
          
!
            
              console.clear()
var log = console.log.bind(console);

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

var bulgeVert  = document.getElementById("bulgeVert").textContent;
var bulgeFrag  = document.getElementById("bulgeFrag").textContent;
var marbleFrag = document.getElementById("marbleFrag").textContent;

//
// BULGE PINCH FILTER
// =========================================================================== 
class BulgePinchFilter extends PIXI.Filter {
  
  constructor(x, y, radius = 100, strength = 0.5) {
    
    // super(bulgeVert, bulgeFrag);
    super(PIXI.Filter.defaultVertexSrc, bulgeFrag);
    
    this.uniforms.center = new PIXI.Point(x, y);
    this.uniforms.radius = radius;
    this.uniforms.strength = strength;
  }
  
  get radius() { return this.uniforms.radius; }
  set radius(value) { this.uniforms.radius = value; }

  get strength() { return this.uniforms.strength; }
  set strength(value) { this.uniforms.strength = value; }

  get center() { return this.uniforms.center; }
  set center(value) { this.uniforms.center = value; }
    
  get x() { return this.uniforms.center.x; }
  set x(value) { this.uniforms.center.x = value; }
    
  get y() { return this.uniforms.center.y; }
  set y(value) { this.uniforms.center.y = value; }
  
  apply(filterManager, input, output) {
      
    this.uniforms.dimensions[0] = input.sourceFrame.width;
    this.uniforms.dimensions[1] = input.sourceFrame.height;

    filterManager.applyFilter(this, input, output);
  }
}

//
// MARBLE FILTER - based on filter in Phaser
// =========================================================================== 
class MarbleFilter extends PIXI.Filter {
  
  constructor(width, height) {
    super(null, marbleFrag);
    
    this.uniforms.time = 0;
    this.uniforms.alpha = 1;
    this.uniforms.fluid_speed = 10;
    this.uniforms.color_intensity = 0.30;
    this.uniforms.mouse = new PIXI.Point();
    this.uniforms.resolution = new PIXI.Point(width, height);
  }
  
  get time() { return this.uniforms.time; }
  set time(value) { this.uniforms.time = value; }
  
  get alpha() { return this.uniforms.alpha; }
  set alpha(value) { this.uniforms.alpha = value; }
  
  get speed() { return this.uniforms.fluid_speed; }
  set speed(value) { this.uniforms.fluid_speed = value; }
  
  get intensity() { return this.uniforms.color_intensity; }
  set intensity(value) { this.uniforms.color_intensity = value; }
  
  get mouse() { return this.uniforms.mouse; }
  set mouse(value) { this.uniforms.mouse = value; }
    
  get width() { return this.uniforms.resolution.width; }
  set width(value) { this.uniforms.resolution.width = value; }
  
  get height() { return this.uniforms.resolution.height; }
  set height(value) { this.uniforms.resolution.height = value; }
}

var vw = window.innerWidth;
var vh = window.innerHeight;

var app = new PIXI.Application(vw, vh, {
  view: document.getElementById("stage"),
  resolution: window.devicePixelRatio || 1,
  backgroundColor: 0x000000,
  antialias: true,
  autoResize: true
});

var background = new PIXI.Sprite();
var container = new PIXI.Container();
var resized = false;
var count = 0;
var moves = 0;

var loader = new PIXI.loaders.Loader(baseUrl)
  .add("muppets", "statler-waldorf-1.png?v=1")
  .load(onLoad);

var muppets, bulgeFilter, marbleFilter;

function onLoad(loader, resources) {
  
  muppets = new PIXI.Sprite(resources.muppets.texture);
  muppets.anchor.set(0.5, 1);
  muppets.pivot.set(0.5, 1);
  
  resize();  
  
  bulgeFilter  = new BulgePinchFilter(vw / 2, vh / 2, 300, 1);
  marbleFilter = new MarbleFilter(vw, vh);
    
  background.filters = [marbleFilter];
  container.filters = [bulgeFilter];  
  background.filterArea = app.screen;
  container.filterArea = app.screen;
  
  container.addChild(background, muppets);
  app.stage.addChild(container);
   
  app.stage.interactive = true;
  app.stage.on("pointermove", onPointerMove);
  app.stage.on("pointermove", removeInfo);
  
  app.ticker.add(() => {
  
    if (resized) { resize(); }
    
    // count += 0.1;
    count += 0.05;
    marbleFilter.time = count;
  });
  
  window.addEventListener("resize", () => resized = true);   
  
  var tl1 = new TimelineMax({ repeat: -1, repeatDelay: 5, yoyo: true })
    .to(bulgeFilter, 5, { strength: -0.6, ease: Power1.easeInOut });
}

function resize() {
      
  vw = window.innerWidth;
  vh = window.innerHeight;
  
  var r1 = (vw - 100) / muppets.texture.width;
  var r2 = (vh - 100) / muppets.texture.height;
    
  muppets.scale.set(Math.min(r1, r2))
  
  muppets.x = vw / 2;
  muppets.y = vh;
  background.width = vw;
  background.height = vh;
  
  app.renderer.resize(vw, vh);
  
  resized = false;
}

function onPointerMove(event) {  
  var global = event.data.global;
  bulgeFilter.center.copy(global);
}

function removeInfo() {
  
  if (++moves > 1000) {
    TweenLite.to("#info", 3, { autoAlpha: 0 });
    app.stage.off("pointermove", removeInfo);
  }
}
            
          
!
999px
Loading ..................

Console