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

Support replacing node when filter #395

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

compilelife
Copy link

This pull request introduce supporting for replacing node when doing filter.

The original demand is to use first frame of video instead of blank(transparent) when capturing a node.

See what it can do :

Assuming that we have a node:

<div>
   <p>Hello Video</p>
   <video src='http://mock.video.addr.com/1.mp4'></video>
</div>

A regular call to domtoimage.toPng(theDiv) would result to a image only contain the 'Hello world' text, leave blank for video area.

Now you can do:

const convertVideoToCanvas = (node)=>{
  if (node instanceof HTMLVideoElement) {
    const c = document.createElement('canvas');
    c.width = node.clientWidth;
    c.height = node.clientHeight;
    c.setAttribute('style', c.getAttribute('style') || '')
    const ctx = c.getContext('2d');
    if (ctx) {
      if (node.readyState >= 3) {
        ctx.drawImage(node, 0, 0);
      }
    }
    return c;
  }
  return node
}
const options = {
  filter: convertVideoToCanvas
}
domtoimage.toPng(theDiv, option)

This will create a image contains both text and video.

And you can do other similar node replacing trick.

On the other hand, this patch keep the api compatibility of filter unchanged.

So you can still do boolean filter like you always do:

const option = {
  filter: (node)=>{return node.nodeName !== 'video'}
}

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

Successfully merging this pull request may close these issues.

None yet

1 participant