https://jsfiddle.net/The95Chaps/2L4t9saq/92/ contains the following code:
var createGrid = function(rows, columns, width, height) {
for (var r = 1; r < rows + 1; r++) {
var rowHtml = "<span class='inline'>";
for (var c = 1; c < columns + 1; c++) {
rowHtml += "<div class='pixels' x='" + c + "' y='" + r + "'></div>";
}
rowHtml += "</span>";
$("#main").append(rowHtml);
}
$(".pixels").css("background-color", "gray");
$(".pixels").css("width", width);
$(".pixels").css("height", height);
};
var modifyGrid = function(code) {
for (var i = 1; i < gridx + 1; i++) {
for (var j = 1; j < gridy + 1; j++) {
$("[x=" + j + "][y=" + i + "]").css("background-color", "blue");
}
}
};
var gridx = 64;
var gridy = 64;
createGrid(gridx, gridy, 1, 1);
.
<div canvas="canvas" id="main"></div>
.
.inline { display: block; }
.pixels { display: inline-block; }
#main {
font-size:0;
}
The initial setup creates a 64 by 64 grid where each pixel is 1 unit in size, totaling 4096 pixels.
In the modifyGrid()
function, the intention is to take a JavaScript array and convert it into an image. The challenge lies in the jQuery selectors.
Currently, the selection relies on
$("element[attribute=value]").somefunction();
. The question at hand is how to select elements with two attributes, specifically when both 'x' and 'y' should match certain values simultaneously.
Within the for
loop, the goal is to identify pixels where the 'x' attribute matches 'i' and the 'y' attribute matches 'j', and then change their background color to blue using $(selector[x=someValue][y=someValue]).css("background-color","blue");