2
我需要在畫布上繪製多邊形,但我必須用鼠標點擊。例如:點擊一下鼠標點擊兩棵樹,然後點擊鼠標左鍵,最多點擊10次,然後點擊一行以填充所有點擊的點。純JS。如何用鼠標點擊畫布上的多邊形? Pure JS
function drawPolygon(position, sides, angle) {
var coordinates = [],
radius = Math.sqrt(Math.pow((dragStartLocation.x - position.x), 2) + Math.pow((dragStartLocation.y - position.y), 2)),
index = 0;
for (index = 0; index < sides; index++) {
coordinates.push({x: dragStartLocation.x + radius * Math.cos(angle), y: dragStartLocation.y - radius * Math.sin(angle)});
angle += (2 * Math.PI)/sides;
}
context.beginPath();
context.moveTo(coordinates[0].x, coordinates[0].y);
for (index = 1; index < sides; index++) {
context.lineTo(coordinates[index].x, coordinates[index].y);
}
context.closePath();
}
感謝支持我這工作得很好!另一個問題是否可以選擇我的多邊形並拖放(可拖動)? – 2015-04-04 18:26:49
@coreid,很高興能幫到你。拖放確實是一個單獨的問題:-) – markE 2015-04-04 19:10:51