Reputation: 121
I have been working on a project in Javascript and after a certain development stage, the code stopped working. I've narrowed the problem down to creating and indexing "multi-dimensional" arrays in Javascript. I've included code just to test creating arrays of arrays, assigning color values to the arrays, and then a test if the values can be displayed on-screen via a loop that iterates through the "multi-dimensional array".
I've tried just about every method of creating arrays of arrays in Javascript -- some code snippets from answers even here -- but nothing has worked.
function setRGB(image, width1, x1, y1, r, g, b, a) {
var t1 = y1 * width1 * 4 + x1 * 4;
image.data[t1] = r;
image.data[t1 + 1] = g;
image.data[t1 + 2] = b;
image.data[t1 + 3] = a;
}
function draw() {
var pixels = [];
for (var i = 0; i < height; ++i) {
var row = [];
for (var j = 0; j < width; ++j) {
row[j] = 0;
}
pixels[i] = row;
}
var k = 12;
var j = 29;
console.log(pixels[12][29].toString());
var canvas = document.getElementById('test');
var width = 500;
var height = 500;
canvas.width = width;
canvas.height = height;
var ctx = canvas.getContext('2d');
var canvasImage = ctx.getImageData(0, 0, width, height);
var testImage = ctx.createImageData(width, height);
for (var y = 0; y < height; ++y) {
for (var x = 0; x < width; ++x) {
pixels[y][x] = Math.round(Math.random() * 255);
}
}
for (y = 0; y < height; ++y) {
for (x = 0; x < width; ++x) {
console.log(pixels[y][x].toString());
}
}
for (var y = 0; y < height; ++y) {
for (var x = 0; x < width; ++x) {
setRGB(testImage, width, pixels[y][x], pixels[y][x], pixels[y][x], 255);
ctx.putImageData(testImage, 0, 0);
canvasImage = testImage;
ctx.putImageData(canvasImage, 0, 0);
}
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Array test</title>
</head>
<body onload="draw();">
<canvas id="test">Sorry, but your browser doesn't support the 'canvas' element.</canvas>
</body>
</html>
In Google Chrome, nothing is displayed on-screen and the web console shows the error :
array-plot-test-1.html:26 Uncaught TypeError: Cannot read property '29' of undefined
at draw (array-plot-test-1.html:26)
at onload (array-plot-test-1.html:60)
even though the array of arrays is indexed in its creation loop.
The first time the array is indexed outside its creation loop consists of the test code :
var k = 12;
var j = 29;
console.log(pixels[12][29].toString());
In Mozilla Firefox, I get the error :
TypeError: pixels[12] is undefined[Learn More] array-plot-test-1.html:26:3
draw file:///[censored]/[censored]/[censored]/array-plot-test-1.html:26
onload file:///[censored]/[censored]/[censored]/array-plot-test-1.html:1
I've rewritten the array test code about a dozen different times, at least, using "solutions" provided by sites such as Stack Overflow, but nothing has worked so far. I expect there is some basic error that I've made.
Curiously, if I create a "multi-dimensional array" in Javascript like so
array1 = [];
for (var i=0; i < max; ++i) {
var temp = [1, 2, 3];
array[i] = temp;
}
with a pre-defined array, then Javascript "multi-dimensional arrays" work fine, but I need arrays of arrays of arbitrary size which is why I can't use the above code snippet.
Upvotes: 0
Views: 350
Reputation: 121
Seems the problems were caused by some dumb errors including accidentally not noticing that the width and height variables hadn't been declared yet and also my failure to pass setRGB() its x and y values.
Thanks to all who helped :)
jdb2
Upvotes: 0
Reputation:
You defined width
and height
after using them. You need to define them before using them.
You also were not passing x and y to setRGB
I suggest you learn how to use the debugger in your browser.
Not sure why you had this
ctx.putImageData(testImage, 0, 0);
canvasImage = testImage;
ctx.putImageData(canvasImage, 0, 0)
inside your loop. You’re drawing the same thing twice plus your drawing both of those 500x500 times. You won’t see the result until your draw
function exits so there’s no reason to do that inside the loop. Just moved it after the loop.
Also, put
“use strict”;
at the top of your JavaScript and never use var
, use const
and let
as they will help you find these kinds of issues.
function setRGB(image, width1, x1, y1, r, g, b, a) {
var t1 = y1 * width1 * 4 + x1 * 4;
image.data[t1] = r;
image.data[t1 + 1] = g;
image.data[t1 + 2] = b;
image.data[t1 + 3] = a;
}
function draw() {
var width = 500;
var height = 500;
var pixels = [];
for (var i = 0; i < height; ++i) {
var row = [];
for (var j = 0; j < width; ++j) {
row[j] = 0;
}
pixels[i] = row;
}
var k = 12;
var j = 29;
console.log(pixels[12][29].toString());
var canvas = document.getElementById('test');
canvas.width = width;
canvas.height = height;
var ctx = canvas.getContext('2d');
var testImage = ctx.createImageData(width, height);
for (var y = 0; y < height; ++y) {
for (var x = 0; x < width; ++x) {
pixels[y][x] = Math.round(Math.random() * 255);
}
}
for (var y = 0; y < height; ++y) {
for (var x = 0; x < width; ++x) {
setRGB(testImage, width, x, y, pixels[y][x], pixels[y][x], pixels[y][x], 255);
}
}
ctx.putImageData(testImage, 0, 0);
}
draw();
<canvas id="test"></canvas>
Upvotes: 1