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) } var save = document.getElementById("to-data-url"); save.onclick = function(){ var xmlhttp = new XMLHttpRequest(); xmlhttp.open("POST", "http://students.a-level.com.ua:10012"); var dataURL = canvas.toDataURL(); console.log(dataURL); var resImg = "" xmlhttp.onreadystatechange = function(){ if(xmlhttp.readyState == 4 && xmlhttp.status == 200){ console.log(xmlhttp.responseText); } } console.log(xmlhttp.resImg); xmlhttp.send(JSON.stringify({ func: 'addMessage', nick: "d", message: resImg})) }