代码拉取完成,页面将自动刷新
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebGL Draw Points</title>
<script src="./main.js"></script>
<script src="./flyLine/fly.js"></script>
<style>
canvas {
display: block;
border: none;
background-color: #000;
}
</style>
</head>
<body>
<canvas id="glcanvas" width="640" height="480"></canvas>
<script>
const canvas = document.getElementById('glcanvas');
let gl = null;
try {
gl = canvas.getContext('webgl');
} catch (e) { }
if (!gl) {
alert('Unable to initialize WebGL. Your browser may not support it.');
throw new Error('WebGL not supported');
}
// 假设points是一个点的数组,这里需要根据实际情况定义points
// const points = [{ x: 100, y: 100 }, { x: 200, y: 200 }, { x: 300, y: 300 }];
const newPoints = points.map(item => [item.x / canvas.width, item.y / canvas.height]).flat();
const vertices = new Float32Array(newPoints);
const len = points.length;
const percentArr = points.map((item, index) => 1 - (index + 1) / len);
console.log('percentArr ------>', percentArr)
const buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
const vertexShaderSource = `
attribute vec2 aPosition;
uniform float uPointSize;
attribute float percent;
void main() {
gl_PointSize = uPointSize;
gl_Position = vec4(aPosition, 0.0, 1.0);
}
`;
const fragmentShaderSource = `
void main() {
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
`;
const program = createShaderProgram(gl, vertexShaderSource, fragmentShaderSource);
gl.useProgram(program);
const positionAttributeLocation = gl.getAttribLocation(program, 'aPosition');
const pointSizeLocation = gl.getUniformLocation(program, 'uPointSize');
gl.enableVertexAttribArray(positionAttributeLocation);
gl.vertexAttribPointer(positionAttributeLocation, 2, gl.FLOAT, false, 0, 0);
const percentVertexAttributeLocation = gl.getAttribLocation(program, 'percent');
const buffer1 = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer1);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(percentArr), gl.STATIC_DRAW);
// 绘制点
gl.drawArrays(gl.POINTS, 0, newPoints.length / 2);
</script>
</body>
</html>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。