【发布时间】:2019-08-01 02:18:57
【问题描述】:
我有一个画布,在其中绘制了一个元素 svg(例如一个圆圈),用户负责用鼠标通过这个图进行绘制,我将用户绘制的点 x 和 y 保存在一个数组中,但是我不知道如何仅从 svg stroke 获取点。
我的问题是: 使用 isPointInStroke() 我可以查看该点是否在笔画中,但如果我没有笔画的总点数数组,则无法知道用户是否已绘制 100% 的 SVG 图。在之前的方式中,如果用户画了一半但正确,它会给我100%的成功。
function init() {
canvas = document.getElementById('can');
ctx = canvas.getContext("2d");
w = canvas.width;
h = canvas.height;
var svgPathCirculo=" M125,200a75,75 0 1,0 150,0a75,75 0 1,0 -150,0";
var circulo = new Path2D(svgPathCirculo);
ctx.lineWidth = 5;
ctx.setLineDash([5, 15]);
ctx.stroke(circulo);
// Just example to check if it works
if(ctx.isPointInStroke(circulo, 125, 200)){
ctx.arc(200,200,3,0,2*Math.PI);
ctx.fill();
};
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);
}
我使用画布在其上绘图,并使用 svg 显示预定义的形状,供用户在绘图时作为模板遵循(例如为幼儿绘制小册子)。
function draw() {
ctx.beginPath();
ctx.moveTo(prevX, prevY);
ctx.lineTo(currX, currY);
ctx.strokeStyle = x;
ctx.lineWidth = y;
ctx.stroke();
ctx.closePath();
}
function findxy(res, e) {
if (res == 'down') {
prevX = currX;
prevY = currY;
currX = e.clientX - canvas.offsetLeft;
currY = e.clientY - canvas.offsetTop;
if(!arrayCoordenadas.includes({x:currX,y:currY})){
arrayCoordenadas.push({x:currX,y:currY});
}
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 = e.clientX - canvas.offsetLeft;
currY = e.clientY - canvas.offsetTop;
if(!arrayCoordenadas.includes({x:currX,y:currY})){
arrayCoordenadas.push({x:currX,y:currY});
}
draw();
}
}
}
我需要知道 svg 笔画路径的 x 和 y 坐标。
【问题讨论】:
-
我很困惑:你标记你的问题 SVG 然后你谈论画布和isPointInPath() - 画布上下文的方法。你能添加一些代码。一个可行的例子会很棒。
-
现在问题已经形成,非常感谢您
标签: javascript html svg coordinates mouse