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

211
Visualizações
Image size recovery with javascript

hi I would like to create a site where around an image you can see radial writing (link) (like sun rays). In fact I tried to put the image and through canvas I am trying to superimpose a circle and then connect the radial writings to it (a next step would be to make the writings move) however in fact I cannot generate the canvas rectangle of the same size image (to then have the right proportions to create the circle). I've tried getAttribute or $ {nameclass.width} and others. but i can't recover the image size with javascript. (On css I use the percentages to have a better adaptation with the subsequent implementations of @media)

<canvas id="myCanvas" >Error</canvas>

  <div  >
    <img class="logo_centrale" src="img.png" alt="Errore" >
    <script>
         document.getElementById("myCanvas").style.border = ' 3px solid #f00 ' ;
         document.write(document.getElementsByClassName("logo_centrale").width);
         var l = document.getElementsByClassName("logo_centrale").width;/*doesn't work*/
         var c = document.getElementById("myCanvas");
         var ctx = c.getContext("2d");
         ctx.beginPath();
         ctx.strokeStyle = ' #fff ';
         l = l / 2 ;
         a = a / 2 ;
         ctx.arc( l , a ,200,0,2*Math.PI);
         ctx.stroke();
   </script>
  </div>
body {
  position: fixed;
  background-color: black;
}
*{
  border: dashed;
  border-color: white;
}
.logo_centrale{   
z-index: -1;
    max-width: 45%;
    position: relative;
    display: block;
    margin-top: 10%;
    margin-left: auto;
    margin-right: auto;

}

#myCanvas{  z-index: 2;
    position:absolute;
    width: 630px;
    height: 430px;
    margin-top: 11%;
    margin-left: 27.3%;

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

0

You have to wait for the image to be ready before trying to get the size, then you can use .width and .height:

<img class="logo_centrale" src="img.png" alt="Errore" onload="process(event)">
<script>
    function process(event) {
         console.log(event.target.width)
         console.log(event.target.height)
         // your canvas code
    }
</script>

https://jsfiddle.net/geb58asc/

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