andyruwruw / chess-image-generator

Accepts FEN, PGN or array data for chess board and generates PNG or buffer.
https://www.npmjs.com/package/chess-image-generator
MIT License
55 stars 22 forks source link

Frame Errors #5

Closed LandonSchropp closed 3 years ago

LandonSchropp commented 3 years ago

chess-image-generator version: 1.0.9

Steps to reproduce

import ChessImageGenerator from "chess-image-generator";

const FEN = "r2qk2r/p1p5/bpnbp2p/1N1p1p2/P2P1p1N/2PQ2P1/1P2PPBP/R4RK1 b kq - 1 13";

const SIZE = "1024";
const LIGHT_COLOR = "#efefef";
const DARK_COLOR = "#8777b7";

(async () => {
  let imageGenerator = new ChessImageGenerator({
    size: SIZE,
    light: LIGHT_COLOR,
    dark: DARK_COLOR,
    style: "merida"
  });

  await imageGenerator.loadFEN(FEN);

  await imageGenerator.generatePNG("example.png");
})();

Expected behavior

This should output an image.

Actual behavior

It fails with this error:

/Users/landon/Development/personal/chess-flashcards/node_modules/chess-image-generator/src/chess-image-generator.js:172
      fs.write(fd, buffer, 0, buffer.length, null, (writeError) => {
                                     ^

TypeError: Cannot read property 'length' of undefined
    at /Users/landon/Development/personal/chess-flashcards/node_modules/chess-image-generator/src/chess-image-generator.js:172:38
    at FSReqCallback.oncomplete (fs.js:163:23)

It looks like the generateBuffer function always returns undefind because frame.toBuffer() returns undefined.

LandonSchropp commented 3 years ago

I figured it out—I was using a string for the size instead of an int.

One thing I did discover while doing this is you can drop the canvas-to-buffer dependency. The canvas library you're using has a toBuffer function you can call like this:

return canvas.toBuffer("image/png");

When I used the library version, it gave me an error message that helped me identify and fix my issue.

andyruwruw commented 3 years ago

Very nice find, I'll 100% remove that dependency and implement that 😀

One step further from package phobia!

LandonSchropp commented 3 years ago

👍