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 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, 1);
    }
</script>


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


    #define ITERATIONS 512.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;
    uniform float u_camera_x;
    uniform float u_camera_x_min;
    uniform float u_camera_x_max;
    uniform float u_camera_y;
    uniform float u_camera_y_min;
    uniform float u_camera_y_max;
    uniform float u_zoom;
    uniform float u_zoom_min;
    uniform float u_zoom_max;


    float formula(float, float);    
    vec3 hsv2rgb(vec3);


    void main() {
        float y0 = (((u_canvas_height / 2.0) - gl_FragCoord.y) / u_zoom) + u_camera_y;
        float x0 = ((gl_FragCoord.x - (u_canvas_width / 2.0)) / u_zoom) + u_camera_x;

        float result = formula(x0, y0) / ITERATIONS;
        vec3 color = vec3(0.0, 0.0, 0.0);
        
        if (result > 0.0) {
            color = hsv2rgb(vec3(result, 1.0, 1.0));
        }
        
        gl_FragColor = vec4(color, 1.0);
    }

    
    vec3 hsv2rgb(vec3 c) {
        vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
        vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
        
        return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
    }
    

    float formula(float x0, float y0) {
        float x1 = x0;
        float y1 = y0;

        for (float i = 0.0; i < ITERATIONS; i++) {
            float x2 = x1 * x1;
            float y2 = y1 * y1;

            if (4.02 < x2 + y2) {
                return i;
            }

            float xNew = x2 - y2 + x0;
            float yNew = 2.0 * x1 * y1 + y0;

            x1 = xNew;
            y1 = yNew;
        }

        return -1.0;
    }

</script>

              
            
!

CSS

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

body {
    overflow: hidden;
}

              
            
!

JS

              
                // Technically, most of these uniforms are not required here.
// It's just a part of another demo with replaced shaders.

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',
    camera_x:       'u_camera_x',
    camera_x_min:   'u_camera_x_min',
    camera_x_max:   'u_camera_x_max',
    camera_y:       'u_camera_y',
    camera_y_min:   'u_camera_y_min',
    camera_y_max:   'u_camera_y_max',
    zoom:           'u_zoom',
    zoom_min:       'u_zoom_min',
    zoom_max:       'u_zoom_max',
});


const DEFAULT_UNIFORM_VALUES = Object.freeze({
    u_canvas_height: 1,
    u_canvas_width:  1,
    u_time:          0,
    u_mouse_x:       0,
    u_mouse_y:       0,
    u_camera_x:      0,
    u_camera_x_max:  3,
    u_camera_x_min: -3,
    u_camera_y:      0,
    u_camera_y_max:  1.7,
    u_camera_y_min: -1.7,
    u_zoom:          200,
    u_zoom_min:      200,
    u_zoom_max:      1000000
});


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.isMouseDrags = false;

        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.26, 1, 0.93, 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);
    }


    onMouseDown(e) {
        this.setUniform(UNIFORMS.mouse_x, e.clientX);
        this.setUniform(UNIFORMS.mouse_y, e.clientY);
        this.isMouseDrags = true;
    }


    onMouseUp() {
        this.isMouseDrags = false;
    }


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

        let newCameraX = this.uniforms[UNIFORMS.camera_x]
            + (this.uniforms[UNIFORMS.mouse_x] - e.clientX)
                / this.uniforms[UNIFORMS.zoom];
        let newCameraY = this.uniforms[UNIFORMS.camera_y]
            + (this.uniforms[UNIFORMS.mouse_y] - e.clientY)
                / this.uniforms[UNIFORMS.zoom];

        if (newCameraX > DEFAULT_UNIFORM_VALUES[UNIFORMS.camera_x_max]) {
            newCameraX = DEFAULT_UNIFORM_VALUES[UNIFORMS.camera_x_max];
        }

        if (newCameraX < DEFAULT_UNIFORM_VALUES[UNIFORMS.camera_x_min]) {
            newCameraX = DEFAULT_UNIFORM_VALUES[UNIFORMS.camera_x_min];
        }

        if (newCameraY > DEFAULT_UNIFORM_VALUES[UNIFORMS.camera_y_max]) {
            newCameraY = DEFAULT_UNIFORM_VALUES[UNIFORMS.camera_y_max];
        }

        if (newCameraY < DEFAULT_UNIFORM_VALUES[UNIFORMS.camera_y_min]) {
            newCameraY = DEFAULT_UNIFORM_VALUES[UNIFORMS.camera_y_min];
        }

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

        this.setUniform(UNIFORMS.camera_x, newCameraX);
        this.setUniform(UNIFORMS.camera_y, newCameraY);
    }


    onMouseWheel(e) {
        let delta = e.deltaY;

        if (Math.abs(delta) < 10) {
            delta *= 10
        }

        let newZoom = this.uniforms[UNIFORMS.zoom]
            + (delta * this.uniforms[UNIFORMS.zoom])
                / DEFAULT_UNIFORM_VALUES[UNIFORMS.zoom];

        if (delta > 0) {
            if (newZoom > DEFAULT_UNIFORM_VALUES[UNIFORMS.zoom_max]) {
                newZoom = DEFAULT_UNIFORM_VALUES[UNIFORMS.zoom_max];
            }
        } else {
            if (newZoom < DEFAULT_UNIFORM_VALUES[UNIFORMS.zoom_min]) {
                newZoom = DEFAULT_UNIFORM_VALUES[UNIFORMS.zoom_min];
            }
        }

        this.setUniform(UNIFORMS.zoom, newZoom);
    }


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

        document.addEventListener('mousedown', this.onMouseDown.bind(this));
        document.addEventListener('mousemove', this.onMouseMove.bind(this));
        document.addEventListener('mouseup',   this.onMouseUp.bind(this));
        document.addEventListener('wheel',     this.onMouseWheel.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