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

              
                 <script id="shader-fs" type="x-shader/x-fragment">
    // shader program
    precision mediump float;
    uniform vec4 color;
    void main(void) {
      gl_FragColor = color;
    }
  </script>
  
  <script id="shader-vs" type="x-shader/x-vertex">
    // 我們在這裡撰寫 GSGL
    // vec4 代表一個向量,接收四個參數(x,y,z,w)
    // 0,0,0 在 3d 座標中代表中心
    
    attribute vec4 position;
    attribute float size;
    
    void main(void) {
      gl_Position = position;
      gl_PointSize = size;
    }
  </script>
  <canvas width="600" height="600" id="canvas">

              
            
!

CSS

              
                
              
            
!

JS

              
                var POINTS_COUNT = 1000;

function initGL() {
   var canvas = document.querySelector('#canvas');
   var gl = canvas.getContext('webgl');
  
   gl.viewport(0,0, canvas.width, canvas.height);
   gl.clearColor(1,1,1,1);
  
   return gl;
}

// shader
// OpenGL Shader
// GLSL

// vec4 vector 3 x,y,z w
function createShaders(gl, type) {
  var shaderScript = '';
  var shader;

  switch(type) {
    case 'fragment':
      shaderScript = document.querySelector('#shader-fs').textContent;
      shader = gl.createShader(gl.FRAGMENT_SHADER);
      break;
    case 'vertex':
      shaderScript = document.querySelector('#shader-vs').textContent;
      shader = gl.createShader(gl.VERTEX_SHADER);
      break;
  }

  gl.shaderSource(shader, shaderScript);
  gl.compileShader(shader);
  
  return shader;
}

function initShaders(gl) {
  var vertexShader = createShaders(gl, 'vertex');
  var fragmentShader = createShaders(gl, 'fragment');
  
  var shaderProgram = gl.createProgram();
  gl.attachShader(shaderProgram, vertexShader);
  gl.attachShader(shaderProgram, fragmentShader);
  gl.linkProgram(shaderProgram);
  gl.useProgram(shaderProgram);
  
  return shaderProgram;
}

function times(times) {
  var array = [];
  for(var i = 0; i < times; i++) {
    array.push(null);
  }
  
  return array;
}

function createPoints(gl, program) {
  var points = gl.getAttribLocation(program, "position");
  var size = gl.getAttribLocation(program, "size");
  var vertices = [];
  
  
  // 伯朗隨機運動
  // vertices = times(POINTS_COUNT * 2)
  //   .map((val, i) => Math.random() * 2 - 1);
  // 圓形運動
  vertices = times(POINTS_COUNT * 2)
    .map((val, i) => Math.sin(Math.random()) *- 0.01);
  
  
  var buffer = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
  //                 type                    data              usage
  // gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW)
  gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.DYNAMIC_DRAW);
  
  
  // gl.vertexAttrib3f(points, 0.0, 0, 0);
  gl.vertexAttribPointer(points, 2, gl.FLOAT, false, 0 , 0);
  gl.enableVertexAttribArray(points);
  
  gl.vertexAttrib1f(size, 1.0);
  
  var color = gl.getUniformLocation(program, "color");
  gl.uniform4f(color, 1,0,0,1);
  
  return vertices;
}

function draw(gl, vertices) {
  
  for(var i = 0; i < POINTS_COUNT * 2; i += 2) {
    vertices[i] += Math.random() * 0.01 - 0.005;
    vertices[i + 1] += Math.random() * 0.01 - 0.005;
  }
  
  // gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
  gl.bufferSubData(gl.ARRAY_BUFFER, 0, new Float32Array(vertices));
  gl.clear(gl.COLOR_BUFFER_BIT);
  gl.drawArrays(gl.POINTS, 0, POINTS_COUNT);
  
  const bindDraw = (gl, vertices) => () => draw(gl, vertices);
  requestAnimationFrame(bindDraw(gl, vertices));
}

function main() {
  var gl = initGL();
  var shaderProgram = initShaders(gl);
  var vertices = createPoints(gl, shaderProgram);
  draw(gl, vertices);
}

main();
              
            
!
999px

Console