Unable to create canvas drawings using fingertips on mobile web browsers

Check out the code snippet below:

canvas = document.getElementById("canvas");
ctx = canvas.getContext('2d');
tmp_ctx = element[0].getContext('2d');

element.bind('mousemove touchmove', function(event){
                if(drawing){
                    if(event.offsetX!==undefined){
                        currentX = event.offsetX;
                        currentY = event.offsetY;
                    } else {
                        currentX = event.layerX - event.currentTarget.offsetLeft;
                        currentY = event.layerY - event.currentTarget.offsetTop;
                    }

                    if(currentTool=="karandaw" || currentTool=="lastik" || currentTool=="brush"){
                        drawPen(lastX, lastY, currentX, currentY);
                    }
                    if(currentTool=="oval"){
                        console.log("drawing ellipse");
                        drawEllipse(initX, initY, event.offsetX, event.offsetY);
                    }
                    if(currentTool=="crop"){
                        drawCrop(initX, initY, event.offsetX, event.offsetY);
                    }

                    lastX = currentX;
                    lastY = currentY;
                }
            });


function drawEllipse(x1, y1, x2, y2){
                console.log(tmp_ctx);
                tmp_ctx.clearRect(0, 0, tmp_canvas.width, tmp_canvas.height);
                tmp_ctx.setLineDash([1,0]);
                tmp_ctx.beginPath();
                tmp_ctx.lineWidth = 3;
                tmp_ctx.strokeStyle = 'rgba('+curColor.r+','+curColor.g+','
                    +curColor.b+','+curColor.a+')';
                ctx.globalCompositeOperation='source-over';
                var mpx = (x1+x2)/2;
                var mpy = (y1+y2)/2;
                tmp_ctx.ellipse(mpx, mpy, Math.abs(mpx-x1), Math.abs(mpy-y1), 0, 0, Math.PI*2);
                tmp_ctx.stroke();
                tmp_ctx.closePath();
            }

The code functions correctly on desktop browsers, but it's not rendering anything on mobile devices despite the logs confirming that the drawing functions are being invoked.

I need help identifying any potential errors in the code provided.

HTML for the Canvas Elements:

 <div id="canvas_container">
                        <canvas id="canvas"></canvas>
                        <canvas id="tmp_canvas" drawing></canvas>
                        <div id="canvas_area_1" ng-show="canvas_area_1"></div>
                        <div id="canvas_area_2" ng-show="canvas_area_2"></div>
                        <div id="canvas_area_3" ng-show="canvas_area_3"></div>
                    </div>

Additional Information:

EDIT 1: Mobile browser used was Chrome version 47

EDIT 2: The functionality is implemented using a directive with AngularJS. Below is the complete directive code:

app.directive("drawing", function(){
    //Directive implementation here...
});

Answer №1

It seems like you're referring to using jquery.bind() instead of native javascript .bind();

If you decide to stick with pure JavaScript, consider using addEventListener.

function handleMove(event){
    if(moving){
       if(event.offsetX !== undefined){
           positionX = event.offsetX;
           //.... additional code here
       }         
    }
}
element.addEventListener('touchmove', handleMove);
element.addEventListener('mousemove', handleMove);

Instead of $(element).bind

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

Transfer Data from a Factory to a Controller in AngularJS

Although it may seem like a simple question, it has taken me nearly 3 hours to try and figure out what went wrong here. Perhaps someone could help identify the issue and provide a solution (it seems like an easy fix, but I'm just not seeing it). So, h ...

Canvas 'toDataURL' Execution Error Occurs with Angular Dynamic Routing

For my Web application, I am using AngularJS on top of Node/Express. One of the routes in my application has an HTML5 canvas which I convert to an image. When I navigate to that route directly with this code, there are no problems with converting the canva ...

The Mysteries of Key Codes in JavaScript

I'm curious about the character codes used for keyboards. Recently, I came across an informative article discussing this topic. In particular, I am interested in finding out which key code to use when a user presses both the alt and down arrow keys s ...

Developing an Addon for Firefox using XUL and JavaScript

I am interested in creating a Firefox Addon that dynamically generates a button in the webpage's DOM when a specific page like www.google.com is loaded. This button, when clicked, will redirect to another page such as www.stackoverflow.com. It is imp ...

AngularJS routing always displays 'index.html#!' in the URL

In my AngularJS app within a website project in Visual Studio, I am utilizing ui-router. The issue is that it only functions with the route http://localhost/index.html#!/my-page, when what I desire is for the route to simply be http://localhost/my-page. I ...

How come jQuery won't let me activate the 'change' event on a radio button?

Click the link below to access the page: Below are the original javascript codes that are functioning correctly: $(document).ready(function(){ $(".open_gene").on('change', function(event) { $('#Gene_field').show(); }); ...

Having trouble with the join and leave command? The leave function seems to be malfunction

I've written a code for joining and leaving, but I'm having trouble with the leave function. (The join function works fine, but every time I try to leave, it crashes) > client.on('message', async message => { if (!message.gui ...

Sending data in JSON format using the POST method with Restangular in an AngularJS application

I'm a newcomer to Angular JS and I need to use restangular POST to send form data in JSON format. I'm also unfamiliar with the POST functionalities, can someone guide me on how to achieve this? My Index Page: <form ng-controller="registerCtr ...

The error message "TypeError: 'undefined' is not an object ('_this.props')" indicates that the property '_this

Having trouble solving this issue. Need assistance with evaluating 'this.props.speciesSelection.modalize' <BarcodeInput speciesSelection={this.props.speciesSelection} species={species[0]} barcode={{ manufacturerValue: ...

The screen-responsive navigation bar is experiencing functionality issues

I am facing an issue with my navigation bar on both desktop and mobile. When I maximize the window while the mobile navbar is open, it disappears as expected but the desktop navbar does not appear. I am using a bootstrap template and I am unsure if solving ...

I desire to perform a specific task when there is a modification in the path using react router framework

Though I am mindful of it. props.history.listen((location, action) => { console.log("when route changes",location); }) However, I need to implement it in a slightly different way. For instance, let's cons ...

What is the best way to transmit data to a PHP script using AJAX?

I'm facing an issue with my basic ajax script not running. The code I have should work fine. The ajax script is: <html> <head><title>Testing ajax</title> <script type="text/javascript"> function ajax() { ...

Is it possible to update the content page without having to refresh the master page?

Our website features a Master page with main menu options and corresponding sub menus. When a user clicks on a main menu item, the relevant sub menus should be displayed. After clicking on a sub menu, the content page should update without having to refr ...

Implement a click event using jQuery specifically for Internet Explorer version 7

How can I add an onclick attribute using jQuery that is compatible with IE7? So far, the following code works in browsers other than IE8 and Mozilla: idLink = Removelst(); var newClick = new Function(idLink); $(test1).attr('onclick', null).clic ...

Executing code on the server-side (back-end) using Javascript: A step-by-step guide

Imagine wanting to execute JavaScript on the server side instead of exposing the code to the client. For example: Starting with the JS native window Object from the browser. Performing all JavaScript processing in the backend by passing the window Object ...

Exploring Node troubleshooting with WebPack and Feathers

Currently, I am part of a team working on a project that involves using Node, Webpack, TypeScript, and Express/Feathers. While my fellow developers are experienced in these technologies, I have limited experience with JavaScript mainly on the client-side a ...

Combining Array Attributes to Create a New Property as a 'JSON String'

I'm attempting to combine the attributes of an array into a JSON-like string as a new attribute. For example: [{ { "orderNo":"1", "description":"Item 1", "note": "Note 1" }, { "orderNo":"2", "description":"Item 2", ...

Retention of user-entered data when navigating away from a page in Angular

I need to find a way to keep the data entered in a form so that it remains visible in the fields even if the user navigates away from the page and then comes back. I attempted to use a solution from Stack Overflow, but unfortunately, it did not work as exp ...

Include backing for the trial syntax 'classProperties' within an npm module

I am facing a challenge while trying to publish an npm package containing some functions for my create-react-app project. The functions work fine when I import them from the js file within the create-react-app project, but I encounter an error once I insta ...

Functionality not functioning within Shadow DOM

After creating and exporting an Angular Element as a single script tag (user-poll.js), using the element on a host site is simple. Just include the following two lines: <user-poll></user-poll> <script src="path/to/user-poll.js"></sc ...