-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ refactor(lib/pdf): Factor out isomorphic canvas helpers.
- Loading branch information
1 parent
7d7671e
commit 4b932fa
Showing
2 changed files
with
58 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import {type Canvas} from 'canvas/types'; | ||
|
||
export { | ||
type Canvas, | ||
type JpegConfig, | ||
type PdfConfig, | ||
type PngConfig, | ||
} from 'canvas/types'; | ||
|
||
type CreateCanvasOptions = { | ||
width: number; | ||
height: number; | ||
}; | ||
|
||
const createCanvasIso = async ({ | ||
width, | ||
height, | ||
}: CreateCanvasOptions): Promise<HTMLCanvasElement | Canvas> => { | ||
if (Meteor.isServer) { | ||
const {createCanvas} = await import('canvas'); | ||
return createCanvas(width, height); | ||
} | ||
|
||
const browserCanvas = document.createElement('canvas'); | ||
browserCanvas.width = width; | ||
browserCanvas.height = height; | ||
return browserCanvas; | ||
}; | ||
|
||
export const destroyCanvasIso = (canvas: HTMLCanvasElement | Canvas) => { | ||
canvas.width = 0; | ||
canvas.height = 0; | ||
if (Meteor.isClient) { | ||
(canvas as HTMLCanvasElement).remove(); | ||
} | ||
}; | ||
|
||
type CreateContextOptions = {} & CreateCanvasOptions; | ||
|
||
export const createContextIso = async ( | ||
options: CreateContextOptions, | ||
): Promise<CanvasRenderingContext2D> => { | ||
return createCanvasIso(options).then( | ||
(canvas) => canvas.getContext('2d') as CanvasRenderingContext2D, | ||
); | ||
}; | ||
|
||
export const destroyContextIso = (context: CanvasRenderingContext2D) => { | ||
destroyCanvasIso(context.canvas); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters