【发布时间】:2020-12-26 20:48:31
【问题描述】:
所以,我正在创建一些绘图工具,但是当我移动鼠标时,它不会在一条线上画圆。我不知道怎么说好,但我想让它从A点(鼠标的最后位置)到B点(鼠标的当前位置)画一条线。
从这里开始画,你就会明白:
const c = document.getElementById('c')
const ctx = c.getContext('2d')
c.height = window.innerHeight
c.width = window.innerWidth
function pen(e, colorHEX) {
let mouseX = e.clientX
let mouseY = e.clientY
ctx.fillStyle = colorHEX
ctx.beginPath()
ctx.arc(mouseX, mouseY, 2, 0, Math.PI * 2)
ctx.fill()
ctx.closePath()
}
$('body').mousedown(function(eObj){
$('body').mousemove(function(e) {
pen(e, '#000000')
})
})
$('body').mouseup(function(eObj) {
$('body').unbind('mousemove')
})
body {
margin: 0;
overflow: hidden;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link href="style.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<canvas id="c"></canvas>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script src="script.js"></script>
</body>
</html>
【问题讨论】:
-
鼠标按下获取坐标。移动鼠标并在鼠标上获取坐标。现在计算线 y=mx + b 的公式。然后循环点创建线
-
嗯...你能解释一下慢一点吗?我不明白。如果可以的话,在代码中。