I would like to embed a png image into svg container but wish to keep aspect ratio, so I only add width property in png element. how can I get the height of the png image in javascript for this case:
<!DOCTYPE html>
<html>
<head>
<style>
#img {
outline: 1px solid red;
}
</style>
</head>
<body>
<svg xmlns="http://www.w3.org/2000/svg" id="mysvg" viewBox="0 0 800 400" preserveAspectRatio="xMidYMid meet">
<title>SVG demo</title>
<desc>example SVG</desc>
<image id="img" xlink:href="input.png" width="500" x="100" y="100" />
</svg>
<script>
// initialize
const
svg = document.getElementById('mysvg'),
img = document.getElementById('img');
img.onload = function() {
console.log("x:",img.getAttribute('x'));
console.log("y:",img.getAttribute('y'));
console.log("width:",img.getAttribute('width'));
console.log("height:",img.getAttribute('height'));
}
</script>
</body>
</html>
This example will output as below:
x:100
y:100
width:500
height:null
I would like to read out the height value!