tsayen / dom-to-image

Generates an image from a DOM node using HTML5 canvas
Other
10.21k stars 1.68k forks source link

How to keep ratio without adding blank space around the image #460

Closed equinusocio closed 11 months ago

equinusocio commented 1 year ago

Use case: description, code

I have a fluid element on the page which has width: 100% and aspec-ratio: 16/9 css properties applied. When exporting the element using toPNG() if i set a width of 2000 the image results in almost empty space.

The target element still the same size rendered on the page, so width is not applied to the element itself but just to the image canvas. The image below show a PNG generated with width set to 2000, but the real content is the same size as it was when rendered on the page, instead of filling the whole canvas.

pattern

My options

{
  width: 2000,
  height: 2000 * (9 / 16),
  bgcolor: 'transparent',
},

Expected behavior

The target element should be the size specified inside options

Actual behavior (stack traces, console logs etc)

Target element should be the size specified when rendered as image.

Library version

Browsers

forever0714yuan commented 1 year ago

你好,我已收到你的来信。辛苦了!!