Convert Figma logo to code with AI

KhronosGroup logoWebGL

The Official Khronos WebGL Repository

2,627
667
2,627
228

Top Related Projects

101,622

JavaScript 3D Library.

Babylon.js is a powerful, beautiful, simple, and open game and rendering engine packed into a friendly JavaScript framework.

43,513

The HTML5 Creation Engine: Create beautiful digital content with the fastest, most flexible 2D WebGL renderer.

5,213

👑 Functional WebGL

15,084

GPU Accelerated JavaScript

9,523

JavaScript game engine built on WebGL, WebGPU, WebXR and glTF

Quick Overview

WebGL (Web Graphics Library) is a JavaScript API for rendering interactive 2D and 3D graphics within any compatible web browser without the use of plug-ins. It is maintained by the Khronos Group and is based on OpenGL ES 2.0, providing similar rendering functionality in an HTML5 canvas element.

Pros

  • Cross-platform compatibility, allowing for high-performance graphics on various devices and browsers
  • Hardware-accelerated rendering, leveraging the power of the GPU for improved performance
  • Seamless integration with web technologies, enabling rich graphical experiences directly in web applications
  • Large and active community, providing extensive resources, libraries, and tools

Cons

  • Steep learning curve, especially for developers not familiar with graphics programming
  • Limited built-in functionality compared to higher-level graphics libraries, requiring more low-level code
  • Potential performance issues on older or less capable devices
  • Inconsistent implementation across different browsers and devices, leading to compatibility challenges

Code Examples

  1. Creating a WebGL context:
const canvas = document.getElementById('myCanvas');
const gl = canvas.getContext('webgl');

if (!gl) {
  console.error('WebGL not supported');
}
  1. Clearing the canvas with a color:
gl.clearColor(0.0, 0.0, 0.0, 1.0);  // Set clear color (black)
gl.clear(gl.COLOR_BUFFER_BIT);      // Clear the color buffer
  1. Drawing a simple triangle:
const vertexShaderSource = `
  attribute vec4 a_position;
  void main() {
    gl_Position = a_position;
  }
`;

const fragmentShaderSource = `
  precision mediump float;
  void main() {
    gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);  // Red color
  }
`;

// Compile shaders and create program (omitted for brevity)

const positions = new Float32Array([
  0.0,  0.5,
 -0.5, -0.5,
  0.5, -0.5
]);

const positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STATIC_DRAW);

gl.useProgram(program);
gl.enableVertexAttribArray(positionAttributeLocation);
gl.vertexAttribPointer(positionAttributeLocation, 2, gl.FLOAT, false, 0, 0);

gl.drawArrays(gl.TRIANGLES, 0, 3);

Getting Started

To get started with WebGL:

  1. Include a canvas element in your HTML:

    <canvas id="myCanvas" width="800" height="600"></canvas>
    
  2. Initialize WebGL context in your JavaScript:

    const canvas = document.getElementById('myCanvas');
    const gl = canvas.getContext('webgl');
    
    if (!gl) {
      console.error('WebGL not supported');
      return;
    }
    
    // Your WebGL code here
    
  3. Start with simple shapes and gradually build up complexity as you learn more about shaders, buffers, and the WebGL rendering pipeline.

Competitor Comparisons

101,622

JavaScript 3D Library.

Pros of three.js

  • Higher-level abstraction, making 3D graphics more accessible
  • Rich set of built-in geometries, materials, and effects
  • Active community with frequent updates and extensive documentation

Cons of three.js

  • Larger file size and potential performance overhead
  • Less flexibility for low-level GPU operations
  • May abstract away some WebGL concepts, limiting fine-grained control

Code Comparison

WebGL (raw):

const gl = canvas.getContext('webgl');
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
// ... more low-level code for shaders, buffers, etc.

three.js:

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

Summary

WebGL provides low-level access to GPU capabilities, offering maximum flexibility and performance for experienced developers. three.js, built on top of WebGL, simplifies 3D graphics programming with a higher-level API, making it more accessible for rapid development and prototyping. While three.js sacrifices some low-level control, it compensates with a rich feature set and easier learning curve for 3D web graphics.

Babylon.js is a powerful, beautiful, simple, and open game and rendering engine packed into a friendly JavaScript framework.

Pros of Babylon.js

  • Higher-level 3D engine with built-in features for scene management, physics, and animations
  • Extensive documentation and active community support
  • Cross-platform compatibility, including WebGL, WebGPU, and native platforms

Cons of Babylon.js

  • Larger file size and potential performance overhead compared to raw WebGL
  • Steeper learning curve for developers familiar with low-level graphics programming
  • Less flexibility for custom rendering pipelines and shader implementations

Code Comparison

WebGL (raw):

const gl = canvas.getContext('webgl');
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
// Additional setup and rendering code required

Babylon.js:

const engine = new BABYLON.Engine(canvas);
const scene = new BABYLON.Scene(engine);
const camera = new BABYLON.FreeCamera("camera", new BABYLON.Vector3(0, 0, -10), scene);
const light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(0, 1, 0), scene);
engine.runRenderLoop(() => scene.render());

The WebGL repository provides low-level access to graphics programming, offering maximum flexibility and control. Babylon.js, on the other hand, abstracts much of the complexity, providing a more accessible and feature-rich environment for 3D development at the cost of some performance and customization options.

43,513

The HTML5 Creation Engine: Create beautiful digital content with the fastest, most flexible 2D WebGL renderer.

Pros of PixiJS

  • Higher-level abstraction, easier to use for 2D rendering and interactive graphics
  • Rich set of built-in features for sprite manipulation, animations, and UI elements
  • Extensive documentation and active community support

Cons of PixiJS

  • Less flexible for low-level graphics programming compared to WebGL
  • Potentially higher performance overhead due to abstraction layer
  • Limited to 2D graphics, not suitable for complex 3D rendering

Code Comparison

WebGL (raw):

const gl = canvas.getContext('webgl');
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
// Additional setup and drawing code required

PixiJS:

const app = new PIXI.Application();
document.body.appendChild(app.view);
const sprite = PIXI.Sprite.from('image.png');
app.stage.addChild(sprite);

PixiJS provides a more straightforward API for creating and manipulating graphics, while WebGL offers lower-level control at the cost of increased complexity. PixiJS is ideal for 2D games and interactive visualizations, whereas WebGL is better suited for custom rendering pipelines and 3D graphics. The choice between the two depends on the specific requirements of your project and the level of control you need over the rendering process.

5,213

👑 Functional WebGL

Pros of regl

  • Higher-level abstraction, simplifying WebGL programming
  • Functional approach, promoting cleaner and more maintainable code
  • Automatic resource management, reducing boilerplate code

Cons of regl

  • Less fine-grained control over WebGL operations
  • Potential performance overhead due to abstraction layer
  • Smaller community and ecosystem compared to raw WebGL

Code Comparison

WebGL:

const gl = canvas.getContext('webgl');
const program = gl.createProgram();
// ... (shader compilation and linking)
gl.useProgram(program);
gl.drawArrays(gl.TRIANGLES, 0, 3);

regl:

const regl = require('regl')();
const drawTriangle = regl({
  frag: '...',
  vert: '...',
  count: 3
});
drawTriangle();

regl provides a more concise and declarative approach to WebGL programming, abstracting away many low-level details. However, WebGL offers more direct control over the rendering pipeline, which may be necessary for complex or performance-critical applications. The choice between the two depends on the specific requirements of the project and the developer's familiarity with WebGL concepts.

15,084

GPU Accelerated JavaScript

Pros of gpu.js

  • Higher-level abstraction, easier to use for GPU computing
  • Cross-platform compatibility (WebGL, Node.js, and browser)
  • Automatic fallback to CPU if GPU is unavailable

Cons of gpu.js

  • Less flexible than direct WebGL for complex graphics tasks
  • May have performance overhead for simple operations
  • Limited to specific GPU computing tasks, not a full graphics API

Code Comparison

WebGL (low-level):

const gl = canvas.getContext('webgl');
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
// Additional setup and drawing code required

gpu.js (high-level):

const gpu = new GPU();
const multiplyMatrix = gpu.createKernel(function(a, b) {
  let sum = 0;
  for (let i = 0; i < 512; i++) {
    sum += a[this.thread.y][i] * b[i][this.thread.x];
  }
  return sum;
}).setOutput([512, 512]);

WebGL is a low-level graphics API providing direct access to GPU functionality, suitable for complex graphics rendering and general-purpose GPU computing. It requires more setup and understanding of graphics programming concepts.

gpu.js is a JavaScript library for GPU-accelerated computing in the browser. It provides a higher-level abstraction, making it easier to perform parallel computations on the GPU without dealing with the intricacies of WebGL. However, it's primarily focused on computational tasks rather than graphics rendering.

9,523

JavaScript game engine built on WebGL, WebGPU, WebXR and glTF

Pros of PlayCanvas engine

  • Higher-level abstraction, providing a complete game engine framework
  • Extensive documentation and examples for rapid development
  • Built-in asset pipeline and scene editor for streamlined workflow

Cons of PlayCanvas engine

  • Less flexibility for low-level graphics programming
  • Potentially larger file size and overhead compared to raw WebGL
  • Steeper learning curve for developers familiar with vanilla WebGL

Code comparison

PlayCanvas engine:

const canvas = document.getElementById('application-canvas');
const app = new pc.Application(canvas);

// Create a box entity
const box = new pc.Entity('cube');
box.addComponent('render', {
    type: 'box'
});
app.root.addChild(box);

WebGL:

const canvas = document.getElementById('glcanvas');
const gl = canvas.getContext('webgl');

const vertexShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vertexShader, vertexShaderSource);
gl.compileShader(vertexShader);

PlayCanvas engine provides a higher-level API for creating 3D objects and scenes, while WebGL requires more low-level code for setting up shaders and rendering primitives. PlayCanvas abstracts away much of the complexity, making it easier for developers to create 3D applications quickly, but at the cost of some flexibility and control over the underlying graphics operations.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

The Official Khronos WebGL Repository

This is the official home of the Khronos WebGL repository for the WebGL specifications and the WebGL conformance test suite.

Before adding a new test or editing an existing test please read these guidelines.

You can find live versions of the specifications at https://www.khronos.org/webgl/

The newest work in progress WebGL conformance test suite for the next version of the spec can be accessed at. https://www.khronos.org/registry/webgl/sdk/tests/webgl-conformance-tests.html

Official live versions of the conformance test suite can be found at https://www.khronos.org/registry/webgl/conformance-suites/

The WebGL Wiki can be found here https://www.khronos.org/webgl/wiki/

This repository is licensed under the MIT license and all contributions are covered under the MIT CLA.

Cloning this repository

When cloning this repository please pass the --recursive flag to git:

git clone --recursive [URL]

This will properly install the WebGLDeveloperTools repository as a git submodule under sdk/devtools/.

The last edited date in several specifications is automatically updated via a smudge filter. To benefit from this you must issue the following commands in the root of your clone.

On Unix (Linux, Mac OS X, etc.) platforms and Windows using Git for Windows' Git Bash or Cygwin's bash terminal:

./install-gitconfig.sh
rm specs/latest/*/index.html
git checkout !$

On Windows with the Command Prompt (requires git.exe in a directory on your %PATH%):

install-gitconfig.bat
del specs/latest/1.0/index.html specs/latest/2.0/index.html 
git checkout specs/latest/1.0/index.html specs/latest/2.0/index.html 

The first command adds an [include] of the repo's .gitconfig to the local git config file.git/config in your clone of the repo. .gitconfig contains the config of the "dater" filter. The remaining commands force a new checkout of the index.html files to smudge them with the date. These two are unnecessary if you plan to edit these files. All are unecessary if you do not care about having the dates shown.