我有这段代码,可以在画布上画一条线
var canvas, ctx, flag = false,
prevX = 0,
currX = 0,
prevY = 0,
currY = 0,
dot_flag = false;
var x = "black",
y = 10;
function initialize() {
canvas = document.getElementById('can');
const bounds = canvas.getBoundingClientRect();
canvas.width = bounds.width;
canvas.height = bounds.height;
ctx = canvas.getContext("2d");
w = canvas.width;
h = canvas.height;
canvas.addEventListener("mousemove", function (e) {
findxy('move', e)
}, false);
canvas.addEventListener("mousedown", function (e) {
findxy('down', e)
}, false);
canvas.addEventListener("mouseup", function (e) {
findxy('up', e)
}, false);
canvas.addEventListener("mouseout", function (e) {
findxy('out', e)
}, false);
}
function draw(e) {
ctx.beginPath();
ctx.moveTo(prevX, prevY);
ctx.lineTo(currX, currY);
ctx.strokeStyle = x;
ctx.lineWidth = y;
ctx.stroke();
ctx.closePath();
}
function getMousePos(canvas, evt) {
var rect = canvas.getBoundingClientRect();
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
};
}
function findxy(res, e) {
var pos = getMousePos(canvas, e);
if (res == 'down') {
prevX = currX;
prevY = currY;
currX = pos.x;
currY = pos.y;
flag = true;
dot_flag = true;
if (dot_flag) {
ctx.beginPath();
ctx.fillStyle = x;
ctx.fillRect(currX, currY, 2, 2);
ctx.closePath();
dot_flag = false;
}
}
if (res == 'up' || res == "out") {
flag = false;
}
if (res == 'move') {
if (flag) {
prevX = currX;
prevY = currY;
currX = pos.x;
currY = pos.y;
draw();
}
}
}
如果
y
的no较高,例如10,则该线将被绘制为粗水平线,我知道它可以绘制一条圆形线,像autodraw这样的站点可以在那里绘制漂亮的草图。这是它制作https://www.youtube.com/watch?v=e0SdFLDAb6U&feature=youtu.be的行类型的屏幕记录
最佳答案
在画线之前画一条弧。这将使绘制的线“圆”。
var canvas, ctx, flag = false,
prevX = 0,
currX = 0,
prevY = 0,
currY = 0,
dot_flag = false;
var x = "black",
y = 5;
initialize()
function initialize() {
canvas = document.getElementById('can');
const bounds = canvas.getBoundingClientRect();
canvas.width = bounds.width;
canvas.height = bounds.height;
ctx = canvas.getContext("2d");
w = canvas.width;
h = canvas.height;
canvas.addEventListener("mousemove", function(e) {
findxy('move', e)
}, false);
canvas.addEventListener("mousedown", function(e) {
findxy('down', e)
}, false);
canvas.addEventListener("mouseup", function(e) {
findxy('up', e)
}, false);
canvas.addEventListener("mouseout", function(e) {
findxy('out', e)
}, false);
}
function draw(e) {
//arc
ctx.beginPath();
ctx.arc(currX, currY, y, 0, Math.PI * 2);
ctx.fillStyle = x;
ctx.fill();
//line
ctx.beginPath();
ctx.moveTo(prevX, prevY);
ctx.lineTo(currX, currY);
ctx.lineWidth = y * 2;
ctx.strokeStyle = x;
ctx.stroke();
}
function getMousePos(canvas, evt) {
var rect = canvas.getBoundingClientRect();
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
};
}
function findxy(res, e) {
var pos = getMousePos(canvas, e);
if (res == 'down') {
prevX = currX;
prevY = currY;
currX = pos.x;
currY = pos.y;
flag = true;
dot_flag = true;
if (dot_flag) {
ctx.beginPath();
ctx.fillStyle = x;
ctx.fillRect(currX, currY, 2, 2);
ctx.closePath();
dot_flag = false;
}
}
if (res == 'up' || res == "out") {
flag = false;
}
if (res == 'move') {
if (flag) {
prevX = currX;
prevY = currY;
currX = pos.x;
currY = pos.y;
draw();
}
}
}
canvas { border: 1px solid }
<canvas id="can" width="200" height="200"></canvas>