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

              
                <canvas id='canvas'></canvas>


<script id='vertex-shader' type='x-shader/x-vertex'>
    precision highp float;

    attribute vec2 a_position;

    void main() {
        gl_Position = vec4(a_position, 0.0, 1.0);
    }
</script>


<script id='fragment-shader' type='x-shader/x-fragment'>
    precision highp float;
    
    #define GREEN vec4(0.4, 0.8, 0.5, 1.0);
    #define WHITE vec4(1.0, 1.0, 1.0, 1.0);
    #define BLACK vec4(0.0, 0.0, 0.0, 1.0);

    uniform float u_canvas_height;
    uniform float u_canvas_width;
    uniform float u_time;
    uniform float u_mouse_x;
    uniform float u_mouse_y;
    
    
    void draw_texture();

    
    void main() {
        draw_texture();
    }

    
    void draw_texture() {
        gl_FragColor = GREEN;
        
        vec2 uv = vec2(
            gl_FragCoord.x / u_canvas_width,
            gl_FragCoord.y / u_canvas_height
        );
        
        float dx = abs(u_mouse_x - gl_FragCoord.x);
        float dy = abs(u_canvas_height - gl_FragCoord.y - u_mouse_y);
        float r2 = dx * dx + dy * dy;
        
        bool is_in_cursor = (r2 < 200.0);
        
        if (is_in_cursor) {
            gl_FragColor = BLACK;
            return;
        }
        
        float sin_1 = sin(3.0 * uv.y + 2.0 * u_time);
        float sin_2 = sin(5.0 * uv.y + 3.0 * u_time);
        float modifier = 0.5 * r2 / min(u_canvas_height, u_canvas_width);
        float translate_x = 0.1;
        
        float line_path_x = (sin_1 + sin_2) / modifier + translate_x;
                
        for (int line_number = 0; line_number < 9; line_number++) {
            bool is_in_line =
                   (uv.x > line_path_x - 0.02)
                && (uv.x < line_path_x + 0.02);
            
            if (is_in_line) {
                gl_FragColor = WHITE;
            }
            
            line_path_x += translate_x;
        }
    }

</script>

              
            
!

CSS

              
                * {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    overflow: hidden;
    cursor: none;
}

              
            
!

JS

              
                // Template pen without animations: https://codepen.io/sfi0zy/pen/zYoxdbw

const UNIFORMS = Object.freeze({
    canvas_height:  'u_canvas_height',
    canvas_width:   'u_canvas_width',
    time:           'u_time',
    mouse_x:        'u_mouse_x',
    mouse_y:        'u_mouse_y'
});


const DEFAULT_UNIFORM_VALUES = Object.freeze({
    u_canvas_height: 1,
    u_canvas_width:  1,
    u_time:          0,
    u_mouse_x:       window.innerWidth / 2,
    u_mouse_y:       window.innerHeight / 2
});


class Example {
    constructor(options) {
        this.options = options;
        this.canvas = document.getElementById(options.canvas);
        this.gl = this.canvas.getContext('webgl');
        this.program = null;
        this.uniforms = {};

        this.clearCanvas();
        this.createPlane();
        this.createProgram();
        
        for (const uniform in UNIFORMS) {
            this.setUniform(UNIFORMS[uniform],
                DEFAULT_UNIFORM_VALUES[UNIFORMS[uniform]]);
        }
        
        this.onResize();
        this.initEventListeners();
        this.draw();
    }


    clearCanvas() {
        this.gl.clearColor(0.0, 0.0, 0.0, 1.0);
        this.gl.clear(this.gl.COLOR_BUFFER_BIT);
    }


    createPlane() {
        this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.gl.createBuffer());
        this.gl.bufferData(
            this.gl.ARRAY_BUFFER,
            new Float32Array([
                -1, -1,
                -1,  1,
                1, -1,
                1,  1
            ]),
            this.gl.STATIC_DRAW
        );
    }


    createProgram() {
        const shaders = this.getShaders();

        this.program = this.gl.createProgram();

        this.gl.attachShader(this.program, shaders.vertex);
        this.gl.attachShader(this.program, shaders.fragment);
        this.gl.linkProgram(this.program);

        const vertexPositionAttribute = this.gl.getAttribLocation(this.program, 'a_position');

        this.gl.enableVertexAttribArray(vertexPositionAttribute);
        this.gl.vertexAttribPointer(vertexPositionAttribute, 2, this.gl.FLOAT, false, 0, 0);

        this.gl.useProgram(this.program);
    }


    getShaders() {
        return {
            vertex: this.compileShader(
                this.gl.VERTEX_SHADER,
                document.getElementById(this.options.shaders.vertex).textContent
            ),
            fragment: this.compileShader(
                this.gl.FRAGMENT_SHADER,
                document.getElementById(this.options.shaders.fragment).textContent
            )
        };
    }


    compileShader(type, source) {
        const shader = this.gl.createShader(type);

        this.gl.shaderSource(shader, source);
        this.gl.compileShader(shader);

        console.log(this.gl.getShaderInfoLog(shader));

        return shader;
    }


    setUniform(name, value) {
        this.gl.uniform1f(this.gl.getUniformLocation(this.program, name), value);

        this.uniforms[name] = value;
    }


    onResize() {
        this.canvas.height = window.innerHeight;
        this.canvas.width = window.innerWidth;

        this.gl.viewport(0, 0, this.gl.canvas.width, this.gl.canvas.height);

        this.setUniform(UNIFORMS.canvas_height, this.canvas.height);
        this.setUniform(UNIFORMS.canvas_width,  this.canvas.width);
    }
    

    onMouseMove(e) {  
        this.setUniform(UNIFORMS.mouse_x, e.clientX);
        this.setUniform(UNIFORMS.mouse_y, e.clientY);
    }


    initEventListeners() {
        window.addEventListener('resize', this.onResize.bind(this));

        document.addEventListener('mousemove', this.onMouseMove.bind(this));
    }


    draw(timeStamp) {
        this.setUniform(UNIFORMS.time, timeStamp / 1000.0);

        this.gl.drawArrays(this.gl.TRIANGLE_STRIP, 0, 4);

        requestAnimationFrame(this.draw.bind(this));
    }
}


const example = new Example({
    canvas: 'canvas',
    shaders: {
        vertex: 'vertex-shader',
        fragment: 'fragment-shader'
    }
});

              
            
!
999px

Console