CanvasDraw.prototype.drawPolygon = function (points, text, lineColor, fillColor) { var len = points.length; if (len < 3) { console.log('点不够呀!'); return; }
var cxt = this.cxt; // for (var i = 0; i < len; i++) { // // 点 // this.drawPoint(points[i].x, points[i].y); // } cxt.beginPath(); cxt.strokeStyle = lineColor || this.config.polygonLineColor; cxt.fillStyle = fillColor || this.config.polygonFillColor; for (var i = 0; i < len; i++) { // 画线 if (i === 0) { cxt.moveTo(points[i].x, points[i].y); } else { cxt.lineTo(points[i].x, points[i].y); cxt.stroke(); } } cxt.lineTo(points[0].x, points[0].y); cxt.stroke(); cxt.closePath(); cxt.fill();
// 文字 if (!text) return; var cx = 0, cy = 0; for (var i = 0; i < len; i++) { cx += points[i].x; cy += points[i].y; } cx /= len; cy /= len;
// 画点及线 var pLen = me.points.length; if (pLen > 0) { for (var i = 0; i < me.points.length - 1; i++) { // 点 me.drawPoint(me.points[i].x, me.points[i].y); // 线 me.drawLine(me.points[i].x, me.points[i].y, me.points[i+1].x, me.points[i+1].y); } // 最后那个点 me.drawPoint(me.points[pLen-1].x, me.points[pLen-1].y); } // 画多边形 for (var i = 0; i < me._polygons.length; i++) { var params = [me._polygons[i].points, me._polygons[i].text]; if (me._polygons[i].active) { params.push(me.config.polygonActiveLineColor); params.push(me.config.polygonActiveFillColor); } me.drawPolygon.apply(this, params); }
for (var i = 0; i < me.polygons.length; i++) { me.drawPolygon(me.polygons[i]); } }
CanvasDraw.prototype.isIn = function (x, y) { var me = this; if (0 < x && me.canvas.width > x && 0 < y && me.canvas.height > y) { returntrue; } returnfalse; }
CanvasDraw.prototype.initEventListener = function() { var me = this; me.addHandler(me.canvas, 'mousedown', function(e) { if (me.polygons.length === me.config.max) return;
// 鼠标左键 if (e.button === 0) { var x = e.offsetX; var y = e.offsetY;