为什么遍历数组可以绘制多个点?

来源:2-7 通过鼠标控制绘制

慕运维5378130

2023-01-30

 ctx.addEventListener("click", (e) => {
    const x = e.clientX
    const y = e.clientY
    const {top, left, width, height} = e.target.getBoundingClientRect()
    const domx = x - left;
    const domy = y - top;
    const halfWidth = width / 2;
    const halfHeight = height / 2;
    const clickX = (domx - halfWidth) / halfWidth;
    const clickY = (halfHeight - domy) / halfHeight;
    gl.vertexAttrib2f(aPosition, clickX, clickY)
    gl.drawArrays(gl.POINTS, 0, 1)   
})
  const points = [];
  ctx.addEventListener("click", (e) => {
    const x = e.clientX
    const y = e.clientY
    const {top, left, width, height} = e.target.getBoundingClientRect()
    const domx = x - left;
    const domy = y - top;
    const halfWidth = width / 2;
    const halfHeight = height / 2;
    const clickX = (domx - halfWidth) / halfWidth;
    const clickY = (halfHeight - domy) / halfHeight;
    points.push({
      clickX,clickY
    })
    for (let i = 0; i < points.length; i++) {
      const {clickX, clickY} = points[i];
      gl.vertexAttrib2f(aPosition, clickX, clickY)
      gl.drawArrays(gl.POINTS, 0, 1)
    }
  })

我理解的是上面那种方法和下面那种都是重复绘制,为什么上面那种是覆盖,下面那种是添加?

写回答

3回答

yancy

2023-01-31

短时间多次调用绘制方法,会暂存webgl的缓冲区内容。上图这个方法,如果连续调用多次也可以看到绘制多个点的效果。

2
2
慕运维5378130
非常感谢!
2024-02-28
共2条回复

慕雪6296728

2023-06-27

下面的方法每次点击都有去遍历数组进行绘制

0
0

慕尼黑5723975

2023-05-08

没覆盖吧。。点2下就是画了2个点啊

0
0

WebGL+Three.js 入门与实战,系统学习 Web3D 技术

前端的技术蓝海,涨薪好选择

1081 学习 · 294 问题

查看课程