Skip to content

Commit 4d71486

Browse files
committed
refactor: enhance canvasFactory to support both Node.js and browser environments with improved error handling
1 parent 814b19b commit 4d71486

File tree

1 file changed

+89
-43
lines changed

1 file changed

+89
-43
lines changed

packages/thumbnail/src/canvasFactory.ts

Lines changed: 89 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,52 +9,98 @@ let canvasUtils: CanvasUtils;
99

1010
if (typeof document === 'undefined') {
1111
// Node.js/Bun environment
12-
const canvasModule = require('@napi-rs/canvas') as typeof NapiRs;
13-
const path = require('path') as typeof Path;
14-
15-
const {
16-
createCanvas: nodeCreateCanvas,
17-
loadImage: nodeLoadImage,
18-
GlobalFonts,
19-
} = canvasModule;
20-
21-
const getPath = (filename: string) => {
22-
const workingDir = process.cwd();
23-
const fullPath = path.join(workingDir, filename.split('/').join(path.sep));
24-
25-
return fullPath;
26-
};
27-
28-
const saveToImage = (canvas: NapiRs.Canvas) => canvas.encode('png');
29-
30-
const useFont = () => {
31-
const path = getPath('assets/fonts/Lato-Regular.ttf').toString();
32-
console.log('Font path: ', path);
33-
34-
GlobalFonts.registerFromPath(path, 'Lato');
35-
};
36-
37-
let noteBlockImage: Promise<any>;
38-
3912
try {
40-
const path = getPath('assets/img/note-block-grayscale.png');
41-
42-
noteBlockImage = nodeLoadImage(path);
13+
const canvasModule = require('@napi-rs/canvas') as typeof NapiRs;
14+
const path = require('path') as typeof Path;
15+
16+
const {
17+
createCanvas: nodeCreateCanvas,
18+
loadImage: nodeLoadImage,
19+
GlobalFonts,
20+
} = canvasModule;
21+
22+
const getPath = (filename: string) => {
23+
const workingDir = process.cwd();
24+
const fullPath = path.join(
25+
workingDir,
26+
filename.split('/').join(path.sep),
27+
);
28+
29+
return fullPath;
30+
};
31+
32+
const saveToImage = (canvas: NapiRs.Canvas) => canvas.encode('png');
33+
34+
const useFont = () => {
35+
const path = getPath('assets/fonts/Lato-Regular.ttf').toString();
36+
console.log('Font path: ', path);
37+
38+
GlobalFonts.registerFromPath(path, 'Lato');
39+
};
40+
41+
let noteBlockImage: Promise<any>;
42+
43+
try {
44+
const path = getPath('assets/img/note-block-grayscale.png');
45+
46+
noteBlockImage = nodeLoadImage(path);
47+
} catch (error) {
48+
console.error('Error loading image: ', error);
49+
noteBlockImage = Promise.reject(error);
50+
}
51+
52+
canvasUtils = {
53+
createCanvas: nodeCreateCanvas,
54+
loadImage: nodeLoadImage,
55+
getPath,
56+
useFont,
57+
saveToImage,
58+
noteBlockImage,
59+
DrawingCanvas: canvasModule.Canvas,
60+
RenderedImage: canvasModule.Image,
61+
};
4362
} catch (error) {
44-
console.error('Error loading image: ', error);
45-
noteBlockImage = Promise.reject(error);
63+
// Fallback for when @napi-rs/canvas is not available (e.g., in browser build)
64+
console.warn('@napi-rs/canvas not available, using browser fallbacks');
65+
66+
const createCanvas = (width: number, height: number) => {
67+
if (typeof OffscreenCanvas !== 'undefined') {
68+
return new OffscreenCanvas(width, height);
69+
}
70+
throw new Error('OffscreenCanvas not available');
71+
};
72+
73+
const loadImage = (src: string): Promise<any> => {
74+
return Promise.reject(
75+
new Error('loadImage not available in this environment'),
76+
);
77+
};
78+
79+
const getPath = (filename: string) => filename;
80+
81+
const saveToImage = (_canvas: any) => {
82+
throw new Error('saveToImage not implemented in browser');
83+
};
84+
85+
const useFont = () => {
86+
// No-op in fallback
87+
};
88+
89+
const noteBlockImage = Promise.reject(
90+
new Error('noteBlockImage not available in this environment'),
91+
);
92+
93+
canvasUtils = {
94+
createCanvas,
95+
loadImage,
96+
getPath,
97+
useFont,
98+
saveToImage,
99+
noteBlockImage,
100+
DrawingCanvas: HTMLCanvasElement as any,
101+
RenderedImage: HTMLImageElement as any,
102+
};
46103
}
47-
48-
canvasUtils = {
49-
createCanvas: nodeCreateCanvas,
50-
loadImage: nodeLoadImage,
51-
getPath,
52-
useFont,
53-
saveToImage,
54-
noteBlockImage,
55-
DrawingCanvas: canvasModule.Canvas,
56-
RenderedImage: canvasModule.Image,
57-
};
58104
} else {
59105
// Browser environment
60106
const createCanvas = (width: number, height: number) => {

0 commit comments

Comments
 (0)