The following set of codes adds a nw function to Raphael Set and Raphael Elements. The usage is to simply call .cloneToPaper(targetPaper)
on any set or element.
(function (R) {
var cloneSet; // to cache set cloning function for optimisation
/**
* Clones Raphael element from one paper to another
*
* @param {Paper} targetPaper is the paper to which this element
* has to be cloned
*
* @return RaphaelElement
*/
R.el.cloneToPaper = function (targetPaper) {
return (!this.removed &&
targetPaper[this.type]().attr(this.attr()));
};
/**
* Clones Raphael Set from one paper to another
*
* @param {Paper} targetPaper is the paper to which this element
* has to be cloned
*
* @return RaphaelSet
*/
R.st.cloneToPaper = function (targetPaper) {
targetPaper.setStart();
this.forEach(cloneSet || (cloneSet = function (el) {
el.cloneToPaper(targetPaper);
}));
return targetPaper.setFinish();
};
}(Raphael));
For a sample implementation, you may check out this fiddle: http://jsfiddle.net/shamasis/39yTS/
Note that if you have events on the source elements, they will not be cloned to the target paper.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…