admin管理员组文章数量:1516870
Why this code draws oval instead of circle at the position (75, 75) with the radius 50?
<canvas id=c1 style="width:400;height:400">
<script>
ctx = c1.getContext('2d');
ctx.fillStyle = '#7ef';
ctx.fillRect(0, 0, 400, 400);
ctx.fillStyle = '#000';
ctx.beginPath();
ctx.arc(75,75,50,0,Math.PI*2,true)
ctx.stroke();
</script>
Why this code draws oval instead of circle at the position (75, 75) with the radius 50?
<canvas id=c1 style="width:400;height:400">
<script>
ctx = c1.getContext('2d');
ctx.fillStyle = '#7ef';
ctx.fillRect(0, 0, 400, 400);
ctx.fillStyle = '#000';
ctx.beginPath();
ctx.arc(75,75,50,0,Math.PI*2,true)
ctx.stroke();
</script>
Share
Improve this question
asked Jan 17, 2014 at 7:02
exebookexebook
33.9k40 gold badges151 silver badges241 bronze badges
2 Answers
Reset to default 27If you change this line:
<canvas id=c1 style="width:400;height:400">
to:
<canvas id=c1 width=400 height=400></canvas>
it should work. Don't use CSS to set Canvas sizes as this only affects the element but not the bitmap itself. For canvas you need to use it's dedicated properties (width/height) to also set the bitmap size or the bitmap is just stretched/scaled to match the size of the element.
The default size of canvas if not specified is 300x150 pixels. In this case those pixels are stretched (as an image) to 400x400 which is why you get an oval instead.
In cases where the size of the canvas is dynamic and not known at development time, you can set the size using JavaScript when you know that the browser has already positioned and sized the element appropriately:
canvas.width = canvas.offsetWidth;
canvas.height = canvas.offsetHeight;
This is useful when developing for mobile either as a website or a PhoneGap/Cordova app.
本文标签:
版权声明:本文标题:javascript - Canvas: arc(75,75,50,0,3.1415,true) draws oval instead of circle - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.betaflare.com/web/1738425852a2086118.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论