JavaScript How do I take grayscale values of an image and put it into an array of pixels? (html canvas) [duplicate]

I draw an image in html canvas.

This is how you calculate the pixels

for (var i = 0; i < data.length; i += 4) {

          var red = data[i]; // red
          var green= data[i + 1]; // green
          var blue = data[i + 2] ; // blue
             }

and this is how you create the base64 code out of it

var dataURL = canvas.toDataURL('image/png');

But this creates base64 code for a png image with RGBa. What do I do to create the base64 code from only the first channel data[i] is there a way to manually encode it to base64 or give the toDataURL options to do so? I tried looking up the documentation but I couldn't find anything

Thanks

EDIT

edit with markE suggestion:

    var imageData = ctx.getImageData(0,0,cw, ch);
     var data= imageData.data        
for (var i = 0; i < data.length; i += 4) {
          var grayscale = data[i]*0.2126 + data[i +1]*0.7152 + data[i +2]*0.0722;
          data[i]     = grayscale; // red
          data[i + 1] = grayscale; // green
          data[i + 2] = grayscale; // blue
             }


var binary = new Uint8Array(data.length);
for (var i=0; i<data.length; i++){
  binary[i]=data[i];
}
var s=binary.toString();
 var base = btoa(s);
Answer:1

I am using tableexport.js to export my html table into different formats like excel, csv, pdf etc. In tableexport.js, there is code for filename and extension which is not working. window.open('...

I am using tableexport.js to export my html table into different formats like excel, csv, pdf etc. In tableexport.js, there is code for filename and extension which is not working. window.open('...

  1. tableexport jquery plugin download
  2. tableexport.jquery.plugin
  3. tableexport.jquery.plugin example
  4. tableexport.jquery.plugin cdn
  5. tableexport.jquery.plugin/html2canvas.js
  6. tableexport.jquery.plugin npm
  7. tableexport.jquery.plugin xlsx
  8. tableexport.jquery.plugin-master
  9. tableexport.jquery.plugin demo
  10. tableexport.jquery.plugin not working
  11. tableexport.jquery.plugin header
  12. tableexport.jquery.plugin/tableexport.min.js
  13. kayalshri/tableexport.jquery.plugin
  14. github table export jquery plugin
  15. html table export jquery plugin
  16. html table export jquery plugin example
  17. tableexport.jquery.plugin/master/tableexport.js
  18. how to use tableexport.jquery.plugin
  19. tableexport.jquery.plugin hhurz

So i have a modal that pops up, it makes an ajax call and gives me 3 variables back to show. In the modal there is a save button and cancel button, if the save button is clicked i want to make another ...

So i have a modal that pops up, it makes an ajax call and gives me 3 variables back to show. In the modal there is a save button and cancel button, if the save button is clicked i want to make another ...

I was reading this JavaScript code. What I don't understand, is why the author does this: (function(namespace) { //more stuff here namespace.Game = Game; })(window); What is the purpose of ...

I was reading this JavaScript code. What I don't understand, is why the author does this: (function(namespace) { //more stuff here namespace.Game = Game; })(window); What is the purpose of ...

  1. using namespace js
  2. namespace with javascript

Hi i have the following td element: <td ng-model="name" contenteditable='true'></td> Is there anyway I can pass this ng-model value from the contenteditable td to my controller? Thanks ...

Hi i have the following td element: <td ng-model="name" contenteditable='true'></td> Is there anyway I can pass this ng-model value from the contenteditable td to my controller? Thanks ...