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

Prevent any future dates from being entered

My Thoughts: <input type="text" class="datepicker" placeholder="DD/MM/YYYY" id="datepicker" ng-model="datepicker" name="datepicker" style="width:100%" tabindex="4" required/>` Controller: `$('#datepicker').datepicker({ format: &a ...

Creating a CSS animation to slide a div outside of its container is

I currently have a flexbox set up with two adjacent divs labeled as DIV 1 and DIV 2. By default, both DIV 1 and DIV 2 are visible. DIV 2 has a fixed width, occupying around 40% of the container's width. DIV 1 dynamically adjusts its width to ac ...

Incorporating SQLSRV results into clickable <td> elements: A dynamic approach

As a newcomer to the world of JS/PHP/web development, I'm seeking help with a seemingly simple task. My goal is to make each <td> element in a table clickable, and retrieve the text contained within the clicked <td>. Currently, I have a S ...

Divide the parsed rss findings into separate parts

I am in the process of developing a project that involves aggregating job listings from various websites by parsing their RSS feeds. I am utilizing rss-parser for this purpose. Each feed has its own format for the title field, resulting in varying structu ...

Unable to proceed due to lint errors; after conducting research, the issue still remains

I'm still getting the hang of tslint and typescript. The error I'm encountering has me stumped. Can someone guide me on resolving it? I've searched extensively but haven't been able to find a solution. Sharing my code snippet below. (n ...

Discover the steps to retrieve a fresh array in PHP after utilizing sortable.js

Hi there! I am currently utilizing the sortable javascript feature found on Although the script is working perfectly fine for me, being a non-expert in javascript poses a challenge when it comes to generating the necessary code to retrieve an array in php ...

Numerous applications of a singular shop within a single webpage

I have a store that uses a fetch function to retrieve graph data from my server using the asyncAction method provided by mobx-utils. The code for the store looks like this: class GraphStore { @observable public loading: boolean; @observable ...

Having trouble choosing an option from the dropdown menu with Puppeteer Js

I need help with Puppeteer JS to select the initial element in a dropdown. Any suggestions? Once I input the city name in the text field, I want to choose the first option from the dropdown menu. const puppeteer = require('puppeteer'); (async ...

Tips for preventing 'Index out of Bound' errors when working with Protractor ElementArrayFinder

I'm completely new to Protractor and I've just started using it in combination with chai and chai-as-promised. Right now, I'm trying to find the best approach for handling a situation where my ElementArrayFinder doesn't contain the elem ...

The issue with Ajax file upload is that it only processes the first file in the filelist array for

I am struggling with an issue while using jquery and materialize for asynchronous file upload and form submit. The code seems to work fine when I use get(0).files[0], but it only returns the first file at index [0]. However, when I attempt to loop through ...

Whenever I am building a React application, I encounter a bug that states: "node:fs:1380 const result = binding.mkdir()"

Whenever I try to enter the command: create-react-app my-app --template typescript I keep encountering this error message: node:fs:1380 const result = binding.mkdir( ^ Error: EPERM: operation not permitted, mkdir 'D:\ ...

Parsing of CSS and Javascript is disabled within iframes

Within my node.js application, I have configured an endpoint where I can load some parsed HTML code. This is achieved through the following code: app.get('/code', function (req, res) { res.setHeader('Content-Type', 'text/html& ...

What is the method for incorporating locales into getStaticPaths in Next.js?

I am currently utilizing Strapi as a CMS and dealing with querying for slugs. My goal is to generate static pages using getStaticPaths and getStaticProps in Next.js. Since I'm working with multiple locales, I have to iterate through the locales to re ...

AngularJS: Enhancing Dropdown Menus

I currently have a pair of drop down lists. The first dropdown looks like this: <select ng-model="vm.eesAdminSetupData.Type" class="form-control" id="Type" name="Type" required> <option value="Form">Form</option> <option value="List ...

Is this filter selector in jQuery correct?

It appears to function correctly, but I am unsure if there is room for improvement. I aim to target any HTML element with a class of edit-text-NUM or edit-html-NUM and adjust its color. This is the code snippet I am currently utilizing... jQuery(document ...

Storing an HTML page in a PHP variable, parsing it, and displaying it correctly

I am currently using the simple_html_dom parser to extract information from an HTML page. After making some modifications, I would like to display this content on my own page. $page = file_get_html($url); foreach($page->find('div#someDIv') as ...

Changing a particular field value within an array of objects in Angular 4

I have a scenario where I created a class called security: class security { public id:number; public name:string; } Next, I initialized an array of security objects: let s:security[]=[{id:1,name:'Alex'},{id:2,name:'John'},{id:3,nam ...

Tips for resolving the error message "Uncaught TypeError: Cannot read property '0' of undefined" in React rendering

When I try to map through my list in the render function, it doesn't work, even though it works fine within my component class DesktopList extends Component { constructor(props) { super(props); this.state = { i ...

The audio event continues to trigger even after it has been removed using removeEventListener

In my React component, specifically handling an audio track with an HTML <audio> element, I have implemented the following lifecycle methods: componentDidMount() { const {track} = this.props; this.refs.audio.src = track.getTrackUrl(); _.each(t ...

What could be the reason for XMLHttpRequest's readystate being something other than 4?

Currently delving into the realm of AJAX for the first time, and endeavoring to construct a basic application that computes the required grade on a final exam based on past assessment results. The user keys in their grades into the form, subsequent to whi ...