decouple drawing the selection from destroying it

main
Isaiah Odhner 2014-04-07 18:25:13 -04:00
parent 4051cbe0e2
commit 2a0e53bb18
1 changed files with 12 additions and 7 deletions

19
app.js
View File

@ -54,6 +54,7 @@ app.open = function(){
implemented: "kinda", implemented: "kinda",
mousedown: function(){ mousedown: function(){
if(selection){ if(selection){
selection.draw();
selection.destroy(); selection.destroy();
} }
var mouse_has_moved = false; var mouse_has_moved = false;
@ -61,13 +62,16 @@ app.open = function(){
mouse_has_moved = true; mouse_has_moved = true;
}); });
$G.one("mouseup", function(){ $G.one("mouseup", function(){
if(!mouse_has_moved){ if(!mouse_has_moved && selection){
selection && selection.destroy(); selection.draw();
selection.destroy();
selection = null;
} }
}); });
var s = selection = new Selection(mouse.x, mouse.y, 1, 1); var s = selection = new Selection(mouse.x, mouse.y, 1, 1);
$canvas.one("mousedown", function(){ $canvas.one("mousedown", function(){
if(selection === s){ if(selection === s){
selection.draw();
selection.destroy(); selection.destroy();
selection = null; selection = null;
} }
@ -469,7 +473,7 @@ app.open = function(){
sel.position(); sel.position();
if(e.shiftKey){ if(e.shiftKey){
ctx.drawImage(sel.canvas, sel._x, sel._y); sel.draw();
} }
}; };
sel.$ghost.on("mousedown", function(e){ sel.$ghost.on("mousedown", function(e){
@ -491,11 +495,10 @@ app.open = function(){
height: this._h, height: this._h,
}); });
}; };
Selection.prototype.draw = function(){
try{ctx.drawImage(this.canvas, this._x, this._y);}catch(e){}
};
Selection.prototype.destroy = function(){ Selection.prototype.destroy = function(){
if(this.canvas){
try{ctx.drawImage(this.canvas, this._x, this._y);}catch(e){}
}
this.$ghost.remove(); this.$ghost.remove();
$canvas_handles.show(); $canvas_handles.show();
}; };
@ -1029,6 +1032,7 @@ app.open = function(){
cd.setData("URL", data); cd.setData("URL", data);
cd.setData("image/png", data); cd.setData("image/png", data);
if(e.type === "cut"){ if(e.type === "cut"){
selection.draw();
selection.destroy(); selection.destroy();
selection = null; selection = null;
} }
@ -1066,6 +1070,7 @@ app.open = function(){
} }
function paste_img(){ function paste_img(){
if(selection){ if(selection){
selection.draw();
selection.destroy(); selection.destroy();
} }
selection = new Selection(0, 0, img.width, img.height); selection = new Selection(0, 0, img.width, img.height);