function LinesAndCircles() { this.circle = []; this.circleCount = 30; this.wHeight = $(window).innerHeight(); this.wWidth = $(window).innerWidth(); this.color = '#000'; this.lineDistance = 75; this.text = "Lorem Ipsum"; this.canvas = document.getElementById('canvas'); this.ctx = canvas.getContext('2d'); this._setCanvasSize(); this.init(); } LinesAndCircles.prototype._setCanvasSize = function () { var self = this; function setSize() { self.canvas.setAttribute('height', self.wHeight); self.canvas.setAttribute('width', self.wWidth); }; setSize(); } LinesAndCircles.prototype.init = function () { var self = this; for (var i = 0; i < self.circleCount; i++) { self.drawCircle( _.random(0, self.wWidth), // x _.random(0, self.wHeight), // y _.random(5, 20), // radius 0, // start Math.PI * 2, // end true, // clockwise self.color, // fill color i // iteration ); } self.drawLine(); self.addText(); }; LinesAndCircles.prototype.drawCircle = function(x, y, radius, start, end, clockwise, fillColor, i) { var self = this; var ctx = this.ctx; this.circle[i] = {}; this.circle[i].x = x; this.circle[i].y = y; ctx.beginPath(); ctx.arc(x, y, radius, start, end, clockwise) ctx.fillStyle = fillColor; ctx.fill(); }; LinesAndCircles.prototype.drawLine = function() { var ctx = this.ctx; for (var i = 0; i < this.circle.length; i++) { for (var j = 0; j < this.circle.length; j++) { if ( (this.circle[i].x - this.circle[j].x) < this.lineDistance && (this.circle[i].y - this.circle[j].y) < this.lineDistance && (this.circle[i].x - this.circle[j].x) > - this.lineDistance && (this.circle[i].y - this.circle[j].y) > - this.lineDistance ) { window.console.log('x', this.circle[i].x , this.circle[j].x, 'y', this.circle[i].y, this.circle[j].y); ctx.beginPath(); ctx.moveTo(this.circle[i].x, this.circle[i].y); ctx.lineTo(this.circle[j].x, this.circle[j].y); ctx.strokeStyle = this.color; ctx.stroke(); ctx.closePath(); } } } }; LinesAndCircles.prototype.addText = function() { var ctx = this.ctx; ctx.globalCompositeOperation = 'xor'; ctx.font = '100pt Open Sans'; ctx.fillStyle = '#000'; ctx.fillText(this.text, 30, this.wHeight - 30); } var makeStuff = new LinesAndCircles();