javascript - 如何在js中找到画布的中心
问题描述
我有canvas
。我有一个ctx
. 我希望在 的中心ctx
绘制,但是canvas
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>
它可以完美地在画布的中心绘制一个矩形。
推荐阅读
- javascript - 如何在服务器(cpanel)上设置 nodejs 代码
- python - 将mysql查询结果(对象数组)转换为json
- git - 由于空间问题,如何更改 git-bare 存储库位置?
- angular - 离子搜索栏过滤器不是功能
- php - 在 iCalendar 文件 (.ics) 中设置管理器属性
- python - Pyspark Dataframe GroupBy Count 每次都不同
- python - 绘制 3D numpy 数组的第三轴
- php - PHP json_encode 不适用于 ibase
- android - Admob 测试广告在真实设备上展示
- python - Python - 在列表中查找重复项并按键分组