Skip to content

Commit d81f21d

Browse files
committed
refactor: consolidate canvas handling by removing deprecated files and updating type references
1 parent 4facf56 commit d81f21d

File tree

4 files changed

+87
-186
lines changed

4 files changed

+87
-186
lines changed

shared/features/thumbnail/canvasBun.ts

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 79 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,135 @@
11
/* eslint-disable @typescript-eslint/no-var-requires */
2+
import type Path from 'path';
3+
4+
import type NapiRs from '@napi-rs/canvas';
5+
6+
/*
7+
import type {
8+
Canvas as NapiCanvas,
9+
Image as NapiImage,
10+
GlobalFonts as NapiGlobalFonts,
11+
} from '@napi-rs/canvas';
12+
*/
13+
14+
export interface CanvasUtils {
15+
createCanvas(width: number, height: number): any;
16+
loadImage(src: string): Promise<any>;
17+
getPath(filename: string): string | URL;
18+
useFont(): void;
19+
saveToImage(canvas: HTMLCanvasElement | NapiRs.Canvas): Promise<Uint8Array>;
20+
noteBlockImage: Promise<any> | any;
21+
DrawingCanvas: any;
22+
RenderedImage: any;
23+
}
224

3-
let content = {} as any;
25+
let canvasUtils: CanvasUtils;
426

527
if (typeof document === 'undefined') {
6-
// Assume Node.js environment
7-
const canvasModule = require('@napi-rs/canvas');
8-
const { createCanvas, loadImage, registerFont, GlobalFonts } = canvasModule;
9-
10-
const path = require('path');
28+
// Node.js/Bun environment
29+
const canvasModule = require('@napi-rs/canvas') as typeof NapiRs;
30+
const path = require('path') as typeof Path;
1131

12-
const Canvas = canvasModule.Canvas;
13-
const Image = canvasModule.Image;
32+
const {
33+
createCanvas: nodeCreateCanvas,
34+
loadImage: nodeLoadImage,
35+
GlobalFonts,
36+
} = canvasModule;
1437

1538
const getPath = (filename: string) => {
16-
const dir = path.join(
17-
__dirname,
18-
'..',
19-
'..',
20-
'..',
21-
'assets',
22-
filename.split('/').join(path.sep),
39+
return (
40+
'file://' +
41+
path.join(
42+
__dirname,
43+
'..',
44+
'..',
45+
'..',
46+
'web',
47+
filename.split('/').join(path.sep),
48+
)
2349
);
24-
25-
console.log('dir', dir);
26-
return dir;
2750
};
2851

29-
const saveToImage = (canvas: typeof Canvas) => {
30-
return canvas.encode('png');
52+
const saveToImage = (canvas: NapiRs.Canvas) => canvas.encode('png');
53+
54+
const useFont = () => {
55+
GlobalFonts.registerFromPath(
56+
'file:' + getPath('/fonts/Lato-Regular.ttf').toString(),
57+
'Lato',
58+
);
3159
};
3260

33-
// Load note block image
34-
let noteBlockImage;
61+
let noteBlockImage: Promise<any>;
3562

3663
try {
37-
noteBlockImage = loadImage(getPath('/img/note-block-grayscale.png'));
64+
noteBlockImage = nodeLoadImage(
65+
new URL(getPath('public/img/note-block-grayscale.png')),
66+
);
3867
} catch (error) {
39-
console.log('Error loading image: ', error);
68+
console.error('Error loading image: ', error);
69+
noteBlockImage = Promise.reject(error);
4070
}
4171

42-
const useFont = () => {
43-
GlobalFonts.registerFromPath(getPath('/fonts/Lato-Regular.ttf'), 'Lato');
44-
};
45-
46-
useFont();
47-
48-
content = {
49-
createCanvas,
50-
loadImage,
51-
registerFont,
72+
canvasUtils = {
73+
createCanvas: nodeCreateCanvas,
74+
loadImage: nodeLoadImage,
5275
getPath,
5376
useFont,
5477
saveToImage,
5578
noteBlockImage,
56-
Canvas,
57-
Image,
79+
DrawingCanvas: canvasModule.Canvas,
80+
RenderedImage: canvasModule.Image,
5881
};
5982
} else {
60-
// Assume browser environment
83+
// Browser environment
6184
const createCanvas = (width: number, height: number) => {
62-
const canvas = new OffscreenCanvas(width, height);
63-
return canvas;
85+
return new OffscreenCanvas(width, height);
6486
};
6587

66-
const loadImage = function (src: string): Promise<HTMLImageElement> {
88+
const loadImage = (src: string): Promise<HTMLImageElement> => {
6789
return new Promise((resolve, reject) => {
68-
const img = document.createElement('img');
90+
const img = new Image();
6991
img.onload = () => resolve(img);
7092
img.onerror = reject;
7193
img.src = src;
7294
});
7395
};
7496

75-
const getPath = (filename: string) => {
76-
return filename;
77-
};
97+
const getPath = (filename: string) => filename;
7898

79-
const saveToImage = (canvas: HTMLCanvasElement) => {
80-
console.log('Not implemented');
99+
const saveToImage = (_canvas: any) => {
100+
console.warn('saveToImage not implemented in browser');
101+
throw new Error('saveToImage not implemented in browser');
81102
};
82103

83-
// TODO: refactor into resources attribute of some sort
84-
85-
// Load note block image
86-
const noteBlockImage = loadImage(getPath('/img/note-block-grayscale.png'));
87-
88-
// Register font
89104
const useFont = () => {
90-
const f = new FontFace('Lato', 'url(/fonts/Lato-Regular.ttf)');
105+
const font = new FontFace('Lato', 'url(/fonts/Lato-Regular.ttf)');
91106

92-
f.load().then((font) => {
93-
document.fonts.add(font);
107+
font.load().then((loadedFont) => {
108+
document.fonts.add(loadedFont);
94109
});
95110
};
96111

97-
useFont();
98-
99-
const Canvas = HTMLCanvasElement;
100-
const Image = HTMLImageElement;
112+
const noteBlockImage = loadImage(getPath('/img/note-block-grayscale.png'));
101113

102-
content = {
114+
canvasUtils = {
103115
createCanvas,
104116
loadImage,
105117
getPath,
118+
useFont,
106119
saveToImage,
107120
noteBlockImage,
108-
Canvas,
109-
Image,
121+
DrawingCanvas: HTMLCanvasElement,
122+
RenderedImage: HTMLImageElement,
110123
};
111124
}
112125

113-
const {
114-
createCanvas,
115-
loadImage,
116-
getPath,
117-
useFont,
118-
saveToImage,
119-
noteBlockImage,
120-
Canvas,
121-
Image,
122-
} = content;
123-
124-
export {
126+
export const {
125127
createCanvas,
126128
loadImage,
127129
getPath,
128130
useFont,
129131
saveToImage,
130132
noteBlockImage,
131-
Canvas,
132-
Image,
133-
};
133+
DrawingCanvas,
134+
RenderedImage,
135+
} = canvasUtils;

shared/features/thumbnail/canvasWeb.ts

Lines changed: 0 additions & 54 deletions
This file was deleted.

shared/features/thumbnail/index.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
2-
Canvas,
3-
Image,
2+
DrawingCanvas,
3+
RenderedImage,
44
createCanvas,
55
noteBlockImage,
66
saveToImage,
@@ -21,8 +21,8 @@ interface DrawParams {
2121
imgHeight: number;
2222
}
2323

24-
type Canvas = typeof Canvas;
25-
type Image = typeof Image;
24+
type Canvas = typeof DrawingCanvas;
25+
type Image = typeof RenderedImage;
2626

2727
const instrumentColors = [
2828
'#1964ac',
@@ -146,7 +146,7 @@ export async function drawNotesOffscreen({
146146
zoomLevel,
147147
backgroundColor,
148148
canvasWidth,
149-
canvasHeight,
149+
//canvasHeight,
150150
imgWidth = 1280,
151151
imgHeight = 768,
152152
}: DrawParams) {
@@ -246,7 +246,9 @@ export async function drawToImage(params: DrawParams): Promise<Buffer> {
246246
}
247247

248248
const output = await drawNotesOffscreen(params);
249-
const buffer = saveToImage(output);
249+
const byteArray = await saveToImage(output);
250250

251+
// Convert to Buffer
252+
const buffer = Buffer.from(byteArray);
251253
return buffer;
252254
}

0 commit comments

Comments
 (0)