webtoon / psd

Fast zero-dependency PSD parser for the web and Node.js

Home Page:https://webtoon.github.io/psd

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Keep the document width and height , when exporting a layer (animations)

gcmartijn opened this issue · comments

H!

Maybe someone can use this in the future.
I have made some animations inside photoshop, within the same document.
each layer is a frame, and have different sizes.

When you export the layer, then you get different sizes.
With this code, you will keep the document sizes, and the correct layer position in the document.
So you can replay the animation again, inside a game or whatever.

import * as fs from "fs"
import Psd from "@webtoon/psd"
import { createCanvas, createImageData } from 'canvas'

const psdData = fs.readFileSync("talk-sideways.psd")
const psdFile = Psd.parse(psdData.buffer)

    let layerPixelData = await layer.composite()

    const canvas = createCanvas(psdFile.width, psdFile.height)
    const ctx = canvas.getContext('2d')

    const image = createImageData(layerPixelData, layer.width, layer.height)
    ctx.putImageData(image, layer.left, layer.top)

    const out = fs.createWriteStream('out.png')
    const stream = canvas.createPNGStream()
    stream.pipe(out)
    out.on('finish', () => console.log('The PNG file was created.'))