WebGL: 2D and 3D graphics for the web - Web APIs | MDN (2024)

WebGL (Web Graphics Library) is a JavaScript API for rendering high-performance interactive 3D and 2D graphics within any compatible web browser without the use of plug-ins. WebGL does so by introducing an API that closely conforms to OpenGL ES 2.0 that can be used in HTML <canvas> elements. This conformance makes it possible for the API to take advantage of hardware graphics acceleration provided by the user's device.

Support for WebGL is present in all modern browsers (see the compatibility tables below); however, the user's device must also have hardware that supports these features.

The WebGL 2 API introduces support for much of the OpenGL ES 3.0 feature set; it's provided through the WebGL2RenderingContext interface.

The <canvas> element is also used by the Canvas API to do 2D graphics on web pages.

Reference

Standard interfaces

  • WebGLRenderingContext
  • WebGL2RenderingContext
  • WebGLActiveInfo
  • WebGLBuffer
  • WebGLContextEvent
  • WebGLFramebuffer
  • WebGLProgram
  • WebGLQuery
  • WebGLRenderbuffer
  • WebGLSampler
  • WebGLShader
  • WebGLShaderPrecisionFormat
  • WebGLSync
  • WebGLTexture
  • WebGLTransformFeedback
  • WebGLUniformLocation
  • WebGLVertexArrayObject

Extensions

  • ANGLE_instanced_arrays
  • EXT_blend_minmax
  • EXT_color_buffer_float
  • EXT_color_buffer_half_float
  • EXT_disjoint_timer_query
  • EXT_float_blend Experimental
  • EXT_frag_depth
  • EXT_shader_texture_lod
  • EXT_sRGB
  • EXT_texture_compression_bptc
  • EXT_texture_compression_rgtc
  • EXT_texture_filter_anisotropic
  • EXT_texture_norm16
  • KHR_parallel_shader_compile
  • OES_draw_buffers_indexed
  • OES_element_index_uint
  • OES_fbo_render_mipmap
  • OES_standard_derivatives
  • OES_texture_float
  • OES_texture_float_linear
  • OES_texture_half_float
  • OES_texture_half_float_linear
  • OES_vertex_array_object
  • OVR_multiview2
  • WEBGL_color_buffer_float
  • WEBGL_compressed_texture_astc
  • WEBGL_compressed_texture_etc
  • WEBGL_compressed_texture_etc1
  • WEBGL_compressed_texture_pvrtc
  • WEBGL_compressed_texture_s3tc
  • WEBGL_compressed_texture_s3tc_srgb
  • WEBGL_debug_renderer_info
  • WEBGL_debug_shaders
  • WEBGL_depth_texture
  • WEBGL_draw_buffers
  • WEBGL_lose_context
  • WEBGL_multi_draw

Events

  • webglcontextlost
  • webglcontextrestored
  • webglcontextcreationerror

Constants and types

WebGL 2

WebGL 2 is a major update to WebGL which is provided through the WebGL2RenderingContext interface. It is based on OpenGL ES 3.0 and new features include:

  • 3D textures,
  • Sampler objects,
  • Uniform Buffer objects,
  • Sync objects,
  • Query objects,
  • Transform Feedback objects,
  • Promoted extensions that are now core to WebGL 2: Vertex Array objects, instancing, multiple render targets, fragment depth.

See also the blog post "WebGL 2 lands in Firefox" and webglsamples.org/WebGL2Samples for a few demos.

Guides and tutorials

Below, you'll find an assortment of guides to help you learn WebGL concepts and tutorials that offer step-by-step lessons and examples.

Guides

Data in WebGL

A guide to variables, buffers, and other types of data used when writing WebGL code.

WebGL best practices

Tips and suggestions to help you improve the quality, performance, and reliability of your WebGL content.

Using extensions

A guide to using WebGL extensions.

Tutorials

WebGL tutorial

A beginner's guide to WebGL core concepts. A good place to start if you don't have previous WebGL experience.

Examples

A basic 2D WebGL animation example

This example demonstrates the simple animation of a one-color shape. Topics examined are adapting to aspect ratio differences, a function to build shader programs from sets of multiple shaders, and the basics of drawing in WebGL.

WebGL by example

A series of live samples with short explanations that showcase WebGL concepts and capabilities. The examples are sorted according to topic and level of difficulty, covering the WebGL rendering context, shader programming, textures, geometry, user interaction, and more.

Advanced tutorials

WebGL model view projection

A detailed explanation of the three core matrices that are typically used to represent a 3D object view: the model, view and projection matrices.

Matrix math for the web

A useful guide to how 3D transform matrices work, and can be used on the web — both for WebGL calculations and in CSS transforms.

Resources

Libraries

  • three.js is an open-source, fully featured 3D WebGL library.
  • Babylon.js is a powerful, simple, and open game and 3D rendering engine packed into a friendly JavaScript framework.
  • Pixi.js is a fast, open-source 2D WebGL renderer.
  • Phaser is a fast, free and fun open source framework for Canvas and WebGL powered browser games.
  • PlayCanvas is an open-source game engine.
  • glMatrix is a JavaScript matrix and vector library for high-performance WebGL apps.
  • twgl is a library for making webgl less verbose.
  • RedGL is an open-source 3D WebGL library.
  • vtk.js is a JavaScript library for scientific visualization in your browser.
  • webgl-lint will help find errors in your WebGL code and provide useful info

Specifications

Specification
WebGL Specification
# 5.14
WebGL 2.0 Specification
# 3.7

Browser compatibility

api.WebGLRenderingContext

BCD tables only load in the browser

api.WebGL2RenderingContext

BCD tables only load in the browser

Compatibility notes

In addition to the browser, the GPU itself also needs to support the feature. So, for example, S3 Texture Compression (S3TC) is only available on Tegra-based tablets. Most browsers make the WebGL context available through the webgl context name, but older ones need experimental-webgl as well. In addition, the upcoming WebGL 2 is fully backwards-compatible and will have the context name webgl2.

Gecko notes

WebGL debugging and testing

Firefox provides two preferences available which let you control the capabilities of WebGL for testing purposes:

webgl.min_capability_mode

A Boolean property that, when true, enables a minimum capability mode. When in this mode, WebGL is configured to only support the bare minimum feature set and capabilities required by the WebGL specification. This lets you ensure that your WebGL code will work on any device or browser, regardless of their capabilities. This is false by default.

webgl.disable_extensions

A Boolean property that, when true, disables all WebGL extensions. This is false by default.

See also

  • Canvas API
  • Compatibility info about WebGL extensions
WebGL: 2D and 3D graphics for the web - Web APIs | MDN (2024)

FAQs

Is WebGL a graphics API? ›

WebGL is an API for rendering graphics in web browsers, which is based on the functionality of the OpenGL ES graphics library. WebGL 1.0 roughly matches OpenGL ES 2.0 functionality, and WebGL 2.0 roughly matches OpenGL ES 3.0 functionality.

What is 2D WebGL? ›

WebGL (Web Graphics Library) is a JavaScript API for rendering high-performance interactive 3D and 2D graphics within any compatible web browser without the use of plug-ins.

Which browser supports graphics API WebGL 2? ›

The Khronos Group announced that WebGL 2.0, which renders interactive 2D and 3D graphics without using plug-ins, is now supported in all major browsers. Initially, it was shipped in Firefox and Chrome, but now it's also available in Safari and Microsoft Edge.

Does WebGL support 3D? ›

WebGL (short for 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. WebGL is fully integrated with other web standards, allowing GPU-accelerated usage of physics, image processing, and effects in the HTML canvas.

What is WebGL used for? ›

WebGL is used by developers to provide a platform-independent means of creating interactive graphical applications on the web. WebGL is not only used to draw the graphics of 2D and 3D games, but also to accelerate the functions of web based image editors and their effects, as well as physics simulations.

What will replace WebGL? ›

WebGPU is a JavaScript API provided by a web browser that enables webpage scripts to efficiently utilize a device's graphics processing unit (GPU). This is achieved with the underlying Vulkan, Metal, or Direct3D 12 system APIs. On relevant devices, WebGPU is intended to supersede the older WebGL standard.

Is WebGL still a thing? ›

Yes. But you need to be aware that WebGL is unlike any other supported platform.

Why do I need WebGL? ›

WebGL uses the hardware acceleration capabilities of a computer's graphics card rather than the processor of the computer. What does that mean for your computer? A benefit of hardware acceleration is that it can save computer resources, and reduce how long it takes web pages to load.

Why is Chrome not using WebGL? ›

If you aren't able to run WebGL in Chrome, make sure that you update to the most recent version of Chrome. If you are using the most recent version of Chrome and can't access WebGL content, make sure that hardware acceleration is enabled in your Chrome settings.

Is WebGL difficult? ›

WebGL is a low-level graphics API. In other words, it's challenging for even experienced developers to create high-fidelity 3D models purely through WebGL. The sheer size of these models and the accompanying memory limitations make it difficult to run them in a browser.

Does Google use WebGL? ›

The WebGL Overlay View provides direct access to the same WebGL rendering context Google Maps Platform uses to render the vector basemap. This use of a shared rendering context provides benefits such as depth occlusion with 3D building geometry, and the ability to sync 2D/3D content with basemap rendering.

What is the disadvantage of WebGL? ›

A WebGL browser space that uses Unity has a hard limit of 2Gb of memory to use. And this can get used up very quickly when you add the space, audio, textures, canvases, avatars, among many other elements.

What is the best browser for 3D graphics? ›

Supported browsers
  • Google Chrome was the first browser that introduced 3D support back in 2011. ...
  • Google Chrome Mobile which is now widespread across Android devices, also offers a good WebGL implementation. ...
  • Firefox works with 3D rather well.
  • Safari is the default browser on Macs.

Is WebGL good for 2D? ›

There are a lot of WebGL libraries that can be used, but in terms of 2D games development the most popular ones are Pixi. js and Phaser. js. Both of them can be used to develop 2D web games, but which one would be the best for a particular situation, depends on several circ*mstances.

What is a graphics API? ›

Summary. A GPU (Graphics Processing Unit) supports various Application Programming Interfaces (API)s. The APIs perform 3D graphics rendering and allow software developers to easily target different GPU hardware.

Is WebGL a CPU or GPU? ›

WebGL's inner workings involve a collaborative effort between the CPU, GPU, and shaders. The CPU handles high-level JavaScript code and data preparation, while the GPU specializes in parallel processing to render complex graphics.

Is graphics API OpenGL or DirectX? ›

OpenGL has wider platform support than DirectX, which is mainly limited to Windows and Xbox. Both APIs can achieve high performance and efficiency, but they may have different trade-offs depending on the situation.

What is the difference between WebGL API and canvas API? ›

The Canvas API largely focuses on 2D graphics. The WebGL API, which also uses the <canvas> element, draws hardware-accelerated 2D and 3D graphics.

Top Articles
Latest Posts
Article information

Author: Prof. An Powlowski

Last Updated:

Views: 6353

Rating: 4.3 / 5 (44 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Prof. An Powlowski

Birthday: 1992-09-29

Address: Apt. 994 8891 Orval Hill, Brittnyburgh, AZ 41023-0398

Phone: +26417467956738

Job: District Marketing Strategist

Hobby: Embroidery, Bodybuilding, Motor sports, Amateur radio, Wood carving, Whittling, Air sports

Introduction: My name is Prof. An Powlowski, I am a charming, helpful, attractive, good, graceful, thoughtful, vast person who loves writing and wants to share my knowledge and understanding with you.