简体   繁体   中英

Count Amount of Colored Squares in Canvas

Here is the fiddle: http://jsfiddle.net/sw31uokt/

Here is some of the relevant code for the incrementValue function I set up to count overall clicks within the canvas element.

What I would like to do is be able to display a count of each color, so "you have placed 14 red pixels, 3 blue pixels, 4 black pixels'.

function incrementValue()
{
var value = parseInt(document.getElementById('number').value, 10);
value = isNaN(value) ? 0 : value;
value++;
document.getElementById('number').value = value;
}

$(c_canvas).click(function(evt) {

var pos = getNearestSquare(getMousePos(c_canvas, evt));
if (pos != null) {
    context.fillStyle=(currentColor);
    context.fillRect(pos.x,pos.y,19,19);
    incrementValue(); 

}
});

Basically, what MarkE said above ...

In the outer scope, add two new vars :

var palette = ["333333", "0000ff", "a0522d", "46ad42", "808080", "ffc0cb", "d73952", "ffe2a8", "ffff7d", "ffffff"];//as originally defined in the .spectrum() call.

var gridModel = [];//Eventually a sparse array of sparse arrays, representing colored grid squares. Uncolored grid squares remain undefined.

And two new functions, in the same scope :

function updateGridModel(pos, color) {
    var x = (pos.x - 0.5) / 20;
    var y = (pos.y - 0.5) / 20;
    color = color.substr(1).toLowerCase();
    if (!gridModel[x]) {
        gridModel[x] = [];
    }
    gridModel[x][y] = palette.indexOf(color);
}

function paletteTally() {
    //initialise an array, same length as palettes, with zeros
    var arr = palette.map(function () {
        return 0;
    });
    for (var x = 0; x < gridModel.length; x++) {
        if (gridModel[x]) {
            for (var y = 0; y < gridModel[x].length; y++) {
                if (gridModel[x][y] !== undefined) {
                    arr[gridModel[x][y]] += 1;
                }
            }
        }
    }
    return arr;
}

Modify the canvas's click handler to keep the gridModel up to date :

$(c_canvas).click(function (evt) {
    var pos = getNearestSquare(getMousePos(c_canvas, evt));
    if (pos != null) {
        context.fillStyle = currentColor;
        context.fillRect(pos.x, pos.y, 19, 19);
        incrementValue();
        updateGridModel(pos, currentColor); //keep the gridModel up to date.
    }
});

Modify printColor() as follows :

function printColor(color) {
    currentColor = color.toHexString();
    $(".label").text(currentColor);
}

Modify the .spectrum() options and add an initialising call to printColor() as follows :

$("#showPaletteOnly").spectrum({
    color: palette[0],
    showPaletteOnly: true,
    showPalette: true,
    hideAfterPaletteSelect: true,
    change: printColor,
    palette: [palette] //<<<< palette is now defined as an outer var
});

printColor( $("#showPaletteOnly").spectrum('get') );//initialize currentcolor and $(".label").text(...) .

Now paletteTally() will return an array congruent with palette containing counts of each color.


EDIT 1

Original code above was untested but is now debugged and includes improved spectrum options. Demo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM