I was trying to draw a circle in the canvas when I noticed something strange - basically when I defined the canvas' dimensions by CSS, the element went out of scale - like it looked "squished".
But if I set the dimensions though the "width" and "height" HTML attributes, it rendered normally.
$(document).ready(function() {
function highlightAreaTrial(elementId, x, y, radius) {
console.log("x, y: " + x + ", " + y);
canvas = document.getElementById(elementId);
ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.arc(x, y, radius, 0, 2*Math.PI);
ctx.strokeStyle="#FF0000";
ctx.stroke();
}
highlightAreaTrial("trial", 50, 50, 20);
highlightAreaTrial("myCanvas", 50, 50, 20);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<canvas id="trial" style="height: 100px; width: 100px; border: 1px solid black;"></canvas>
<canvas id="myCanvas" width="100" height="100" style="border:1px solid #d3d3d3;"></canvas>
Here's a fiddle: https://jsfiddle.net/codeapprenti/vnoukemm/
Can someone explain this behaviour?
Thanks.