-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from HashLips/dev
Updated
- Loading branch information
Showing
10 changed files
with
203 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const { createCanvas, loadImage } = require("canvas"); | ||
const isLocal = typeof process.pkg === "undefined"; | ||
const basePath = isLocal ? process.cwd() : path.dirname(process.execPath); | ||
const buildDir = `${basePath}/build/pixel_images`; | ||
const inputDir = `${basePath}/build/images`; | ||
const { format, pixelFormat } = require(path.join(basePath, "/src/config.js")); | ||
const console = require("console"); | ||
const canvas = createCanvas(format.width, format.height); | ||
const ctx = canvas.getContext("2d"); | ||
|
||
const buildSetup = () => { | ||
if (fs.existsSync(buildDir)) { | ||
fs.rmdirSync(buildDir, { recursive: true }); | ||
} | ||
fs.mkdirSync(buildDir); | ||
}; | ||
|
||
const getImages = (_dir) => { | ||
try { | ||
return fs | ||
.readdirSync(_dir) | ||
.filter((item) => { | ||
let extension = path.extname(`${_dir}${item}`); | ||
if (extension == ".png" || extension == ".jpg") { | ||
return item; | ||
} | ||
}) | ||
.map((i) => { | ||
return { | ||
filename: i, | ||
path: `${_dir}/${i}`, | ||
}; | ||
}); | ||
} catch { | ||
return null; | ||
} | ||
}; | ||
|
||
const loadImgData = async (_imgObject) => { | ||
return new Promise(async (resolve) => { | ||
const image = await loadImage(`${_imgObject.path}`); | ||
resolve({ imgObject: _imgObject, loadedImage: image }); | ||
}); | ||
}; | ||
|
||
const draw = (_imgObject) => { | ||
let size = pixelFormat.ratio; | ||
let w = canvas.width * size; | ||
let h = canvas.height * size; | ||
ctx.imageSmoothingEnabled = false; | ||
ctx.drawImage(_imgObject.loadedImage, 0, 0, w, h); | ||
ctx.drawImage(canvas, 0, 0, w, h, 0, 0, canvas.width, canvas.height); | ||
}; | ||
|
||
const saveImage = (_loadedImageObject) => { | ||
fs.writeFileSync( | ||
`${buildDir}/${_loadedImageObject.imgObject.filename}`, | ||
canvas.toBuffer("image/png") | ||
); | ||
}; | ||
|
||
const startCreating = async () => { | ||
const images = getImages(inputDir); | ||
if (images == null) { | ||
console.log("Please generate collection first."); | ||
return; | ||
} | ||
let loadedImageObjects = []; | ||
images.forEach((imgObject) => { | ||
loadedImageObjects.push(loadImgData(imgObject)); | ||
}); | ||
await Promise.all(loadedImageObjects).then((loadedImageObjectArray) => { | ||
loadedImageObjectArray.forEach((loadedImageObject) => { | ||
draw(loadedImageObject); | ||
saveImage(loadedImageObject); | ||
console.log(`Pixelated image: ${loadedImageObject.imgObject.filename}`); | ||
}); | ||
}); | ||
}; | ||
|
||
buildSetup(); | ||
startCreating(); |
File renamed without changes.
Oops, something went wrong.