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
相关推荐
点心的游戏开发世界30 分钟前
GDScript 入门笔记(十一):Lambda 与匿名函数
开发语言·笔记·游戏引擎·godot
sunshine22 girl2 小时前
Java学习一 环境配置2 安装和基本使用Idea
java·学习·intellij-idea
不cong明的亚子2 小时前
web live多屏互动平台
javascript·经验分享·音视频
陆枫Larry2 小时前
Astro 是什么
前端
事圆则缓2 小时前
Kotlin 协程完全指南
开发语言·kotlin
Kobebryant-Manba3 小时前
学习Bert微调
人工智能·学习·bert
我爱写代码i3 小时前
BeLink - 支持生成多种URL 缩短网址PHP源码
开发语言·php·短网址php源码
Java后端的Ai之路3 小时前
20、Python - 备忘录模式
开发语言·人工智能·python·外观模式·备忘录模式
xian_wwq3 小时前
【学习笔记】深度认知系列-第14讲 端侧AI崛起——为什么AI正在从云端走向本地
人工智能·笔记·学习
我爱cope3 小时前
【计算机网络 | 传输层3:TCP 协议概述:面向连接、可靠传输到底意味着什么?】
网络·网络协议·学习·tcp/ip·计算机网络·传输层