回答
我不知道有关的NodeJS,但你可以尝试APNG-canvas。 APNG使用HTML5(-webkit-canvas),JavaScript(jQuery)。 “
”APNG-canvas是一个库,用于在支持canvas(Google Chrome,Internet Explorer 9,Apple Safari)的浏览器中显示动画PNG文件。“
工作demo在这里。
这个库不支持创建APNG文件。 – 1j01
Currently,不,它看起来不像它。 Wikipedia lists the available software,正如你所看到的,它不支持ImageMagick,它有一个节点封装器。但是,如果您发现值得一看的话,您可以下载command line tool apngasm和shell,如果您觉得值得一用Node命令行包装,可以使用child_process
(http://nodejs.org/api/child_process.html)将其挂接到现有应用程序中。
没有这个库,但实现起来非常简单。在Wikipedia描述算法合并多个PNG文件导入到单个APNG:
- 在第一个PNG文件的所有数据块作为构建基础。
- 在图像标题块(IHDR)之后插入一个动画控制块(acTL)。
- 如果第一个PNG要成为动画的一部分,请在图像数据块(IDAT)之前插入一个帧控制块(fcTL)。
- 对于其余每个帧,添加一个帧控制块(fcTL)和一个帧数据块(fdAT)。然后添加图像结束块(IEND)。帧数据块(fdAT)的内容是从其各自源图像的图像数据块(IDAT)中获取的。
下面是一个示例实现:
const fs = require('fs')
const crc32 = require('crc').crc32
function findChunk(buffer, type) {
let offset = 8
while (offset < buffer.length) {
let chunkLength = buffer.readUInt32BE(offset)
let chunkType = buffer.slice(offset + 4, offset + 8).toString('ascii')
if (chunkType === type) {
return buffer.slice(offset, offset + chunkLength + 12)
}
offset += 4 + 4 + chunkLength + 4
}
throw new Error(`Chunk "${type}" not found`)
}
const images = process.argv.slice(2).map(path => fs.readFileSync(path))
const actl = Buffer.alloc(20)
actl.writeUInt32BE(8, 0) // length of chunk
actl.write('acTL', 4) // type of chunk
actl.writeUInt32BE(images.length, 8) // number of frames
actl.writeUInt32BE(0, 12) // number of times to loop (0 - infinite)
actl.writeUInt32BE(crc32(actl.slice(4, 16)), 16) // crc
const frames = images.map((data, idx) => {
const ihdr = findChunk(data, 'IHDR')
const fctl = Buffer.alloc(38)
fctl.writeUInt32BE(26, 0) // length of chunk
fctl.write('fcTL', 4) // type of chunk
fctl.writeUInt32BE(idx ? idx * 2 - 1 : 0, 8) // sequence number
fctl.writeUInt32BE(ihdr.readUInt32BE(8), 12) // width
fctl.writeUInt32BE(ihdr.readUInt32BE(12), 16) // height
fctl.writeUInt32BE(0, 20) // x offset
fctl.writeUInt32BE(0, 24) // y offset
fctl.writeUInt16BE(1, 28) // frame delay - fraction numerator
fctl.writeUInt16BE(1, 30) // frame delay - fraction denominator
fctl.writeUInt8(0, 32) // dispose mode
fctl.writeUInt8(0, 33) // blend mode
fctl.writeUInt32BE(crc32(fctl.slice(4, 34)), 34) // crc
const idat = findChunk(data, 'IDAT')
// All IDAT chunks except first one are converted to fdAT chunks
let fdat;
if (idx === 0) {
fdat = idat
} else {
const length = idat.length + 4
fdat = Buffer.alloc(length)
fdat.writeUInt32BE(length - 12, 0) // length of chunk
fdat.write('fdAT', 4) // type of chunk
fdat.writeUInt32BE(idx * 2, 8) // sequence number
idat.copy(fdat, 12, 8) // image data
fdat.writeUInt32BE(crc32(4, length - 4), length - 4) // crc
}
return Buffer.concat([ fctl, fdat ])
})
const signature = Buffer.from('\211PNG\r\n\032\n', 'ascii')
const ihdr = findChunk(images[0], 'IHDR')
const iend = Buffer.from('0000000049454e44ae426082', 'hex')
const output = Buffer.concat([ signature, ihdr, actl, ...frames, iend ])
fs.writeFileSync('output.png', output)
UPNG.js可以解析,并建立APNG文件 - https://github.com/photopea/UPNG.js
自述 -
UPNG。 js支持APNG,接口期望“帧”。
UPNG.encode(IMGS,W,H,CNUM,[德尔斯])
imgs: array of frames. A frame is an ArrayBuffer containing the pixel data (RGBA, 8 bits per channel) w, h : width and height of the image cnum: number of colors in the result; 0: all colors (lossless PNG) dels: array of delays for each frame (only when 2 or more frames) returns an ArrayBuffer with binary data of a PNG file
UPNG.js可以做的PNG文件,类似于TinyPNG 和其他工具有损耗微小。它使用k-means 算法执行颜色量化。
最后一个参数cnum允许有损压缩。将其设置为 零为无损压缩,或在图像中写入允许的颜色数 。较小的值会生成较小的文件。或者只用0代表无损/ 256代表损耗。
- 1. 如何动画PNG图像
- 2. 对PNG图像的CSS叠加动画
- 3. PNG图像上的动画问题
- 4. 如何使用openlayers3动画图像png
- 5. PNG图像序列动画错误
- 6. 三维动画与PNG图像
- 7. 使用Swing显示动画PNG(apng)?
- 8. 从Java Servlet将画布图像保存为png图像文件
- 9. 使用FabricJS将图案添加到PNG图像(在画布中)
- 10. 下载前有没有区分APNG和PNG图像的方法?
- 11. 将.png图像转换为.gif图像
- 12. 图像动画像
- 13. 将图像插入动画心形
- 14. 加载动画图像
- 15. 将动画添加到图像更改
- 16. jQuery将图像附加到div动画
- 17. 动画图像
- 18. 动画图像
- 19. 如何用KineticJS将另一个PNG图像掩盖PNG图像?
- 20. 动画图像,但图像不移动
- 21. 将png图像添加到VHDL文档
- 22. 动态地将PNG图像添加至图片框?
- 23. IE中的GIF和APNG图像
- 24. 获取APNG图像的元数据
- 25. 如何从多帧PNG图像加载动画光标?
- 26. 不能移动PNG图像?
- 27. 动态色调PNG图像
- 28. 导出画布与图像作为图像(如PNG或JPG)
- 29. 画布纯绘制vs PNG图像
- 30. 图像动画点击图像
我想你会发现你需要根据应用程序支持部分维基百科的内容: http://en.wikipedia.org/wiki/APNG#Application_support –