I've been thinking about making Javascript Prototypes that represent HTML elements. For example a form prototype with build in ajax requests and form element prototypes. Or a list with list item prototypes.
I think that the biggest benefit of this approach is that it reduces repetitive code.
Here is an example of what I have in mind.
var LightParticle = function() {
this.setWidth(10);
this.setHeight(10);
this.setTop(0);
this.setLeft(0);
this.setPosition("absolute");
this.setBackground("white");
this.setClassName("LightParticle");
};
LightParticle.prototype = {
setWidth : function(width) {
this.width = width;
},
getWidth : function() {
return this.width;
},
setHeight : function(height) {
this.height = height;
},
getHeight : function() {
return this.height;
},
setTop : function(top) {
this.top = top;
},
getTop : function() {
return this.top;
},
setLeft : function(left) {
this.left = left;
},
getLeft : function() {
return this.left;
},
setBackground : function(background) {
this.background = background;
},
getBackground : function() {
return this.background;
},
setClassName : function(className) {
this.className = className;
},
getClassName : function() {
return this.className;
},
setElement : function(element) {
this.element = element;
},
getElement : function(element) {
return this.element;
},
setPosition : function(position) {
this.position = position;
},
getPosition : function(position) {
return this.position;
},
setSize : function(size) {
this.setWidth(size);
this.setHeight(size);
},
getStyle : function() {
return {
position: this.getPosition(),
width : this.getWidth(),
height : this.getHeight(),
top : this.getTop(),
left: this.getLeft(),
background: this.getBackground()
}
},
getView : function() {
var element = $("<div></div>");
element
.addClass(this.getClassName())
.css(this.getStyle());
this.setElement(element);
return element;
},
pulsate : function (speed) {
var height = this.getHeight();
var width = this.getWidth();
var top = this.getTop();
var left = this.getLeft();
if(this.getElement().height() == height) {
height = height * 4;
width = width * 4;
top = top - (height/2);
left = left - (width/2);
}
$(this.getElement()).animate({
"height":height,
"width": width,
"top": top,
"left":left
}, speed);
var that = this;
setTimeout(function(){
that.pulsate(speed);
}, speed);
}
}
function addRandomParticle() {
try {
var particle = new LightParticle();
var seed = Math.floor(Math.random() * 70) + 1;
particle.setBackground("#" + Math.floor((Math.abs(Math.sin(seed) * 16777215)) % 16777215).toString(16));
particle.setSize(Math.floor(Math.random() * 70) + 10);
particle.setTop(Math.floor(Math.random() * $(window).height()));
particle.setLeft(Math.floor(Math.random() * $(window).width()));
$('#canvas').append(particle.getView());
particle.pulsate(Math.floor(Math.random() * 2000) + 500);
} catch(error) {
console.log(error.message);
}
}
$(document).ready(function() {
try {
for(var i = 0; i < 100; i++) {
addRandomParticle();
}
} catch(error) {
console.log(error.message);
}
});
So far I'm not satisfied with the getters and setters since they have no datatype validation.
Does anyone have any idea how I can improve this? Or does someone know a completely better approach to reduce javascript code/events?
var particle = $("<div></div>").addClass("LightParticle").css({position: "absolute", width: "10px", height: "10px", background: "white"});
- from there, animate it however you like. E.g. using jquery'sanimate()
... \$\endgroup\$