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

              
                <canvas height="400" id="c" width="800"></canvas>
              
            
!

CSS

              
                
              
            
!

JS

              
                const canvas = document.getElementById('c');
const height = canvas.height;
const width = canvas.width;
// Get GL drawing context
const gl = canvas.getContext('webgl');
gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
const buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
const vertexCount = 6;
const vertexLocations = [
  // X, Y
  -1.0, -1.0,
   1.0, -1.0,
  -1.0,  1.0,
  -1.0,  1.0,
   1.0, -1.0,
   1.0,  1.0
];

gl.bufferData(
  gl.ARRAY_BUFFER,
  new Float32Array(vertexLocations),
  gl.STATIC_DRAW
);

const program = gl.createProgram();
const buildShader = (type, source) => {
  const shader = gl.createShader(type);
  gl.shaderSource(shader, source);
  gl.compileShader(shader);
  gl.attachShader(program, shader);
  return shader;
};

const vertexShader = buildShader(
  gl.VERTEX_SHADER,
  `
attribute vec2 a_position;
void main() {
gl_Position = vec4(a_position, 0.0, 1.0);
}`
);

const fragmentShader = buildShader(
  gl.FRAGMENT_SHADER,
  `
#define M_RAD ${Math.PI * 2}
#define CYCLE_WIDTH 100.0
#define BLADES 3.0
#define BLADES_T_CYCLE_WIDTH 300.0
// Mobiles need this
precision highp float;
uniform float timestamp;
void main() {
  vec2 center = vec2(${width / 2}, ${height / 2});
  float maxDistance = length(vec2(center.x, center.y));
  float scaledTimestamp = floor(timestamp / 10.0 + 2000.0);
  vec2 d = center.xy - gl_FragCoord.xy;
  vec2 dsq = pow(d, vec2(2.0));
  // Flipped axis to counteract flipped Y
  float angle = atan(d.y, d.x) / M_RAD;
  // Arbitrary mangle of the distance, just something that looks pleasant
  float asbs = dsq.x + dsq.y;
  float distanceFromCenter = sqrt(asbs);
  float scaledDistance = (asbs / 400.0) + distanceFromCenter;
  float lerp = 1.0 - mod(abs(scaledDistance - scaledTimestamp + angle * BLADES_T_CYCLE_WIDTH), CYCLE_WIDTH) / CYCLE_WIDTH;
  // Fade R more slowly
  float absoluteDistanceRatioGB = (1.0 - distanceFromCenter / maxDistance);
  float absoluteDistanceRatioR = absoluteDistanceRatioGB * 0.8 + 0.2;

  float fadeB = (50.0 / 255.0) * lerp * absoluteDistanceRatioGB;
  float fadeR = (240.0 / 255.0) * lerp * absoluteDistanceRatioR * (1.0 + lerp) / 2.0;
  float fadeG = (120.0 / 255.0) * lerp * lerp * lerp * absoluteDistanceRatioGB;
  gl_FragColor = vec4(fadeR, fadeG, fadeB, 1.0);
}`
);

gl.linkProgram(program);
gl.useProgram(program);
// Detach and delete shaders as they're no longer needed
gl.detachShader(program, vertexShader);
gl.detachShader(program, fragmentShader);
gl.deleteShader(vertexShader);
gl.deleteShader(fragmentShader);
// Add attribute pointer to our vertex locations
const positionLocation = gl.getAttribLocation(program, 'a_position');
gl.enableVertexAttribArray(positionLocation);
const fieldCount = vertexLocations.length / vertexCount;
gl.vertexAttribPointer(
  positionLocation,
  fieldCount,
  gl.FLOAT,
  gl.FALSE,
  fieldCount * Float32Array.BYTES_PER_ELEMENT,
  0
);

const timestampId = gl.getUniformLocation(program, 'timestamp');

const render = (timestamp) => {
  // Update timestamp
  gl.uniform1f(timestampId, timestamp);
  // Draw
  gl.drawArrays(gl.TRIANGLES, 0, vertexCount);
  window.requestAnimationFrame(render);
};

window.requestAnimationFrame(render);

              
            
!
999px

Console