SOURCE

console 命令行工具 X clear

                    
>
console
var canv=document.getElementById("canv");
var content=canv.getContext("2d");
var i=0;

var img1 = new Image();
img1.onload=function(){
//setInterval("b()",30);
  }
img1.src = 'https://www.baidu.com/img/bd_logo1.png';

var color="red";
function b(sx,sy,mx,my){
//content.clearRect(0,0,1000,1000);
//content.fillRect(m_clientX,m_clientY,2,2);
  content.strokeStyle = color;
       // 设置线条的宽度
 content.lineWidth = 5;
       // 绘制直线
  content.beginPath();
  content.moveTo(sx,sy);
  content.lineTo(mx,my);
  content.stroke();
 // content.drawImage(img1,i,100);
  i++;
}
var is_mouse_sown=false;
var m_clientX,m_clientY;
function get_canvas(ev,obj){
    m_clientX_t = ev.clientX-obj.offsetLeft;
    m_clientY_t = ev.clientY-obj.offsetTop;
  if(is_mouse_down){
    b(m_clientX,m_clientY,m_clientX_t,m_clientY_t);
  }
    m_clientX = ev.clientX-obj.offsetLeft;
    m_clientY = ev.clientY-obj.offsetTop;
}

function color_red(){
  color="red";
}
function color_green(){
  color="green";
}
	canv.onmousedown=function(){
    //alert("d");
		is_mouse_down=true;
		}
	canv.onmouseup=function(){
    //alert("d");
		is_mouse_down=false;
		}
<button onclick="color_red()">红色</button><button onclick="color_green()">绿色</button>
<canvas id="canv" width="1000" height="1000" onmousemove="get_canvas(event,this)" onclick="myclick()"></canvas>