gpt4 book ai didi

javascript - 当用户使用 Javascript 在屏幕上拖动鼠标创建多个圆圈时如何删除特定圆圈

转载 作者:行者123 更新时间:2023-11-27 23:58:54 24 4
gpt4 key购买 nike

我使用 Canvas 创建了圆圈,但无法通过双击删除相同的圆圈。`

var canvas,
context, shapes,
dragging = false, draggingtoMove = false,
dragStartLocation,dragEndLocation,
snapshot;
var numShapes;
function initiate() {
numShapes = 100;
shapes = [];
canvas = document.getElementById('canvas');
context = canvas.getContext('2d');
canvas.addEventListener('mousedown', dragStart, false);
canvas.addEventListener('mousemove', drag, false);
canvas.addEventListener('mouseup', dragStop, false);
canvas.addEventListener('dblclick', dblclickerase);
}
function dblclickerase(evt)
{
var i;
//We are going to pay attention to the layering order of the objects so that if a mouse down occurs over more than object,
//only the topmost one will be dragged.
var highestIndex = -1;

//getting mouse position correctly, being mindful of resizing that may have occured in the browser:
var bRect = canvas.getBoundingClientRect();
mouseX = (evt.clientX - bRect.left)*(canvas.width/bRect.width);
mouseY = (evt.clientY - bRect.top)*(canvas.height/bRect.height);

//find which shape was clicked
for (i=0; i < shapes.length; i++) {
if (hitTest(shapes[i], mouseX, mouseY)) {
//draggingtoMove = true;
if (i > highestIndex) {
// here i want to delete the circle on double click but not getting logic, I can get mouse location but how to select the circle and delete it
}
}
}
}
function getCanvasCoordinates(event) {
var x = event.clientX - canvas.getBoundingClientRect().left,
y = event.clientY - canvas.getBoundingClientRect().top;
return {
x: x,
y: y
};
}

function takeSnapshot() {
snapshot = context.getImageData(0, 0, canvas.width, canvas.height);
}

function restoreSnapshot() {
context.putImageData(snapshot, 0, 0);
}


function draw(position) {
var radius = Math.sqrt(Math.pow((dragStartLocation.x - position.x), 2) + Math.pow((dragStartLocation.y - position.y), 2));
var i=0;
var tempX;
var tempY;
var tempRad;
var tempR;
var tempG;
var tempB;
var tempColor;
tempRad = radius;
tempX = dragStartLocation.x;
tempY = dragStartLocation.y;
tempColor = getRndColor();
tempShape = {x:tempX, y:tempY, rad:tempRad, color:tempColor};
shapes.push(tempShape);
context.beginPath();
context.arc(tempX, tempY, tempRad, 0, 2*Math.PI, false);
//context.closePath();
context.fillStyle = tempColor;
context.fill();
i++;
}

function dragStart(evt) {
dragging = true;
//if (dragging == true) {
dragStartLocation = getCanvasCoordinates(evt);
takeSnapshot();
//}

}

function hitTest(shape,mx,my) {

var dx;
var dy;
dx = mx - shape.x;
dy = my - shape.y;

//a "hit" will be registered if the distance away from the center is less than the radius of the circular object
return (dx*dx + dy*dy < shape.rad*shape.rad);
}

function drag(event) {
var position;
if (dragging === true) {
restoreSnapshot();
position = getCanvasCoordinates(event);
draw(position);
}
}

function dragStop(event) {
dragging = false;
restoreSnapshot();
var position = getCanvasCoordinates(event);
dragEndLocation = getCanvasCoordinates(event);
draw(position);
}

function getRndColor() {
var r = 255 * Math.random() | 0,
g = 255 * Math.random() | 0,
b = 255 * Math.random() | 0;
return 'rgb(' + r + ',' + g + ',' + b + ')';
}

function eraseCanvas() {
context.clearRect(0, 0, canvas.width, canvas.height);
}

addEventListener("load",initiate);

<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<canvas id="canvas" width="1020" height="640"></canvas>
<button onclick="eraseCanvas()" style="float: right;">Reset</button>
</body>
</html>

My question is how to delete the circle when double click on same, I added 'dblClick' eventListener but still I am only able to perform the 'clearRect' which will only clear the rectangle from start and end location which is little odd. Another thing I can't change the color to white which will not be valid.point as if my circle overlaps another will look odd.

最佳答案

你无法像这样删除在 Canvas 上绘制的内容。一旦它被绘制在 Canvas 上,它就会停留在那里,除了读取像素数据之外你无法访问它 - 但这并不能解决你的问题,因为你可以有相同颜色的重叠圆圈。

要解决此问题,您必须跟踪绘制的圆圈,并在每次需要时重新绘制整个 Canvas (添加新圆圈、删除旧圆圈等时)。这样,当您想要删除一个圆圈时,只需将其从圆圈列表中删除即可(一个简单的数组即可)。但重要的是您需要清除并重新绘制整个 Canvas 。

摘要:当 Canvas 不断重绘时(无论是在每次勾选时还是在发生用户交互时),您的单击和拖动功能应该仅将圆添加到圆列表中(指定 x、y、半径等数据) ,颜色),而双击一个圆圈会在列表中查找该圆圈,并将其删除。

关于javascript - 当用户使用 Javascript 在屏幕上拖动鼠标创建多个圆圈时如何删除特定圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32009997/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com