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
相关推荐
我的div丢了肿么办6 分钟前
element-plus表格使用:filter-method过滤导致出现无数据
前端·javascript·vue.js
F_Quant8 分钟前
高股息+破净+PEG三因子:年化20%的背后,是一个“敢于证伪自己”的捡烟蒂策略
前端
程序员清风8 分钟前
双指针与滑动窗口:高效处理连续区间
开发语言·算法
YIAN16 分钟前
入门篇:LangGraph 入门实战:从零搭建最简单的 RAG 问答系统
javascript·langchain
不可能片场18 分钟前
Electron 发布核验 别把没查到当成没发出去
前端·electron
YIAN25 分钟前
高级篇:进阶多跳 RAG:子问题拆解 + 循环检索 + 智能规划,搞定复杂链式问题
javascript·langchain
开开心心就好30 分钟前
文件被占用删不掉?解锁工具强制删除
java·运维·服务器·开发语言·前端·pdf·ocr
YIAN33 分钟前
前置篇:手把手用 Milvus + LangChain 搭建小说向量知识库,《天龙八部》EPUB 一键入库
javascript·langchain
YIAN33 分钟前
进阶篇:RAG 优化第一步:给你的问答系统加上智能路由,实现双分支分流
javascript·langchain
云和数据.ChenGuang42 分钟前
python大小整数范围解析
java·开发语言·python