Navigate through the overlay div to access the canvas, with a delegated click event triggered by the coordinates

Currently, my canvas application does not have a built-in zoom and pan feature.

To work around this limitation, I have created an overlay on top of the canvas with pointer-events: all and opacity: 0, allowing users to zoom and pan seamlessly.

However, I am facing a challenge in delegating a click from the overlay to the canvas. In my attempt to simulate a click by capturing touch-click coordinates and triggering it again with pointer-events: none, I have encountered issues.

$(document).on('touchstart', function (event) {
  showCoordinates(event);
});

function showCoordinates(event) {
  var x = event.touches[0].clientX;
  var y = event.touches[0].clientY;
  console.log(x);
  console.log(y);
  $('#overlay').css('pointer-events','none');
  var myEvent = $.Event( "touchstart", { pageX:x, pageY:y } );
  $("body").trigger(myEvent);
  $('#overlay').css('pointer-events','all');
}

The major issue lies in the part where I try to simulate the click action:

var myEvent = $.Event( "touchstart", { pageX:x, pageY:y } );
$("body").trigger(myEvent);

This approach is failing even when tested in Google Chrome's console. What could be the mistake in my implementation?

Answer №1

The main issue here is that the event being created does not include the event.touches[0].clientX and event.touches[0].clientY properties required by the showCoordinates function. To address this, update the code as follows:

var myEvent = $.Event( "touchstart", { touches: [{clientX: x, clientY: y}] } );

In addition, make sure to assign values to variables x and y in the closing line. If issues persist, such as potential infinite recursion, consider including relevant HTML code for further assistance. It's possible that there may be a simpler solution available.

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

Improving the appearance of dates and times in JSON replies using JavaScript

Upon clicking a link, my JavaScript triggers an AJAX call to retrieve JSON data, which is then used to populate a modal. The link triggering the JavaScript code is as follows: <?= $this->Html->link(__('View'), ['action' => ...

Is it possible to concurrently run two instances of ReactI18Next within a single application?

I'm currently attempting to implement 2 separate instances of React-i18Next within the same application. My goal is to have certain parts of the app translated with instance1 and other parts with instance2. I've familiarized myself with createIn ...

Creating elegant Select Dropdown Box in AngularJS without relying on images

I am currently working on an angular page and have implemented a dropdown with ng-Options to fetch and set values successfully. However, I am now looking to enhance the appearance of this dropdown. After exploring options like ui-select, I realized that be ...

Distinct elements within a JavaScript hash

Is there a jQuery method to add a hash into an array only if it is not already present? var someArray = [ {field_1 : "someValue_1", field_2 : "someValue_2"}, {field_1 : "someValue_3", field_2 : "someValue_4"}, {field_1 : "someValue ...

Having a hard time implementing a subtracting callback function in a JavaScript reduce function

Currently, I am tackling a code challenge in JavaScript that requires me to develop a function called reduce. This function is responsible for reducing a collection to a value by applying a specific operation on each item in the collection over which it it ...

Verify that the message remains at the bottom of the contact form after submitting

I'm running into an issue with my confirmation message when submitting a form. I've set up a contact form using Bootstrap, jQuery, AJAX, and PHP (mail function) for validation. The desired behavior is to hide the form upon submission and display ...

Adjust the spacing between the button and input field in a fixed position form

I have created a sticky form that is fixed at the bottom of the page. How can I reduce the gaps in this sticky form? between the input field and the button between the button and the frame between the input field and the frame Here is the link t ...

The individual blog pages on my site are not receiving the static files they need to display

I'm facing an issue serving a css file to individual blog posts on my website's blog section. Here's how it's supposed to work: Visit /blog- you'll see the main blog page, which is functioning fine. However, when trying to access ...

The Owl carousel seems to be malfunctioning as there are no error messages displayed and the div containing it disappears unexpectedly

I'm having an issue with Owl carousel. I've included the necessary owl.carousel.css, owl.carousel.js, and owl.theme.css files. Created a div with the class "owl-carousel", and called the function $(".owl-carousel").owlCarousel();. However, after ...

Bootstrap 5 - multiple columns of equal width, except for one that fills the remaining space

Imagine a situation where I am using Bootstrap 5 and have the following code snippet... <div class="container"> <div class="row row-cols-auto"> <div class="col">One</div> <div class="c ...

What is the proper way to define the routes for an ajax button_to?

I have successfully integrated an ajax-powered button to manage adding and removing a calendar event from a user's "wish list". The button performs as a toggle-switch, allowing users to add events if they are not already on the wish list, and remove t ...

The hover effect is being applied exclusively to the final React component

When using next.js and tailwindCSS for styling, I created a custom component that includes tags. The issue arises when multiple instances of this component are placed on the page, as only the last one is getting the desired hover effect. "index.js" import ...

Issue with Javascript and Ajax function not resolving

I'm currently working on a feature that updates my database when a user clicks a specific link. Each link has a different value - for example, one increases by 1, another by 2, and so forth. My goal is to submit a form to a separate page with the dat ...

Replace the checkbox display heading with a text box in the designated area

I'm new to using kendo ui Currently, I am attempting to show a text box in the first column header. Instead of the checkboxDisplay heading, I want to replace it with a text box. Could someone please provide guidance on how to resolve this issue? Here ...

Testing a function that utilizes an asynchronous function with Jasmine

Explanation on how to validate this function: let self = this; self.someVariable = "default value"; self.test = function() { self.functionWhichReturnsPromise().then(function() { self.someVariable = "updated value"; }); } A test scenario is pro ...

Utilize jQuery's .buttonset function on an element that has not finished loading yet

I have a functioning code snippet that I'm happy with: $field2.on("change", "#selector", function(){ $field2.load("./index.php?show-options=true", { value: $(this).val() }, function() { $("#options").buttonset(); }); } ...

Tips for transforming promise function into rxjs Observables in Angular 10

As a beginner in typescript and angular, I am trying to understand observables. My query is related to a method that fetches the favicon of a given URL. How can I modify this method to use observables instead of promises? getFavIcon(url: string): Observ ...

Disable visual image preview upon selecting the Clear button

I managed to successfully implement a feature that allows users to upload an image and preview it on the form. However, I'm facing difficulty in removing the preview of the image when the clear button is selected. Below is the script responsible for ...

Dynamic calendar with flexible pricing options displayed within each cell

I've been wracking my brain over this issue for quite some time now, but still can't seem to find a solution! Is there a React Calendar out there that allows for adding prices within the cells? I simply want to show a basic calendar where each c ...

Received a Vue prop as a variable name, rather than the actual string value it represents

In the parent component, my string variable is being passed down. The constant GET_STARTED is equal to the string "Get Started" <script setup> import { GET_STARTED } from '../../constants' import GreenBtn from '@/components/p ...