Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

314
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda