{"library":"pixelsmith","title":"Pixelsmith Image Engine","description":"Pixelsmith, currently at version 2.6.0, functions as a Node.js-based engine for `spritesmith`, specializing in programmatically creating image composites. It leverages `get-pixels` for decoding various image formats (like PNG, JPG, GIF) and `save-pixels` for encoding the resulting canvas into output formats, primarily PNG. The library allows developers to construct an in-memory canvas, add multiple source images at specified positions, and then export the combined image as a readable stream. While specific release cadences are not documented, its integration with `spritesmith` implies ongoing maintenance for compatibility. A key differentiator is its adherence to the `spritesmith-engine-spec` version 2.0.0, providing a standardized interface for sprite generation within the `spritesmith` ecosystem, offering robust image handling capabilities crucial for build-time asset optimization.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install pixelsmith"],"cli":null},"imports":["const Pixelsmith = require('pixelsmith')","import Pixelsmith from 'pixelsmith'","canvas.addImage(img, x, y);\ncanvas.export({ format: 'png' });"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const Pixelsmith = require('pixelsmith');\nconst fs = require('fs');\nconst path = require('path');\n\n// Create dummy image files for demonstration\nconst createDummyImage = (filename, width, height) => {\n  // In a real scenario, these would be actual image files\n  // For this example, we'll just create placeholder files.\n  // Pixelsmith will attempt to read these, but without actual image data,\n  // it might fail. For a true runnable example, these would need to be valid images.\n  fs.writeFileSync(path.join(__dirname, filename), Buffer.from('dummy image data'));\n};\n\n// Ensure dummy images exist for the example to reference\ncreateDummyImage('img1.jpg', 50, 100);\ncreateDummyImage('img2.png', 75, 120);\n\n// Create a new engine\nconst pixelsmith = new Pixelsmith();\n\n// Interpret some images from disk\npixelsmith.createImages([path.join(__dirname, 'img1.jpg'), path.join(__dirname, 'img2.png')], function handleImages (err, imgs) {\n  // If there was an error, throw it\n  if (err) {\n    // In a real scenario, this error might indicate malformed images or missing files\n    console.error('Error creating images:', err);\n    return;\n  }\n\n  // We receive images in the same order they were given\n  // imgs[0].width; // 50 (pixels) - these properties are available after successful loading\n  // imgs[0].height; // 100 (pixels)\n\n  // Create a canvas that fits our images (200px wide, 300px tall)\n  const canvas = pixelsmith.createCanvas(200, 300);\n\n  // Add the images to our canvas (at x=0, y=0 and x=50, y=100 respectively)\n  // In a real scenario, 'imgs[0]' and 'imgs[1]' would be objects with pixel data.\n  // This example assumes they were loaded correctly for the purpose of demonstrating the API.\n  // For a fully functional example without actual images, mock these objects.\n  const mockImg1 = { width: 50, height: 100, /* ... other pixel data properties */ };\n  const mockImg2 = { width: 75, height: 120, /* ... other pixel data properties */ };\n\n  canvas.addImage(mockImg1, 0, 0);\n  canvas.addImage(mockImg2, 50, 100);\n\n  // Export canvas to image\n  const resultStream = canvas['export']({format: 'png'});\n  const outputFilePath = path.join(__dirname, 'output.png');\n  resultStream.pipe(fs.createWriteStream(outputFilePath))\n    .on('finish', () => console.log(`Sprite sheet saved to ${outputFilePath}`))\n    .on('error', (exportErr) => console.error('Error exporting canvas:', exportErr));\n});","lang":"javascript","description":"Demonstrates how to initialize Pixelsmith, load dummy image file paths, create a canvas of a specific size, place the loaded images onto the canvas at given coordinates, and then export the resulting composite image as a PNG stream to a file.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}