Jquery画布绘制不一致的形状
Jquery Canvas drawing inconsistent shapes
我一直在网上搜索,盯着这段代码看了一段时间,我就是弄不明白。如有任何帮助,我将不胜感激。
我正在尝试制作一个图表,其中用户输入尺寸,并且画布持有一个图表,其中用户可以单击每个框,并更改该框的阴影。然而,由于某些原因,这些方框只填充了图表的一部分。
my HTML file:
<body>
<canvas id="box"></canvas>
<script>
var canvas = document.getElementById('box');
var ctx = canvas.getContext('2d');
</script>
...
<button type="button" id="buildDiagram">Go!</button>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/diagramFuncts.js"></script>
</body>
diagramFuncts.js:
$('#buildUry').click(function() {
....
ctx.fillStyle = "rgb(252,252,252)";
ctx.fillRect(0, 0, width, height);
//each box is 50 by 50 and the boxes populate the canvas from the upper left corner
for (i = 0; i < rows; ++i) {
for (j = 0; j < cols; ++j) {
ctx.fillRect(50*j, 50*i, 50, 50);
ctx.strokeRect(50*j, 50*i, 50, 50);
}
}
});
这段代码生成了一个正确大小的画布,但是没有正确填充。例如,一个2 × 3的图表是100x150像素,但是里面的盒子太小了。
如果有人发现我代码中的任何错误,我将非常感激。
现场演示
这两行只需要交换i和j。
ctx.fillRect(50 * i, 50 * j, 50, 50);
ctx.strokeRect(50 * i, 50 * j, 50, 50);
列(J)将是Y的第二个参数,行(I)将是x的第一个参数。
var canvas = document.getElementById('box');
var ctx = canvas.getContext('2d');
var rows = 2,
cols = 3,
width = rows*50,
height = cols*50;
canvas.width = width;
canvas.height = height;
ctx.fillStyle = "rgb(252,252,252)";
ctx.fillRect(0, 0, width, height);
//each box is 50 by 50 and the boxes populate the canvas from the upper left corner
for (i = 0; i < rows; ++i) {
for (j = 0; j < cols; ++j) {
ctx.fillRect(50 * i, 50 * j, 50, 50);
ctx.strokeRect(50 * i, 50 * j, 50, 50);
}
}
相关文章:
- 当属性不一致时,如何根据属性对JS对象列表进行排序
- Android和JavaScript解析API之间不一致
- Javascript的行为与PHP包含文件不一致
- 对这种与document.domain和CORS相关的不一致行为的解释是什么
- 点击链接行为不一致
- Passport.js`isAuthenticated()`不一致的行为;当它应该是真的时候是假的
- 与显示不一致
- Ext 4.2.1 模型日期解析在浏览器之间不一致
- 通过 Dropbox API 上传时的文件内容不一致
- JavaScript - 对象属性不一致
- Javascript 混淆了 null、instanceof 和 typeof 的语法不一致
- localeCompare 显示使用前导变音字符对单词进行排序时不一致的行为
- SVG 对象部分上的 JQuery click() 不一致
- 使用forEach和.shift()时结果不一致
- Window.open浏览器的高度不一致
- 不同浏览器中的LinkButton PageMethod行为不一致
- 当活动处于后台时,Android WebView不一致
- 安卓浏览器中的画布绘制不一致
- Jquery画布绘制不一致的形状
- 拉斐尔的线形图库可以绘制不一致的时间序列数据吗?