Open In App

Emulation in ElectronJS

ElectronJS is an Open Source Framework used for building Cross-Platform native desktop applications using web technologies such as HTML, CSS, and JavaScript which are capable of running on Windows, macOS, and Linux operating systems. It combines the Chromium engine and NodeJS into a Single Runtime.

In traditional web applications, we have the Toggle View functionality in which we are given the choice of whether we would like to view the Desktop version or the Mobile version of the website. This depends upon the device from which we are currently viewing the website. This is also a part of Emulation. Emulation refers to the ability of software to emulate (or imitate) another program or take on characteristics that are different from the original/intended one. This is done to enhance the user experience or provide developers with the ability to test the application on different platforms/characteristics. Electron provides us with a way by which we can Emulate a Desktop or a Mobile device using the Instance methods and events of the built-in BrowserWindow object and the webContents property. This tutorial will demonstrate how to Emulate a device in Electron.



We assume that you are familiar with the prerequisites as covered in the above-mentioned link. For Electron to work, node and npm need to be pre-installed in the system.


Example: Follow the Steps given in Dynamic Styling in ElectronJS to set up the basic Electron Application. Copy the Boilerplate code for the main.js file and the index.html file as provided in the article. Also, perform the necessary changes mentioned for the package.json file to launch the Electron Application. We will continue building our application using the same code base. The basic steps required to set up the Electron application remain the same. 
package.json: 



{
  "name": "electron-emulate",
    "version": "1.0.0",
      "description": "Device Emulation in Electron",
        "main": "main.js",
          "scripts": {
    "start": "electron ."
  },
  "keywords": [
    "electron"
  ],
    "author": "Radhesh khanna",
      "license": "ISC",
        "dependencies": {
    "electron": "^8.3.0"
  }
}

Output: At this point, our basic Electron Application is set up. Upon launching the application, we should see the following result. 

Device Emulation in Electron: The BrowserWindow Instance and webContents Property are part of the Main Process. To import and use BrowserWindow in the Renderer Process, we will be using Electron remote module. 




<h3>
    Emulation in ElectronJS
</h3>
<button id="emulate">
    Emulate Mobile
</button>
<button id="desktop">
    Emulate Desktop
</button>
<button id="disable">
    Disable Device Emulation
</button>




// Importing BrowserWindow from Main
// Process with Electron remote
const BrowserWindow = electron.remote.BrowserWindow;
let win = BrowserWindow.getFocusedWindow();
// let win = BrowserWindow.getAllWindows()[0];
 
let emulate = document.getElementById("emulate");
emulate.addEventListener("click", () => {
    win.webContents.enableDeviceEmulation({
        screenPosition: "mobile",
 
        // Defined as Size of Galaxy S5 in
        // Chromium Browser
        screenSize: {
            width: 360,
            height: 640,
        },
        viewPosition: {
            x: 0,
            y: 0,
        },
        // Defined as Size of Galaxy S5 in
        // Chromium Browser
        viewSize: {
            width: 360,
            height: 640,
        },
        deviceScaleFactor: 0,
        scale: 1,
    });
});
 
let desktop = document.getElementById("desktop");
desktop.addEventListener("click", () => {
    win.webContents.enableDeviceEmulation({
        screenPosition: "desktop",
 
        // Same as BrowserWindow Instance
        // in Main Process
        screenSize: {
            width: 800,
            height: 600,
        },
        viewPosition: {
            x: 0,
            y: 0,
        },
        // Same as BrowserWindow Instance
        // in Main Process
        viewSize: {
            width: 800,
            height: 600,
        },
        deviceScaleFactor: 0,
        scale: 1,
    });
});
 
let disable = document.getElementById("disable");
disable.addEventListener("click", () => {
    win.webContents.disableDeviceEmulation();
});

A detailed Explanation of all the Instance methods of the webContents property used in the code is given below. 

To get the current BrowserWindow Instance in the Renderer Process, we can use some of the Static Methods provided by the BrowserWindow object.

Output:


Article Tags :