1

I have a requirement where I need to take screenshot of the viewport and use it later.

Issue is, user can interact with page and add/delete elements. Over that, if an element is partially visible, I need to capture that only.

I have tried

  • HTML2Canvas
  • WebRTC

Issue with this HTML2Canvas take screenshot of an element. So if part of element is not visible, even that is added.

Issue with WebRTC is, first it requires permission from user. So if user declines it, my code will fail. Apart from that, it takes screenshot of entire screen and not just viewport.

So, is there a way where you can take screenshot of viewport?

Foe example, in below example, if you run in SO's output window, you will see the scroll. So given position at top, I just want to capture the visible content and not the entire div.

HTML2Canvas fiddle

.container {
  display: flex;
  flex-direction: row;
}

.container > div {
  padding: 5px;
  margin: 5px;
  border: 1px solid gray;
  font-size: 2em;
}
<div class='container'>
  <div>
    Paragraphs are the building blocks of papers. Many students define paragraphs in terms of length: a paragraph is a group of at least five sentences, a paragraph is half a page long, etc. In reality, though, the unity and coherence of ideas among sentences is what constitutes a paragraph. A paragraph is defined as “a group of sentences or a single sentence that forms a unit” (Lunsford and Connors 116). Length and appearance do not determine whether a section in a paper is a paragraph. For instance, in some styles of writing, particularly journalistic styles, a paragraph can be just one sentence long. Ultimately, a paragraph is a sentence or group of sentences that support one main idea. In this handout, we will refer to this as the “controlling idea,” because it controls what happens in the rest of the paragraph.
  </div>
  <div>
    Paragraphs are the building blocks of papers. Many students define paragraphs in terms of length: a paragraph is a group of at least five sentences, a paragraph is half a page long, etc. In reality, though, the unity and coherence of ideas among sentences is what constitutes a paragraph. A paragraph is defined as “a group of sentences or a single sentence that forms a unit” (Lunsford and Connors 116). Length and appearance do not determine whether a section in a paper is a paragraph. For instance, in some styles of writing, particularly journalistic styles, a paragraph can be just one sentence long. Ultimately, a paragraph is a sentence or group of sentences that support one main idea. In this handout, we will refer to this as the “controlling idea,” because it controls what happens in the rest of the paragraph.
  </div>
</div>

References of my research:

Rajesh
  • 24,354
  • 5
  • 48
  • 79
  • you can use puppeteer to take screenshots if you would like, if it fits your use purpose – lanxion Nov 10 '20 at 14:59
  • Let me try this as well. For readers' reference: [Puppeteer Doc](https://developers.google.com/web/tools/puppeteer) – Rajesh Nov 10 '20 at 15:02
  • what are your constraints for this ? is it running in a browser you control (via WebDriver for example), do you have a custom browser extension loaded, or does it have to work in a vanilla browser (for which you will definitely need user permission) ? – Touffy Nov 10 '20 at 15:03
  • Preferred is if works without permission. It will be rendered on browser or webviewer. Are you referring to WebRTC? I tried it but cannot get it to work for just viewport. – Rajesh Nov 10 '20 at 15:06

0 Answers0