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
相关推荐
井川不擦几秒前
前端安全通信方案:RSA + AES 混合加密
前端
孜孜不倦不忘初心3 分钟前
Ant Design Vue 表格组件空数据统一处理 踩坑
前端·vue.js·ant design
weixin_423533993 分钟前
windows11安装飞桨paddlepaddle,python3.13
开发语言
AD_wjk3 分钟前
Android13系统集成方案
前端
Joyee6914 分钟前
RN 的新通信模型 JSI
前端·react native
somebody4 分钟前
零经验学 react 的第6天 - 循环渲染和条件渲染
前端
2501_924952695 分钟前
嵌入式C++电源管理
开发语言·c++·算法
青晚舟6 分钟前
AI 时代前端还要学 Docker & K8s 吗?我用一次真实部署经历说清楚
前端·github
墨鱼笔记8 分钟前
不使用微前端:如何实现主应用和子模块动态管理与通信实现
前端
2401_8426236510 分钟前
C++中的访问者模式高级应用
开发语言·c++·算法