var canvas = document.getElementById('canvas') var ctx = canvas.getContext('2d') var width = canvas.width; var height = canvas.height; function Drawable(){ Drawable.prototype.addInstance(this); } Drawable.prototype.draw = function(){}; Drawable.prototype.instances = []; Drawable.prototype.selected = []; Drawable.prototype.addInstance = function(item){ Drawable.prototype.instances.push(item); } Drawable.prototype.drawAll = function(){ ctx.clearRect(0,0,width,height); for(var i = 0; i>16)&255, (c>>8)&255, c&255].join(',')+',0.3)'; } throw new Error('Bad Hex'); } canvas.onclick = function(event){ if(!event.ctrlKey){ return; } Drawable.prototype.forAll(function(obj){ var x = event.clientX; var y = event.clientY; if(obj.name === "circle"){ if(obj.coords.x - obj.radius < x && x < obj.coords.x + obj.radius && obj.coords.y - obj.radius < y && y < obj.coords.y + obj.radius) { obj.color = "red"; Drawable.prototype.selected.push(obj); Drawable.prototype.drawAll(); } } if((obj.x1 < x && x < obj.x2 + obj.x1 && obj.y1 < y && y < obj.y2 + obj.y1) || (obj.x1 < x && x < obj.x2 && obj.y1 < y && y < obj.y2)){ obj.color = "red"; Drawable.prototype.selected.push(obj); Drawable.prototype.drawAll(); } }); } document.getElementById('color').onchange = function(){ Drawable.prototype.forSelected(function(obj){ obj.color = document.getElementById('color').value; Drawable.prototype.drawAll(); }) } document.getElementById("size").onchange = function(){ Drawable.prototype.forSelected(function(obj){ obj.lineWidth = document.getElementById('size').value; obj.radius = document.getElementById('size').value; Drawable.prototype.drawAll(); }) } document.getElementById("delete").onclick = function(event){ for (var i = 0; i < Drawable.prototype.instances.length; i++) { if(Drawable.prototype.instances[i].color === "red") delete Drawable.prototype.instances[i]; Drawable.prototype.drawAll() } console.log(Drawable.prototype.instances) console.log(Drawable.prototype.selected) }