Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!