HashLips / generative-art-opensource

Create generative art by using the canvas api and node js, feel free to contribute to this repo with new ideas.
MIT License
1.36k stars 695 forks source link

TypeError: Cannot read properties of undefined (reading 'fileName') #227

Open chiaminghong opened 2 years ago

chiaminghong commented 2 years ago

I am following the hashlip youtube video - Code generate art Update Part 1. https://www.youtube.com/watch?v=Ksb3JIomuqM&list=PLvfQp12V0hS3AVImYdMNfkdvRtZEe7xqY&index=5

Dont really understand why I Keep getting the error below when fileName existed. Sometimes, the result returns more than 1 but it got stuck after another try.

PS D:\PROJECTS\test> node .\index.js 3 [] created 91390203887582 red big_sr.png D:\PROJECTS\test\index.js:54 ${_layer.location}${_layer.selectedElement.fileName} ^ TypeError: Cannot read properties of undefined (reading 'fileName') at D:\PROJECTS\test\index.js:54:57 at new Promise () at loadLayerImg (D:\PROJECTS\test\index.js:51:12) at D:\PROJECTS\test\index.js:117:37 at Array.forEach () at startCreating (D:\PROJECTS\test\index.js:116:21) at Object. (D:\PROJECTS\test\index.js:140:1) at Module._compile (node:internal/modules/cjs/loader:1097:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10) at Module.load (node:internal/modules/cjs/loader:975:32)

Here are my codes:

const startCreating = async () => {
    writeMetaData("");
    let editionCount = 1;

    while(editionCount <= editionSize) {
        let newDna = createDna(layers.length * 2 - 1);
        console.log(dnaList);
        if (isDnaUnique(dnaList, newDna)) {
            console.log(`created ${newDna}`);
            let results = constructLayerToDna(newDna, layers);
            let loadedElements = []; //promise array

            results.forEach ((layer) => {
                loadedElements.push(loadLayerImg(layer)); 
            })

         await Promise.all(loadedElements).then((elementArray) => {
                elementArray.forEach((element) => {
                    drawElement(element);
                });

                signImage(`${editionCount}`);
                saveImage(editionCount);
                addMetadata(newDna, editionCount);
                console.log(`Created edition: ${editionCount} with DNA: ${newDna}`);
            });

            dnaList.push(newDna);
            editionCount++;
        } else {
            console.log("DNA Exists!!");
        }
    }
    writeMetaData(JSON.stringify(metadataList));
};

is it something wrong with the while loop and layer images??

images of my layers image

would appreciate some explanation if possible.