我正在尝试使用鼠标移动事件在画布上绘图
你可以看到这个闪电战的工作演示 我在鼠标移动时调用这个函数来绘制一个矩形
updateDraw(e: MouseEvent) {
this.previousCoordinates = this.currentCoordinates;
this.currentCoordinates = HelloComponent.getCoordinatesOnCanvas(this.canvas.nativeElement, e);
if (this.drawingMode) {
HelloComponent.createShape(this.shapes, this.ctx, this.startCoordinates,
this.currentCoordinates, this.previousCoordinates, true);
this.endCoordinates = this.currentCoordinates;
}
}
问题是,如果我将鼠标移动得太快,我会得到多个矩形,(我假设清除矩形不起作用,因为鼠标移动太快)我怎样才能避免这种情况在 1 次拖动中应该只有 1 个矩形?
编辑:我希望能够绘制超过 1 个矩形,这里我正在跟踪和清除以前的坐标
private static createShape(shape: Shapes, context: CanvasRenderingContext2D,
start: Coordinates, end: Coordinates, prev: Coordinates,
dotted: boolean) {
context.clearRect(start.x, start.y, (prev.x - start.x), (prev.y - start.y));