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

198
Visualizações
Canvas .drawImage() function doesn't draw the requested thumbnail of a video

I'm trying to create a function where a certain time frame from a video will be displayed as an image. the function accept 2 inputs, namely the path to the video, and the certain second, where the time frame should be displayed.

<!DOCTYPE html>
<html>
<body>

<div id="imageDiv" width="350" height="200"
style="border:1px solid #c3c3c3;">
</div>

<video id="myVideo" width="320" height="176" controls>
</video>

<script type="text/javascript">

// src will be the path to the video
// sec will be the second we want to display from the video

function dispImg(src, sec) {

    var video = document.getElementById("myVideo");
    video.src = src;
    video.type = "video/mp4";
    video.currentTime = sec;

    var canvas = document.createElement("canvas");
    var ctx = canvas.getContext("2d");
    ctx.drawImage(video, 0, 0, 350, 200);
    document.getElementById("imageDiv").appendChild(canvas);
}

dispImg("sample.mp4", 10)
</script>
</body>
</html>

from my current code, I expect the thumbnail displayed in the video section (id="myVideo") is also drawn in the div section (id="imageDiv"). unfortunately it's not the case, since its all white in the div section.

Is there any suggestion to my problem? thank you for any feedback!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

It's probable that the video hasn't loaded far enough or hasn't finished seeking at the time you're calling drawImage.

If that is the cause, you could wait for an event to trigger, like this:

function dispImg(src, sec) {

    var video = document.getElementById("myVideo");
    video.src = src;
    video.type = "video/mp4";

    // For more events, see:
    // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video#events
    video.addEventListener('seeked', (event) => {
        var canvas = document.createElement("canvas");
        var ctx = canvas.getContext("2d");
        ctx.drawImage(video, 0, 0, 350, 200);
        document.getElementById("imageDiv").appendChild(canvas);
    });
    // After creating the event handler, change the position of the video.
    video.currentTime = sec;
}

I think when you use the seeked event, you don't need to wait for events such as canplaythrough.

about 4 years ago · Juan Pablo Isaza Relatório

0

You might wanna see this example JSFiddle Timeline by kmturley

function createImage() {
    var canvas = document.createElement('canvas'),
        ctx = canvas.getContext('2d');
    canvas.width = video.videoWidth;
    canvas.height = video.videoHeight;
    ctx.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
    timeline.appendChild(canvas);
}

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