繁体   English   中英

如何在JS中找到画布的中心

[英]How can I find the center of a canvas in js

我有canvas 我有一个ctx 我希望ctxcanvas中心绘制,但是

canvas.height/2
canvas.width/2

不起作用

http://jsfiddle.net/3hnbarcg/3/

另外,我需要证明它可以接受

看一下下面的代码。

 var centerPointWidth = 10; var centerPointHeight = 10; var canvas = document.getElementById("c"); var ctx = canvas.getContext("2d"); ctx.fillStyle = "#FF0000"; ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.fillStyle = "#000000"; ctx.fillRect((canvas.width / 2) - (centerPointWidth / 2), (canvas.height / 2) - (centerPointHeight / 2), centerPointWidth, centerPointHeight); 
 <canvas id="c"></canvas> 

它非常适合在画布的中心绘制一个矩形。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM