[澄清后,编辑由海报]
该代码将绘制/继续折线到永远的用户点击下一步。
如果用户在它们开始的绿色圆圈中单击,则会填充多段线。
这里是代码和一个小提琴:http://jsfiddle.net/m1erickson/qwd2a/
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<!--[if lt IE 9]><script type="text/javascript" src="../excanvas.js"></script><![endif]-->
<style>
body{ background-color: ivory; padding:10px; }
canvas{border:1px solid red;}
</style>
<script>
$(function(){
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var canvasMouseX;
var canvasMouseY;
var canvasOffset=$("#canvas").offset();
var offsetX=canvasOffset.left;
var offsetY=canvasOffset.top;
var storedLines=[];
var startX=0;
var startY=0;
var radius=7;
ctx.strokeStyle="orange";
ctx.font = '12px Arial';
$("#canvas").mousedown(function(e){handleMouseDown(e);});
function handleMouseDown(e){
canvasMouseX=parseInt(e.clientX-offsetX);
canvasMouseY=parseInt(e.clientY-offsetY);
// Put your mousedown stuff here
if(hitStartCircle(canvasMouseX, canvasMouseY)){
fillPolyline();
return;
}
storedLines.push({x:canvasMouseX,y:canvasMouseY});
if(storedLines.length==1){
startX=canvasMouseX;
startY=canvasMouseY;
ctx.fillStyle="green";
ctx.beginPath();
ctx.arc(canvasMouseX, canvasMouseY, radius, 0 , 2 * Math.PI, false);
ctx.fill();
}else{
var c=storedLines.length-2;
ctx.strokeStyle="orange";
ctx.lineWidth=3;
ctx.beginPath();
ctx.moveTo(storedLines[c].x,storedLines[c].y);
ctx.lineTo(canvasMouseX, canvasMouseY);
ctx.stroke();
}
}
function hitStartCircle(x,y){
var dx=x-startX;
var dy=y-startY;
return(dx*dx+dy*dy<radius*radius)
}
function fillPolyline(){
ctx.strokeStyle="red";
ctx.fillStyle="blue";
ctx.lineWidth=3;
ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.beginPath();
ctx.moveTo(storedLines[0].x,storedLines[0].y);
for(var i=0;i<storedLines.length;i++){
ctx.lineTo(storedLines[i].x,storedLines[i].y);
}
ctx.closePath();
ctx.fill();
ctx.stroke();
storedLines=[];
}
$("#clear").click(function(){
ctx.clearRect(0,0,canvas.width,canvas.height);
storedLines=[];
});
}); // end $(function(){});
</script>
</head>
<body>
<p>Click to draw lines</p>
<p>Click back in the green circle to close+fill</p><br/>
<canvas id="canvas" width=300 height=300></canvas><br/>
<button id="clear">Clear Canvas</button>
</body>
</html>
[上一页答案基于信息较少]
它看起来像你想:
- 让用户点击画布上的点。
- 在storedLines []中收集这些点。
- 稍后,使用storedLines []画线+填充颜色。
- (我假设你不想立即画线,因为你说“我需要检测绘图线何时完成......”。)如果你做要立即提请当用户点击,给我留下了评论,我可以帮你,替代过多;)
如果让我来推荐几个清理在你的代码:
这是让你的画布上鼠标位置的好方法:
当你实际绘制的路径与上下文和storedLines:
- 要关闭路径,请在context.stroke()和context.fill()之前放置context.closePath()。
- 设置fillStyle和strokeStyle。
做context.fill()和context.stroke() - 如果你不填充(),你将不会得到颜色填充。
// close a path
context.closePath();
context.fillStyle=”blue”;
context.strokeStyle=”rgb(180,80,95)”;
context.fill();
context.stroke();
这里是代码和一个小提琴:http://jsfiddle.net/m1erickson/zYsQh/
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<!--[if lt IE 9]><script type="text/javascript" src="../excanvas.js"></script><![endif]-->
<style>
body{ background-color: ivory; padding:10px; }
canvas{border:1px solid red;}
</style>
<script>
$(function(){
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var canvasMouseX;
var canvasMouseY;
var canvasOffset=$("#canvas").offset();
var offsetX=canvasOffset.left;
var offsetY=canvasOffset.top;
var storedLines=[];
ctx.strokeStyle="orange";
ctx.font = '12px Arial';
$("#canvas").mousedown(function(e){handleMouseDown(e);});
function handleMouseDown(e){
canvasMouseX=parseInt(e.clientX-offsetX);
canvasMouseY=parseInt(e.clientY-offsetY);
// Put your mousedown stuff here
storedLines.push({x:canvasMouseX,y:canvasMouseY});
var count=storedLines.length;
var X=canvasMouseX-(count<10?4:7);
ctx.strokeStyle="orange";
ctx.fillStyle="black";
ctx.lineWidth=1;
ctx.beginPath();
ctx.arc(canvasMouseX, canvasMouseY, 8, 0 , 2 * Math.PI, false);
ctx.fillText(storedLines.length, X, canvasMouseY+4);
ctx.stroke();
}
$("#draw").click(function(){
ctx.strokeStyle="red";
ctx.fillStyle="blue";
ctx.lineWidth=3;
ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.beginPath();
ctx.moveTo(storedLines[0].x,storedLines[0].y);
for(var i=0;i<storedLines.length;i++){
ctx.lineTo(storedLines[i].x,storedLines[i].y);
}
ctx.closePath();
ctx.fill();
ctx.stroke();
storedLines=[];
});
$("#clear").click(function(){
ctx.clearRect(0,0,canvas.width,canvas.height);
storedLines=[];
});
}); // end $(function(){});
</script>
</head>
<body>
<p>Click to create storedLines[]</p>
<p>Then press the Draw button to fill the path</p><br/>
<canvas id="canvas" width=300 height=300></canvas><br/>
<button id="clear">Clear Canvas</button>
<button id="draw">Draw</button>
</body>
</html>
喜!谢谢您的帮助。我实际上需要在用户单击并用颜色填充颜色时填写线条 – chamara 2013-03-21 01:59:01
没问题...当用户单击并填充完成时,我已根据绘制线修改了我的答案 - 干杯:) – markE 2013-03-21 02:52:28
您好!需要帮助。我需要在绘图时用光标悬挂绘图线。我试图用mousemove事件,但没有得到它的工作。 – chamara 2013-03-21 10:51:37