I am developing a game and I am struggling to place an image at the bottom of the canvas which has bootstrap column width. But it never returns true height of the canvas for me to position the image at the bottom of the canvas.
<div id="game" class="mt-10">
<div id="gameContainer" class="container-fluid">
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg> <!-- Give this div your desired background color -->
<div class="container">
<div class="row text-center justify-content-center">
<div class="col-md-2">
</div>
<canvas id="gameArea" class="col-md-8"
style="border:1px solid blue !important;padding: 0 !important;margin: 0 !important;">
</canvas>
<div class="col-md-2">
<button class="btn btn-secondary" id="stopGame">Stop Game</button>
</div>
</div>
</div>
</div>
</div>
In my javascript code i am trying to place image using below code.
let canvas = document.getElementById("gameArea");
let ctx = canvas.getContext("2d");
let bowlImage = new Image();
bowlImage.src = 'images/icon.png';
bowlImage.onload = function () {
ctx.drawImage(bowlImage, 0, canvas.getBoundingClientRect().height - 20, 20, 20);
}
But it always draws image outside canvas which i cannot see. when i tested with hard coded y axis which is below 150 it works.
ctx.drawImage(bowlImage, 0, 120, 20, 20);
I am also moving position of image based on mousemove event and i calculate the new x/y coordinate using evt.clientX - rect.left or evt.clientY - rect.top which is never correct.But if i fetch the height of the canvas using canvas.getBoundingClientRect().height , its always more than 150 and around 391. Not sure how do i get the real height.