當使用moveTo和LineTo移動鼠標時,我只畫線。從畫布的鼠標移出時必須省略未連接的點。如何從canvas中刪除未連接的點當我將mouseout從canvas中刪除時,需要刪除未連接的點。
這裏是jQuery代碼:
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
$(function() {
var canvas = $('#canvas');
var context = canvas.get(0).getContext("2d");
var clicked = false;
var b=0;
var storedLines = [];
var storedLine = {};
var mouse = {
x: -1,
y: -1
}
var parentOffset = $('#canvas').offset();
canvas.click(function(e) {
if (b==1)
{
$(this).unbind(e);
}
else
{
clicked = true;
mouse.x = e.pageX - parentOffset.left;
mouse.y = e.pageY - parentOffset.top;
context.moveTo(mouse.x, mouse.y);
if (clicked) {
storedLines.push({
startX: storedLine.startX,
startY: storedLine.startY,
endX: mouse.x,
endY: mouse.y
});
}
storedLine.startX = mouse.x;
storedLine.startY = mouse.y;
$(this).mousemove(function(k) {
context.clearRect(0, 0, 960, 500);
context.beginPath();
context.strokeStyle = "blue";
for (var i = 0; i < storedLines.length; i++) {
var v = storedLines[i];
context.moveTo(v.startX, v.startY);
context.lineTo(v.endX, v.endY);
context.stroke();
}
context.moveTo(mouse.x, mouse.y);
context.lineTo(k.pageX - parentOffset.left, k.pageY - parentOffset.top);
context.stroke();
context.closePath();
});
}
});
$('#canvas').mouseout(function(e){
$(this).unbind("mousemove");
b=1;
});
});
HTML代碼:
<html>
<body>
<canvas id="canvas" width=600 height=600 ></canvas>
</body>
</html>
試過基本數學嗎? http://www.wikihow.com/Algebraically-Find-the-Intersection-of-Two-Lines。有兩個循環。 – Cracker0dks
未連接的點或可能是不相交的線?而不連接的點是單獨的單獨點? – Pinal
不相交的行...在上面的代碼中提到,單擊時,它會觸發mousemove事件並在moveTo和LineTo之間畫線。假設當從畫布鼠標我不想畫線.. – sankar