Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7679001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:46:34+00:00 2026-05-31T17:46:34+00:00

Context: multi-user app (node.js) – 1 painter, n clients Canvas size: 650×400 px (=

  • 0

Context: multi-user app (node.js) – 1 painter, n clients

Canvas size: 650×400 px (= 260,000 px)

For the canvas to be updated frequently (I’m thinking about 10 times a second), I need to keep the data size as small as possible, especially when thinking about upload rates.

The toDataURL() method returning a base64 string is fine but it contains masses of data I don’t even need (23 bit per pixel). Its length is 8,088 (without the preceding MIME information), and assuming the JavaScript strings have 8-bit encoding that would be 8.1 kilobytes of data, 10 times per second.

My next try was using JS objects for the different context actions like moveTo(x, y) or lineTo(x, y), sending them to the server and have the clients receive the data in delta updates (via timestamps). However, this turned out to be even less efficient than the base64 string.

{ 
    "timestamp": 0, 
    "what": { 
       "name": LINE_TO, 
       "args": {"x": x, "y": y}  
    } 
}

It doesn’t work fluently nor precisely because there are nearly 300 lineTo commands already when you swipe your brush shortly. Sometimes there’s a part of the movement missing (making a line straight instead of rounded), sometimes the events aren’t even recognized by the script client-side because it seems to be “overwhelmed” by the mass of events triggered already.

So I have to end up using the base64 string with its 8.1 KB. I don’t want to worry about this much – but even if done asynchronously with delta updates, there will be major lags on a real server, let alone the occasional bandwidth overrun.

The only colors I am using are #000 and #FFF, so I was thinking about a 1-bit data structure with delta updates only. This would basically suffice and I wouldn’t mind any “color” precision losses (it is black after all).

With most of the canvas being white, you could think of additional Huffman run-length encoding to reduce size even further, too. Like a canvas with a size of 50×2 px and a single black pixel at (26, 2) would return the following string: 75W1B74W (50 + 25 white pixels, then 1 black pixel, then 24 more white pixels)

It would even help if the canvas consisted of a 1-bit string like this:

00000000000000000000000000000000000000000000000000 
00000000000000000000000001000000000000000000000000

That would help a lot already.

My first question is: How to write an algorithm to get this data efficiently?

The second is: How could I pass the pure binary canvas data to the clients (via node server)? How do I even send a 1-bit data structure to the server? Would I have to convert my bits to a hexadecimal (or more) number and re-parse?

Would it be possible to use this as a data structure?

Thanks in advance,

Harti

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:46:35+00:00Added an answer on May 31, 2026 at 5:46 pm

    I sorted it out, finally. I used an algorithm to get the image data within a specified area (i.e. the area currently drawn on), and then paste the image data to the same coordinates.

    1. While drawing, I keep my application informed about how big the modified area is and where it starts (stored in currentDrawingCoords).

    2. pixels is an ImageData Array obtained by calling context.getImageData(left, top, width, height) with the stored drawing coordinates.

    3. getDeltaUpdate is called upon onmouseup (yeah, that’s the drawback of the area idea):

      getDeltaUpdate = function(pixels, currentDrawingCoords) {   
          var image = "" + 
              currentDrawingCoords.left + "," + // x
              currentDrawingCoords.top + "," + // y
              (currentDrawingCoords.right - currentDrawingCoords.left) + "," + // width
              (currentDrawingCoords.bottom - currentDrawingCoords.top) + "";  // height
          var blk = 0, wht = 0, d = "|";
      
          // http://stackoverflow.com/questions/667045/getpixel-from-html-canvas
          for (var i=0, n=pixels.length; i < n; i += 4) {
                  if(
                      pixels[i]   > 0 ||
                      pixels[i+1] > 0 ||
                      pixels[i+2] > 0 ||
                      pixels[i+3] > 0
                  ) {
                      // pixel is black
                      if(wht > 0 || (i == 0 && wht == 0)) {
                          image = image + d + wht;        
                          wht = 0;
                          d = ",";
                      }
                      blk++;
      
                      //console.log("Pixel " + i + " is BLACK (" + blk + "-th in a row)");
                  } else {
                      // pixel is white 
                      if(blk > 0) {
                          image = image + d + blk;        
                          blk = 0;
                          d = ",";
                      }
                      wht++;
      
                      //console.log("Pixel " + i + " is WHITE (" + blk + "-th in a row)");
                  }
          }
      
          return image;   
      }
      
    4. image is a string with a header part (x,y,width,height|...) and a data body part (...|w,b,w,b,w,[...])

    5. The result is a string with less characters than the base64 string has (as opposed to the 8k characters string, the delta updates have 1k-6k characters, depending on how many things have been drawn into the modification area)

    6. That string is sent to the server, pushed to all the other clients and reverted to ImageData by using getImageData:

      getImageData = function(imagestring) {
          var data = imagestring.split("|");
          var header = data[0].split(",");
          var body = data[1].split(",");
      
          var where = {"x": header[0], "y": header[1]};
          var image = context.createImageData(header[2], header[3]); // create ImageData object (width, height)
      
          var currentpixel = 0,
          pos = 0,
          until = 0,
          alpha = 0,
          white = true;
      
          for(var i=0, n=body.length; i < n; i++) {
              var pixelamount = parseInt(body[i]); // amount of pixels with the same color in a row
      
              if(pixelamount > 0) {
                  pos = (currentpixel * 4);
                  until = pos + (pixelamount * 4); // exclude
      
                  if(white) alpha = 0;
                  else alpha = 255;
      
                  while(pos < until) {
                      image.data[pos] = 0;
                      image.data[pos+1] = 0;
                      image.data[pos+2] = 0;
                      image.data[pos+3] = alpha;                  
                      pos += 4;
                  }
                  currentpixel += pixelamount;
                  white = (white ? false : true);
              } else {
                  white = false;  
              }
          }
      
          return {"image": image, "where": where};
      }
      
    7. Call context.putImageData(data.image, data.where.x, data.where.y); to put the area on top of everything there is!

    As previously mentioned, this may not be the perfect suit for every kind of monochrome canvas drawing application since the modification area is only submit onmouseup. However, I can live with this trade-off because it’s far less stressful for the server than all the other methods presented in the question.

    I hope I was able to help the people to follow this question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Context: There's an application where you draw things on canvas. Where user clicks there's
Context: I have a WPF App that uses certain unmanaged DLLs in the D:\WordAutomation\MyApp_Source\Executables\MyApp
I'm developing a multi device App with separate views for iPhone and iPad. Within
First of all, there is no chance that this is a multi-user issue, as
This is my code. I want to recognize the multi-stroke gesture in my app
I want to make editable cells with multi-lines content in QTreeWidget and I use
Context: I'm in charge of running a service written in .NET. Proprietary application. It
Context: So, I am attempting to build a ridiculously complex domain model. Talking with
Context : programming a c/c++ win32-mfc library How to know whether we are in
Context PHP 5.3.x Overview After doing a code-review with an associate who uses both

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.