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>Swipe to see animation</p>
<canvas id="web-gl" width="300" height="300"></canvas>
<script id="vert-shader" type="x-shader/x-vertex">
attribute vec2 aVertexPosition;

uniform mat2 scaleMatrix;
uniform mat2 rotationMatrix;

varying vec4 pixelPosition;

void main() {
  // gl_Position is a special variable a vertex shader is responsible for setting
  gl_Position = vec4(scaleMatrix * rotationMatrix * aVertexPosition, 0.0, 1.0);
  // interpolated pixel position
  pixelPosition = vec4(aVertexPosition, 0.0, 1.0);
}
</script>
<script id="frag-shader" type="x-shader/x-fragment">
// Set the default precision
precision highp float;
uniform mat2 rotationMatrix;
varying vec4 pixelPosition;
void main() {
    vec3 color = vec3((pixelPosition.x + 1.0) / 2.0, 0.0, 0.5);
    gl_FragColor = vec4(color, 1.0);
}
</script>
              
            
!

CSS

              
                body, html {
	display: flex;
	flex-direction: column;
	justify-content: center;
	height: 100%;
	align-items: center;
	font-family: Arial, sans-serif;
	color: #ddd;
	text-transform: uppercase;
}

canvas {
    border-radius: 30px;
    box-shadow: 0px 20px 30px -10px #00000047;
}
              
            
!

JS

              
                let canvas;
let gl;
let program;
let vertexBuffer;
let vertexCount;
let scale = 0.5;
let theta = 0;
const vertexNumComponents = 2;

// Load Shaders
let vertexSource = document.getElementById("vert-shader").textContent
let fragmentSource = document.getElementById("frag-shader").textContent

setup(vertexSource, fragmentSource);
run()

// Setup WebGl environment
function setup(vertexSource, fragmentSource) {

    // Initialize canvas and webgl context
    canvas = document.getElementById("web-gl");
    gl = canvas.getContext("webgl");

    // Compile both shaders
    const vertexShader = gl.createShader(gl.VERTEX_SHADER);
    gl.shaderSource(vertexShader, vertexSource);
    gl.compileShader(vertexShader);
    if (!gl.getShaderParameter(vertexShader, gl.COMPILE_STATUS))
        console.log(gl.getShaderInfoLog(vertexShader)); // Log the error if GLSL fails to compile

    const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
    gl.shaderSource(fragmentShader, fragmentSource);
    gl.compileShader(fragmentShader);
    if (!gl.getShaderParameter(fragmentShader, gl.COMPILE_STATUS))
        console.log(gl.getShaderInfoLog(fragmentShader)); // Log the error if GLSL fails to compile

    // Create WebGL Program
    program = gl.createProgram();
    gl.attachShader(program, vertexShader);
    gl.attachShader(program, fragmentShader);
    gl.linkProgram(program);
    if (!gl.getProgramParameter(program, gl.LINK_STATUS))
        console.log(gl.getProgramInfoLog(program));

    // Create shape
    const vertices = [
        -1.0, -1.0,
         1.0, -1.0,
         1.0,  1.0,
        -1.0, -1.0,
         1.0,  1.0,
        -1.0,  1.0,
    ]
    vertexCount = vertices.length / vertexNumComponents;

    vertexBuffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW); // set float32 if doesnt work

  // Set rotation based on mouse position
  const setRot = e => theta = 2 * 3.145 * e.pageX / innerWidth;
  const setRotTouch = e => theta = 2 * 3.145 * e.touches[0].pageX / innerWidth;
  document.addEventListener("mousemove", setRot);
  document.addEventListener("touchmove", setRotTouch);
}

function run() {
    gl.viewport(0, 0, canvas.width, canvas.height);
    gl.clearColor(0.8, 0.9, 1.0, 1.0);
    gl.clear(gl.COLOR_BUFFER_BIT);

    gl.useProgram(program);

    gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);

    // Tranformation Matrices
    const S = [scale, 0, 0, scale]
    const R = [Math.cos(theta), -Math.sin(theta), Math.sin(theta), Math.cos(theta)]
    gl.uniformMatrix2fv( gl.getUniformLocation(program, "scaleMatrix"), false, S );
    gl.uniformMatrix2fv( gl.getUniformLocation(program, "rotationMatrix"), false, R );
    
    // Load square to Vertex Shader (aka Draw)
    const aVertexPosition = gl.getAttribLocation(program, "aVertexPosition");
    gl.enableVertexAttribArray(aVertexPosition);
    gl.vertexAttribPointer(aVertexPosition, vertexNumComponents, gl.FLOAT, false, 0, 0);
    gl.drawArrays(gl.TRIANGLES, 0, vertexCount);

    requestAnimationFrame(run);
}
              
            
!
999px

Console