MCPcopy
hub / github.com/bubkoo/html-to-image / toCanvas

Function toCanvas

src/index.ts:28–59  ·  view source on GitHub ↗
(
  node: T,
  options: Options = {},
)

Source from the content-addressed store, hash-verified

26}
27
28export async function toCanvas<T extends HTMLElement>(
29 node: T,
30 options: Options = {},
31): Promise<HTMLCanvasElement> {
32 const { width, height } = getImageSize(node, options)
33 const svg = await toSvg(node, options)
34 const img = await createImage(svg)
35
36 const canvas = document.createElement('canvas')
37 const context = canvas.getContext('2d')!
38 const ratio = options.pixelRatio || getPixelRatio()
39 const canvasWidth = options.canvasWidth || width
40 const canvasHeight = options.canvasHeight || height
41
42 canvas.width = canvasWidth * ratio
43 canvas.height = canvasHeight * ratio
44
45 if (!options.skipAutoScale) {
46 checkCanvasDimensions(canvas)
47 }
48 canvas.style.width = `${canvasWidth}`
49 canvas.style.height = `${canvasHeight}`
50
51 if (options.backgroundColor) {
52 context.fillStyle = options.backgroundColor
53 context.fillRect(0, 0, canvas.width, canvas.height)
54 }
55
56 context.drawImage(img, 0, 0, canvas.width, canvas.height)
57
58 return canvas
59}
60
61export async function toPixelData<T extends HTMLElement>(
62 node: T,

Callers 4

toPixelDataFunction · 0.85
toPngFunction · 0.85
toJpegFunction · 0.85
toBlobFunction · 0.85

Calls 5

getImageSizeFunction · 0.90
createImageFunction · 0.90
getPixelRatioFunction · 0.90
checkCanvasDimensionsFunction · 0.90
toSvgFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…