1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| function SVGBubble(options) { this.init(options); }
SVGBubble.prototype = { constructor: SVGBubble, opt: { rr: 10, gap: 10, h1: 40, tr: 10, h: 133, w: 200, D: "M ${rr+gap} 0 Q ${gap} 0 ${gap} ${rr} L ${gap} ${h1} 0 ${h1+tr/2} ${gap} ${h1+tr} ${gap} ${h-rr} Q ${gap} ${h} ${gap+rr} ${h} L ${w-rr} ${h} Q ${w} ${h} ${w} ${h-rr} L ${w} ${rr} Q ${w} 0 ${w-rr} 0 L ${gap+rr} 0", DD: "M ${rr} 0 ${w-rr-gap} 0 Q ${w-gap} 0 ${w-gap} ${rr} L ${w-gap} ${h1} ${w} ${h1+tr/2} ${w-gap} ${h1+tr} ${w-gap} ${h-rr} Q ${w-gap} ${h} ${w-rr-gap} ${h} L ${rr} ${h} Q 0 ${h} 0 ${h-rr} L 0 ${rr} Q 0 0 ${rr} 0" }, extend, extend, init: function(options) { this.options = this.extend({}, this.opt, options); this.options.id = options.id || this.id(); this.options.w = Math.round(this.options.w); this.options.h = Math.round(this.options.h); this.options.d = this.generateD(); this.node = this.options.node; this.initNode(); }, html: (function() { var reg = /\$\{([^\}]+)\}/g; var html = '<svg xmlns="http://www.w3.org/2000/svg">' + "<defs>" + '<clipPath id="${id}clip">' + '<path d="${d}" class="${id}path" style="stroke: black; fill: none;" />' + "</clipPath>" + "</defs>" + '<image xmlns:xlink="http://www.w3.org/1999/xlink" class="${id}-path-img" x="0" y="0" width="${w}" height="${h}" style="clip-path: url(#${id}clip);" >' + "</image>" + "</svg>";
return function(options) { return html.replace(reg, function(a, b, c, d) { return options[b]; }); }; })(),
getDTarget: function() { return this.options.type == "left" ? this.options.D : this.options.DD; },
generateD: function() { var that = this; var res = this.getDTarget().replace(/\$\{([^\}]+)\}/g, function( a, b, c, d ) { var myFunction = new Function( "w", "rr", "gap", "h1", "h", "tr", "return " + b ); var res = myFunction( that.options.w, that.options.rr, that.options.gap, that.options.h1, that.options.h, that.options.tr ); return res; }); return res; },
id: function() { return +new Date(); }, initNode: function() { var svgNodes = new DOMParser().parseFromString( this.html(this.options), "image/svg+xml" ); var image = svgNodes.documentElement.childNodes[1]; image.setAttributeNS( "http://www.w3.org/1999/xlink", "href", this.options.src ); this.node.appendChild( this.node.ownerDocument.importNode(svgNodes.documentElement, true) ); } };
|