Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ export type Options = {
* A string value for the background color, any valid CSS color value.
*/
backgroundColor?: string
/**
* Width in pixels to be applied to canvas on export.
*/
canvasWidth?: number
/**
* Height in pixels to be applied to canvas on export.
*/
canvasHeight?: number
/**
* An object whose properties to be copied to node's style before rendering.
*/
Expand Down Expand Up @@ -76,6 +84,7 @@ export type Options = {
function getImageSize(domNode: HTMLElement, options: Options = {}) {
const width = options.width || getNodeWidth(domNode)
const height = options.height || getNodeHeight(domNode)

return { width, height }
}

Expand Down Expand Up @@ -107,10 +116,13 @@ export async function toCanvas(
const ratio = options.pixelRatio || getPixelRatio()
const { width, height } = getImageSize(domNode, options)

canvas.width = width * ratio
canvas.height = height * ratio
canvas.style.width = `${width}`
canvas.style.height = `${height}`
const canvasWidth = options.canvasWidth || width
const canvasHeight = options.canvasHeight || height

canvas.width = canvasWidth * ratio
canvas.height = canvasHeight * ratio
canvas.style.width = `${canvasWidth}`
canvas.style.height = `${canvasHeight}`

if (options.backgroundColor) {
context.fillStyle = options.backgroundColor
Expand Down