tsayen / dom-to-image

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

image have offsetX #281

Closed cailongl closed 5 years ago

cailongl commented 5 years ago

Use case: description, code

unmake = async () => {
    // TODO
    try {
      const element = document.getElementById('artboard-container')
      const {width, height} = element.getBoundingClientRect()
      const dataUrl = await dom2image.toJpeg(element, {
        quality: 0.95,
        width,
        height,
        filter: ele => ele.tagName !== 'IMG' && !['wave-box-tools', 'artboard-cursor', 'artboard-grid'].includes(ele.id)
      })
      const link = document.createElement('a')
      link.download = 'my-image-name.jpeg'
      link.href = dataUrl // URL.createObjectURL(blob)
      link.click()
    } catch (e) {
      console.log(e)
    }
  }

Expected behavior

I want get a jpeg is not offsetX, and it full with the width. The dom that i want get is center of screen.but i want the image not with offsetX

Actual behavior (stack traces, console logs etc)

image

Library version

"dom-to-image": "^2.6.0",

Browsers

Homer1011110 commented 5 years ago

I have the same issue

Here is my solution, I hope this will be helpful for you

// use style option to set margin to 0
domtoimage.toSvg(this.ref, {
    quality: 1,
    style: {
        margin: 0
    }
})
cailongl commented 5 years ago

@Homer1011110 thanks a lot, it worked.

cesswhite commented 2 years ago

I have the same issue

Here is my solution, I hope this will be helpful for you

// use style option to set margin to 0
domtoimage.toSvg(this.ref, {
    quality: 1,
    style: {
        margin: 0
    }
})

Thank you!