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

              
                <script type='json/dither.json'>
{
  "bayer16": [
      0, 128,  32, 160,   8, 136,  40, 168,   2, 130,  34, 162,  10, 138,  42, 170,
    192,  64, 224,  96, 200,  72, 232, 104, 194,  66, 226,  98, 202,  74, 234, 106,
     48, 176,  16, 144,  56, 184,  24, 152,  50, 178,  18, 146,  58, 186,  26, 154,
    240, 112, 208,  80, 248, 120, 216,  88, 242, 114, 210,  82, 250, 122, 218,  90,
     12, 140,  44, 172,   4, 132,  36, 164,  14, 142,  46, 174,   6, 134,  38, 166,
    204,  76, 236, 108, 196,  68, 228, 100, 206,  78, 238, 110, 198,  70, 230, 102,
     60, 188,  28, 156,  52, 180,  20, 148,  62, 190,  30, 158,  54, 182,  22, 150,
    252, 124, 220,  92, 244, 116, 212,  84, 254, 126, 222,  94, 246, 118, 214,  86,
      3, 131,  35, 163,  11, 139,  43, 171,   1, 129,  33, 161,   9, 137,  41, 169,
    195,  67, 227,  99, 203,  75, 235, 107, 193,  65, 225,  97, 201,  73, 233, 105,
     51, 179,  19, 147,  59, 187,  27, 155,  49, 177,  17, 145,  57, 185,  25, 153,
    243, 115, 211,  83, 251, 123, 219,  91, 241, 113, 209,  81, 249, 121, 217,  89,
     15, 143,  47, 175,   7, 135,  39, 167,  13, 141,  45, 173,   5, 133,  37, 165,
    207,  79, 239, 111, 199,  71, 231, 103, 205,  77, 237, 109, 197,  69, 229, 101,
     63, 191,  31, 159,  55, 183,  23, 151,  61, 189,  29, 157,  53, 181,  21, 149,
    255, 127, 223,  95, 247, 119, 215,  87, 253, 125, 221,  93, 245, 117, 213,  85
  ]
}
</script>
<script type='json/cube.json'>
{
  "indices": [
     0,  1,  2,  0,  2,  3,
     4,  5,  6,  4,  6,  7,
     8,  9, 10,  8, 10, 11,
    12, 13, 14, 12, 14, 15,
    16, 17, 18, 16, 18, 19,
    20, 21, 22, 20, 22, 23
  ],
  "vertices": [
    -1, -1,  1,  1, -1,  1,  1,  1,  1, -1,  1,  1,
    -1, -1, -1, -1,  1, -1,  1,  1, -1,  1, -1, -1,
    -1,  1, -1, -1,  1,  1,  1,  1,  1,  1,  1, -1,
    -1, -1, -1,  1, -1, -1,  1, -1,  1, -1, -1,  1,
     1, -1, -1,  1,  1, -1,  1,  1,  1,  1, -1,  1,
    -1, -1, -1, -1, -1,  1, -1,  1,  1, -1,  1, -1
  ],
  "normals": [
     0,  0,  1,  0,  0,  1,  0,  0,  1,  0,  0,  1,
     0,  0, -1,  0,  0, -1,  0,  0, -1,  0,  0, -1,
     0,  1,  0,  0,  1,  0,  0,  1,  0,  0,  1,  0,
     0, -1,  0,  0, -1,  0,  0, -1,  0,  0, -1,  0,
     1,  0,  0,  1,  0,  0,  1,  0,  0,  1,  0,  0,
    -1,  0,  0, -1,  0,  0, -1,  0,  0, -1,  0,  0
  ],
  "texture": [
     0,  0,  1,  0,  1,  1,  0,  1,
     1,  0,  1,  1,  0,  1,  0,  0,
     0,  1,  0,  0,  1,  0,  1,  1,
     1,  1,  0,  1,  0,  0,  1,  0,
     1,  0,  1,  1,  0,  1,  0,  0,
     0,  0,  1,  0,  1,  1,  0,  1
  ]
}
</script>
<script type='shaders/vertex.essl'>
attribute vec4 a_position;
attribute vec3 a_normal;
attribute vec2 a_texture;
uniform mat4 u_model, u_view, u_projection;
uniform vec2 u_size0;
varying vec2 v_texture;
varying vec3 v_light;

const vec4 LIGHT = vec4(0.00, 0.00, 5.00, 75.0);
const vec3 COLOR = vec3(0.50, 0.25, 1.00);

vec2 cover(vec2 v, vec2 r) {
  return 0.5 + (v - 0.5) * (r.x > r.y ?
    vec2(r.y / r.x, 1.0) : vec2(1.0, r.x / r.y));
}

vec3 light(vec4 p, vec3 n, vec4 l, vec3 c) {
  vec3 d = l.xyz - p.xyz;
  return l.w / pow(length(d), 2.0) * dot(n.xyz, normalize(d)) * c;
}

void main(void) {
  vec4 p = mat4(u_model) * a_position;
  vec3 n = mat3(u_model) * a_normal;

  v_light     = light(p, n, LIGHT, COLOR);
  v_texture   = cover(a_texture, u_size0);
  gl_Position = u_projection * u_view * p;
}
</script>
<script type='shaders/fragment.essl'>
#ifdef GL_FRAGMENT_PRECISION_HIGH
  precision highp float;
#else
  precision mediump float;
#endif

uniform sampler2D u_sampler0, u_sampler1;
uniform vec2 u_size1;
varying vec2 v_texture;
varying vec3 v_light;

float srgb2rgb(float c) {
  return c <= 0.04045 ? c / 12.92 : pow((c + 0.055) / 1.055, 2.4);
}

float rgb2srgb(float c) {
  return c <= 0.0031308 ? c * 12.92 : pow(c, 1.0/2.4) * 1.055 - 0.055;
}

vec3 srgb2rgb(vec3 c) {
  return vec3(srgb2rgb(c.r), srgb2rgb(c.g), srgb2rgb(c.b));
}

vec3 rgb2srgb(vec3 c) {
  return vec3(rgb2srgb(c.r), rgb2srgb(c.g), rgb2srgb(c.b));
}

vec3 grayscale(vec3 c) {
  const vec3 LUM = vec3(0.212655, 0.715158, 0.072187);
  return vec3(dot(c.rgb, LUM));
}

vec3 dither(vec3 c, float d, float b) {
  return rgb2srgb(vec3(floor(d + srgb2rgb(c)*b) / b));
}

void main(void) {
  vec4 d = texture2D(u_sampler1, gl_FragCoord.xy / u_size1);
  vec4 c = texture2D(u_sampler0, v_texture);

  gl_FragColor = vec4(dither(grayscale(c.rgb) * v_light, d.a, 1.0), c.a);
}
</script>

              
            
!

CSS

              
                body {
  background: #000;
  overflow: hidden;
}

canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

video {
  position: absolute;
  top: 50%;
  left: 50%;
}

[data-error]::after {
  display: block;
  content: attr(data-error);
  position: absolute;
  top: 0;
  left: 0;
  margin: 1em;
  padding: .75em 1em;
  white-space: pre-wrap;
  font: .8em 'Consolas', monospace;
  background: rgba(0,0,0,.25);
  color: rgba(255,255,255,.75);
}

              
            
!

JS

              
                'use strict'

Math.TAU = Math.PI * 2
Math.RAD = Math.PI / 180
Math.DEG = 180 / Math.PI

video  = document.createElement 'video'
canvas = document.createElement 'canvas'
gl     = canvas.getContext      'webgl'

new Promise (done) ->
  window.addEventListener 'load', done

.then ->
  document.body.appendChild canvas

  video.autoplay = video.loop = true
  video.crossOrigin = 'anonymous'
  video.volume = 0.1
  video.src = 'https://dl.dropboxusercontent.com/u/3116587' +
    '/video/Aye%20Aye%20%28Original%20Mix%29%20-%20Pogo.webm'

  if not gl
    throw new Error 'WebGL is not supported'

  if not gl.getExtension 'OES_texture_float'
    throw new Error 'Float textures are not supported'

  gl.clearColor 0, 0, 0, 0
  gl.enable gl.DEPTH_TEST

  $.loadFiles 'json/dither.json', 'json/cube.json',
    'shaders/vertex.essl', 'shaders/fragment.essl'

.then ([ dither, cube, vertex, fragment ]) ->
  program = $.makeProgram gl, vertex, fragment

  $.makeTexture gl, program, 0
  $.makeTexture gl, program, 1
  $.makeBuffers gl, program, new Uint16Array(cube.indices),
    'a_position', 3, new Float32Array(cube.vertices),
    'a_normal',   3, new Float32Array(cube.normals),
    'a_texture',  2, new Float32Array(cube.texture)

  u_model      = gl.getUniformLocation program, 'u_model'
  u_view       = gl.getUniformLocation program, 'u_view'
  u_projection = gl.getUniformLocation program, 'u_projection'
  u_viewport   = gl.getUniformLocation program, 'u_viewport'

  blank = do ->
    c = document.createElement('canvas').getContext '2d'
    c.canvas.width = c.canvas.height = 1
    c.fillStyle = '#FFF'
    c.fillRect 0, 0, 1, 1
    c.canvas

  for own name, matrix of dither
    dither[name] = $.makeDitherMatrix matrix

  do render = (T = 0) ->
    requestAnimationFrame render

    W = canvas.clientWidth
    H = canvas.clientHeight
    if W isnt canvas.width or H isnt canvas.height
      gl.viewport 0, 0, canvas.width = W, canvas.height = H

    $.fillTexture gl, program, 0, if video.duration then video else blank
    $.fillTexture gl, program, 1, dither.bayer16, gl.ALPHA

    model = mat4.create()
    mat4.rotateX model, model, T/5e3
    mat4.rotateY model, model, T/6e3
    mat4.rotateZ model, model, T/7e3

    view = mat4.create()
    mat4.lookAt view, [0,0,5], [0,0,0], [0,1,0]

    projection = mat4.create()
    mat4.perspective projection, 45 * Math.RAD, W/H, 1e-3, 1e3
    mat4.scale projection, projection, [1,-1,1]

    gl.uniformMatrix4fv u_model,      false, model
    gl.uniformMatrix4fv u_view,       false, view
    gl.uniformMatrix4fv u_projection, false, projection
    gl.uniform2f u_viewport, W, H

    gl.clear gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT
    gl.drawElements gl.TRIANGLES, 36, gl.UNSIGNED_SHORT, 0

.catch (e) ->
  resize = ->
    w = video.videoWidth
    h = video.videoHeight
    s = Math.max window.innerWidth  / w,
                 window.innerHeight / h

    video.style.width      = "#{ w *= s }px"
    video.style.height     = "#{ h *= s }px"
    video.style.marginLeft = "#{ w / -2 }px"
    video.style.marginTop  = "#{ h / -2 }px"

  video.addEventListener 'canplay', resize
  window.addEventListener 'resize', resize

  document.body.dataset.error = e.message
  document.body.replaceChild video, canvas

  canvas = gl = null

.then ->
  target = canvas or video

  target.addEventListener 'wheel', (e) ->
    if delta = `e.deltaY ? e.deltaY < 0 ? 1 : -1 : 0`
      vol = video.volume; base = 1e3
      vol = Math.log(vol * base) / Math.log(base)
      vol = Math.pow(base, vol + delta * 0.025) / base
      vol = Math.min(Math.max(0, vol), 1)
      video.volume = vol
      e.preventDefault()

  target.addEventListener 'mousedown', (e) ->
    if e.button is 0
      if video.paused then video.play() else video.pause()
      e.preventDefault()

  target.addEventListener 'drop', (e) ->
    if file = e.dataTransfer.files[0]
      URL.revokeObjectURL video.src
      video.src = URL.createObjectURL file
      e.preventDefault()

  target.addEventListener 'dragover', (e) ->
    e.preventDefault()

$ =

  loadFiles: (url) ->
    if arguments.length > 1
      return Promise.all Array::map.
        call arguments, (x) -> $.loadFiles x

    # new Promise (done, fail) ->
    #   x = new XMLHttpRequest
    #   x.onreadystatechange = ->
    #     done @ if @readyState is 4
    #   x.onerror = fail
    #   x.open 'GET', url, true
    #   x.send()
    # .then (x) ->
    #   h = x.getResponseHeader 'Content-Type'
    #   r = x.responseText
    #   if /json/i.test h then JSON.parse r else r

    # workaround for codepen
    new Promise (done, fail) ->
      if e = document.querySelector "[type='#{url}']"
        done e.textContent
      else
        fail new Error "#{url} not found"
    .then (x) ->
      if /json$/i.test url then JSON.parse x else x

  makeDitherMatrix: (matrix) ->
    m = 1 + matrix.reduce (a, b) -> Math.max a, b
    new Float32Array matrix.map (x) -> (0.5 + x) / m

  makeShader: (gl, type, source) ->
    shader = gl.createShader type
    gl.shaderSource shader, source
    gl.compileShader shader
    if not gl.getShaderParameter shader, gl.COMPILE_STATUS
      throw new Error gl.getShaderInfoLog shader
    shader

  makeProgram: (gl, vshader, fshader) ->
    program = gl.createProgram()
    gl.attachShader program, $.makeShader gl, gl.VERTEX_SHADER,   vshader
    gl.attachShader program, $.makeShader gl, gl.FRAGMENT_SHADER, fshader
    gl.linkProgram program
    if not gl.getProgramParameter program, gl.LINK_STATUS
      throw new Error gl.getProgramInfoLog program
    gl.useProgram program
    program

  makeBuffer: (gl, type, data) ->
    buffer = gl.createBuffer()
    gl.bindBuffer type, buffer
    gl.bufferData type, data, gl.STATIC_DRAW
    buffer

  makeBuffers: (gl, program, indices) ->
    for i in [3...arguments.length] by 3
      [ name, size, data ] = Array::slice.call arguments, i, i + 3
      $.makeBuffer gl, gl.ARRAY_BUFFER, data
      location = gl.getAttribLocation program, name
      gl.enableVertexAttribArray location
      gl.vertexAttribPointer location, size, gl.FLOAT, false, 0, 0

    if indices
      $.makeBuffer gl, gl.ELEMENT_ARRAY_BUFFER, indices

  makeTexture: (gl, program, id) ->
    texture = gl.createTexture()
    gl.uniform1i gl.getUniformLocation(program, 'u_sampler' + id), id
    gl.activeTexture gl.TEXTURE0 + id
    gl.bindTexture gl.TEXTURE_2D, texture
    texture

  fillTexture: (gl, program, id, source, type = gl.RGBA, width, height) ->
    gl.activeTexture gl.TEXTURE0 + id

    if not width and source.length and type is gl.ALPHA
      width = height = Math.sqrt source.length

    gl.uniform2f gl.getUniformLocation(program, 'u_size' + id),
      w = width  or source.width  or source.videoWidth,
      h = height or source.height or source.videoHeight or w

    if w&w-1 or h&h-1
      gl.texParameteri gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE
      gl.texParameteri gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE

    if width
      gl.texParameteri gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST
      gl.texParameteri gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST
      gl.texImage2D gl.TEXTURE_2D, 0, type, w, h, 0, type, (if Object::toString
        .call(source)[8...13] is 'Float' then gl.FLOAT else gl.UNSIGNED_BYTE), source
    else
      # gl.pixelStorei gl.UNPACK_FLIP_Y_WEBGL, true
      gl.texParameteri gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR
      gl.texParameteri gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR
      gl.texImage2D gl.TEXTURE_2D, 0, type, type, gl.UNSIGNED_BYTE, source

              
            
!
999px

Console