0

for my javascript game project, I'd like to make a 2 dimensional array that checks a PNG image for the color red and then saves tile-wise "true" or "false".

So I made something like this:

var array = new Array(100);

for(i = 0; i < 100; i++)
{
array[i] = new Array(100);
}

First I only found a lot of canvas use cases, but because I am working in app.js (aka my node.js server application), I don't have access to the canvas (well I can use canvas on client side, but that brings a lot of cheating possibilites with it!) So I looked into node modules I could use, and I found jimp which I tried using. There are some issues though. First I encountered a similar error to this, but for some reason it fixed itself after changing the code.

I feel like JIMP isn't the 100% correct solution for my problem though, does node maybe have some default file reading system I can use?

All I really need is a function like

function getPixel(x, y){
return pixelColor('./client/img/map.png', x, y).red;
}

Which I can then use to iterate through my arrays and save the RGB value of red into the array. I feel like I did a really poor job on phrasing what I want to achieve and where my problems are, I hope somebody might be able to help me anyways if not, please ask me for more information if you feel like this is an easy task.

Edit 1: My title says "a lot of images" because my game is split into chunks, and every chunk = "background"-picture of the map, which equates to 1 896x896 file of "tile collisions". My application will need to read like 900 images to get the complete collision data into the array.

  • *does node maybe have some default file reading system I can use* you mean like `const fs = require('fs')` and now `fs` has a bunch of methods like `readFile` etc? Of course, then you need to decode the png file ... are you able to do that? – Bravo Oct 12 '19 at 16:27
  • from a design point of view, the problem with your ideal function `pixelColor('./client/img/map.png', x, y).red;` that would open the file, decode it, get the pixel, close the file for every pixel - you can imagine this would be very inefficient for reading even 2 pixels from the one file, let alone the 10,000 your code suggests, or the 802,000 your `896x896` your edit 1 suggests - and that's just one file - multiply that by 900 and you'll need hours to do what you want. What part of JIMP doesn't meet your requirements? – Bravo Oct 12 '19 at 16:31
  • I don't really know how to decode the png file. True, good point. So ideally I'd open the picture of a chunk and then iterate through all the tiles in it before opening the next picture. Idk, I feel like I don't really get how to use JIMP and the things Im trying to do aren't documented well enough. I run into errors calling functions and I can't seem to find my errors. – SimpleBenji01 Oct 12 '19 at 16:48

0 Answers0