Prevent pinch zoom in webkit (or electron)

Is there a way to prevent pinch zoom in an electron app?

I've tried different methods, such as using event.preventDefault() on touchmove/mousemove events in JavaScript, adding meta viewport tags in HTML, adjusting -webkit-text-size-adjust in CSS, and exploring electron flags/configurations.

Unfortunately, it appears that the --disable-pinch flag is not supported by electron. I couldn't find a solution following the advice given in this Stack Overflow thread.

Do you know of any method, whether for webkit in general or specifically for electron, to disable pinch zooming?

Answer №1

Latest Update:

For those using version 0.31.1 or higher, it is recommended to utilize webFrame.setZoomLevelLimits in the render process. This method proves effective for managing smart zoom on Mac, especially when dealing with document.addEventListener.

Here's an example:

require('electron').webFrame.setZoomLevelLimits(1, 1)


Earlier Update:

The deltaY property for pinch zoom now returns a float value, while normal scroll events still produce an int value. This solution also addresses any issues related to the Ctrl key functionality.

Explore Demo 2.

document.addEventListener('mousewheel', function(e) {
  if(e.deltaY % 1 !== 0) {
    e.preventDefault();
  }
});

Upon inspecting with Chromium's monitorEvents(document), I discovered that the event responsible for this behavior is mousewheel. However, discerning the disparity between regular scrolling and pinch zoom remains a challenge.

During pinch zoom, the attribute e.ctrlKey = true, whereas conventional scrolling events have e.ctrlKey = false. It's worth noting that holding down the ctrl key while scrolling will set e.ctrlKey to true.

Regrettably, a definitive solution continues to elude me. :(

Check out the working demo here: Demo

document.addEventListener('mousewheel', function(e) {
  if(e.ctrlKey) {
    e.preventDefault();
  }
});

Answer №2

Preventing pinch zoom on desktop browsers can be quite challenging.

However, there are a few tactics to consider:

1) Utilize gesture JavaScript libraries such as hammer.js to detect Pinch events and use e.preventDefault to try and prevent them.

OR

2) Design elements using "%" in CSS or newer units like "vm" to ensure content remains consistent even when the page is zoomed.

Good luck implementing these strategies!

Answer №3

Response sourced from GitHub:

"To prevent zooming from the main process, you can utilize the following code:"

const webContents = mainWindow.webContents;
webContents.on('did-finish-load', () => {
  webContents.setZoomFactor(1);
  webContents.setVisualZoomLevelLimits(1, 1);
  webContents.setLayoutZoomLevelLimits(0, 0);
});

The variable mainWindow pertains to where you instantiate new BrowserWindow, for example:

const mainWindow = new BrowserWindow({
  width: 440,
  height: 750,
  // ...
});

const webContents = mainWindow.webContents;
webContents.on("did-finish-load", () => {
  webContents.setZoomFactor(1);
  webContents.setVisualZoomLevelLimits(1, 1);
  webContents.setLayoutZoomLevelLimits(0, 0);
});

Answer №4

Have you considered using the following code instead?

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"/>

This can be placed in the header to prevent devices from zooming in.

Answer №5

After an extensive search for a solution to my problem, I finally came across a plugin called fullpage.js that effectively prevented pinch zoom while still enabling touch gestures. Through the process of eliminating javascript and css options, I stumbled upon a surprisingly simple fix:

touch-action: none;

By adding this line to my full page element, I was able to successfully block pinch zoom without sacrificing the ability to scale fabricjs objects with pinching. A small victory in my coding journey!

Answer №6

const app = require('electron')
app.commandLine.appendSwitch('disable-pinch');

The solution was discovered by referencing the following two sources:

1 - Link 1

2 - Link 2

Answer №7

For some reason, I found that setting { passive: false } was necessary in order to get it functioning properly.

window.addEventListener('wheel', (e) => {
  if(e.ctrlKey) e.preventDefault();
}, { passive: false });

Answer №8

Ensure the meta tag is correctly set up by including minimum-scale=1.0

<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, user-scalable=no">

If that does not resolve the issue, consider adding both minimum and maximum scale values

<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">

P.S. : This setting will specifically disable zooming functionality on mobile devices.

Answer №9

If you're looking for a quick solution to this issue, you can easily do so by adding a few lines of code to your project's index.html or a similar file. Just include the electron library within the script tag and configure the zoom level as shown below:

<script>
const electron = require('electron'); // Importing electron
electron.webFrame.setZoomLevelLimits(1, 1); // Setting minimum and maximum zoom levels to 1
const { ipcRenderer } = electron;
...
</script>

This method works seamlessly on various devices. Unlike using the viewport meta tag, which tends to only resolve the issue on mobile devices but not desktops.

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

The CSS does not display properly on iPad or mobile devices

Recently, I had the opportunity to design a website for a local music school. This project was a great learning experience that has shown me the importance of continuous improvement and practice. Check out the site here Initially, I wanted to make the w ...

What are the best circumstances to utilize jQuery-UI autocomplete feature?

Looking for assistance in creating an input field that auto-populates user-entered values with database entries. Only values that exist in the database should be accepted. Could someone explain the advantages of using jQuery-ui autocomplete for this task ...

Discovering ways to determine if multiple strings are present within a single string using JavaScript

After writing this function, I noticed it only worked with a single string value. contains(input, words) { let input1 = input.split(' '); for (var i = 0; i < input1.length; i++) { if (input1[i] === words) { ...

React App with Material UI V1-beta Integration

I just installed the Create React App example from Material-UI.com. curl https://codeload.github.com/callemall/material-ui/tar.gz/v1-beta | tar -xz --strip=2 material-ui-1-beta/examples/create-react-app Upon installation, I encountered the following erro ...

The CSS scrollbar is visible on the screen, but for some reason,

I'm seeking a solution to enable scrolling within a div container that can scroll both horizontally and vertically. body { height: 100%; width: 100%; overflow: hidden; } .wrapper { width: 75%; padding: 0px 5px 0px 8px; top: 20px; whit ...

Using mobile devices for multipart forms: managing actions that appear upon selecting a file input

My website features a multipart form where users are required to upload images. <form method="POST" encType="multipart/form-data" action="/uploads" id="forminho"> <div className="btn btn-success" id="submitFileBtn"> Select Image ...

Looking to incorporate AAD calling functionality in a React and Node application by utilizing the Graph API for communication/calls

As a newcomer to Microsoft Azure and its services, I recently registered an application with Azure. However, when attempting to integrate a call feature in my Web App using the graph API '/communication/calls', I encountered the following error m ...

Show a selection of assorted files stored in the database

router.get("/alw", function(req, res){ Product.find({"category": "alw"}, function(err, allProduct){ if (err){ console.log(err) } else { res.render("products/alw", {products ...

Eliminating duplicate loading of jQuery in Django SmartSelect

I'm facing an issue with Django's app, smart select, as it tries to load jQuery on its own. I've already loaded jQuery in the header and then loaded JQuery UI related stuff. However, smartselect also loads jQuery again in the body, causing p ...

The functionalities of $scope and this in AngularJS

Currently, I am developing a small application using angularjs. In this project, I am trying to implement a feature that involves deleting a contact. The functionality itself works perfectly fine, however, I am encountering an issue where the 'this.op ...

Identify whether the final digit falls within the range of 1-4 or 5-9, and then apply styling to the corresponding

First, this is the structure of my table: <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/> <table class="table group-9569" width="100%"> <tbody> <tr> <td class=" ...

Elemental SVG Animation

I'm currently exploring the world of animating svg objects using CSS. I have successfully learned how to animate a path with: @keyframes stroke_fill { 0% { fill: white; } 50% { fill: white; stroke-dashoffset: 0; ...

When should we utilize the React.Children API in React applications?

Exploring the potential use cases of the React.Children API. The documentation is a bit confusing for me (Using React v.15.2.0). https://facebook.github.io/react/docs/top-level-api.html#react.children According to the documentation, this.props.children ...

Can the Apache tomcat located at "localhost:8080/app.html" be accessed from a different network?

Currently, I am hosting my webpage on a Tomcat server. While I can easily access it from different computers on the same network, I am curious if there is any way to access this webpage/localhost from a different network? ...

What does the reportProgress function do in HTTP services with JavaScript?

Can someone explain the functionality of reportProgress in JavaScript, specifically when used with Angular Typescript? I am having trouble finding documentation on this. return this.httpClient.request<ProductResponse>('get',`${this.basePath ...

There appears to be some lingering content in the JQuery Mobile slide-out dialog box

My jQuery mobile slide out dialog box is experiencing an issue. The first time the slide out occurs, a comment box appears where users can enter comments and then submit them, followed by a "THANK YOU" message. However, when the user closes the dialog box ...

Ways to retrieve the page name where the script originates from

I have a function that is triggered from three different pages. Each page involves adding an attribute to a specific div. For instance: <div id="posts" page="home"></div> <div id="posts" page="feed"></div> <div id="posts" page= ...

Issue with undefined bindingContext.$data in IE9 on knockout binding handler

I'm attempting to create a unique binding handler that applies role-based access to fields on a page. This custom handler uses the values of other observables from the viewModel to enable or disable input controls based on certain conditions. However ...

Is there a way to automatically initiate the download of a file (such as a PDF) when a webpage loads?

Currently, my objective is to have a form on a webpage that, once filled out by a user, redirects them to a thank you page where a message of gratitude is displayed. What I aim to accomplish is for a PDF file to automatically start downloading as soon as t ...

Display an alert message using alert() if duplicate data is submitted through ajax

I'm using a form and jQuery to submit the form via AJAX. $("form#form").submit(function (event) { //submitting vendor name event.preventDefault(); var formData = new FormData($(this)[0]); //validation for duplicates goes here ...