diff --git a/index.js b/index.js index 679e1270..d573e946 100644 --- a/index.js +++ b/index.js @@ -13,7 +13,19 @@ const { const console = require("console"); const canvas = createCanvas(width, height); const ctx = canvas.getContext("2d"); - +//add noise to the images randomly +const drawNoise = (ctx, width, height, opacity) => { + const imageData = ctx.createImageData(width, height); + const data = imageData.data; + for (let i = 0; i < data.length; i += 4) { + const value = Math.random() * 255; + data[i] = value; + data[i + 1] = value; + data[i + 2] = value; + data[i + 3] = opacity * 255; + } + ctx.putImageData(imageData, 0, 0); +}; // saves the generated image to the output folder, using the edition count as the name const saveImage = (_editionCount) => { fs.writeFileSync( @@ -217,7 +229,7 @@ const startCreating = async () => { // elements are loaded asynchronously // -> await for all to be available before drawing the image - await Promise.all(loadedElements).then((elementArray) => { + await Promise.all(loadedElements).then(async (elementArray) => { // create empty image ctx.clearRect(0, 0, width, height); // draw a random background color @@ -228,7 +240,11 @@ const startCreating = async () => { elementArray.forEach((element) => { drawElement(element); attributesList.push(getAttributeForElement(element)); + drawElement(element); }); + if(applyNoise){ + drawNoise(ctx,width,height,0.1) + } // add an image signature as the edition count to the top left of the image signImage(`#${editionCount}`); // write the image to the output directory