jquery.line
jquery.line copied to clipboard
Modifying an existing line
I had a case where I drew lines, and I wanted to have them redrawn when the browser window was resized. Instead of clearing all lines, I gave each line a ID and the createLine script then checks if such a line exists. If the line exists, it is reused and modified. If it does not exist, it creates a new one.
I am sorry that I dont open a new pull request, but I just wanted to mention my change quickly because it helped me a lot. These are my changes:
createLine: function(lineId, x1, y1, x2, y2, options) {
// Check if browser is Internet Exploder ;)
var isIE = navigator.userAgent.indexOf("MSIE") > -1;
if (x2 < x1){
var temp = x1;
x1 = x2;
x2 = temp;
temp = y1;
y1 = y2;
y2 = temp;
}
// ---> start modified
// Modification to only create a new line if there is no existing line. If
// there is an existing line, it will be modified.
var existingLine = document.getElementById(lineId);
if (existingLine == null) {
var line = document.createElement("div");
} else {
var line = existingLine;
}
if (lineId != null) {
line.setAttribute('id', lineId);
}
// end modified <---
$.fn.line = function(lineId, x1, y1, x2, y2, options, callbacks) {
return $(this).each(function(){
if($.isFunction(options)){
callback = options;
options = null;
}else{
callback = callbacks;
}
options = $.extend({}, $.fn.line.defaults, options);
$(this).append(helpers.createLine(lineId,x1,y1,x2,y2,options)).promise().done(function(){
if($.isFunction(callback)){
callback.call();
}
});
});
};
Nice extension of this script, works very well