html在原生webgl画布的00 点,绘制一张图片,完整demo
时间: 2024-11-30 09:29:45 浏览: 16
HTML5中的WebGL是一种基于OpenGL ES的JavaScript API,它可以在浏览器上创建交互式的3D图形。要在原生WebGL画布上从(0,0)点开始绘制图片,你需要通过创建纹理并将其绑定到合适的顶点数组对象(VAO),然后设置相应的着色器程序。
以下是一个简单的WebGL Demo,用于在WebGL画布上从(0,0)绘制图片:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>WebGL Image Draw</title>
<style>
body { margin: 0; }
canvas { display: block; width: 100%; height: 100%;}
</style>
</head>
<body>
<canvas id="glCanvas" width="640" height="480"></canvas>
<script>
// 创建 WebGL 上下文
const canvas = document.getElementById('glCanvas');
const gl = canvas.getContext('webgl');
if (!gl) {
console.error("Your browser does not support WebGL.");
return;
}
// 加载图片
let image = new Image();
image.src = 'path_to_your_image.jpg'; // 替换为你图片的实际路径
image.onload = function() {
// 设置纹理
const texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
// 将图片位置设置为(0, 0)
const vertices = [
-1, -1,
1, -1,
-1, 1,
1, 1
];
const uvCoordinates = [
0, 0,
1, 0,
0, 1,
1, 1
];
// 创建VAO、VBO
const vao = gl.createVertexArrayObject();
gl.bindVertexArray(vao);
const vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
const uvBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, uvBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(uvCoordinates), gl.STATIC_DRAW);
// 定义顶点着色器
const vertexShaderSource = `
attribute vec2 a_position;
attribute vec2 a_texCoord;
varying vec2 v_texCoord;
void main() {
gl_Position = vec4(a_position * 0.5 + 0.5, 0.0, 1.0);
v_texCoord = a_texCoord;
}
`;
const vertexShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vertexShader, vertexShaderSource);
gl.compileShader(vertexShader);
// 定义片段着色器
const fragmentShaderSource = `
precision mediump float;
uniform sampler2D u_texture;
varying vec2 v_texCoord;
void main() {
gl_FragColor = texture2D(u_texture, v_texCoord);
}
`;
const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fragmentShader, fragmentShaderSource);
gl.compileShader(fragmentShader);
// 绑定着色器
const shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
gl.useProgram(shaderProgram);
// 绑定纹理和顶点数据
gl.uniform1i(gl.getUniformLocation(shaderProgram, "u_texture"), 0); // 第0个纹理单位
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.vertexAttribPointer(gl.getAttribLocation(shaderProgram, "a_position"), 2, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, uvBuffer);
gl.vertexAttribPointer(gl.getAttribLocation(shaderProgram, "a_texCoord"), 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(gl.getAttribLocation(shaderProgram, "a_position"));
gl.enableVertexAttribArray(gl.getAttribLocation(shaderProgram, "a_texCoord"));
// 渲染循环
requestAnimationFrame(drawScene);
};
function drawScene() {
gl.viewport(0, 0, canvas.width, canvas.height);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
requestAnimationFrame(drawScene);
}
</script>
</body>
</html>
```
在这个示例中,当图片加载完成后,我们创建了一个纹理,并将其应用到一个三角形贴图上,使其覆盖整个画布。记得将`image.src`替换为你的图片URL。
阅读全文