是什么让 THREE.js 帆布双倍大小的像素?

What gives THREE.js canvas double size pixles?

提问人:Mr. Developerdude 提问时间:10/12/2023 更新时间:10/12/2023 访问量:33

问:

我第一次尝试使用 THREE.js,这似乎很容易。但是,有一件事我就是做不到;

出于某种奇怪的原因,画布像素是“双倍”大小的。请看以下屏幕截图:double pixels in THREE.js canvas

请注意以下事项:

  1. 画布是橙色部分
  2. 页面背景是白色部分
  3. 包含画布的 div 的黑色边框正好是 1 像素宽

您可以看到呈现的线条 (GridHelper) 没有像预期的那样宽一个像素,但似乎是双倍大小,或者至少大于 1。

我是这样设置渲染的:

this.renderer = new THREE.WebGLRenderer({
      antialias:false
    , alpha: true 
});
console.log("canvas", this.renderer.domElement);
this.camera = new THREE.PerspectiveCamera(45, 1, 0.1, 1000);

我在每次渲染时调用以下函数:

update_canvas_size() {
        const canvas = this.renderer.domElement;
        const parent = canvas.parentElement;
        // look up the size the canvas is being displayed
        const w = parent.clientWidth;
        const h = parent.clientHeight;
        console.log("update_canvas_size <",canvas.width,canvas.height,"> --> <", w,h,">");
        
        // adjust displayBuffer size to match
        if (canvas.width !== w || canvas.height !== h) {
            console.log("SIZE DESCREPENCY DETECTED, RESIZING <",canvas.width,canvas.height,"> --> <", w,h,">");
            this.renderer.setSize(w, h, false);
            this.camera.aspect = w / h;
            this.camera.updateProjectionMatrix();
            

        }
    }

如果您想知道我的 GridHelper 是什么样子的:

    this.grid = new THREE.GridHelper( 10, 10, 0x666666, 0x777777);

是什么原因导致的?

JavaScript 三.js HTML5-画布 像素 分辨率

评论

0赞 Dave Newton 10/12/2023
这不仅仅是混叠?
0赞 Mr. Developerdude 10/12/2023
不,黑色的 1 像素边框很清晰,而网格线很模糊且是双倍大小的。我意识到 imgurl 对图像做了工作。(我可以尝试放大它以使其更清晰。
0赞 init-qy 10/12/2023
DPR 可能是导致问题的原因。
0赞 Dave Newton 10/12/2023
边框是垂直的;无需抗锯齿。

答: 暂无答案