Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
console.log('Rendering vector field...')
var scene = PImage.make(width, height);
var ctx = scene.getContext('2d');
var imgData = ctx.getImageData();
for (var x = 0; x < width; ++x) {
for (var y = 0; y < height; ++y) {
var encodedVelocity = encodeVelocity(velocities[x][y]);
imgData.setPixelRGBA_i(x, y, encodedVelocity.r, encodedVelocity.g, encodedVelocity.b, encodedVelocity.a);
}
}
// Texture is ready. Dump it onto the file system:
var textureName = OUT_IMAGE_NAME + '.png';
PImage.encodePNGToStream(scene, fs.createWriteStream(textureName)).then(()=> {
console.log('wrote out the png file to ' + textureName);
if (saveOriginalLayout) {
// This is just for debugging purposes.
return saveGraphLayoutIntoImage(rect, layout);
}
}).catch(e => {
console.log('there was an error writing', e);
});
}
if (t.length == 2) { // 2
ctx.fillText(t, (size / 2) - (fontSize / 2), size / 1.4);
} else if (t.length == 1) { // 1
ctx.fillText(t, size / 2 - (fontSize / 4), size / 1.4);
} else { // 0
ctx.fillText(t, 0, 0);
}
let writeStream;
if (typeof outputFilePathOrStream === 'string') {
writeStream = fs.createWriteStream(outputFilePathOrStream);
} else {
writeStream = outputFilePathOrStream;
}
PImage.encodePNGToStream(img, writeStream).then(() => {
winston.debug(`wrote out the png file to ${outputFilePathOrStream}`);
callback(outputFilePathOrStream)
}).catch((e) => {
winston.error("there was an error writing", e);
callback(outputFilePathOrStream)
});
});
}
function saveGraphLayoutIntoImage(rect, layout) {
var width = rect.x2 - rect.x1;
var height = rect.y2 - rect.y1;
var scene = PImage.make(width, height);
var ctx = scene.getContext('2d');
clearRectangle(ctx, width, height);
ctx.fillStyle = 'rgba(0, 200, 230, 1)';
layout.forEachBody(body => {
ctx.fillRect(body.pos.x - rect.x1 - 5, body.pos.y - rect.y1 - 5, 10, 10);
});
saveVoronoiIfNeeded(ctx, layout, rect);
var fileName = OUT_IMAGE_NAME + '.layout.png';
PImage.encodePNGToStream(scene, fs.createWriteStream(fileName)).then(()=> {
console.log('wrote out the png file to ' + fileName);
}).catch(e => {
console.log('failed to save layout image', e);
});
}
return await new Promise((resolve, reject) => {
const stream = new PassThrough();
const data: number[] = [];
stream
.on("data", (chunk: typeof data) => data.push(...chunk))
.on("error", (error) => reject(error))
.on("end", () => {
encodingPromise
.then(() => resolve(Buffer.from(data)))
.catch(reject);
});
const encodingPromise = encodePNGToStream(source, stream);
});
}
return new Promise((resolve, reject) => {
let data = [];
const converter = new stream.Writable({
write: function (chunk, encoding, next) {
data.push(chunk);
next();
}
});
converter.on('finish', () => {
resolve(Buffer.concat(data));
});
converter.on('error', error => reject(error));
PImage.encodePNGToStream(pImage, converter);
}).then(buffer => {
return nativeImage.createFromBuffer(buffer, {