--- title: WebGLRenderingContext.bindTexture() slug: Web/API/WebGLRenderingContext/bindTexture tags: - API - Method - Reference - Textures - WebGL - WebGLRenderingContext translation_of: Web/API/WebGLRenderingContext/bindTexture --- <div>{{APIRef("WebGL")}}</div> <p><a href="/en-US/docs/Web/API/WebGL_API">WebGL API</a> 的 <strong><code>WebGLRenderingContext.bindTexture()</code></strong> 方法将给定的 {{domxref("WebGLTexture")}} 绑定到目标(绑定点)。</p> <h2 id="语法">语法</h2> <pre class="syntaxbox">void <var>gl</var>.bindTexture(<var>target</var>, <var>texture</var>); </pre> <h3 id="参数">参数</h3> <dl> <dt>target</dt> <dd>{{domxref("GLenum")}} 指定绑定点(目标)。 可能的值: <ul> <li><code>gl.TEXTURE_2D</code>: 二维纹理。</li> <li><code>gl.TEXTURE_CUBE_MAP</code>: 立方体映射纹理。</li> <li>当使用 {{domxref("WebGL2RenderingContext", "WebGL 2 context", "", 1)}} 时,可以使用以下值: <ul> <li><code>gl.TEXTURE_3D</code>: 三维纹理.</li> <li><code>gl.TEXTURE_2D_ARRAY</code>: 二维数组纹理.</li> </ul> </li> </ul> </dd> <dt>texture</dt> <dd>要绑定的 {{domxref("WebGLTexture")}} 对象。</dd> </dl> <h3 id="返回值">返回值</h3> <p>无。</p> <h3 id="异常">异常</h3> <p>如果目标不是 gl.TEXTURE_2D ,gl.TEXTURE_CUBE_MAP,gl.TEXTURE_3D 或 gl.TEXTURE_2D_ARRAY ,则会抛出 gl.INVALID_ENUM 错误。</p> <h2 id="示例">示例</h2> <h3 id="绑定纹理">绑定纹理</h3> <pre class="brush: js">var canvas = document.getElementById('canvas'); var gl = canvas.getContext('webgl'); var texture = gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D, texture); </pre> <h3 id="获取当前绑定">获取当前绑定</h3> <p>要检查当前纹理绑定,请查询gl.TEXTURE_BINDING_2D或gl.TEXTURE_BINDING_CUBE_MAP常量。</p> <pre class="brush: js">gl.getParameter(gl.TEXTURE_BINDING_2D); </pre> <h2 id="规范">规范</h2> <table class="standard-table"> <tbody> <tr> <th scope="col">Specification</th> <th scope="col">Status</th> <th scope="col">Comment</th> </tr> <tr> <td>{{SpecName('WebGL', "#5.14.8", "bindTexture")}}</td> <td>{{Spec2('WebGL')}}</td> <td>WebGL初始定义。</td> </tr> <tr> <td>{{SpecName('OpenGL ES 2.0', "glBindTexture.xml", "glBindTexture")}}</td> <td>{{Spec2('OpenGL ES 2.0')}}</td> <td>OpenGL ES 2.0 API手册(类似).</td> </tr> <tr> <td>{{SpecName('WebGL2', "#3.7.1", "bindTexture")}}</td> <td>{{Spec2('WebGL2')}}</td> <td>WebGL 2更新定义。<br> 增加: <code>gl.TEXTURE_3D</code> and <code>gl.TEXTURE_2D_ARRAY</code></td> </tr> <tr> <td>{{SpecName('OpenGL ES 3.0', "glBindTexture.xhtml", "glBindTexture")}}</td> <td>{{Spec2('OpenGL ES 3.0')}}</td> <td>OpenGL ES 3.0 API手册(类似)。</td> </tr> </tbody> </table> <h2 id="浏览器兼容性">浏览器兼容性</h2> <p>{{Compat("api/WebGLRenderingContext", "WebGLRenderingContext.bindTexture")}}</p> <h2 id="另见">另见</h2> <ul> </ul> <ul> <li>{{domxref("WebGLRenderingContext.createTexture()")}}</li> <li>{{domxref("WebGLRenderingContext.deleteTexture()")}}</li> <li>{{domxref("WebGLRenderingContext.isTexture()")}}</li> <li>{{domxref("WebGLRenderingContext.texImage2D()")}}</li> </ul>