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

              
                

          
<html>
  <head>      
    </script>
  </head>
  <body>
      <div id="div"></div>
  </body>
</html>

        


              
            
!

CSS

              
                div {
  background: #ccc;
  height: 300px;
  width: 300px;
}
body {
  margin: 0px;
}
              
            
!

JS

              
                /*
NJ Namju Lee
Email: nj.namju@gmail.com
Website: www.njstudio.co.kr
Linkedin: https://www.linkedin.com/in/nj-namju-lee-926b3252/

Materials(Eng)
youtube(eng): https://www.youtube.com/channel/UCP4q3a4ogJN1-SbJclJR3Ww
Medium: https://medium.com/@nj.namju

Materials(KR)
youtube(kr): https://www.youtube.com/channel/UC3Z42uoe9C7Et39h5cLK1dw?view_as=subscriber
Daum Brunch: https://brunch.co.kr/@njnamju
  Data & Design Computaional Design : https://brunch.co.kr/@njnamju/64
Naver Blog: https://blog.naver.com/designju
*/

let startTime = new Date().getTime(); // Get start time for animating


const div = document.getElementById('div');
const canvas = document.createElement('canvas');
canvas.width = 300;
canvas.height = 300;
div.append(canvas)
// const gl: WebGL2RenderingContext = canvas.getContext('webgl2');
const gl = canvas.getContext('webgl2');

////////////////////
const createShader = (gl: WebGL2RenderingContext, type: WebGLShader, shaderString: string) => {
    // console.log(type);
    const shader = gl.createShader(type as unknown as number);
    if (shader === null) { return null; }
    gl.shaderSource(shader, shaderString);
    gl.compileShader(shader);
    const suc = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
    if (suc) { return shader; }
    console.log(gl.getShaderInfoLog(shader));
    gl.deleteShader(shader);
    return null;
}
const createProgram = (gl: WebGL2RenderingContext, vertexShader: WebGLShader, fragmentShader: WebGLShader, needValidate: boolean = true): WebGLProgram | null => {
    const program = gl.createProgram();
    if (program === null) { return null; }
    gl.attachShader(program, vertexShader);
    gl.attachShader(program, fragmentShader);
    gl.linkProgram(program);

    // Check that shader program was able to link to WebGL
    if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
        const error = gl.getProgramInfoLog(program);
        console.log('Failed to link program: ' + error);
        gl.deleteProgram(program);
        gl.deleteShader(fragmentShader);
        gl.deleteShader(vertexShader);
        return null;
    }
    if (needValidate) {
        gl.validateProgram(program);
        if (!gl.getProgramParameter(program, gl.VALIDATE_STATUS)) {
            console.error('Failed to validate program', gl.getProgramInfoLog(program));
            gl.deleteProgram(program);
            gl.deleteShader(fragmentShader);
            gl.deleteShader(vertexShader);
            return null;
        }
    }
    gl.deleteShader(fragmentShader);
    gl.deleteShader(vertexShader);
    return program;
}
class Quad {
    v = `#version 300 es
        in vec3 a_position;

        uniform vec2 uMouseLoc;

        out vec3 pos;
        out vec2 mPos;

        void main(void) {
            float d = distance(uMouseLoc, vec2(a_position.xy)) * 0.005;
            // color = vec3(d, 1.0 - d, 0);
            
            gl_Position = vec4(a_position, 1.0);
            
            pos = a_position;
            mPos = uMouseLoc;
        }
    `;
    f = `#version 300 es
        precision mediump float;

        in vec3 pos;
        in vec2 mPos;
        out vec4 finalColor;

        void main(void) {
            float d = distance(vec2(pos.xy), mPos); 
            
            if (d > 1.0) {
              discard; 
            }
            
            finalColor = vec4(1. - d, 0, 0.0, 1.0 - d);

        }
    `;

    gl: WebGL2RenderingContext;

    vShader: WebGLShader;
    fShader: WebGLShader;

    program: WebGLProgram;

    aPositionLoc: number;
    uMouseLoc: WebGLUniformLocation | null;

    size: number = 100;
    time: number = 0.0;

    m: number[] = [0, 0];

    constructor(gl: WebGL2RenderingContext) {
        this.gl = gl;
        this.initShader(gl);
    }
    public initShader(gl: WebGL2RenderingContext) {
        let vShader = createShader(gl, gl.VERTEX_SHADER as unknown as WebGLShader, this.v);
        let fShader = createShader(gl, gl.FRAGMENT_SHADER as unknown as WebGLShader, this.f);
        if (vShader && fShader) {
            this.vShader = vShader;
            this.fShader = fShader;
            let program = createProgram(gl, this.vShader, this.fShader, true);

            if (program) {
                this.program = program;
                gl.useProgram(this.program);
                this.aPositionLoc = gl.getAttribLocation(this.program, 'a_position');
                this.uMouseLoc = gl.getUniformLocation(this.program, 'uMouseLoc');
                gl.useProgram(null);

                this.initGeo(gl);

            }
        } else {
            console.error('vertex fragment shader error');
        }
    }
    public initGeo(gl: WebGL2RenderingContext) {
        let aryVerts = new Float32Array([
            -1, 1, 0,
            1, -1, 0,
            -1, -1, 0,

            1, -1, 0,
            -1, 1, 0,
            1, 1, 0,
        ]);
        let bufVerts = gl.createBuffer();

        gl.bindBuffer(gl.ARRAY_BUFFER, bufVerts);
        gl.bufferData(gl.ARRAY_BUFFER, aryVerts, gl.STATIC_DRAW);
        gl.bindBuffer(gl.ARRAY_BUFFER, null);

        gl.useProgram(this.program);
        gl.uniform2f(this.uMouseLoc, 10, 10);

        // how its down without VAOs
        gl.bindBuffer(gl.ARRAY_BUFFER, bufVerts);
        gl.enableVertexAttribArray(this.aPositionLoc);
        gl.vertexAttribPointer(this.aPositionLoc, 3, gl.FLOAT, false, 0, 0);
        gl.bindBuffer(gl.ARRAY_BUFFER, null);

    }
    public Render(gl: WebGL2RenderingContext) {
        gl.uniform2f(this.uMouseLoc, this.m[0], this.m[1]);
        // gl.uniform2f(this.uMouseLoc, 15 + (this.size * (Math.cos(this.time) + 1)));
        gl.drawArrays(gl.TRIANGLE_STRIP, 0, 6);
        this.time += 0.1;
    }
}

////////////////////

const mesh = new Quad(gl);

const animate = () => {
  requestAnimationFrame( animate );
  // // update
  let now = new Date().getTime();
  let currentTime = ( now - startTime ) / 1000;
  // console.log(currentTime);
  // uTime.set( currentTime );
  // uMouse.set( mouseX, mouseY );
  // // render
  // billboard.render( gl );
  // // animate next frame
  mesh.Render(gl);
}
animate()


canvas.addEventListener( 'mousemove', ( event ) => {
  // console.log(canvas)
  let mouseX = ((event.clientX / +canvas.width ) * 2 ) -1 ;
  let mouseY = ((canvas.height - event.clientY) /+canvas.height * 2) - 1;
  // console.log(mouseX, mouseY)
  mesh.m[0] = mouseX;
  mesh.m[1] = mouseY;
});

              
            
!
999px

Console