svgdotjs / svg.js

The lightweight library for manipulating and animating SVG
https://svgjs.dev
Other
11.04k stars 1.08k forks source link

Image: During onLoad event not always width/height of the image available #1068

Closed janmaslik closed 1 year ago

janmaslik commented 4 years ago

Bug report

1/ This example from your docs doesnt work at all: image.on('load', function (e) { console.log(e.naturalWidth, e.naturalHeight) }) e.naturalWidth/e.naturalHeight are undefined

2/ I tried to use something like this: var img = svg.image("url-to-image"); img.on('load',function(){ console.log(this.image.node.width.baseVal.value); console.log(this.image.width()); }) BUT sometimes i get zeros in both console outputs (same page reload without code change). I think mine load-handler is in same order-level as your internal handler which fills those values.

tested on Chrome , webpack + typescript

Fuzzyma commented 4 years ago

Please create a jsfiddle showing your problem

Fuzzyma commented 4 years ago

I stumbled over this yesterday and now re-found this issue. The mistake is, that e is not the image but the event. So you need to access e.target instead. And then you can use naturalWidth: e.target.naturalWidth. Can you point me to the place where its written in the docs?