Skip to content

Commit

Permalink
don't create a new canvas every frame of /cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
3vorp committed Oct 28, 2023
1 parent 795aa8d commit ccd3c6a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/commands/images/cycle.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SlashCommand } from "@interfaces";
import { SlashCommandBuilder } from "discord.js";
import { ChatInputCommandInteraction, Message } from "@client";
import { cycleComparison } from "@images/cycle";
import { cycleComparison } from "@functions/cycleComparison";
import parseTextureName from "@functions/parseTextureName";
import { textureChoiceEmbed } from "@helpers/choiceEmbed";

Expand Down
2 changes: 1 addition & 1 deletion src/components/menus/cycleSelect.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Client, Message, StringSelectMenuInteraction } from "@client";
import { Component } from "@interfaces";
import { info } from "@helpers/logger";
import { cycleComparison } from "@images/cycle";
import { cycleComparison } from "@functions/cycleComparison";

export default {
id: "cycleSelect",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Image, createCanvas, loadImage } from "@napi-rs/canvas";
import { AttachmentBuilder } from "discord.js";
import GIFEncoder from "./GIFEncoder";
import GIFEncoder from "@images/GIFEncoder";
import { Client, EmbedBuilder } from "@client";
import { addPathsToEmbed } from "@functions/getTexture";
import axios from "axios";
Expand Down Expand Up @@ -35,16 +35,19 @@ export async function imagesToGIF(images: Image[], framerate = 1) {
encoder.start();
encoder.setTransparent(true);

const canvas = createCanvas(finalWidth, finalHeight);
const ctx = canvas.getContext("2d");
ctx.imageSmoothingEnabled = false;
for (const image of images) {
ctx.clearRect(0, 0, finalWidth, finalHeight);
// convert Image to a Canvas so the encoder can accept it
const canvas = createCanvas(finalWidth, finalHeight);
const ctx = canvas.getContext("2d");
ctx.imageSmoothingEnabled = false;
ctx.drawImage(image, 0, 0, finalWidth, finalHeight);

// interface takes ms but our framerate is in seconds
encoder.setDelay(1000 * framerate);
encoder.addFrame(ctx);
}

encoder.finish();
return new AttachmentBuilder(encoder.out.getData(), { name: "cycled.gif" });
}
Expand Down

0 comments on commit ccd3c6a

Please sign in to comment.