简体   繁体   English

计算画布中彩色方块的数量

[英]Count Amount of Colored Squares in Canvas

Here is the fiddle: http://jsfiddle.net/sw31uokt/ 这是小提琴: 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. 这是我为统计canvas元素内的总点击而设置的incrementValue函数的一些相关代码。

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'. 我想做的是能够显示每种颜色的计数,因此“您已经放置了14个红色像素,3个蓝色像素,4个黑色像素”。

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 ... 基本上,MarkE上面所说的...

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 : 修改画布的单击处理程序以使gridModel保持最新:

$(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 : 修改printColor()如下:

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

Modify the .spectrum() options and add an initialising call to printColor() as follows : 修改.spectrum()选项,并添加对printColor()的初始化调用,如下所示:

$("#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. 现在, paletteTally()将返回一个与包含每种颜色计数的palette一致的数组。


EDIT 1 编辑1

Original code above was untested but is now debugged and includes improved spectrum options. 上面的原始代码未经测试,但现已调试,其中包括改进的spectrum选项。 Demo . 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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