svelte icon indicating copy to clipboard operation
svelte copied to clipboard

img bind naturalWidth and naturalHeight

Open danbulant opened this issue 2 years ago • 0 comments

Describe the problem

Svelte doesn't allow binding to naturalWidth and naturalHeight of img tags. Those are read only properties that reflect the natural width of an image (it's native resolution).

Describe the proposed solution

I'd like to be able to bind to those properties, so I can get those values simply out of images.

<script>
  let naturalWidth = 0;
</script>

<img src="something.jpg" bind:naturalWidth>

The images natural width is {naturalWidth}

Alternatives considered

Without binding, I can react to onload and get the naturalWidth/naturalHeight there. Using binds seems more like "the svelte way".

<script>
  let naturalWidth = 0;
  function imageLoaded(e) { naturalWidth = e.target.naturalWidth }
</script>

<img src="something.jpg" on:load={imageLoaded}>
The imaages natural width is {naturalWidth}

Importance

nice to have

danbulant avatar Aug 08 '22 09:08 danbulant