mirror of https://github.com/luileito/jsketch.git
Fixed even handlers
This commit is contained in:
parent
23e14d255c
commit
738e1a7bfb
|
|
@ -161,9 +161,9 @@
|
|||
elem.unbind("mouseup", mouseupHandler);
|
||||
elem.unbind("mousemove", mousemoveHandler);
|
||||
elem.unbind("mousedown", mousedownHandler);
|
||||
elem.unbind("touchstart", touchHandler);
|
||||
elem.unbind("touchmove", touchHandler);
|
||||
elem.unbind("touchend", touchHandler);
|
||||
elem.unbind("touchstart", touchdownHandler);
|
||||
elem.unbind("touchmove", touchmoveHandler);
|
||||
elem.unbind("touchend", touchupHandler);
|
||||
}
|
||||
elem.removeData(_ns);
|
||||
|
||||
|
|
|
|||
|
|
@ -3,14 +3,14 @@
|
|||
*/
|
||||
/**
|
||||
* @name $
|
||||
* @class
|
||||
* See <a href="http://jquery.com/">the jQuery library</a> for full details.
|
||||
* @class
|
||||
* See <a href="http://jquery.com/">the jQuery library</a> for full details.
|
||||
* This just documents the method that is added to jQuery by this plugin.
|
||||
*/
|
||||
/**
|
||||
* @name $.fn
|
||||
* @class
|
||||
* See <a href="http://jquery.com/">the jQuery library</a> for full details.
|
||||
* @class
|
||||
* See <a href="http://jquery.com/">the jQuery library</a> for full details.
|
||||
* This just documents the method that is added to jQuery by this plugin.
|
||||
*/
|
||||
;(function($) {
|
||||
|
|
@ -23,12 +23,12 @@
|
|||
* var mc = new MementoCanvas( $('canvas-selector') );
|
||||
*/
|
||||
var MementoCanvas = function($canvas) {
|
||||
|
||||
|
||||
// Private stuff //////////////////////////////////////////////////////////
|
||||
var stack = [];
|
||||
var stpos = -1;
|
||||
var self = this;
|
||||
|
||||
|
||||
function prev() {
|
||||
if (stpos > 0) {
|
||||
stpos--;
|
||||
|
|
@ -59,8 +59,8 @@
|
|||
data.sketch.graphics.drawImage(snapshot, 0,0);
|
||||
});
|
||||
};
|
||||
|
||||
// Key event manager.
|
||||
|
||||
// Key event manager.
|
||||
// Undo: "Ctrl + Z"
|
||||
// Redo: "Ctrl + Y" or "Ctrl + Shift + Z"
|
||||
// TODO: decouple shortcut definition, perhaps via jquery.hotkeys plugin.
|
||||
|
|
@ -79,10 +79,10 @@
|
|||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// Public stuff ///////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
|
||||
/**
|
||||
* Goes back to the last saved state, if available.
|
||||
* @name undo
|
||||
* @memberOf MementoCanvas
|
||||
|
|
@ -90,10 +90,11 @@
|
|||
this.undo = function() {
|
||||
prev();
|
||||
$canvas.sketchable('handler', function(elem, data) {
|
||||
data.strokes = stack[stpos].strokes.slice();
|
||||
if (stack[stpos])
|
||||
data.strokes = stack[stpos].strokes.slice();
|
||||
});
|
||||
};
|
||||
/**
|
||||
/**
|
||||
* Goes forward to the last saved state, if available.
|
||||
* @name redo
|
||||
* @memberOf MementoCanvas
|
||||
|
|
@ -101,10 +102,11 @@
|
|||
this.redo = function() {
|
||||
next();
|
||||
$canvas.sketchable('handler', function(elem, data) {
|
||||
data.strokes = stack[stpos].strokes.slice();
|
||||
if (stack[stpos])
|
||||
data.strokes = stack[stpos].strokes.slice();
|
||||
});
|
||||
};
|
||||
/**
|
||||
/**
|
||||
* Resets stack.
|
||||
* @name reset
|
||||
* @memberOf MementoCanvas
|
||||
|
|
@ -113,7 +115,7 @@
|
|||
stack = [];
|
||||
stpos = -1;
|
||||
};
|
||||
/**
|
||||
/**
|
||||
* Save state.
|
||||
* @name save
|
||||
* @memberOf MementoCanvas
|
||||
|
|
@ -125,7 +127,7 @@
|
|||
stack.push({ image: elem[0].toDataURL(), strokes: data.strokes.slice() });
|
||||
});
|
||||
};
|
||||
/**
|
||||
/**
|
||||
* Init instance.
|
||||
* @name init
|
||||
* @memberOf MementoCanvas
|
||||
|
|
@ -135,7 +137,7 @@
|
|||
$(document).off("keypress", keyManager);
|
||||
$(document).on("keypress", keyManager);
|
||||
};
|
||||
/**
|
||||
/**
|
||||
* Destroy instance.
|
||||
* @name destroy
|
||||
* @memberOf MementoCanvas
|
||||
|
|
@ -144,19 +146,19 @@
|
|||
$(document).off("keypress", keyManager);
|
||||
this.reset();
|
||||
};
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
// Bind plugin extension ////////////////////////////////////////////////////
|
||||
|
||||
|
||||
var plugin = $.fn.sketchable;
|
||||
var availMethods = plugin('methods');
|
||||
|
||||
|
||||
function configure(elem, opts) {
|
||||
var self = elem, options = $.extend(true, plugin.defaults, opts);
|
||||
// Actually this plugin is singleton, so exit early.
|
||||
if (!options.interactive) return opts;
|
||||
|
||||
|
||||
var mc = new MementoCanvas(elem);
|
||||
var callbacks = {
|
||||
init: function(elem, data) {
|
||||
|
|
@ -171,7 +173,7 @@
|
|||
data.memento.destroy();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// A helper function to override user-defined event listeners.
|
||||
function override(ev) {
|
||||
if (options && options.events && typeof options.events[ev] === 'function') {
|
||||
|
|
@ -196,7 +198,7 @@
|
|||
}
|
||||
plugin.isMementoReady = true;
|
||||
}
|
||||
|
||||
|
||||
// Expose public API for jquery.sketchable plugin.
|
||||
$.extend(availMethods, {
|
||||
undo: function() {
|
||||
|
|
@ -206,13 +208,13 @@
|
|||
mc.redo();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return plugin.defaults;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
/**
|
||||
* Creates a new memento-capable jQuery.sketchable object.
|
||||
* @param {String|Object} method name of the method to invoke,
|
||||
* @param {String|Object} method name of the method to invoke,
|
||||
* or a configuration object.
|
||||
* @return jQuery
|
||||
* @class
|
||||
|
|
@ -225,5 +227,5 @@
|
|||
var conf = configure(this, opts);
|
||||
return initfn.call(this, conf);
|
||||
};
|
||||
|
||||
|
||||
})(jQuery);
|
||||
|
|
|
|||
|
|
@ -1,4 +1 @@
|
|||
/*!
|
||||
* Memento plugin for jQuery sketchable | v1.1 | Luis A. Leiva | MIT license
|
||||
*/
|
||||
(function(e){var a=function(m){var h=[];var g=-1;var i=this;function l(){if(g>0){g--;var o=new Image();o.src=h[g].image;o.onload=function(){j(this)}}}function k(){if(g<h.length-1){g++;var o=new Image();o.src=h[g].image;o.onload=function(){j(this)}}}function j(o){m.sketchable("handler",function(p,q){q.sketch.clear();q.sketch.graphics.drawImage(o,0,0)})}function n(o){if(o.ctrlKey){switch(o.which){case 26:if(o.shiftKey){i.redo()}else{i.undo()}break;case 25:i.redo();break;default:break}}}this.undo=function(){l();m.sketchable("handler",function(o,p){p.strokes=h[g].strokes.slice()})};this.redo=function(){k();m.sketchable("handler",function(o,p){p.strokes=h[g].strokes.slice()})};this.reset=function(){h=[];g=-1};this.save=function(){g++;if(g<h.length){h.length=g}m.sketchable("handler",function(o,p){h.push({image:o[0].toDataURL(),strokes:p.strokes.slice()})})};this.init=function(){e(document).off("keypress",n);e(document).on("keypress",n)};this.destroy=function(){e(document).off("keypress",n);this.reset()}};var d=e.fn.sketchable;var f=d("methods");function c(j,g){var o=j,p=e.extend(true,d.defaults,g);if(!p.interactive){return g}var m=new a(j);var l={init:function(i,q){q.memento=m;q.memento.save();q.memento.init()},mouseup:function(i,q,r){q.memento.save()},destroy:function(i,q){q.memento.destroy()}};function h(q){if(p&&p.events&&typeof p.events[q]==="function"){var i=p.events[q];p.events[q]=function(){var r=Array.prototype.slice.call(arguments,0);i.apply(o,r);l[q].apply(o,r)}}else{d.defaults.events[q]=l[q]}}if(!d.isMementoReady){var n="init mouseup destroy".split(" ");for(var k=0;k<n.length;k++){h(n[k])}d.isMementoReady=true}e.extend(f,{undo:function(){m.undo()},redo:function(){m.redo()}});return d.defaults}var b=f.init;f.init=function(h){var g=c(this,h);return b.call(this,g)}})(jQuery);
|
||||
(function($){var MementoCanvas=function($canvas){var stack=[];var stpos=-1;var self=this;function prev(){if(stpos>0){stpos--;var snapshot=new Image;snapshot.src=stack[stpos].image;snapshot.onload=function(){restore(this)}}}function next(){if(stpos<stack.length-1){stpos++;var snapshot=new Image;snapshot.src=stack[stpos].image;snapshot.onload=function(){restore(this)}}}function restore(snapshot){$canvas.sketchable("handler",function(elem,data){data.sketch.clear();data.sketch.graphics.drawImage(snapshot,0,0)})}function keyManager(e){if(e.ctrlKey){switch(e.which){case 26:if(e.shiftKey)self.redo();else self.undo();break;case 25:self.redo();break;default:break}}}this.undo=function(){prev();$canvas.sketchable("handler",function(elem,data){if(stack[stpos])data.strokes=stack[stpos].strokes.slice()})};this.redo=function(){next();$canvas.sketchable("handler",function(elem,data){if(stack[stpos])data.strokes=stack[stpos].strokes.slice()})};this.reset=function(){stack=[];stpos=-1};this.save=function(){stpos++;if(stpos<stack.length)stack.length=stpos;$canvas.sketchable("handler",function(elem,data){stack.push({image:elem[0].toDataURL(),strokes:data.strokes.slice()})})};this.init=function(){$(document).off("keypress",keyManager);$(document).on("keypress",keyManager)};this.destroy=function(){$(document).off("keypress",keyManager);this.reset()}};var plugin=$.fn.sketchable;var availMethods=plugin("methods");function configure(elem,opts){var self=elem,options=$.extend(true,plugin.defaults,opts);if(!options.interactive)return opts;var mc=new MementoCanvas(elem);var callbacks={init:function(elem,data){data.memento=mc;data.memento.save();data.memento.init()},mouseup:function(elem,data,e){data.memento.save()},destroy:function(elem,data){data.memento.destroy()}};function override(ev){if(options&&options.events&&typeof options.events[ev]==="function"){var fn=options.events[ev];options.events[ev]=function(){var args=Array.prototype.slice.call(arguments,0);fn.apply(self,args);callbacks[ev].apply(self,args)}}else{plugin.defaults.events[ev]=callbacks[ev]}}if(!plugin.isMementoReady){var events="init mouseup destroy".split(" ");for(var i=0;i<events.length;i++){override(events[i])}plugin.isMementoReady=true}$.extend(availMethods,{undo:function(){mc.undo()},redo:function(){mc.redo()}});return plugin.defaults}var initfn=availMethods.init;availMethods.init=function(opts){var conf=configure(this,opts);return initfn.call(this,conf)}})(jQuery);
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue