Skip to content

Instantly share code, notes, and snippets.

@bliotti
Created May 6, 2023 03:43
Show Gist options
  • Save bliotti/07644f6c98ecaac618ee5f1907e636ce to your computer and use it in GitHub Desktop.
Save bliotti/07644f6c98ecaac618ee5f1907e636ce to your computer and use it in GitHub Desktop.
Stack images with canvas
....
const generateCardImage = async (cardNumber, cardChoices) => {
const canvas = createCanvas(format.width, format.height);
const ctx = canvas.getContext("2d");
console.log("Creating #", cardNumber)
ctx.clearRect(0, 0, format.width, format.height);
ctx.globalAlpha = 1;
ctx.globalCompositeOperation = "source-over";
const foo = async (choice, index) => {
const layerNum = index
const tt = await getImage(layerNum, choice)
ctx.drawImage(
tt,
0,
0,
format.width,
format.height
)
}
let layerNum = 1
for await (const x of cardChoices) {
foo(x, layerNum)
layerNum++
}
const cornerLayer = await loadImage("./input/Overall_Card_Corners/CardCorners.png")
ctx.drawImage(
cornerLayer,
0,
0,
format.width,
format.height
)
writeCardImage(canvas.toBuffer(), cardNumber)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment