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

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

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 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