我想用画布在一个图像上绘制多个图形。
在我的代码中,我使用ctx.clearRect(0,0,canvas.width,canvas.height);
因此,它不允许我做多个绘图。如果我没有使用clearRect(),程序将无法正常工作。另外,我试图改变clearRect()的位置,但它也不适合我。

var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');

ctx.clearRect(0,0,canvas.width,canvas.height); //clear canvas
ctx.beginPath();
var width = mousex-last_mousex;
var height = mousey-last_mousey;
ctx.rect(last_mousex,last_mousey,width,height);
ctx.strokeStyle = "blue";
ctx.lineWidth = 10;
ctx.stroke();

//Canvas
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');

//Variables
var canvasx = $(canvas).offset().left;
var canvasy = $(canvas).offset().top;
var last_mousex = last_mousey = 0;
var mousex = mousey = 0;
var mousedown = false;

//Mousedown
$(canvas).on('mousedown', function(e) {
  last_mousex = parseInt(e.clientX - canvasx);
  last_mousey = parseInt(e.clientY - canvasy);
  mousedown = true;
});

//Mouseup
$(canvas).on('mouseup', function(e) {
  mousedown = false;
});

//Mousemove
$(canvas).on('mousemove', function(e) {
  mousex = parseInt(e.clientX - canvasx);
  mousey = parseInt(e.clientY - canvasy);
  if (mousedown) {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.beginPath();
    var width = mousex - last_mousex;
    var height = mousey - last_mousey;
    ctx.rect(last_mousex, last_mousey, width, height);
    ctx.strokeStyle = "blue";
    ctx.lineWidth = 10;
    ctx.stroke();
  }
  //Output
  $('#output').html('current: ' + mousex + ', ' + mousey + '<br/>last: ' + last_mousex + ', ' + last_mousey + '<br/>mousedown: ' + mousedown);
});

canvas {
  cursor: crosshair;
  border: 1px solid #000000;
  background-image: url("kroki2v3.png");
  background-repeat: no-repeat;
}

<html>

<head>
  <meta charset="utf-8" />
  <script src="https://code.jquery.com/jquery-2.1.3.js" integrity="sha256-goy7ystDD5xbXSf+kwL4eV6zOPJCEBD1FBiCElIm+U8=" crossorigin="anonymous"></script>

</head>

<body>
  <canvas id="canvas" width="3200" height="1400"></canvas>
  <div id="output"></div>
</body>

</html>

最佳答案

必须将每个矩形位置和大小存储在数组中,并通过循环打印。

jQuery(document).ready(function($) {

var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');

ctx.clearRect(0,0,canvas.width,canvas.height); //clear canvas
ctx.beginPath();
var width = mousex-last_mousex;
var height = mousey-last_mousey;
ctx.rect(last_mousex,last_mousey,width,height);
ctx.strokeStyle = "blue";
ctx.lineWidth = 10;
ctx.stroke();
//Canvas
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');

//Variables
var canvasx = $(canvas).offset().left;
var canvasy = $(canvas).offset().top;
var last_mousex = 0;
var last_mousey = 0;
var mousex = 0;
var mousey = 0;
var mousedown = false;
var shapes = [];
var width;
var height;
//Mousedown
$(canvas).on('mousedown', function(e) {
  last_mousex = parseInt(e.clientX - canvasx);
  last_mousey = parseInt(e.clientY - canvasy);
  mousedown = true;
});

//Mouseup
$(canvas).on('mouseup', function(e) {
  const lastPos = {
  	lastMouseX : last_mousex,
  	lastMouseY : last_mousey,
  	rectWidth : width,
  	rectHeight : height
  }
  shapes.push(lastPos);
  mousedown = false;
});

//Mousemove
$(canvas).on('mousemove', function(e) {
  mousex = parseInt(e.clientX - canvasx);
  mousey = parseInt(e.clientY - canvasy);
  if (mousedown) {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.beginPath();
    width = mousex - last_mousex;
    height = mousey - last_mousey;
    if (shapes.length > 0){
    	for(var i in shapes){
		    ctx.rect(shapes[i].lastMouseX, shapes[i].lastMouseY, shapes[i].rectWidth, shapes[i].rectHeight);
    	}
    }
    ctx.rect(last_mousex, last_mousey, width, height);
    ctx.strokeStyle = "blue";
    ctx.lineWidth = 10;
    ctx.stroke();
  }
  //Output
  $('#output').html('current: ' + mousex + ', ' + mousey + '<br/>last: ' + last_mousex + ', ' + last_mousey + '<br/>mousedown: ' + mousedown);
});
});

canvas {
	cursor: crosshair;
	border: 1px solid #000000;
	background-image: url("kroki2v3.png");
	background-repeat: no-repeat;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<canvas id="canvas" width="3200" height="1400"></canvas>
<div id="output"></div>

关于javascript - 如何不对多个图形使用canvas clearRect()?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56846500/

10-09 01:54