index-node.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { exportToCanvas } from "./scene/export";
  2. import { getDefaultAppState } from "./appState";
  3. const { registerFont, createCanvas } = require("canvas");
  4. const elements = [
  5. {
  6. id: "eVzaxG3YnHhqjEmD7NdYo",
  7. type: "diamond",
  8. x: 519,
  9. y: 199,
  10. width: 113,
  11. height: 115,
  12. strokeColor: "#000000",
  13. backgroundColor: "transparent",
  14. fillStyle: "hachure",
  15. strokeWidth: 1,
  16. roughness: 1,
  17. opacity: 100,
  18. seed: 749612521,
  19. },
  20. {
  21. id: "7W-iw5pEBPTU3eaCaLtFo",
  22. type: "ellipse",
  23. x: 552,
  24. y: 238,
  25. width: 49,
  26. height: 44,
  27. strokeColor: "#000000",
  28. backgroundColor: "transparent",
  29. fillStyle: "hachure",
  30. strokeWidth: 1,
  31. roughness: 1,
  32. opacity: 100,
  33. seed: 952056308,
  34. },
  35. {
  36. id: "kqKI231mvTrcsYo2DkUsR",
  37. type: "text",
  38. x: 557.5,
  39. y: 317.5,
  40. width: 43,
  41. height: 31,
  42. strokeColor: "#000000",
  43. backgroundColor: "transparent",
  44. fillStyle: "hachure",
  45. strokeWidth: 1,
  46. roughness: 1,
  47. opacity: 100,
  48. seed: 1683771448,
  49. text: "test",
  50. font: "20px Virgil",
  51. baseline: 22,
  52. },
  53. ];
  54. registerFont("./public/Virgil.woff2", { family: "Virgil" });
  55. registerFont("./public/Cascadia.woff2", { family: "Cascadia" });
  56. const canvas = exportToCanvas(
  57. elements as any,
  58. {
  59. ...getDefaultAppState(),
  60. offsetTop: 0,
  61. offsetLeft: 0,
  62. width: 0,
  63. height: 0,
  64. },
  65. {}, // files
  66. {
  67. exportBackground: true,
  68. viewBackgroundColor: "#ffffff",
  69. },
  70. createCanvas,
  71. );
  72. const fs = require("fs");
  73. const out = fs.createWriteStream("test.png");
  74. const stream = (canvas as any).createPNGStream();
  75. stream.pipe(out);
  76. out.on("finish", () => {
  77. console.info("test.png was created.");
  78. });