Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

312
Views
How to capture what is visible through a div?

Suppose I have an image and a div whose position is absolute and is above that image (z-index of div more than z-index of image).Something like this : enter image description here

I want to take screenshot of what is visible through the div using JavaScript. At first I thought of changing the div to a canvas and then I wrote this code:

<div class="utility-btn">
        <button class="enquiry-btn" onclick="openEnquiry()">?</button>
    </div>
    <div id="enquiry">
        <button id="close" onclick="closeEnquiry()">X</button>
        <div class="cover">
            <canvas id="capture"></canvas>
            <button class="btn" onclick="takeScreenshot()">
                Click to enquiry
            </button>
        </div>
    </div>

Function to take screenshot:

function takeScreenshot() {
            var canvas = document.getElementById('capture');
            ctx = canvas.getContext('2d');

            var backCanvas = document.createElement('canvas');
            backCanvas.width = canvas.width;
            backCanvas.height = canvas.height;
            var backCtx = backCanvas.getContext('2d');

            backCtx.drawImage(canvas, 0, 0);

            ctx.drawImage(backCanvas, 0, 0);
            var dataURL = backCanvas.toDataURL();
            console.log(dataURL);
        }

But the image of dataURL was not what I expected it was just a blank image:enter image description here

How can I implement this feature. How can I do it without using any external library?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

There are two problems:

#1

If we look at your bit of code responsible for actually taking the screenshot

        function takeScreenshot() {
            var canvas = document.getElementById('capture');
            ctx = canvas.getContext('2d');

            var backCanvas = document.createElement('canvas');
            backCanvas.width = canvas.width;
            backCanvas.height = canvas.height;
            var backCtx = backCanvas.getContext('2d');

            backCtx.drawImage(canvas, 0, 0);

            ctx.drawImage(backCanvas, 0, 0);
            var dataURL = backCanvas.toDataURL();
            console.log(dataURL);
        }

we can see that canvas is the element you want to have the screenshot taken onto. Later on you're creating an empty new canvas backCanvas and make it the size of the first canvas. Afterwards you're drawing the empty first canvas on the second and finally the empty second canvas back to the first.

So that does not make too much sense.

Instead you need to take the actual canvas generated by threeJS. These lines of your code append a <canvas> element to the container <div>, threeJS is using:

const container = document.getElementById('container');
container.appendChild(renderer.domElement);

We can reference it using:

document.getElementById("container").children[0]

#2

As threeJS uses WebGL to draw stuff, it's rendering context is not the regular and for performance reasons the browser is clearing the drawing buffer after something has been drawn onto - so your screenshot would come up empty all the time. There is an option you need to pass to the THREE.WebGLRenderer constructor to keep the drawingbuffer called preserveDrawingBuffer.

So change this line:

renderer = new THREE.WebGLRenderer();

to this

renderer = new THREE.WebGLRenderer({preserveDrawingBuffer: true});

and your screenshot function to this:

function takeScreenshot() {
    var canvas = document.getElementById('capture');
    ctx = canvas.getContext('2d');

    ctx.drawImage(document.getElementById("container").children[0], 0, 0);
    var dataURL = canvas.toDataURL();
    console.log(dataURL);
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!