Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

313
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda