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 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

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

              
                <script id="vs_quad" type="x-shader/x-vertex">
  attribute vec4 a_position;
  attribute vec2 a_texcoord;
  
  varying vec2 v_texcoord;
  
  void main() {
    // v_clipcoord = a_position.xy;
    v_texcoord = a_texcoord;
    gl_Position = a_position;
  }
</script>

<script id="fs_vel" type="x-shader/x-fragment">
  precision highp float;
  
  varying vec2 v_texcoord;
  
  uniform float u_friction;
  uniform float u_gravity;
  uniform sampler2D u_position;
  uniform sampler2D u_velocity;
  uniform vec4 u_deviation;
  uniform vec2 u_resolution;
  
  float bell(float v) {
    return exp(-(v * v));
  }
  
  float spring(float v) {
    v -= 1.0;
    return atan(v / 100.0);
  }
  
  void main() {
    vec4 pos = texture2D(u_position, v_texcoord);
    gl_FragColor = u_friction * texture2D(u_velocity, v_texcoord) - u_gravity * pos;
    gl_FragColor += u_deviation * bell(4.0 * distance(v_texcoord, vec2(0.5, 0.5)));
    // vec4 mean = texture2D(u_position, v_texcoord - vec2(1.0, 0.0) / u_resolution);
    // mean += texture2D(u_position, v_texcoord + vec2(1.0, 0.0) / u_resolution);
    // mean += texture2D(u_position, v_texcoord - vec2(0.0, 1.0) / u_resolution);
    // mean += texture2D(u_position, v_texcoord + vec2(0.0, 1.0) / u_resolution);
    // gl_FragColor += (mean / 4.0 - pos) / 100.0;
  }
</script>

<script id="fs_pos" type="x-shader/x-fragment">
  precision highp float;
  
  varying vec2 v_texcoord;
  
  uniform sampler2D u_position;
  uniform sampler2D u_velocity;
  uniform vec2 u_resolution;
  
  float bell(float v) {
    return exp(-(v * v));
  }
  
  void main() {
    gl_FragColor = 0.9 * texture2D(u_position, v_texcoord) + texture2D(u_velocity, v_texcoord);
    vec4 mean = texture2D(u_position, v_texcoord - vec2(1.0, 0.0) / u_resolution);
    mean += texture2D(u_position, v_texcoord + vec2(1.0, 0.0) / u_resolution);
    mean += texture2D(u_position, v_texcoord - vec2(0.0, 1.0) / u_resolution);
    mean += texture2D(u_position, v_texcoord + vec2(0.0, 1.0) / u_resolution);
    gl_FragColor += mean / (pow(length(mean), 2.0 / 3.0) + 1.0);
  }
</script>

<script id="vs_grid" type="x-shader/x-vertex">
  attribute vec2 a_position;
  attribute vec2 a_texcoord;
  
  // varying float v_alpha;
  
  uniform sampler2D u_position;
  uniform sampler2D u_texture;
  uniform vec2 u_resolution;
    
  void main() {
    gl_PointSize = 2.0 + 2.0 * texture2D(u_texture, a_texcoord).a;
    gl_Position = vec4(a_position + texture2D(u_position, a_texcoord).xy / u_resolution, 0.0, 1.0);
  }
</script>
<script id="fs_out" type="x-shader/x-fragment">
  precision highp float;
  
  // varying float v_alpha;
  
  uniform float u_pi;
  
  void main() {
    // vec2 rg = atan(v_depth / 10.0) / u_pi + 0.5;
    gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
  }
</script>
<canvas></canvas>
              
            
!

CSS

              
                body {
  margin: 0;
}
              
            
!

JS

              
                class Cloth {
  constructor(cvs, cdim, mdim) {
    cvs.width = cvs.height = cdim;
    this.gl = twgl.getContext(cvs);
    // this.gl.enable(this.gl.BLEND);
    this.txr = twgl.createTexture(this.gl, {src:"https://upload.wikimedia.org/wikipedia/commons/5/53/Intel_Inside_Logo_(1968-2003).svg", flipY: true})
    this.qbf = twgl.primitives.createXYQuadBufferInfo(this.gl);
    this.gbf = twgl.createBufferInfoFromArrays(this.gl, {
      position: {
        numComponents: 2,
        data: Array(mdim)
          .fill()
          .map((_, i) =>
            Array(mdim)
              .fill()
              .map((_, j) => [-0.5 + (i + 0.5) / mdim, -0.5 + (j + 0.5) / mdim])
          )
          .flat()
          .flat()
      },
      texcoord: {
        numComponents: 2,
        data: Array(mdim)
          .fill()
          .map((_, i) =>
            Array(mdim)
              .fill()
              .map((_, j) => [(i + 0.5) / mdim, (j + 0.5) / mdim])
          )
          .flat()
          .flat()
      }
    });
    this.pgi = {
      pos: twgl.createProgramInfo(this.gl, ["vs_quad", "fs_pos"]),
      vel: twgl.createProgramInfo(this.gl, ["vs_quad", "fs_vel"]),
      out: twgl.createProgramInfo(this.gl, ["vs_grid", "fs_out"])
    };
    for (let key of ["pos", "vel"]) {
      this[key] = Array(2)
        .fill()
        .map(() =>
          twgl.createFramebufferInfo(
            this.gl,
            [
              {
                internalFormat: this.gl.RGBA32F
              }
            ],
            mdim,
            mdim
          )
        );
    }
    this.cdim = cdim;
    this.mdim = mdim;
  }
  draw(dev) {
    this.gl.blendFunc(this.gl.ONE, this.gl.ZERO);
    twgl.bindFramebufferInfo(this.gl, this.vel[1]);
    twgl.drawObjectList(this.gl, [
      {
        programInfo: this.pgi.vel,
        bufferInfo: this.qbf,
        uniforms: {
          u_friction: 0.9,
          u_gravity: 0.01,
          u_position: this.pos[0].attachments[0],
          u_velocity: this.vel[0].attachments[0],
          u_deviation: dev,
          u_resolution: [this.mdim, this.mdim]
        }
      }
    ]);
    twgl.bindFramebufferInfo(this.gl, this.pos[1]);
    twgl.drawObjectList(this.gl, [
      {
        programInfo: this.pgi.pos,
        bufferInfo: this.qbf,
        uniforms: {
          u_position: this.pos[0].attachments[0],
          u_velocity: this.vel[0].attachments[0],
          u_resolution: [this.mdim, this.mdim],
        }
      }
    ]);
    this.gl.blendFunc(this.gl.SRC_ALPHA, this.gl.ONE);
    twgl.bindFramebufferInfo(this.gl, null);
    twgl.drawObjectList(this.gl, [
      {
        type: this.gl.POINTS,
        programInfo: this.pgi.out,
        bufferInfo: this.gbf,
        uniforms: {
          u_resolution: [this.mdim, this.mdim],
          u_pi: Math.PI,
          u_position: this.pos[1].attachments[0],
          u_texture: this.txr,
          u_resolution: [this.mdim, this.mdim]
        }
      }
    ]);
    this.pos = [this.pos[1], this.pos[0]];
    this.vel = [this.vel[1], this.vel[0]];
  }
}
(function main() {
  twgl.setDefaults({ attribPrefix: "a_" });
  const shr = new Cloth(document.querySelector("canvas"), 512, 64);
  let dev = [0, 0, 0, 0];
  window.onmousedown = function () {
    dev = Array(4).fill().map(()=>Math.random()-0.5);
  };
  window.onmouseup = function () {
    dev = [0, 0, 0, 0];
  };
  (function frame() {
    shr.draw(dev);
    window.requestAnimationFrame(frame);
  })();
})();

              
            
!
999px

Console