【发布时间】:2014-04-03 12:07:42
【问题描述】:
这基本上是我的要求,而不是问题,我必须在画布上绘制多边形形状(与油漆相同),但我坚持我的要求,我必须在悬停在绘制的每个多边形上时显示工具提示.我知道对于像矩形这样的形状,我可以使用简单的 for 循环来管理坐标会更容易,但是如何为多边形处理它。甚至可能吗? 下面是我绘制多边形的代码
var startPointX = "", startPointY = "", endpointX, endpointY, isnewShape = false;
tools.polygon = function () {
var tool = this;
this.started = false;
this.mousedown = function (ev) {
tool.started = true;
tool.x0 = ev._x;
tool.y0 = ev._y;
if ((Math.abs(startPointX - ev._x) < 5) && (Math.abs(startPointY - ev._y) < 5) && (startPointX != ev._x && startPointY != ev._y) && !isnewShape) {
alert('point matched');
startPointX = "";
startPointY = "";
isnewShape = true;
context.clearRect(0, 0, canvas.width, canvas.height);
context.beginPath();
context.moveTo(endpointX, endpointY);
context.lineTo(ev._x, ev._y);
endpointX = ev._x;
endpointY = ev._y;
context.stroke();
context.closePath();
img_update();
tool.started = false;
}
else {
// console.log(isnewShape);
if (startPointX == "" || startPointY == "")
return;
context.clearRect(0, 0, canvas.width, canvas.height);
context.beginPath();
context.moveTo(endpointX, endpointY);
isnewShape = false;
context.lineTo(ev._x, ev._y);
endpointX = ev._x;
endpointY = ev._y;
context.stroke();
context.closePath();
img_update();
}
};
this.mousemove = function (ev) {
if (!tool.started) {
return;
}
// console.log('mousemove');
context.clearRect(0, 0, canvas.width, canvas.height);
context.beginPath();
context.lineTo(ev._x, ev._y);
endpointX = ev._x;
endpointY = ev._y;
context.stroke();
context.closePath();
};
this.mouseup = function (ev) {
if (tool.started) {
if (startPointX == "" && startPointY == "") {
startPointX = tool.x0;
startPointY = tool.y0;
}
tool.started = false;
img_update();
}
};
};
【问题讨论】:
-
在 svg 中处理元素会比在 canvas 中更容易