main.js:

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

let isDrawing = false;
let lastX = 0;
let lastY = 0;
const AXIS_LENGTH = 50; // Length of the axis lines
let mouseX;
let mouseY;

// history so we can always redraw
var points = []
var allPoints = []
var invertedX, invertedY

canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

function startDrawing(e) {
  isDrawing = true;
  [lastX, lastY] = [canvas.width - (e.pageX - canvas.offsetLeft), canvas.height - (e.pageY - canvas.offsetTop)];
  points = []
  allPoints.push(points)
  points.push([lastX, lastY])


}

function draw(e) {
  if (!isDrawing) return;

  const x = canvas.width - (e.pageX - canvas.offsetLeft);
  const y = canvas.height - (e.pageY - canvas.offsetTop);

  [lastX, lastY] = [x, y];
  points.push([lastX, lastY])

}

function stopDrawing() {
  isDrawing = false;
}


function clearCanvas() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
}

function resizeCanvas() {
  canvas.width = window.innerWidth;
  canvas.height = window.innerHeight;
}

function drawAllPoints() {
  allPoints.forEach(drawPoints)

}

function drawPoints(points) {

  if (points.length) {
    ctx.save()
    ctx.strokeStyle = 'black';
    ctx.lineWidth = 5;
    ctx.lineCap = 'round';
    ctx.beginPath();
    ctx.moveTo(points[0][0], points[0][1]);
    for (var i = 1; i < points.length; i++) {
      ctx.lineTo(points[i][0], points[i][1]);
      ctx.stroke();

    }
    ctx.restore()
  }
}

function drawEverything() {
  clearCanvas()
  drawAxis();
  drawAllPoints()

}

function drawAxis() {

  mouseX = invertedX
  mouseY = invertedY
  // Draw vertical axis line
  ctx.save()
  ctx.beginPath();
  ctx.moveTo(mouseX, mouseY - AXIS_LENGTH / 2);
  ctx.lineTo(mouseX, mouseY + AXIS_LENGTH / 2);
  ctx.stroke();

  // Draw horizontal axis line
  ctx.beginPath();
  ctx.moveTo(mouseX - AXIS_LENGTH / 2, mouseY);
  ctx.lineTo(mouseX + AXIS_LENGTH / 2, mouseY);
  ctx.stroke();
  ctx.restore()
}


canvas.addEventListener('mousedown', startDrawing);
canvas.addEventListener('mousemove', draw);
canvas.addEventListener('mouseup', stopDrawing);
canvas.addEventListener('mouseout', stopDrawing);
canvas.addEventListener('touchstart', (e) => {
  e.preventDefault();
  startDrawing(e.touches[0]);
});
canvas.addEventListener('touchmove', (e) => {
  e.preventDefault();
  draw(e.touches[0]);
});
canvas.addEventListener('touchend', stopDrawing);
window.addEventListener('resize', resizeCanvas);
document.addEventListener("mousemove", function(event) {
  const mouseX = event.clientX;
  const mouseY = event.clientY;

  invertedX = window.innerWidth - mouseX;
  invertedY = window.innerHeight - mouseY;

  drawEverything()
});

当我试图通过单击擦除按钮来擦除画布时,画布上的每一个笔画都变成了白色.我还有一个十字叉,可以显示绘制笔画的位置.控制是倒置的.我使用if else来绘制和擦除.我使用白色作为笔画,这使得它作为橡皮擦工具.但当我单击bTN并try 拖动时,一切都变成白色,但再次单击bTN会使一切都变成黑色.

推荐答案

嘿,又是Merging two canvas file岁的我

请记住,画布是从内存变量allPoints重新绘制的.因此,我们将向该变量添加 colored颜色 .顺便说一句,这将使保存和恢复图形变得更容易,因为它已经表示为变量.

此外,我们正在mousemove上重新绘制画布.让我们纠正错误并使用requestAnimationFrame

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

let isDrawing = false;
let lastX = 0;
let lastY = 0;
const AXIS_LENGTH = 50; // Length of the axis lines
let mouseX;
let mouseY;


// history so we can always redraw
var points = []
var stroke = {}
var allPoints = []
var invertedX, invertedY
var chosenColor = "black"

canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

function startDrawing(e) {
  isDrawing = true;
  [lastX, lastY] = [canvas.width - (e.pageX - canvas.offsetLeft), canvas.height - (e.pageY - canvas.offsetTop)];
  points = []
  stroke = {
    points: points,
    color: chosenColor
  }
  allPoints.push(stroke)
  points.push([lastX, lastY])
}


function drawColorButtons() {
  document.querySelectorAll(".toolbar .btn").forEach(function(btn) {
    var color = btn.getAttribute("data-color")
    btn.style.backgroundColor = color
    btn.addEventListener('click', function() {
      chosenColor = color
    })
  })
}
drawColorButtons()

function draw(e) {
  if (!isDrawing) return;

  const x = canvas.width - (e.pageX - canvas.offsetLeft);
  const y = canvas.height - (e.pageY - canvas.offsetTop);

  [lastX, lastY] = [x, y];
  points.push([lastX, lastY])

}

function stopDrawing() {
  isDrawing = false;
}


function clearCanvas() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
}

function resizeCanvas() {
  canvas.width = window.innerWidth;
  canvas.height = window.innerHeight;
}

function drawAllPoints() {
  allPoints.forEach(drawStroke)

}

function drawStroke(stroke) {
  drawPoints(stroke.points, stroke.color)
}

function drawPoints(points, color) {
  if (points.length) {
    ctx.save()
    ctx.strokeStyle = color;
    ctx.lineWidth = 5;
    ctx.lineCap = 'round';
    ctx.beginPath();
    ctx.moveTo(points[0][0], points[0][1]);
    for (var i = 1; i < points.length; i++) {
      ctx.lineTo(points[i][0], points[i][1]);
      ctx.stroke();
    }
    ctx.restore()
  }
}

function drawEverything() {
  clearCanvas()
  drawAllPoints()
  drawAxis();
  requestAnimationFrame(drawEverything)
}
drawEverything()


function drawAxis() {

  mouseX = invertedX
  mouseY = invertedY
  // Draw vertical axis line
  ctx.save()
  ctx.strokeStyle = chosenColor;

  ctx.beginPath();
  ctx.moveTo(mouseX, mouseY - AXIS_LENGTH / 2);
  ctx.lineTo(mouseX, mouseY + AXIS_LENGTH / 2);
  ctx.stroke();

  // Draw horizontal axis line
  ctx.beginPath();
  ctx.moveTo(mouseX - AXIS_LENGTH / 2, mouseY);
  ctx.lineTo(mouseX + AXIS_LENGTH / 2, mouseY);
  ctx.stroke();
  ctx.restore()
}


canvas.addEventListener('mousedown', startDrawing);
canvas.addEventListener('mousemove', draw);
canvas.addEventListener('mouseup', stopDrawing);
canvas.addEventListener('mouseout', stopDrawing);
canvas.addEventListener('touchstart', (e) => {
  e.preventDefault();
  startDrawing(e.touches[0]);
});
canvas.addEventListener('touchmove', (e) => {
  e.preventDefault();
  draw(e.touches[0]);
});
canvas.addEventListener('touchend', stopDrawing);
window.addEventListener('resize', resizeCanvas);
document.addEventListener("mousemove", function(event) {
  const mouseX = event.clientX;
  const mouseY = event.clientY;

  invertedX = window.innerWidth - mouseX;
  invertedY = window.innerHeight - mouseY;

});
body {
  padding: 0;
  margin: 0;
  overflow: hidden;
}

canvas {
  background: gray;
  display: block;
}

.toolbar {
  position: absolute;
  z-index: 1;
  top: 20px;
  left: 20px;
  display: flex;
}

.btn {
  width: 20px;
  height: 20px;
  border: 1px solid black;
  margin-right: 2px;
  cursor: pointer;
}
<canvas id="canvas"></canvas>
<div class="toolbar">
  <div class="btn" data-color="black"></div>
  <div class="btn" data-color="white"></div>
  <div class="btn" data-color="gray"></div>
  <div class="btn" data-color="red"></div>
  <div class="btn" data-color="green"></div>
  <div class="btn" data-color="yellow"></div>
  <div class="btn" data-color="blue"></div>
</div>

Javascript相关问答推荐

使用reaction创建可排序的表不起作用

为什么我无法使用useMemo()停止脚本渲染?

React redux状态未在React-Router库中呈现

reaction useEffect KeyDown for each 条目配音输出

如何通过onClick为一组按钮分配功能;

如何修复循环HTML元素附加函数中的问题?

通过在页面上滚动来移动滚动条

深嵌套的ng-container元素仍然可以在Angular 布局组件中正确渲染内容吗?

模块与独立组件

从mat—country—select获取整个Country数组

Google图表时间轴—更改hAxis文本 colored颜色

网页自检测外部元素无法加载

Google maps API通过API返回ZERO_RESULTS,以获得方向请求,但适用于Google maps

如果Arrow函数返回函数,而不是为useEffect返回NULL,则会出现错误

WP Bootstrap NavWaker:下拉菜单一次打开所有下拉菜单

检索相加到点的子项

如何在不影响隐式类型的情况下将类型分配给对象?

当我在Reaction中创建一个输入列表时,我的输入行为异常

如何 for each 输入动态设置输入变更值

Socket.IO在刷新页面时执行函数两次