admin管理员组文章数量:1022989
As the question suggests, I am trying to draw a line on a HTML5 canvas. I have tested the values and find that it WILL draw a line from 0,0 to 1920,1040 (the size of the client area) but will not for any other values entered. I'm pretty sure that it will be a simple mistake, but I have yet to spot it and really need to move on. Thank you for your help!
Here's the code:
function DrawGrid() {
var canvas = document.getElementById("Grid");
var context = canvas.getContext("2d");
context.beginPath();
context.moveTo(872, 432);
context.lineTo(1048, 432);
context.strokeStyle = "#FF0000";
context.lineWidth = 1;
context.stroke();
context.closePath();
}
As the question suggests, I am trying to draw a line on a HTML5 canvas. I have tested the values and find that it WILL draw a line from 0,0 to 1920,1040 (the size of the client area) but will not for any other values entered. I'm pretty sure that it will be a simple mistake, but I have yet to spot it and really need to move on. Thank you for your help!
Here's the code:
function DrawGrid() {
var canvas = document.getElementById("Grid");
var context = canvas.getContext("2d");
context.beginPath();
context.moveTo(872, 432);
context.lineTo(1048, 432);
context.strokeStyle = "#FF0000";
context.lineWidth = 1;
context.stroke();
context.closePath();
}
Share
Improve this question
edited Sep 25, 2013 at 15:08
JamesT
asked Sep 25, 2013 at 14:35
JamesTJamesT
472 silver badges9 bronze badges
1
-
Where are
CenterX
andCenterY
defined? – Matthew Burke Commented Sep 25, 2013 at 14:42
2 Answers
Reset to default 4There must be something wrong with your LineStartX
and LineStartY
variables (Unable to test because you haven't provided CenterX
and CenterY
), replacing them with hard coded integers seems to work.
DrawGrid();
function DrawGrid() {
var canvas = document.getElementById("Grid");
var context = canvas.getContext("2d");
//var LineStartX = CenterX - (width / 2);
// var LineStartY = CenterY - (height / 2);
context.beginPath();
context.moveTo(10, 20);
context.lineTo(100, 20);
context.strokeStyle = "#FFAACC";
context.lineWidth = 1;
context.stroke();
}
http://jsfiddle/4Mf8C/
I discovered the problem was caused by where I was calling the function.
As the question suggests, I am trying to draw a line on a HTML5 canvas. I have tested the values and find that it WILL draw a line from 0,0 to 1920,1040 (the size of the client area) but will not for any other values entered. I'm pretty sure that it will be a simple mistake, but I have yet to spot it and really need to move on. Thank you for your help!
Here's the code:
function DrawGrid() {
var canvas = document.getElementById("Grid");
var context = canvas.getContext("2d");
context.beginPath();
context.moveTo(872, 432);
context.lineTo(1048, 432);
context.strokeStyle = "#FF0000";
context.lineWidth = 1;
context.stroke();
context.closePath();
}
As the question suggests, I am trying to draw a line on a HTML5 canvas. I have tested the values and find that it WILL draw a line from 0,0 to 1920,1040 (the size of the client area) but will not for any other values entered. I'm pretty sure that it will be a simple mistake, but I have yet to spot it and really need to move on. Thank you for your help!
Here's the code:
function DrawGrid() {
var canvas = document.getElementById("Grid");
var context = canvas.getContext("2d");
context.beginPath();
context.moveTo(872, 432);
context.lineTo(1048, 432);
context.strokeStyle = "#FF0000";
context.lineWidth = 1;
context.stroke();
context.closePath();
}
Share
Improve this question
edited Sep 25, 2013 at 15:08
JamesT
asked Sep 25, 2013 at 14:35
JamesTJamesT
472 silver badges9 bronze badges
1
-
Where are
CenterX
andCenterY
defined? – Matthew Burke Commented Sep 25, 2013 at 14:42
2 Answers
Reset to default 4There must be something wrong with your LineStartX
and LineStartY
variables (Unable to test because you haven't provided CenterX
and CenterY
), replacing them with hard coded integers seems to work.
DrawGrid();
function DrawGrid() {
var canvas = document.getElementById("Grid");
var context = canvas.getContext("2d");
//var LineStartX = CenterX - (width / 2);
// var LineStartY = CenterY - (height / 2);
context.beginPath();
context.moveTo(10, 20);
context.lineTo(100, 20);
context.strokeStyle = "#FFAACC";
context.lineWidth = 1;
context.stroke();
}
http://jsfiddle/4Mf8C/
I discovered the problem was caused by where I was calling the function.
本文标签: javascriptHTML5 Canvas lines not drawingStack Overflow
版权声明:本文标题:javascript - HTML5 Canvas lines not drawing - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745593801a2158040.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论