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
相关推荐
YIAN7 分钟前
从 SSE 流式到结构化输出:LangChain 三大 OutputParser 与 ToolCall 方案全实战
前端·langchain·node.js
FYKJ_201017 分钟前
express皖美特色农产品网售系统53118-计算机课程设计、毕业设计
javascript·vue.js·spring boot·mysql·typescript·spark·express
zhanghaha131418 分钟前
Python进阶教程:35_PyQt (保姆级零基础教程)
开发语言·python·pyqt
YIAN19 分钟前
从 SSE 流式原理到 LangChain 结构化输出:打字机效果与 JSON 解析全方案实战
前端·langchain
释厄62338 分钟前
多黑洞具现论·物理/生物/金融三域显化·虚拟宇宙黑洞·奇点=0 圆圈
开发语言·数据结构·人工智能·程序人生·安全威胁分析
Moment1 小时前
如果你在做 RAG,可能会需要 pdf-inspector
前端·后端·面试
gnip1 小时前
Flutter 原生插件开发实战指南
前端·flutter
晓得迷路了1 小时前
栗子前端技术周刊第 148 期 - Turborepo 2.11、Chrome 154 iframe、Node.js 26...
前端·javascript·css
IT_陈寒1 小时前
Vite热更新失效?你可能漏了这个配置项
前端·人工智能·后端
UIU1142 小时前
递归算法与汉诺塔
c++·学习·算法·c#·递归