M
M
Millerish2017-01-31 15:43:45
JavaScript
Millerish, 2017-01-31 15:43:45

JS: how to accurately count pixels?

Help to accurately calculate the number of pixels. I'm using the fabricjs library and I need to be able to get the total area (of any combination of shapes).
To solve this problem, I count the pixels on the canvas:

function aaa() {
    var myConvasPosion = myConvas.getBoundingClientRect();
    var count = 0;
    for(var i = myConvasPosion.left; i < myConvasPosion.left + myConvasPosion.width; i++){
         for(var j = myConvasPosion.top; j < myConvasPosion.top + myConvasPosion.bottom; j++){
            var c = canvas.getContext('2d');
            var p = c.getImageData(i, j, 1, 1).data;
            if(p[0] == 236 && p[1] == 205 && p[2] == 174){
                count = count + 1;
            }
         }
    }
   console.log(count);
}

But the dimensions are constantly inaccurate, the discrepancy is approximately - + 1/20.
Video bug
Link to calculator
How to get a more accurate value?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question