This repository was archived by the owner on Mar 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapToGif.js
More file actions
79 lines (62 loc) · 2.12 KB
/
MapToGif.js
File metadata and controls
79 lines (62 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Imports
import chalk from 'chalk';
import promptsync from 'prompt-sync'
import sharp from 'sharp';
import gifenc from 'gifenc'
// Consts
const prompt = promptsync();
const { GIFEncoder, quantize, applyPalette } = gifenc;
const gif = GIFEncoder();
const MAX_FRAMES = 64;
console.log(chalk.green(`
MinecraftCapes CapeGifTools
by james090500
`))
var fileName = prompt("Please enter the file name: ")
// Load the image
const sharpImage = await sharp(fileName)
const imageMeta = await sharpImage.metadata()
const { width, height } = imageMeta
const capeWidth = width
const capeHeight = ((width / 22) * 17)
// Loop through each image and make a new map
const canvasWidth = (width / 22) * 64
const canvasHeight = canvasWidth / 2
const frames = height / capeHeight
// Default Cape is
// W = 22
// H = 17
for(let i = 0; i < frames; i++) {
if(i > MAX_FRAMES) break;
console.log(chalk.blue(`Processing Frame #${i}`))
//Create each frame
var frame = await sharp('elytra.png').resize({
kernel: sharp.kernel.nearest,
width: canvasWidth,
height: canvasHeight,
}).composite([{
input: await sharpImage.clone().extract({
left: 0,
top: capeHeight * i,
width: capeWidth,
height: capeHeight
}).png().toBuffer(),
top: 0,
left: 0,
}]);
//Convert data to a raw rgba
var { data } = await frame.raw().toBuffer({ resolveWithObject: true });
var pixelArray = new Uint8ClampedArray(data.buffer);
// Quantize your colors to a 256-color RGB palette palette
const palette = quantize(pixelArray, 256);
// Get an indexed bitmap by reducing each pixel to the nearest color palette
const index = applyPalette(pixelArray, palette);
//Write the frame
gif.writeFrame(index, MAX_FRAMES * (width / 22), canvasHeight, { palette, transparent: true })
}
console.log(chalk.blue(`Layer Manipulation Finished`))
// Write end-of-stream character
gif.finish();
// Get the Uint8Array output of your binary GIF file
await sharp(gif.bytes(), { animated: true }).toFile('out.gif');
console.log(chalk.green(`Finished`))