简体   繁体   English

在圆形画布上单击鼠标并绘制线条?

[英]Click mouse on circle canvas and draw lines?

How to make clicking on the circle began to drawn a straight line and when you click on the second point stop draw line? 如何使单击圆开始画一条直线,而当您单击第二个点停止画线时呢? In my case, drawing a line erases the entire canvas. 就我而言,画一条线会擦除整个画布。 Sorry for my bad English 对不起,我的英语不好

   var canvas = document.getElementById('circle');
    var ctx = canvas.getContext( '2d' );
   canvas.width = 800;
   canvas.height = 600;

   var rect = can.getBoundingClientRect();


function dots1() {
ctx.beginPath();
ctx.arc(50 , 80, 4, 0, 2*Math.PI, true);
ctx.fillStyle ="#" +  Math.floor(Math.random()*0xFFFFFF).toString(16);
ctx.fill();
ctx.stroke();
 };

function dots2() {
ctx.beginPath();
ctx.arc(120 , 220, 4, 0, 2*Math.PI, true);
ctx.fillStyle ="#" +  Math.floor(Math.random()*0xFFFFFF).toString(16);
ctx.fill();
ctx.stroke();
 };

canvas.onmousemove = function () {
      c.clearRect(0,0,canvas.width,canvas.height);
      c.strokeStyle = 'blue';
      c.lineWidth = 1;
      c.beginPath();
      c.moveTo(letsdraw.x, letsdraw.y);
      c.lineTo(event.clientX - rect.left, event.clientY - rect.top);
      c.stroke();


   };
   canvas.onmousedown = function () {
       letsdraw = {
      x:event.clientX - rect.left,
      y:event.clientY - rect.top
      };

     };
canvas.onmouseup = function() {

  letsdraw = null;

 };

看起来像是一个简单的错字-我认为您在onmousemove函数中用的是'ctx'而不是'c'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM