JavaScript WebGL - WebGL 引入(获取绘图上下文、获取最大支持纹理尺寸)

获取绘图上下文

1、WebGLRenderingContext
  • 官方文档:https://developer.mozilla.org/zh-CN/docs/Web/API/WebGLRenderingContext
  1. WebGLRenderingContext 接口提供基于 OpenGL ES 2.0 的绘图上下文,用于在 HTML <canvas> 元素内绘图

  2. 要获得这个接口的对象,可以通过在 <canvas> 元素上调用 getContext 方法,调用时传入 webgl 参数

js 复制代码
const canvas = document.createElement("canvas");

const gl = canvas.getContext("webgl");

console.log(gl);
复制代码
# 输出结果

WebGLRenderingContext {canvas: canvas, drawingBufferWidth: 300, drawingBufferHeight: 150, drawingBufferColorSpace: 'srgb', unpackColorSpace: 'srgb', ...}
2、WebGL2RenderingContext
  • 官方文档:https://developer.mozilla.org/zh-CN/docs/Web/API/WebGL2RenderingContext
  1. WebGL2RenderingContext 接口在底层使用了 OpenGL ES 3.0 为 HTML 的 <canvas> 元素提供了绘图上下文

  2. 要获取该接口的对象需要调用一个 <canvas> 标签对象的 getContext 函数,将 webgl2 作为参数传递

js 复制代码
const canvas = document.createElement("canvas");

const gl = canvas.getContext("webgl2");

console.log(gl);
复制代码
# 输出结果

WebGL2RenderingContext {canvas: canvas, drawingBufferWidth: 300, drawingBufferHeight: 150, drawingBufferColorSpace: 'srgb', unpackColorSpace: 'srgb', ...}

获取最大支持纹理尺寸

  • 官方文档:https://developer.mozilla.org/zh-CN/docs/Web/API/WebGLRenderingContext/getParameter
js 复制代码
function getMaxTextureSize() {
    const canvas = document.createElement("canvas");

    const gl = canvas.getContext("webgl");
    
    return gl.getParameter(gl.MAX_TEXTURE_SIZE);
}

const maxSize = getMaxTextureSize();

console.log(`最大支持纹理尺寸: ${maxSize}px`);
复制代码
# 输出结果

最大支持纹理尺寸: 8192px
相关推荐
辛-夷2 小时前
2025年高频面试题整理(vue系列一)
前端·javascript·vue.js·前端框架
GISer_Jing2 小时前
ByteDance AI战略:前端生态的颠覆者
前端·人工智能·aigc
程序猿零零漆2 小时前
Spring之旅 - 记录学习 Spring 框架的过程和经验(一)BeanFactory和ApplicationContext入门和关系
java·学习·spring
凌冰_2 小时前
Thymeleaf 访问域对象
java·开发语言
想唱rap2 小时前
哈希(C++)
服务器·开发语言·c++·算法·哈希算法
2501_930707782 小时前
使用C#代码向 Word 文档添加文档属性
开发语言·c#·word
加成BUFF2 小时前
Qt开发核心工具:CMake与qmake全面解析
开发语言·qt·cmake·qmake
大布布将军2 小时前
⚡️ 性能加速器:利用 Redis 实现接口高性能缓存
前端·数据库·经验分享·redis·程序人生·缓存·node.js
野生风长2 小时前
从零开始的C语言:文件操作与数据管理(下)(fseek,ftell,rewind,文件的编译和链接)
android·java·c语言·开发语言·visual studio