The impact of devicePixelRatio on WebGLRenderer.setSize function in Chrome for Android

Exploring mobile support for Three.js led me to uncover an interesting quirk involving the setSize function and multiple views. Let me paint you a picture of the situation.

When attempting to load the webgl_multiple_views demo on my trusty Nexus 7 running Android OS 4.3 using Chrome for Android (version 29.0.1547.59), I noticed that the entire rendered window appeared misaligned, as evidenced by this screenshot.

Initially, I suspected that there might be an issue with setViewport. However, upon closer inspection, it became apparent that the setSize function in WebGLRenderer.js was adjusting the WebGL canvas context size by multiplying it by the devicePixelRatio like so:

_canvas.width = width * this.devicePixelRatio;
_canvas.height = height * this.devicePixelRatio;

While this approach seems logical, the problem arises with certain Android devices where the system seemingly already accounts for this calculation, resulting in a distorted scene.

I discovered that by assuming a default pixel ratio of 1, I could resolve the issue. However, I am concerned that this fix may disrupt properly functioning mobile devices. Here's the proposed "fix":

renderer = new THREE.WebGLRenderer( { antialias: true, alpha: false, devicePixelRatio: 1 } );

My inquiry is this - has anyone else faced a similar challenge? Is there a more reliable solution for scaling a canvas context to accommodate device pixel ratio without causing issues on some android devices?

Thank you in advance for any guidance or insight provided.

Answer №1

Check out this useful function that accurately captures the dimensions of various devices. I have thoroughly tested it myself and it successfully retrieves the device width, height, and a computedPixelRatio for all devices I've experimented with...

function captureDeviceDimensions()                  // obtain the width and height of the viewing device based on its operating system
{
    screenWidth = window.screen.width;                                                  // retrieve the width
    screenHeight = window.screen.height;                                                // fetch the height 
    var computedPixelRatio =  (window.screen.availWidth / document.documentElement.clientWidth);                        // a more accurate measurement of device pixel ratio considering android firefox bugs...
    computedPixelRatio = window.devicePixelRatio >= computedPixelRatio ? window.devicePixelRatio : computedPixelRatio;  // determine which value is larger between different pixel ratio methods
    if (navigator.userAgent.indexOf('Android') != -1)                                   // check if the user is using an android device
    {
        screenWidth = screenWidth / computedPixelRatio;                                 // adjust reported width by dividing it by the pixel ratio
        screenHeight = screenHeight / computedPixelRatio;                               // adjust reported height by dividing it by the pixel ratio
    }
    //alert(screenWidth + " " + screenHeight + " " + window.devicePixelRatio + " " + computedPixelRatio);
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error in TypeScript: Trying to set property 'test' on an undefined object

Trying to grasp TypeScript concepts, but struggling with a misunderstood notion... Encountering this Error: TypeError: Cannot set property 'test' of undefined at testResponse (/home/mcfly/bimbim/geoloc_API/dist/routes/geoRouter.js:11:19) a ...

Attention: It is not possible to update the React state on a component that is not mounted. Please use the useEffect cleanup function

Can you assist with solving this issue? https://i.sstatic.net/yKFzr.png //ReviewProductDetail.jsx : Initially, the page was populated from this particular component import React, { useEffect, useState } from 'react'; import RatingCardProductD ...

Steps for generating random numbers from a set of given numbers

I am faced with a scenario where I need to generate random numbers based on a given set of numbers. For instance, if I have an array num=[23,56,12,22], I would like to obtain a random number from this array. ...

Updating the border color on angular-archwizard's steps

I am utilizing angular-archwizard to create circular steps for navigation. When I click on different steps, I can see the circle step border change color (in my case, orange) until I click on the last step. Once I click on the last step, all the other step ...

Tips for personalizing the tooltip on a line chart in Vue.js using Chart.js

Attempting to create a line graph using chart js and vue, but new to both technologies. In the chart below, I am trying to change the tooltip to display "10 Answers July 19, 2023". I have attempted to use the beforeLabel and afterLabel options without succ ...

Exploring the documentation of JQuery's $.Ajax Function

Can anyone help me locate the parameters in the JQuery Docs? jqXHR.done(function( data, textStatus, jqXHR ) {}); http://api.jquery.com/deferred.done/ I've been trying to determine what data represents but haven't had any luck. I've notic ...

Tips for ensuring all my onclick event are clickable in iOS browser

Currently, I am developing a web page using HTML5 and JQuery Mobile. I have encountered an issue where the onclick function does not work on iOS device browsers. Is there a way to change all onclick events to be accessible through tapping instead? This wou ...

Access information from multiple div elements using JavaScript data-attributes

Having trouble retrieving data-attribute values from multiple divs with the same class when clicked. The goal is to display the data value of the clicked div. function playSound(e) { const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`) ...

What steps can be taken to retrieve a user's login information when their provided ID and Password match the records within the database?

When a user enters their ID and password in this form, the system checks whether it matches the data in the database. If there is a match, the user is allowed to log in and redirected to the "action.php" page. If there is no match, an error message is di ...

Transmit the date to the WCF RESTful service

I am using JQuery AJAX to call a WCF REST service from my JavaScript code. The service is set up with a bare style for the body, and I am unable to change it to wrapped. I need help sending a date parameter to the service. Can someone please assist me wi ...

What is the best method to determine the cumulative time spent filling out a text box on a web page using Angular?

I am working on a web page that contains two text boxes. My goal is to track the total time spent inside each box when the user clicks the submit button at the end of the page. clickInside() { this.text = 'clicked inside'; this.wasIns ...

When I toggle the password visibility and then click the submit button, it functions correctly

I am currently working on an application that combines Vue with Laravel. One of the Vue components I created is called UsersCreate. This component is responsible for gathering user data and sending it to a Laravel controller using axios. However, I have en ...

The continuous firing of the postback event of the Asp.Net Button is

Why does the postback event keep firing for my button? Strangely, when I try to debug with Firebug and set a break point on the function(e) part, the code seems to skip right over it. Even using return false doesn't seem to resolve the issue. <sc ...

Display recommended contacts on Android when opening mailbox

Implementing a button on Android to open a mailbox, but a pop-in appears with suggested contacts. Is there a way to remove these contacts and only show the mailbox? Here is the screenshot: https://i.sstatic.net/58geo.png My intent configuration code: ...

When attempting to access endpoints from other computers, the connection to Express.js is being refused

I have set up an Express server and React for the frontend. The express server is running on port 5000 and React on port 3000. Additionally, I am using JWT tokens for authentication. When I login to the app from the computer it is running on, everything w ...

Tips for merging arrays conditionally in JavaScript

Currently facing a dilemma without a clear solution, aiming to provide minimal context and focus solely on the problem at hand A worker logs their daily hours at work, stored in the "dayli_data" array. If the worker misses logging hours, it triggers the " ...

Search for specific data within a table while still keeping the left header column in

I have a table with a fixed left column and I am trying to implement a search function that will show the specific column, not row. So far, I have only come across a solution that displays the row: How to perform a real time search and filter on a HTML ta ...

Facing issues with integrating Mixpanel with NestJS as the tracking function cannot be located

While utilizing mixpanel node (yarn add mixpanel) in conjunction with NestJS, I have encountered an issue where only the init function is recognized. Despite calling this function, I am unable to invoke the track function and receive the error message: Ty ...

Having trouble locating an external Javascript file in a Node.JS/Express app with Jade template?

In my Node.JS/Express app, I am using the Jade template engine. The issue arises when trying to reference a server-side Javascript file named common_routines. Despite placing the Javascript file in the directory above my views directory and referencing it ...

Updating the style sheet of a selected menu item on an ASP.NET master page

I created an asp.net master page with a menu setup like this: <menu id="menu"> <nav id="main_nav"> <ul id="menu-primary"> <li ><a href="./">Home</a></li> <li><a href="staff.aspx"& ...