Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Background-Image not displayed after capture #185

Open
1 of 2 tasks
MatanYadaev opened this issue Feb 5, 2018 · 19 comments
Open
1 of 2 tasks

Background-Image not displayed after capture #185

MatanYadaev opened this issue Feb 5, 2018 · 19 comments

Comments

@MatanYadaev
Copy link

Expected behavior

When I capture div with background-image, display this image in the captured image

Actual behavior (stack traces, console logs etc)

No image is displayed, but the div contents displayed

Library version

2.6.0

Browsers

  • Chrome 49+
  • Firefox 45+
@leonardoResendeLima
Copy link

+1

4 similar comments
@mwierzba
Copy link

mwierzba commented Feb 8, 2018

+1

@vishal-px
Copy link

+1

@SwatiJadhav46
Copy link

+1

@ZhuRuiHeng
Copy link

+1

@alexmootassem
Copy link

+1, any workaround ?

@Raven-T
Copy link

Raven-T commented Jul 13, 2018

I had a similar problem with loading an img HTML tag. The problem was that the Image itself takes longer to generate then the actual DOM element. I fixed the problem after many tries with a Timeout before executing the domtoimage.toPng function.

import { Component, OnInit, AfterViewInit, ViewChild, ElementRef } from '@angular/core';
import domtoimage from 'dom-to-image-chrome-fix'; //Use your normal dom to image import

export class EndpageComponent implements OnInit, AfterViewInit {
ngAfterViewInit() {
setTimeout(() => {
var node = document.getElementById('capture');

domtoimage.toPng(node)
.then(function (dataUrl) {
var img = new Image();
img.src = dataUrl;
document.body.appendChild(img);
})
.catch(function (error) {
console.error('oops, something went wrong!', error);
});

}, 100);
}

@abhisheksdm
Copy link

abhisheksdm commented Feb 12, 2019

same isssue in reactjs, background color is not displaying but elements are displaying
image

@rafaeldcastro
Copy link

@Raven-T
Whats that 'dom-to-image-chrome-fix'?

@terry623
Copy link

terry623 commented Mar 2, 2019

+1

1 similar comment
@prettyboyweiwei
Copy link

+1

@rebitco
Copy link

rebitco commented Sep 27, 2019

+1, if set background with BASE64 type , it`s ok! But set background with imageURL were not OK.

@rylax
Copy link

rylax commented Mar 20, 2020

+1

@rylax
Copy link

rylax commented Mar 20, 2020

I had a similar problem with loading an img HTML tag. The problem was that the Image itself takes longer to generate then the actual DOM element. I fixed the problem after many tries with a Timeout before executing the domtoimage.toPng function.

import { Component, OnInit, AfterViewInit, ViewChild, ElementRef } from '@angular/core';
import domtoimage from 'dom-to-image-chrome-fix'; //Use your normal dom to image import

export class EndpageComponent implements OnInit, AfterViewInit {
ngAfterViewInit() {
setTimeout(() => {
var node = document.getElementById('capture');

domtoimage.toPng(node)
.then(function (dataUrl) {
var img = new Image();
img.src = dataUrl;
document.body.appendChild(img);
})
.catch(function (error) {
console.error('oops, something went wrong!', error);
});

}, 100);
}

doesnt seem to work properly. It worked a couple of times. However, doesnt render well on mobile and browsers other than Chrome.

@mtsee
Copy link

mtsee commented Dec 18, 2020

在我们的项目中使用了此插件(h5ds.com),最终我们将background图片修改成base64即可解决此问题,修改 大概600行方法:inlineAll

function getBase64(src) {
    console.log('urlurlurlurl', src);
    return new Promise((resolve, reject) => {
      var img = new Image();
      img.crossOrigin = 'Anonymous'; // 允许跨域
      img.onload = () => {
        var width = img.naturalWidth;
        var height = img.naturalHeight;
        var canvas = $('<canvas width="' + width + '" height="' + height + '"></canvas>')[0];
        var ctx = canvas.getContext('2d');
        ctx.drawImage(img, 0, 0, width, height, 0, 0, width, height);
        resolve(canvas.toDataURL());
      };
      img.onerror = err => {
        console.error('图片转base64失败!');
        reject(err);
      };
      img.src = src;
    });
  }

  async function inlineAll(string, baseUrl, get) {
    if (nothingToInline()) return Promise.resolve(string);

    // 背景图转化成base64解决浏览器不能截取的问题
    var url = string.split(' ')[0];
    if (url.indexOf('url(') !== -1) {
      url = url.replace(/"|'|url|\(|\)/g, '');
      var base64 = await getBase64(url + '?dom-to-image');
      string = string.replace(url, base64);
    }

    return Promise.resolve(string)
      .then(readUrls)
      .then(function (urls) {
        var done = Promise.resolve(string);
        urls.forEach(function (url) {
          if (!urlsCache[url]) {
            done = done.then(function (string) {
              return inline(string, url, baseUrl, get);
            });
            urlsCache[url] = done;
          }
        });
        return done;
      });

    function nothingToInline() {
      return !shouldProcess(string);
    }
  }

@david-storm94
Copy link

+1

1 similar comment
@ArtemisGraphic
Copy link

+1

@BlueBeret
Copy link

BlueBeret commented Jul 13, 2023

+1 not working for some image

edit:
turns out it just image size issue. My original image was 17 MB (yeah thats really big). I compressed the image and it works.

@Luokun2016
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests