# Condition (if) to set engine.setHardwareScalingLevel from pixelRatio of Retina display / Normal display

**URL:** <https://forum.babylonjs.com/t/condition-if-to-set-engine-sethardwarescalinglevel-from-pixelratio-of-retina-display-normal-display/29191>\
**Category:** Questions\
**Created:** [April 8, 2022, 5:35pm UTC](https://forum.babylonjs.com/t/condition-if-to-set-engine-sethardwarescalinglevel-from-pixelratio-of-retina-display-normal-display/29191 "2022-04-08T17:35:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nickkilo](https://avatars.discourse-cdn.com/v4/letter/n/d07c76/32.png) [@nickkilo](https://forum.babylonjs.com/u/nickkilo)\
**Post date:** [April 8, 2022, 5:35pm UTC](https://forum.babylonjs.com/t/condition-if-to-set-engine-sethardwarescalinglevel-from-pixelratio-of-retina-display-normal-display/29191/1 "2022-04-08T17:35:29Z")

</div>

Hi Everybody,

I’m trying to set two different HardwareScalingLevel for retina and no-retina displays in order to scale the GUI (and the sharpness of the render) to a proper size.  
As far as I know, Retina displays have a devicePixelRatio \>= 2 while no-Retina displays have a devicePixelRatio \<= 1.

I’ve tried the following code but it does not work:

```
const pixelRatio = window.devicePixelRatio;

const ToggleHardwareScalingLevel = () => {
  if ( pixelRatio > 1) {
     engine.setHardwareScalingLevel(1.5 / pixelRatio); // in a Retina display, to have the GUI a little bigger
     } 
  else {
     engine.setHardwareScalingLevel(1 / pixelRatio); // in a no-Retina display, to have the GUI a little smaller
    }
 return;
};

```

Do somebody have any idea what’s wrong?

Of course, I’ve tried the following code and it works but for all time of devices generating this GUI scaling issue that I want to fix:

```
engine.setHardwareScalingLevel(1.5 / pixelRatio);

```

Thank you so much all in advance!

---

<div class="post-metadata">

**Author:** ![syntheticmagus](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/syntheticmagus/32/395_2.png) [@syntheticmagus](https://forum.babylonjs.com/u/syntheticmagus)\
**Post date:** [April 8, 2022, 7:39pm UTC](https://forum.babylonjs.com/t/condition-if-to-set-engine-sethardwarescalinglevel-from-pixelratio-of-retina-display-normal-display/29191/2 "2022-04-08T19:39:20Z")

</div>

Hi nickkilo,

Welcome to Babylon! Can you elaborate on the exact manner in which the code you posted does not work? Or, better, could you make a [Playground](https://playground.babylonjs.com/) that shows your scenario in action? It’ll be much easier to help you figure out what’s going on if we can see what isn’t working directly. Thanks!

---

<div class="post-metadata">

**Author:** ![brianzinn](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/brianzinn/32/28013_2.png) [@brianzinn](https://forum.babylonjs.com/u/brianzinn)\
**Post date:** [April 8, 2022, 7:50pm UTC](https://forum.babylonjs.com/t/condition-if-to-set-engine-sethardwarescalinglevel-from-pixelratio-of-retina-display-normal-display/29191/3 "2022-04-08T19:50:05Z")

</div>

There’s a construction parameter when you make an engine called `adaptDeviceRatio`:  
[Engine | Babylon.js Documentation (babylonjs.com)](https://doc.babylonjs.com/typedoc/classes/babylon.engine#constructor)

It doesn’t pick up on if you switch screens, but you can listen in on resize events and that usually picks those up. My previous S8 android I think actually had a 1.5 ratio! If you look in the code you will see these kinds of snippets:

```auto
this._hardwareScalingLevel = adaptToDeviceRatio ? 1.0 / Math.min(limitDeviceRatio, devicePixelRatio) : 1.0;

```

I think you want `1.0/window.devicePixelRatio`

---

<div class="post-metadata">

**Author:** ![nickkilo](https://avatars.discourse-cdn.com/v4/letter/n/d07c76/32.png) [@nickkilo](https://forum.babylonjs.com/u/nickkilo)\
**Post date:** [April 9, 2022, 8:59pm UTC](https://forum.babylonjs.com/t/condition-if-to-set-engine-sethardwarescalinglevel-from-pixelratio-of-retina-display-normal-display/29191/4 "2022-04-09T20:59:16Z")

</div>

Hi again,

thank you all for your suggestions.

The issue I was facing by setting an only (high resolution) hardwareScalingLevel value for all devices was that the GUI in Retina/retina/HDPIs displays was becoming too small and on no-retina displays too big.

The goal was to scale the GUI dimension (and global resolution) to a certain value for retina/HDPI displays (without compromising performances) and to another value for no-HDPI displays.

So in order to set different scaling parameters for various pixel ratios, I finally made it simpler as follow:

```
        const pixelRatio = window.devicePixelRatio;

        if ( pixelRatio >= 1) {
            scalingValue = 1.6 ; // HDPI displays
            } 
        else {
            scalingValue = 1.2; // no-HDPI displays
            };

        engine.setHardwareScalingLevel(scalingValue / pixelRatio);

```

I just tried on retina and normal displays and works perfect. I will try as well with other HDPI devices (@brianzinn like some android phones) and in case add other conditions to have all cases right.

Thanks again all for the support!
