我正在尝试编写一个程序,当用户第一次点击时会在屏幕上绘制一个圆圈,然后对于每次连续点击,它将绘制另一个圆圈并将第一个圆圈连接到新的圆圈。直线。除了根据用户点击次数绘制圆圈之外,我有点困惑。
这是我的代码
var app = angular.module('plunker', []);
app.controller('MainController', function($scope) {
//alert("test");
$scope.doClick = function(event){
var x = event.clientX;
var y = event.clientY;
var offsetX = event.offsetX;
var offsetY = event.offsetY;
//alert(x, y, offsetX, offsetY);
/// These are the 2 new lines, see you target the canvas element then apply it to getContext
var canvasElement = document.getElementById("canvas");
var ctx = canvasElement.getContext("2d");
//draw a circle
ctx.beginPath();
ctx.arc(x, y, 5, 0, Math.PI*2, true);
ctx.closePath();
ctx.fill();
};
});
这是plnk的链接
http://plnkr.co/edit/rYVLgB14IutNh1F4MN6T?p=preview
任何帮助表示赞赏
答案 0 :(得分:1)
你已经很好地绘制了圆圈......这里是如何做连接线的!
您可以使用合成在先前绘制的内容
下绘制连接线
特别是ctx.globalCompositeOperation='destination-over'
会导致您的新连接线在
以下是示例代码和演示:
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var cw=canvas.width;
var ch=canvas.height;
function reOffset(){
var BB=canvas.getBoundingClientRect();
offsetX=BB.left;
offsetY=BB.top;
}
var offsetX,offsetY;
reOffset();
window.onscroll=function(e){ reOffset(); }
var isDown=false;
var startX,startY;
var radius=10;
var lastX,lastY;
ctx.fillStyle='red';
$("#canvas").mousedown(function(e){handleMouseDown(e);});
function drawCircle(cx,cy){
if(lastX){
ctx.globalCompositeOperation='destination-over';
ctx.beginPath();
ctx.moveTo(lastX,lastY);
ctx.lineTo(cx,cy);
ctx.stroke();
ctx.globalCompositeOperation='source-over';
}
ctx.beginPath();
ctx.arc(cx,cy,radius,0,Math.PI*2);
ctx.closePath();
ctx.fill();
}
function handleMouseDown(e){
// tell the browser we're handling this event
e.preventDefault();
e.stopPropagation();
mx=parseInt(e.clientX-offsetX);
my=parseInt(e.clientY-offsetY);
drawCircle(mx,my);
lastX=mx;
lastY=my;
}

body{ background-color: ivory; }
#canvas{border:1px solid red;}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<h4>Click on the canvas to draw connected circles</h4>
<canvas id="canvas" width=300 height=300></canvas>
&#13;
[添加了所有新圈子连接到第一个的示例]
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var cw=canvas.width;
var ch=canvas.height;
function reOffset(){
var BB=canvas.getBoundingClientRect();
offsetX=BB.left;
offsetY=BB.top;
}
var offsetX,offsetY;
reOffset();
window.onscroll=function(e){ reOffset(); }
var isDown=false;
var startX,startY;
var radius=10;
var lastX,lastY;
ctx.fillStyle='red';
$("#canvas").mousedown(function(e){handleMouseDown(e);});
function drawCircle(cx,cy){
if(lastX){
ctx.globalCompositeOperation='destination-over';
ctx.beginPath();
ctx.moveTo(lastX,lastY);
ctx.lineTo(cx,cy);
ctx.stroke();
ctx.globalCompositeOperation='source-over';
}else{
lastX=cx;
lastY=cy;
}
ctx.beginPath();
ctx.arc(cx,cy,radius,0,Math.PI*2);
ctx.closePath();
ctx.fill();
}
function handleMouseDown(e){
// tell the browser we're handling this event
e.preventDefault();
e.stopPropagation();
mx=parseInt(e.clientX-offsetX);
my=parseInt(e.clientY-offsetY);
drawCircle(mx,my);
}
&#13;
body{ background-color: ivory; }
#canvas{border:1px solid red;}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<h4>Click on the canvas to draw connected circles</h4>
<canvas id="canvas" width=300 height=300></canvas>
&#13;