Android canvas is experiencing a sudden freeze

My canvas-based javascript application is functioning on android, however, I am encountering a problem where dragging across the screen with my finger causes the entire web page to freeze. Is there a solution to prevent this issue from happening?

Answer №1

Your question could benefit from providing more detailed information. It seems like web apps may have some limitations in fully utilizing Android features.

When you mentioned having a javascript application running in a canvas, it gave the impression that you might be using an android canvas in your app.

Here is a helpful link that discusses running html5 canvas on android.

Answer №2

When dealing with a MotionEvent.ACTION_DOWN and MOVE in your code, it is important to have a routine that handles drawing on the canvas without interruptions from other routines.

If you are facing issues or need some guidance, consider the following example:

 protected void onDraw(Canvas canvas) {
  // fills the canvas with black
  canvas.drawColor(Color.BLACK);
  // draw various items on the canvas
  background.draw(canvas);  
  basket.draw(canvas);
  for (int i = 0; i < appleList.size(); i++){
      appleList.get(i).draw(canvas);
  }  
 }

The handling of the motionevent can be implemented as shown below (with relevant comments included within the code):

 @Override
 public boolean onTouchEvent(MotionEvent event) {

 if (!isTouchDisabled){
      if (event.getAction() == MotionEvent.ACTION_DOWN) {

        // add code to transfer the action to the objects
           basket.handleActionDown((int) event.getX(), (int) event.getY());
          }

          if (event.getAction() == MotionEvent.ACTION_MOVE) {
           // handle gestures

        // only pass the move events to the touched objects (determined by previous touch actions)
        if (basket.isTouched()) {
            // the basket is being dragged 
            basket.setX((int) event.getX());
            basket.setY((int) event.getY());

           }
          }

          if (event.getAction() == MotionEvent.ACTION_UP) {
           // Check button interactions
              if (btnMoreFruit.isTouched()){
                  btnMoreFruit.handleActionUp((int) event.getX(), (int) event.getY());
                  btnMoreFruit.setTouched(false);
              }
              if (btnLessFruit.isTouched()){
                  btnLessFruit.handleActionUp(this.getContext(), (int) event.getX(), (int) event.getY());
                  btnLessFruit.setTouched(false);
              }

          }
 }

Answer №3

When a user drags their finger or mouse across a canvas in an app, various processes are triggered based on the code implemented. For instance, painting apps record the XY position of the user's focus each time it transitions between pixels. If the code within these events is heavy, the client may experience delays as it struggles to keep up with the processing demands.

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 request's body in the PUT method is void

I seem to be having an issue with my PUT request. While all my other requests are functioning properly, the req.body appears to remain empty, causing this error message to occur: errmsg: "'$set' is empty. You must specify a field like so: ...

Assigning path handlers to external modules in NodeJS using Express

I've been trying to minimize the complexity in my routes.js file. Check it out: module.exports = function(app, db) { app.get('/', function(req, res) { res.render('index') }); app.get('/contact-us', function(req, re ...

Setting a default value for the longText field in Laravel

I have come across an issue in Laravel where I am unable to assign a default value to longText or text fields. Specifically, I am dealing with a field called "body" that will likely contain some text or HTML. How can I set a default value for this field ...

extract information from an external JSON document

I have a JSON file filled with data, along with a JSX file containing a button and a div. I'm looking to extract the data from the JSON file and display it in the div when the button is clicked. However, I'm at a loss on how to achieve this. The ...

What is the reason for this JavaScript code not activating the input field (aspx)?

Thank you for taking the time to review this. I understand that for most of you client side scripting experts, this task is a breeze. However, all I am trying to achieve is to display a message at the top notifying the user that their click has been regist ...

Can variables be bound bidirectionally between nested isolated scopes?

Looking into the complexities of nested directives, I am facing a challenge in establishing two-way binding between isolate scopes. The main goal is to have the outerPower variable bind to the innerPower variable and update whenever the innerPower value i ...

JavaScript for Detecting Hits

I have recently ventured into coding as a beginner, and I have already created a calculator using Javascript, HTML, and CSS. Currently, I am working on developing a platformer game using JavaScript and HTML. I have set up the player, obstacles, canvas, fra ...

Switch up primary and secondary color schemes with the Material UI Theme swap

Exploring Material UI themes for React JS is a new venture for me. I am facing a scenario where I need to dynamically change the theme colors (Primary & Secondary) based on a selected type from a dropdown menu. Similar to the color customization options av ...

Saving a variable within a for loop in JavaScript

Hey everyone, I could really use your help right now. Here's the code block I'm struggling with: function readyToSubmit(answerPack, answerArr, len) { for (var i = 0; i < answerArr.length; i++) { var questionId = answerArr[i].id; con ...

Vue composable yields a string value

I am currently using a Vue composable method that looks like this: import { ref } from 'vue'; const useCalculator = (num1: number, num2: number, operation: string) => { const result = ref(0); switch (operation) { case 'add& ...

While the NPM package functions properly when used locally, it does not seem to work when installed from the

I have recently developed a command-line node application and uploaded it to npm. When I execute the package locally using: npm run build which essentially runs rm -rf lib && tsc -b and then npm link npx my-package arguments everything works sm ...

Is it possible to dynamically create new input fields by starting their index from the highest number after deletion?

I am working on a feature where users can add new rows with unique IDs and names. Everything works well, but the issue arises when deleting a row other than the last one. Here is an example of my code: $("#add_row").on('click', addRow); funct ...

How can we simplify this React component to reduce its verbosity?

After creating a test project to delve into react, react-router and react-redux, I revisited the Settings.jsx file. Now, I am pondering on how to streamline it and reduce potential errors. import React, { Component } from "react"; import { connect } from ...

Adaptive Container with Images that are not stretched to full width

Is there a way to achieve the same effect as seen in images 2 and 3 here: Although these images already have their own "padding," I'm curious if it can be replicated using just jQuery and CSS? I would appreciate any help or insights on this. Thank y ...

Error alert: Unable to find the specified word

Having trouble writing a word to the database due to an error: ReferenceError: Patikrinta is not defined. Below is my ajax script for sending data to a PHP file, and then the PHP script itself if needed. Haven't found a solution on Stack Overflow. $s ...

Waiting for the code to execute once the filtering process is completed in Next.js using Javascript

I'm seeking a way to ensure that my code waits for the completion of my filter function before proceeding. The issue arises because my filter function, which incorporates another function called useLocalCompare, causes a delay in execution. This delay ...

Executing numerous GET requests with varying parameters in AngularJS

Update: My apologies to those who answered, it turns out that the code was correct, but requests were being intercepted and losing all parameters. I am attempting to send repeated HTTP GET requests to a REST API based on the response, using the solution I ...

Utilizing chrome.scripting to inject scripts in TypeScript

I am currently facing an issue wherein I am attempting to integrate chrome extension JavaScript code with TypeScript: const [tab] = await chrome.tabs.query({ active: true, currentWindow: true }); let result; try { [{ result }] = await c ...

The functionality of window.localStorage.removeItem seems to be malfunctioning when used within an $http

Currently, I am sending a post request to my web service and upon successful completion, I am attempting to remove a token from local storage. Here is the code snippet: $http.post("MyService/MyAction").success(function (res) { if ( ...

Having issues with currency exchange functionality on my Android device

I've encountered a roadblock while working on my Android application. The problem I'm facing is the inability to create an onTabListener for my tabs. I have three tabs with different names, but they all display the same tab count. In my code, I t ...