|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const isLocal = typeof process.pkg === "undefined"; |
| 4 | +const basePath = isLocal ? process.cwd() : path.dirname(process.execPath); |
| 5 | +const fs = require("fs"); |
| 6 | +const path = require("path"); |
| 7 | +const { createCanvas, loadImage } = require("canvas"); |
| 8 | +const buildDir = `${basePath}/build`; |
| 9 | +const imageDir = path.join(buildDir, "/images"); |
| 10 | +const { format, preview_gif, } = require(path.join(basePath, "/src/config.js")); |
| 11 | +const canvas = createCanvas(format.width, format.height); |
| 12 | +const ctx = canvas.getContext("2d"); |
| 13 | + |
| 14 | +const HashlipsGiffer = require(path.join( |
| 15 | + basePath, |
| 16 | + "/modules/HashlipsGiffer.js" |
| 17 | +)); |
| 18 | +let hashlipsGiffer = null; |
| 19 | + |
| 20 | +const loadImg = async (_img) => { |
| 21 | + return new Promise(async (resolve) => { |
| 22 | + const loadedImage = await loadImage(`${_img}`); |
| 23 | + resolve({ loadedImage: loadedImage }); |
| 24 | + }); |
| 25 | +}; |
| 26 | + |
| 27 | +// read image paths |
| 28 | +const imageList = []; |
| 29 | +const rawdata = fs.readdirSync(imageDir).forEach(file => { |
| 30 | + imageList.push(loadImg(`${imageDir}/${file}`)); |
| 31 | +}); |
| 32 | + |
| 33 | +const saveProjectPreviewGIF = async (_data) => { |
| 34 | + // Extract from preview config |
| 35 | + const { numberOfImages, order, repeat, quality, delay, imageName } = preview_gif; |
| 36 | + // Extract from format config |
| 37 | + const { width, height } = format; |
| 38 | + // Prepare canvas |
| 39 | + const previewCanvasWidth = width; |
| 40 | + const previewCanvasHeight = height; |
| 41 | + |
| 42 | + if(_data.length<numberOfImages) { |
| 43 | + console.log( |
| 44 | + `You do not have enough images to create a gif with ${numberOfImages} images.` |
| 45 | + ); |
| 46 | + } |
| 47 | + else { |
| 48 | + // Shout from the mountain tops |
| 49 | + console.log( |
| 50 | + `Preparing a ${previewCanvasWidth}x${previewCanvasHeight} project preview with ${_data.length} images.` |
| 51 | + ); |
| 52 | + const previewPath = `${buildDir}/${imageName}`; |
| 53 | + |
| 54 | + ctx.clearRect(0, 0, width, height); |
| 55 | + |
| 56 | + hashlipsGiffer = new HashlipsGiffer( |
| 57 | + canvas, |
| 58 | + ctx, |
| 59 | + `${previewPath}`, |
| 60 | + repeat, |
| 61 | + quality, |
| 62 | + delay |
| 63 | + ); |
| 64 | + hashlipsGiffer.start(); |
| 65 | + |
| 66 | + await Promise.all(_data).then((renderObjectArray) => { |
| 67 | + // Determin the order of the Images before creating the gif |
| 68 | + if(order == 'ASC') { |
| 69 | + // Do nothing |
| 70 | + } |
| 71 | + else if(order == 'DESC') { |
| 72 | + renderObjectArray.reverse(); |
| 73 | + } |
| 74 | + else if(order == 'MIXED') { |
| 75 | + renderObjectArray = renderObjectArray.sort(() => Math.random() - 0.5); |
| 76 | + } |
| 77 | + |
| 78 | + // Reduce the size of the array of Images to the desired amount |
| 79 | + if(parseInt(numberOfImages)>0) { |
| 80 | + renderObjectArray = renderObjectArray.slice(0, numberOfImages); |
| 81 | + } |
| 82 | + |
| 83 | + renderObjectArray.forEach((renderObject, index) => { |
| 84 | + ctx.globalAlpha = 1; |
| 85 | + ctx.globalCompositeOperation = 'source-over'; |
| 86 | + ctx.drawImage(renderObject.loadedImage, 0, 0, previewCanvasWidth, previewCanvasHeight); |
| 87 | + hashlipsGiffer.add(); |
| 88 | + }); |
| 89 | + }); |
| 90 | + hashlipsGiffer.stop(); |
| 91 | + } |
| 92 | +}; |
| 93 | + |
| 94 | +saveProjectPreviewGIF(imageList); |
0 commit comments