/** * tagcanvas.js和tagcloud.js是 标签云所需要的 */ (function(){ "use strict"; var i, j, abs = math.abs, sin = math.sin, cos = math.cos, max = math.max, min = math.min, ceil = math.ceil, sqrt = math.sqrt, pow = math.pow, hexlookup3 = {}, hexlookup2 = {}, hexlookup1 = { 0:"0,", 1:"17,", 2:"34,", 3:"51,", 4:"68,", 5:"85,", 6:"102,", 7:"119,", 8:"136,", 9:"153,", a:"170,", a:"170,", b:"187,", b:"187,", c:"204,", c:"204,", d:"221,", d:"221,", e:"238,", e:"238,", f:"255,", f:"255," }, oproto, tproto, tcproto, mproto, vproto, tsproto, tcvproto, doc = document, ocanvas, handlers = {}; for(i = 0; i < 256; ++i) { j = i.tostring(16); if(i < 16) j = '0' + j; hexlookup2[j] = hexlookup2[j.touppercase()] = i.tostring() + ','; } function defined(d) { return typeof d != 'undefined'; } function isobject(o) { return typeof o == 'object' && o != null; } function clamp(v, mn, mx) { return isnan(v) ? mx : min(mx, max(mn, v)); } function nop() { return false; } function timenow() { return new date().valueof(); } function sortlist(l, f) { var nl = [], tl = l.length, i; for(i = 0; i < tl; ++i) nl.push(l[i]); nl.sort(f); return nl; } function shuffle(a) { var i = a.length-1, t, p; while(i) { p = ~~(math.random()*i); t = a[i]; a[i] = a[p]; a[p] = t; --i; } } function vector(x, y, z) { this.x = x; this.y = y; this.z = z; } vproto = vector.prototype; vproto.length = function() { return sqrt(this.x * this.x + this.y * this.y + this.z * this.z); }; vproto.dot = function(v) { return this.x * v.x + this.y * v.y + this.z * v.z; }; vproto.cross = function(v) { var x = this.y * v.z - this.z * v.y, y = this.z * v.x - this.x * v.z, z = this.x * v.y - this.y * v.x; return new vector(x, y, z); }; vproto.angle = function(v) { var dot = this.dot(v), ac; if(dot == 0) return math.pi / 2.0; ac = dot / (this.length() * v.length()); if(ac >= 1) return 0; if(ac <= -1) return math.pi; return math.acos(ac); }; vproto.unit = function() { var l = this.length(); return new vector(this.x / l, this.y / l, this.z / l); }; function makevector(lg, lt) { lt = lt * math.pi / 180; lg = lg * math.pi / 180; var x = sin(lg) * cos(lt), y = -sin(lt), z = -cos(lg) * cos(lt); return new vector(x, y, z); } function matrix(a) { this[1] = {1: a[0], 2: a[1], 3: a[2]}; this[2] = {1: a[3], 2: a[4], 3: a[5]}; this[3] = {1: a[6], 2: a[7], 3: a[8]}; } mproto = matrix.prototype; matrix.identity = function() { return new matrix([1,0,0, 0,1,0, 0,0,1]); }; matrix.rotation = function(angle, u) { var sina = sin(angle), cosa = cos(angle), mcos = 1 - cosa; return new matrix([ cosa + pow(u.x, 2) * mcos, u.x * u.y * mcos - u.z * sina, u.x * u.z * mcos + u.y * sina, u.y * u.x * mcos + u.z * sina, cosa + pow(u.y, 2) * mcos, u.y * u.z * mcos - u.x * sina, u.z * u.x * mcos - u.y * sina, u.z * u.y * mcos + u.x * sina, cosa + pow(u.z, 2) * mcos ]); } mproto.mul = function(m) { var a = [], i, j, mmatrix = (m.xform ? 1 : 0); for(i = 1; i <= 3; ++i) for(j = 1; j <= 3; ++j) { if(mmatrix) a.push(this[i][1] * m[1][j] + this[i][2] * m[2][j] + this[i][3] * m[3][j]); else a.push(this[i][j] * m); } return new matrix(a); }; mproto.xform = function(p) { var a = {}, x = p.x, y = p.y, z = p.z; a.x = x * this[1][1] + y * this[2][1] + z * this[3][1]; a.y = x * this[1][2] + y * this[2][2] + z * this[3][2]; a.z = x * this[1][3] + y * this[2][3] + z * this[3][3]; return a; }; function pointsonsphere(n,xr,yr,zr,magic) { var i, y, r, phi, pts = [], off = 2/n, inc; inc = math.pi * (3 - sqrt(5) + (parsefloat(magic) ? parsefloat(magic) : 0)); for(i = 0; i < n; ++i) { y = i * off - 1 + (off / 2); r = sqrt(1 - y*y); phi = i * inc; pts.push([cos(phi) * r * xr, y * yr, sin(phi) * r * zr]); } return pts; } function cylinder(n,o,xr,yr,zr,magic) { var phi, pts = [], off = 2/n, inc, i, j, k, l; inc = math.pi * (3 - sqrt(5) + (parsefloat(magic) ? parsefloat(magic) : 0)); for(i = 0; i < n; ++i) { j = i * off - 1 + (off / 2); phi = i * inc; k = cos(phi); l = sin(phi); pts.push(o ? [j * xr, k * yr, l * zr] : [k * xr, j * yr, l * zr]); } return pts; } function ring(o, n, xr, yr, zr, j) { var phi, pts = [], inc = math.pi * 2 / n, i, k, l; for(i = 0; i < n; ++i) { phi = i * inc; k = cos(phi); l = sin(phi); pts.push(o ? [j * xr, k * yr, l * zr] : [k * xr, j * yr, l * zr]); } return pts; } function pointsoncylinderv(n,xr,yr,zr,m) { return cylinder(n, 0, xr, yr, zr, m) } function pointsoncylinderh(n,xr,yr,zr,m) { return cylinder(n, 1, xr, yr, zr, m) } function pointsonringv(n, xr, yr, zr, offset) { offset = isnan(offset) ? 0 : offset * 1; return ring(0, n, xr, yr, zr, offset); } function pointsonringh(n, xr, yr, zr, offset) { offset = isnan(offset) ? 0 : offset * 1; return ring(1, n, xr, yr, zr, offset); } function centreimage(t) { var i = new image; i.onload = function() { var dx = i.width / 2, dy = i.height / 2; t.centrefunc = function(c, w, h, cx, cy) { c.settransform(1, 0, 0, 1, 0, 0); c.globalalpha = 1; c.drawimage(i, cx - dx, cy - dy); }; }; i.src = t.centreimage; } function setalpha(c,a) { var d = c, p1, p2, ae = (a*1).toprecision(3) + ')'; if(c[0] === '#') { if(!hexlookup3[c]) if(c.length === 4) hexlookup3[c] = 'rgba(' + hexlookup1[c[1]] + hexlookup1[c[2]] + hexlookup1[c[3]]; else hexlookup3[c] = 'rgba(' + hexlookup2[c.substr(1,2)] + hexlookup2[c.substr(3,2)] + hexlookup2[c.substr(5,2)]; d = hexlookup3[c] + ae; } else if(c.substr(0,4) === 'rgb(' || c.substr(0,4) === 'hsl(') { d = (c.replace('(','a(').replace(')', ',' + ae)); } else if(c.substr(0,5) === 'rgba(' || c.substr(0,5) === 'hsla(') { p1 = c.lastindexof(',') + 1, p2 = c.indexof(')'); a *= parsefloat(c.substring(p1,p2)); d = c.substr(0,p1) + a.toprecision(3) + ')'; } return d; } function newcanvas(w,h) { // if using excanvas, give up now if(window.g_vmlcanvasmanager) return null; var c = doc.createelement('canvas'); c.width = w; c.height = h; return c; } // i think all browsers pass this test now... function shadowalphabroken() { var cv = newcanvas(3,3), c, i; if(!cv) return false; c = cv.getcontext('2d'); c.strokestyle = '#000'; c.shadowcolor = '#fff'; c.shadowblur = 3; c.globalalpha = 0; c.strokerect(2,2,2,2); c.globalalpha = 1; i = c.getimagedata(2,2,1,1); cv = null; return (i.data[0] > 0); } function setgradient(c, l, o, g) { var gd = c.createlineargradient(0, 0, l, 0), i; for(i in g) gd.addcolorstop(1 - i, g[i]); c.fillstyle = gd; c.fillrect(0, o, l, 1); } function findgradientcolour(tc, p, r) { var l = 1024, h = 1, gl = tc.weightgradient, cv, c, i, d; if(tc.gcanvas) { c = tc.gcanvas.getcontext('2d'); h = tc.gcanvas.height; } else { if(isobject(gl[0])) h = gl.length; else gl = [gl]; tc.gcanvas = cv = newcanvas(l, h); if(!cv) return null; c = cv.getcontext('2d'); for(i = 0; i < h; ++i) setgradient(c, l, i, gl[i]); } r = max(min(r || 0, h - 1), 0); d = c.getimagedata(~~((l - 1) * p), r, 1, 1).data; return 'rgba(' + d[0] + ',' + d[1] + ',' + d[2] + ',' + (d[3]/255) + ')'; } function textset(ctxt, font, colour, strings, padx, pady, shadowcolour, shadowblur, shadowoffsets, maxwidth, widths, align) { var xo = padx + (shadowblur || 0) + (shadowoffsets.length && shadowoffsets[0] < 0 ? abs(shadowoffsets[0]) : 0), yo = pady + (shadowblur || 0) + (shadowoffsets.length && shadowoffsets[1] < 0 ? abs(shadowoffsets[1]) : 0), i, xc; ctxt.font = font; ctxt.textbaseline = 'top'; ctxt.fillstyle = colour; shadowcolour && (ctxt.shadowcolor = shadowcolour); shadowblur && (ctxt.shadowblur = shadowblur); shadowoffsets.length && (ctxt.shadowoffsetx = shadowoffsets[0], ctxt.shadowoffsety = shadowoffsets[1]); for(i = 0; i < strings.length; ++i) { xc = 0; if(widths) { if('right' == align) { xc = maxwidth - widths[i]; } else if('centre' == align) { xc = (maxwidth - widths[i]) / 2; } } ctxt.filltext(strings[i], xo + xc, yo); yo += parseint(font); } } function rrect(c, x, y, w, h, r, s) { if(r) { c.beginpath(); c.moveto(x, y + h - r); c.arcto(x, y, x + r, y, r); c.arcto(x + w, y, x + w, y + r, r); c.arcto(x + w, y + h, x + w - r, y + h, r); c.arcto(x, y + h, x, y + h - r, r); c.closepath(); c[s ? 'stroke' : 'fill'](); } else { c[s ? 'strokerect' : 'fillrect'](x, y, w, h); } } function textcanvas(strings, font, w, h, maxwidth, stringwidths, align, valign, scale) { this.strings = strings; this.font = font; this.width = w; this.height = h; this.maxwidth = maxwidth; this.stringwidths = stringwidths; this.align = align; this.valign = valign; this.scale = scale; } tcvproto = textcanvas.prototype; tcvproto.setimage = function(image, w, h, position, padding, align, valign, scale) { this.image = image; this.iwidth = w * this.scale; this.iheight = h * this.scale; this.ipos = position; this.ipad = padding * this.scale; this.iscale = scale; this.ialign = align; this.ivalign = valign; }; tcvproto.align = function(size, space, a) { var pos = 0; if(a == 'right' || a == 'bottom') pos = space - size; else if(a != 'left' && a != 'top') pos = (space - size) / 2; return pos; }; tcvproto.create = function(colour, bgcolour, bgoutline, bgoutlinethickness, shadowcolour, shadowblur, shadowoffsets, padding, radius) { var cv, cw, ch, c, x1, x2, y1, y2, offx, offy, ix, iy, iw, ih, rr, sox = abs(shadowoffsets[0]), soy = abs(shadowoffsets[1]), shadowcv, shadowc; padding = max(padding, sox + shadowblur, soy + shadowblur); x1 = 2 * (padding + bgoutlinethickness); y1 = 2 * (padding + bgoutlinethickness); cw = this.width + x1; ch = this.height + y1; offx = offy = padding + bgoutlinethickness; if(this.image) { ix = iy = padding + bgoutlinethickness; iw = this.iwidth; ih = this.iheight; if(this.ipos == 'top' || this.ipos == 'bottom') { if(iw < this.width) ix += this.align(iw, this.width, this.ialign); else offx += this.align(this.width, iw, this.align); if(this.ipos == 'top') offy += ih + this.ipad; else iy += this.height + this.ipad; cw = max(cw, iw + x1); ch += ih + this.ipad; } else { if(ih < this.height) iy += this.align(ih, this.height, this.ivalign); else offy += this.align(this.height, ih, this.valign); if(this.ipos == 'right') ix += this.width + this.ipad; else offx += iw + this.ipad; cw += iw + this.ipad; ch = max(ch, ih + y1); } } cv = newcanvas(cw, ch); if(!cv) return null; x1 = y1 = bgoutlinethickness / 2; x2 = cw - bgoutlinethickness; y2 = ch - bgoutlinethickness; rr = min(radius, x2 / 2, y2 / 2); c = cv.getcontext('2d'); if(bgcolour) { c.fillstyle = bgcolour; rrect(c, x1, y1, x2, y2, rr); } if(bgoutlinethickness) { c.strokestyle = bgoutline; c.linewidth = bgoutlinethickness; rrect(c, x1, y1, x2, y2, rr, true); } if(shadowblur || sox || soy) { // use a transparent canvas to draw on shadowcv = newcanvas(cw, ch); if(shadowcv) { shadowc = c; c = shadowcv.getcontext('2d'); } } // don't use textset shadow support because it adds space for shadow textset(c, this.font, colour, this.strings, offx, offy, 0, 0, [], this.maxwidth, this.stringwidths, this.align); if(this.image) c.drawimage(this.image, ix, iy, iw, ih); if(shadowc) { // draw the text and image with the added shadow c = shadowc; shadowcolour && (c.shadowcolor = shadowcolour); shadowblur && (c.shadowblur = shadowblur); c.shadowoffsetx = shadowoffsets[0]; c.shadowoffsety = shadowoffsets[1]; c.drawimage(shadowcv, 0, 0); } return cv; }; function expandimage(i, w, h) { var cv = newcanvas(w, h), c; if(!cv) return null; c = cv.getcontext('2d'); c.drawimage(i, (w - i.width) / 2, (h - i.height) / 2); return cv; } function scaleimage(i, w, h) { var cv = newcanvas(w, h), c; if(!cv) return null; c = cv.getcontext('2d'); c.drawimage(i, 0, 0, w, h); return cv; } function addbackgroundtoimage(i, w, h, scale, colour, othickness, ocolour, padding, radius, ofill) { var cw = w + ((2 * padding) + othickness) * scale, ch = h + ((2 * padding) + othickness) * scale, cv = newcanvas(cw, ch), c, x1, y1, x2, y2, ocanvas, cc, rr; if(!cv) return null; othickness *= scale; radius *= scale; x1 = y1 = othickness / 2; x2 = cw - othickness; y2 = ch - othickness; padding = (padding * scale) + x1; // add space for outline c = cv.getcontext('2d'); rr = min(radius, x2 / 2, y2 / 2); if(colour) { c.fillstyle = colour; rrect(c, x1, y1, x2, y2, rr); } if(othickness) { c.strokestyle = ocolour; c.linewidth = othickness; rrect(c, x1, y1, x2, y2, rr, true); } if(ofill) { // use compositing to colour in the image and border ocanvas = newcanvas(cw, ch); cc = ocanvas.getcontext('2d'); cc.drawimage(i, padding, padding, w, h); cc.globalcompositeoperation = 'source-in'; cc.fillstyle = ocolour; cc.fillrect(0, 0, cw, ch); cc.globalcompositeoperation = 'destination-over'; cc.drawimage(cv, 0, 0); cc.globalcompositeoperation = 'source-over'; c.drawimage(ocanvas, 0, 0); } else { c.drawimage(i, padding, padding, i.width, i.height); } return {image: cv, width: cw / scale, height: ch / scale}; } /** * rounds off the corners of an image */ function roundimage(i, r, iw, ih, s) { var cv, c, r1 = parsefloat(r), l = max(iw, ih); cv = newcanvas(iw, ih); if(!cv) return null; if(r.indexof('%') > 0) r1 = l * r1 / 100; else r1 = r1 * s; c = cv.getcontext('2d'); c.globalcompositeoperation = 'source-over'; c.fillstyle = '#fff'; if(r1 >= l/2) { r1 = min(iw,ih) / 2; c.beginpath(); c.moveto(iw/2,ih/2); c.arc(iw/2,ih/2,r1,0,2*math.pi,false); c.fill(); c.closepath(); } else { r1 = min(iw/2,ih/2,r1); rrect(c, 0, 0, iw, ih, r1, true); c.fill(); } c.globalcompositeoperation = 'source-in'; c.drawimage(i, 0, 0, iw, ih); return cv; } /** * creates a new canvas containing the image and its shadow * returns an object containing the image and its dimensions at z=0 */ function addshadowtoimage(i, w, h, scale, sc, sb, so) { var sw = abs(so[0]), sh = abs(so[1]), cw = w + (sw > sb ? sw + sb : sb * 2) * scale, ch = h + (sh > sb ? sh + sb : sb * 2) * scale, xo = scale * ((sb || 0) + (so[0] < 0 ? sw : 0)), yo = scale * ((sb || 0) + (so[1] < 0 ? sh : 0)), cv, c; cv = newcanvas(cw, ch); if(!cv) return null; c = cv.getcontext('2d'); sc && (c.shadowcolor = sc); sb && (c.shadowblur = sb * scale); so && (c.shadowoffsetx = so[0] * scale, c.shadowoffsety = so[1] * scale); c.drawimage(i, xo, yo, w, h); return {image: cv, width: cw / scale, height: ch / scale}; } function findtextboundingbox(s,f,ht) { var w = parseint(s.tostring().length * ht), h = parseint(ht * 2 * s.length), cv = newcanvas(w,h), c, idata, w1, h1, x, y, i, ex; if(!cv) return null; c = cv.getcontext('2d'); c.fillstyle = '#000'; c.fillrect(0,0,w,h); textset(c,ht + 'px ' + f,'#fff',s,0,0,0,0,[],'centre') idata = c.getimagedata(0,0,w,h); w1 = idata.width; h1 = idata.height; ex = { min: { x: w1, y: h1 }, max: { x: -1, y: -1 } }; for(y = 0; y < h1; ++y) { for(x = 0; x < w1; ++x) { i = (y * w1 + x) * 4; if(idata.data[i+1] > 0) { if(x < ex.min.x) ex.min.x = x; if(x > ex.max.x) ex.max.x = x; if(y < ex.min.y) ex.min.y = y; if(y > ex.max.y) ex.max.y = y; } } } // device pixels might not be css pixels if(w1 != w) { ex.min.x *= (w / w1); ex.max.x *= (w / w1); } if(h1 != h) { ex.min.y *= (w / h1); ex.max.y *= (w / h1); } cv = null; return ex; } function fixfont(f) { return "'" + f.replace(/(\'|\")/g,'').replace(/\s*,\s*/g, "', '") + "'"; } function addhandler(h,f,e) { e = e || doc; if(e.addeventlistener) e.addeventlistener(h,f,false); else e.attachevent('on' + h, f); } function removehandler(h,f,e) { e = e || doc; if(e.removeeventlistener) e.removeeventlistener(h, f); else e.detachevent('on' + h, f); } function addimage(i, o, t, tc) { var s = tc.imagescale, mscale, ic, bc, oc, iw, ih; // image not loaded, wait for image onload if(!o.complete) return addhandler('load',function() { addimage(i,o,t,tc); }, o); if(!i.complete) return addhandler('load',function() { addimage(i,o,t,tc); }, i); // yes, this does look like nonsense, but it makes sure that both the // width and height are actually set and not just calculated. this is // required to keep proportional sizes when the images are hidden, so // the images can be used again for another cloud. o.width = o.width; o.height = o.height; if(s) { i.width = o.width * s; i.height = o.height * s; } // the standard width of the image, with imagescale applied t.iw = i.width; t.ih = i.height; if(tc.txtopt) { ic = i; mscale = tc.zoommax * tc.txtscale; iw = t.iw * mscale; ih = t.ih * mscale; if(iw < o.naturalwidth || ih < o.naturalheight) { ic = scaleimage(i, iw, ih); if(ic) t.fimage = ic; } else { iw = t.iw; ih = t.ih; mscale = 1; } if(parsefloat(tc.imageradius)) t.image = t.fimage = i = roundimage(t.image, tc.imageradius, iw, ih, mscale); if(!t.hastext()) { if(tc.shadow) { ic = addshadowtoimage(t.image, iw, ih, mscale, tc.shadow, tc.shadowblur, tc.shadowoffset); if(ic) { t.fimage = ic.image; t.w = ic.width; t.h = ic.height; } } if(tc.bgcolour || tc.bgoutlinethickness) { bc = tc.bgcolour == 'tag' ? getproperty(t.a, 'background-color') : tc.bgcolour; oc = tc.bgoutline == 'tag' ? getproperty(t.a, 'color') : (tc.bgoutline || tc.textcolour); iw = t.fimage.width; ih = t.fimage.height; if(tc.outlinemethod == 'colour') { // create the outline version first, using the current image state ic = addbackgroundtoimage(t.fimage, iw, ih, mscale, bc, tc.bgoutlinethickness, t.outline.colour, tc.padding, tc.bgradius, 1); if(ic) t.oimage = ic.image; } ic = addbackgroundtoimage(t.fimage, iw, ih, mscale, bc, tc.bgoutlinethickness, oc, tc.padding, tc.bgradius); if(ic) { t.fimage = ic.image; t.w = ic.width; t.h = ic.height; } } if(tc.outlinemethod == 'size') { if(tc.outlineincrease > 0) { t.iw += 2 * tc.outlineincrease; t.ih += 2 * tc.outlineincrease; iw = mscale * t.iw; ih = mscale * t.ih; ic = scaleimage(t.fimage, iw, ih); t.oimage = ic; t.fimage = expandimage(t.fimage, t.oimage.width, t.oimage.height); } else { iw = mscale * (t.iw + (2 * tc.outlineincrease)); ih = mscale * (t.ih + (2 * tc.outlineincrease)); ic = scaleimage(t.fimage, iw, ih); t.oimage = expandimage(ic, t.fimage.width, t.fimage.height); } } } } t.init(); } function getproperty(e,p) { var dv = doc.defaultview, pc = p.replace(/\-([a-z])/g,function(a){return a.charat(1).touppercase()}); return (dv && dv.getcomputedstyle && dv.getcomputedstyle(e,null).getpropertyvalue(p)) || (e.currentstyle && e.currentstyle[pc]); } function findweight(a, wfrom, theight) { var w = 1, p; if(wfrom) { w = 1 * (a.getattribute(wfrom) || theight); } else if(p = getproperty(a,'font-size')) { w = (p.indexof('px') > -1 && p.replace('px','') * 1) || (p.indexof('pt') > -1 && p.replace('pt','') * 1.25) || p * 3.3; } return w; } function eventtocanvasid(e) { return e.target && defined(e.target.id) ? e.target.id : e.srcelement.parentnode.id; } function eventxy(e, c) { var xy, p, xmul = parseint(getproperty(c, 'width')) / c.width, ymul = parseint(getproperty(c, 'height')) / c.height; if(defined(e.offsetx)) { xy = {x: e.offsetx, y: e.offsety}; } else { p = abspos(c.id); if(defined(e.changedtouches)) e = e.changedtouches[0]; if(e.pagex) xy = {x: e.pagex - p.x, y: e.pagey - p.y}; } if(xy && xmul && ymul) { xy.x /= xmul; xy.y /= ymul; } return xy; } function mouseout(e) { var cv = e.target || e.fromelement.parentnode, tc = tagcanvas.tc[cv.id]; if(tc) { tc.mx = tc.my = -1; tc.unfreeze(); tc.enddrag(); } } function mousemove(e) { var i, t = tagcanvas, tc, p, tg = eventtocanvasid(e); for(i in t.tc) { tc = t.tc[i]; if(tc.tttimer) { cleartimeout(tc.tttimer); tc.tttimer = null; } } if(tg && t.tc[tg]) { tc = t.tc[tg]; if(p = eventxy(e, tc.canvas)) { tc.mx = p.x; tc.my = p.y; tc.drag(e, p); } tc.drawn = 0; } } function mousedown(e) { var t = tagcanvas, cb = doc.addeventlistener ? 0 : 1, tg = eventtocanvasid(e); if(tg && e.button == cb && t.tc[tg]) { t.tc[tg].begindrag(e); } } function mouseup(e) { var t = tagcanvas, cb = doc.addeventlistener ? 0 : 1, tg = eventtocanvasid(e), tc; if(tg && e.button == cb && t.tc[tg]) { tc = t.tc[tg]; mousemove(e); if(!tc.enddrag() && !tc.touchstate) tc.clicked(e); } } function touchdown(e) { var tg = eventtocanvasid(e), tc = (tg && tagcanvas.tc[tg]), p; if(tc && e.changedtouches) { if(e.touches.length == 1 && tc.touchstate == 0) { tc.touchstate = 1; tc.begindrag(e); if(p = eventxy(e, tc.canvas)) { tc.mx = p.x; tc.my = p.y; tc.drawn = 0; } } else if(e.targettouches.length == 2 && tc.pinchzoom) { tc.touchstate = 3; tc.enddrag(); tc.beginpinch(e); } else { tc.enddrag(); tc.endpinch(); tc.touchstate = 0; } } } function touchup(e) { var tg = eventtocanvasid(e), tc = (tg && tagcanvas.tc[tg]); if(tc && e.changedtouches) { switch(tc.touchstate) { case 1: tc.draw(); tc.clicked(); break; case 2: tc.enddrag(); break; case 3: tc.endpinch(); } tc.touchstate = 0; } } function touchmove(e) { var i, t = tagcanvas, tc, p, tg = eventtocanvasid(e); for(i in t.tc) { tc = t.tc[i]; if(tc.tttimer) { cleartimeout(tc.tttimer); tc.tttimer = null; } } tc = (tg && t.tc[tg]); if(tc && e.changedtouches && tc.touchstate) { switch(tc.touchstate) { case 1: case 2: if(p = eventxy(e, tc.canvas)) { tc.mx = p.x; tc.my = p.y; if(tc.drag(e, p)) tc.touchstate = 2; } break; case 3: tc.pinch(e); } tc.drawn = 0; } } function mousewheel(e) { var t = tagcanvas, tg = eventtocanvasid(e); if(tg && t.tc[tg]) { e.cancelbubble = true; e.returnvalue = false; e.preventdefault && e.preventdefault(); t.tc[tg].wheel((e.wheeldelta || e.detail) > 0); } } function scroll(e) { var i, t = tagcanvas; cleartimeout(t.scrolltimer); for(i in t.tc) { t.tc[i].pause(); } t.scrolltimer = settimeout(function() { var i, t = tagcanvas; for(i in t.tc) { t.tc[i].resume(); } }, t.scrollpause); } function drawcanvas() { drawcanvasraf(timenow()); } function drawcanvasraf(t) { var tc = tagcanvas.tc, i; tagcanvas.nextframe(tagcanvas.interval); t = t || timenow(); for(i in tc) tc[i].draw(t); } function abspos(id) { var e = doc.getelementbyid(id), r = e.getboundingclientrect(), dd = doc.documentelement, b = doc.body, w = window, xs = w.pagexoffset || dd.scrollleft, ys = w.pageyoffset || dd.scrolltop, xo = dd.clientleft || b.clientleft, yo = dd.clienttop || b.clienttop; return { x: r.left + xs - xo, y: r.top + ys - yo }; } function project(tc,p1,sx,sy) { var m = tc.radius * tc.z1 / (tc.z1 + tc.z2 + p1.z); return { x: p1.x * m * sx, y: p1.y * m * sy, z: p1.z, w: (tc.z1 - p1.z) / tc.z2 }; } /** * @constructor * for recursively splitting tag contents on
tags */ function textsplitter(e) { this.e = e; this.br = 0; this.line = []; this.text = []; this.original = e.innertext || e.textcontent; } tsproto = textsplitter.prototype; tsproto.empty = function() { for(var i = 0; i < this.text.length; ++i) if(this.text[i].length) return false; return true; }; tsproto.lines = function(e) { var r = e ? 1 : 0, cn, cl, i; e = e || this.e; cn = e.childnodes; cl = cn.length; for(i = 0; i < cl; ++i) { if(cn[i].nodename == 'br') { this.text.push(this.line.join(' ')); this.br = 1; } else if(cn[i].nodetype == 3) { if(this.br) { this.line = [cn[i].nodevalue]; this.br = 0; } else { this.line.push(cn[i].nodevalue); } } else { this.lines(cn[i]); } } r || this.br || this.text.push(this.line.join(' ')); return this.text; }; tsproto.splitwidth = function(w, c, f, h) { var i, j, words, text = []; c.font = h + 'px ' + f; for(i = 0; i < this.text.length; ++i) { words = this.text[i].split(/\s+/); this.line = [words[0]]; for(j = 1; j < words.length; ++j) { if(c.measuretext(this.line.join(' ') + ' ' + words[j]).width > w) { text.push(this.line.join(' ')); this.line = [words[j]]; } else { this.line.push(words[j]); } } text.push(this.line.join(' ')); } return this.text = text; }; /** * @constructor */ function outline(tc,t) { this.ts = null; this.tc = tc; this.tag = t; this.x = this.y = this.w = this.h = this.sc = 1; this.z = 0; this.pulse = 1; this.pulsate = tc.pulsateto < 1; this.colour = tc.outlinecolour; this.adash = ~~tc.outlinedash; this.agap = ~~tc.outlinedashspace || this.adash; this.aspeed = tc.outlinedashspeed * 1; if(this.colour == 'tag') this.colour = getproperty(t.a, 'color'); else if(this.colour == 'tagbg') this.colour = getproperty(t.a, 'background-color'); this.draw = this.pulsate ? this.drawpulsate : this.drawsimple; this.radius = tc.outlineradius | 0; this.setmethod(tc.outlinemethod); } oproto = outline.prototype; oproto.setmethod = function(om) { var methods = { block: ['predraw','drawblock'], colour: ['predraw','drawcolour'], outline: ['postdraw','drawoutline'], classic: ['lastdraw','drawoutline'], size: ['predraw','drawsize'], none: ['lastdraw'] }, funcs = methods[om] || methods.outline; if(om == 'none') { this.draw = function() { return 1; } } else { this.drawfunc = this[funcs[1]]; } this[funcs[0]] = this.draw; }; oproto.update = function(x,y,w,h,sc,z,xo,yo) { var o = this.tc.outlineoffset, o2 = 2 * o; this.x = sc * x + xo - o; this.y = sc * y + yo - o; this.w = sc * w + o2; this.h = sc * h + o2; this.sc = sc; // used to determine frontmost this.z = z; }; oproto.ants = function(c) { if(!this.adash) return; var l = this.adash, g = this.agap, s = this.aspeed, length = l + g, l1 = 0, l2 = l, g1 = g, g2 = 0, seq = 0, ants; if(s) { seq = abs(s) * (timenow() - this.ts) / 50; if(s < 0) seq = 8.64e6 - seq; s = ~~seq % length; } if(s) { if(l >= s) { l1 = l - s; l2 = s; } else { g1 = length - s; g2 = g - g1; } ants = [l1, g1, l2, g2]; } else { ants = [l,g]; } c.setlinedash(ants); } oproto.drawoutline = function(c,x,y,w,h,colour) { var r = min(this.radius, h/2, w/2); c.strokestyle = colour; this.ants(c); rrect(c, x, y, w, h, r, true); }; oproto.drawsize = function(c,x,y,w,h,colour,tag,x1,y1) { var tw = tag.w, th = tag.h, m, i, sc; if(this.pulsate) { if(tag.image) sc = (tag.image.height + this.tc.outlineincrease) / tag.image.height; else sc = tag.oscale; i = tag.fimage || tag.image; m = 1 + ((sc - 1) * (1-this.pulse)); tag.h *= m; tag.w *= m; } else { i = tag.oimage; } tag.alpha = 1; tag.draw(c, x1, y1, i); tag.h = th; tag.w = tw; return 1; }; oproto.drawcolour = function(c,x,y,w,h,colour,tag,x1,y1) { if(tag.oimage) { if(this.pulse < 1) { tag.alpha = 1 - pow(this.pulse, 2); tag.draw(c, x1, y1, tag.fimage); tag.alpha = this.pulse; } else { tag.alpha = 1; } tag.draw(c, x1, y1, tag.oimage); return 1; } return this[tag.image ? 'drawcolourimage' : 'drawcolourtext'](c,x,y,w,h,colour,tag,x1,y1); }; oproto.drawcolourtext = function(c,x,y,w,h,colour,tag,x1,y1) { var normal = tag.colour; tag.colour = colour; tag.alpha = 1; tag.draw(c,x1,y1); tag.colour = normal; return 1; }; oproto.drawcolourimage = function(c,x,y,w,h,colour,tag,x1,y1) { var ccanvas = c.canvas, fx = ~~max(x,0), fy = ~~max(y,0), fw = min(ccanvas.width - fx, w) + .5|0, fh = min(ccanvas.height - fy,h) + .5|0, cc; if(ocanvas) ocanvas.width = fw, ocanvas.height = fh; else ocanvas = newcanvas(fw, fh); if(!ocanvas) return this.setmethod('outline'); // if using ie and images, give up! cc = ocanvas.getcontext('2d'); cc.drawimage(ccanvas,fx,fy,fw,fh,0,0,fw,fh); c.clearrect(fx,fy,fw,fh); if(this.pulsate) { tag.alpha = 1 - pow(this.pulse, 2); } else { tag.alpha = 1; } tag.draw(c,x1,y1); c.settransform(1,0,0,1,0,0); c.save(); c.beginpath(); c.rect(fx,fy,fw,fh); c.clip(); c.globalcompositeoperation = 'source-in'; c.fillstyle = colour; c.fillrect(fx,fy,fw,fh); c.restore(); c.globalalpha = 1; c.globalcompositeoperation = 'destination-over'; c.drawimage(ocanvas,0,0,fw,fh,fx,fy,fw,fh); c.globalcompositeoperation = 'source-over'; return 1; }; oproto.drawblock = function(c,x,y,w,h,colour) { var r = min(this.radius, h/2, w/2); c.fillstyle = colour; rrect(c, x, y, w, h, r); }; oproto.drawsimple = function(c, tag, x1, y1, ga, usega) { var t = this.tc; c.settransform(1,0,0,1,0,0); c.strokestyle = this.colour; c.linewidth = t.outlinethickness; c.shadowblur = c.shadowoffsetx = c.shadowoffsety = 0; c.globalalpha = usega ? ga : 1; return this.drawfunc(c,this.x,this.y,this.w,this.h,this.colour,tag,x1,y1); }; oproto.drawpulsate = function(c, tag, x1, y1) { var diff = timenow() - this.ts, t = this.tc, ga = t.pulsateto + ((1 - t.pulsateto) * (0.5 + (cos(2 * math.pi * diff / (1000 * t.pulsatetime)) / 2))); this.pulse = ga = tagcanvas.smooth(1,ga); return this.drawsimple(c, tag, x1, y1, ga, 1); }; oproto.active = function(c,x,y) { var a = (x >= this.x && y >= this.y && x <= this.x + this.w && y <= this.y + this.h); if(a) { this.ts = this.ts || timenow(); } else { this.ts = null; } return a; }; oproto.predraw = oproto.postdraw = oproto.lastdraw = nop; /** * @constructor */ function tag(tc, text, a, v, w, h, col, bcol, bradius, boutline, bothickness, font, padding, original) { this.tc = tc; this.image = null; this.text = text; this.text_original = original; this.line_widths = []; this.title = a.title || null; this.a = a; this.position = new vector(v[0], v[1], v[2]); this.x = this.y = this.z = 0; this.w = w; this.h = h; this.colour = col || tc.textcolour; this.bgcolour = bcol || tc.bgcolour; this.bgradius = bradius | 0; this.bgoutline = boutline || this.colour; this.bgoutlinethickness = bothickness | 0; this.textfont = font || tc.textfont; this.padding = padding | 0; this.sc = this.alpha = 1; this.weighted = !tc.weight; this.outline = new outline(tc,this); } tproto = tag.prototype; tproto.init = function(e) { var tc = this.tc; this.textheight = tc.textheight; if(this.hastext()) { this.measure(tc.ctxt,tc); } else { this.w = this.iw; this.h = this.ih; } this.setshadowcolour = tc.shadowalpha ? this.setshadowcolouralpha : this.setshadowcolourfixed; this.setdraw(tc); }; tproto.draw = nop; tproto.hastext = function() { return this.text && this.text[0].length > 0; }; tproto.equalto = function(e) { var i = e.getelementsbytagname('img'); if(this.a.href != e.href) return 0; if(i.length) return this.image.src == i[0].src; return (e.innertext || e.textcontent) == this.text_original; }; tproto.setimage = function(i) { this.image = this.fimage = i; }; tproto.setdraw = function(t) { this.draw = this.fimage ? (t.ie > 7 ? this.drawimageie : this.drawimage) : this.drawtext; t.noselect && (this.checkactive = nop); }; tproto.measuretext = function(c) { var i, l = this.text.length, w = 0, wl; for(i = 0; i < l; ++i) { this.line_widths[i] = wl = c.measuretext(this.text[i]).width; w = max(w, wl); } return w; }; tproto.measure = function(c,t) { var extents = findtextboundingbox(this.text, this.textfont, this.textheight), s, th, f, soff, cw, twidth, theight, img, tcv; // add the gap at the top to the height to make equal gap at bottom theight = extents ? extents.max.y + extents.min.y : this.textheight; c.font = this.font = this.textheight + 'px ' + this.textfont; twidth = this.measuretext(c); if(t.txtopt) { s = t.txtscale; th = s * this.textheight; f = th + 'px ' + this.textfont; soff = [s * t.shadowoffset[0], s * t.shadowoffset[1]]; c.font = f; cw = this.measuretext(c); tcv = new textcanvas(this.text, f, cw + s, (s * theight) + s, cw, this.line_widths, t.textalign, t.textvalign, s); if(this.image) tcv.setimage(this.image, this.iw, this.ih, t.imageposition, t.imagepadding, t.imagealign, t.imagevalign, t.imagescale); img = tcv.create(this.colour, this.bgcolour, this.bgoutline, s * this.bgoutlinethickness, t.shadow, s * t.shadowblur, soff, s * this.padding, s * this.bgradius); // add outline image using highlight colour if(t.outlinemethod == 'colour') { this.oimage = tcv.create(this.outline.colour, this.bgcolour, this.outline.colour, s * this.bgoutlinethickness, t.shadow, s * t.shadowblur, soff, s * this.padding, s * this.bgradius); } else if(t.outlinemethod == 'size') { extents = findtextboundingbox(this.text, this.textfont, this.textheight + t.outlineincrease); th = extents.max.y + extents.min.y; f = (s * (this.textheight + t.outlineincrease)) + 'px ' + this.textfont; c.font = f; cw = this.measuretext(c); tcv = new textcanvas(this.text, f, cw + s, (s * th) + s, cw, this.line_widths, t.textalign, t.textvalign, s); if(this.image) tcv.setimage(this.image, this.iw + t.outlineincrease, this.ih + t.outlineincrease, t.imageposition, t.imagepadding, t.imagealign, t.imagevalign, t.imagescale); this.oimage = tcv.create(this.colour, this.bgcolour, this.bgoutline, s * this.bgoutlinethickness, t.shadow, s * t.shadowblur, soff, s * this.padding, s * this.bgradius); this.oscale = this.oimage.width / img.width; if(t.outlineincrease > 0) img = expandimage(img, this.oimage.width, this.oimage.height); else this.oimage = expandimage(this.oimage, img.width, img.height); } if(img) { this.fimage = img; twidth = this.fimage.width / s; theight = this.fimage.height / s; } this.setdraw(t); t.txtopt = !!this.fimage; } this.h = theight; this.w = twidth; }; tproto.setfont = function(f, c, bc, boc) { this.textfont = f; this.colour = c; this.bgcolour = bc; this.bgoutline = boc; this.measure(this.tc.ctxt, this.tc); }; tproto.setweight = function(w) { var tc = this.tc, modes = tc.weightmode.split(/[, ]/), m, s, wl = w.length; if(!this.hastext()) return; this.weighted = true; for(s = 0; s < wl; ++s) { m = modes[s] || 'size'; if('both' == m) { this.weight(w[s], tc.ctxt, tc, 'size', tc.min_weight[s], tc.max_weight[s], s); this.weight(w[s], tc.ctxt, tc, 'colour', tc.min_weight[s], tc.max_weight[s], s); } else { this.weight(w[s], tc.ctxt, tc, m, tc.min_weight[s], tc.max_weight[s], s); } } this.measure(tc.ctxt, tc); }; tproto.weight = function(w, c, t, m, wmin, wmax, wnum) { w = isnan(w) ? 1 : w; var nweight = (w - wmin) / (wmax - wmin); if('colour' == m) this.colour = findgradientcolour(t, nweight, wnum); else if('bgcolour' == m) this.bgcolour = findgradientcolour(t, nweight, wnum); else if('bgoutline' == m) this.bgoutline = findgradientcolour(t, nweight, wnum); else if('outline' == m) this.outline.colour = findgradientcolour(t, nweight, wnum); else if('size' == m) { if(t.weightsizemin > 0 && t.weightsizemax > t.weightsizemin) { this.textheight = t.weightsize * (t.weightsizemin + (t.weightsizemax - t.weightsizemin) * nweight); } else { // min textheight of 1 this.textheight = max(1, w * t.weightsize); } } }; tproto.setshadowcolourfixed = function(c,s,a) { c.shadowcolor = s; }; tproto.setshadowcolouralpha = function(c,s,a) { c.shadowcolor = setalpha(s, a); }; tproto.drawtext = function(c,xoff,yoff) { var t = this.tc, x = this.x, y = this.y, s = this.sc, i, xl; c.globalalpha = this.alpha; c.fillstyle = this.colour; t.shadow && this.setshadowcolour(c,t.shadow,this.alpha); c.font = this.font; x += xoff / s; y += (yoff / s) - (this.h / 2); for(i = 0; i < this.text.length; ++i) { xl = x; if('right' == t.textalign) { xl += this.w / 2 - this.line_widths[i]; } else if('centre' == t.textalign) { xl -= this.line_widths[i] / 2; } else { xl -= this.w / 2; } c.settransform(s, 0, 0, s, s * xl, s * y); c.filltext(this.text[i], 0, 0); y += this.textheight; } }; tproto.drawimage = function(c,xoff,yoff,im) { var x = this.x, y = this.y, s = this.sc, i = im || this.fimage, w = this.w, h = this.h, a = this.alpha, shadow = this.shadow; c.globalalpha = a; shadow && this.setshadowcolour(c,shadow,a); x += (xoff / s) - (w / 2); y += (yoff / s) - (h / 2); c.settransform(s, 0, 0, s, s * x, s * y); c.drawimage(i, 0, 0, w, h); }; tproto.drawimageie = function(c,xoff,yoff) { var i = this.fimage, s = this.sc, w = i.width = this.w*s, h = i.height = this.h * s, x = (this.x*s) + xoff - (w/2), y = (this.y*s) + yoff - (h/2); c.settransform(1,0,0,1,0,0); c.globalalpha = this.alpha; c.drawimage(i, x, y); }; tproto.calc = function(m,a) { var pp, t = this.tc, mnb = t.minbrightness, mxb = t.maxbrightness, r = t.max_radius; pp = m.xform(this.position); this.xformed = pp; pp = project(t, pp, t.stretchx, t.stretchy); this.x = pp.x; this.y = pp.y; this.z = pp.z; this.sc = pp.w; this.alpha = a * clamp(mnb + (mxb - mnb) * (r - this.z) / (2 * r), 0, 1); return this.xformed; }; tproto.updateactive = function(c, xoff, yoff) { var o = this.outline, w = this.w, h = this.h, x = this.x - w/2, y = this.y - h/2; o.update(x, y, w, h, this.sc, this.z, xoff, yoff); return o; }; tproto.checkactive = function(c,xoff,yoff) { var t = this.tc, o = this.updateactive(c, xoff, yoff); return o.active(c, t.mx, t.my) ? o : null; }; tproto.clicked = function(e) { var a = this.a, t = a.target, h = a.href, evt; if(t != '' && t != '_self') { if(self.frames[t]) { self.frames[t].document.location = h; } else{ try { if(top.frames[t]) { top.frames[t].document.location = h; return; } } catch(err) { // different domain/port/protocol? } window.open(h, t); } return; } if(doc.createevent) { evt = doc.createevent('mouseevents'); evt.initmouseevent('click', 1, 1, window, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, null); if(!a.dispatchevent(evt)) return; } else if(a.fireevent) { if(!a.fireevent('onclick')) return; } doc.location = h; }; /** * @constructor */ function tagcanvas(cid,lctr,opt) { var i, p, c = doc.getelementbyid(cid), cp = ['id','class','innerhtml'], raf; if(!c) throw 0; if(defined(window.g_vmlcanvasmanager)) { c = window.g_vmlcanvasmanager.initelement(c); this.ie = parsefloat(navigator.appversion.split('msie')[1]); } if(c && (!c.getcontext || !c.getcontext('2d').filltext)) { p = doc.createelement('div'); for(i = 0; i < cp.length; ++i) p[cp[i]] = c[cp[i]]; c.parentnode.insertbefore(p,c); c.parentnode.removechild(c); throw 0; } for(i in tagcanvas.options) this[i] = opt && defined(opt[i]) ? opt[i] : (defined(tagcanvas[i]) ? tagcanvas[i] : tagcanvas.options[i]); this.canvas = c; this.ctxt = c.getcontext('2d'); this.z1 = 250 / max(this.depth, 0.001); this.z2 = this.z1 / this.zoom; this.radius = min(c.height, c.width) * 0.0075; // fits radius of 100 in canvas this.max_radius = 100; this.max_weight = []; this.min_weight = []; this.textfont = this.textfont && fixfont(this.textfont); this.textheight *= 1; this.imageradius = this.imageradius.tostring(); this.pulsateto = clamp(this.pulsateto, 0, 1); this.minbrightness = clamp(this.minbrightness, 0, 1); this.maxbrightness = clamp(this.maxbrightness, this.minbrightness, 1); this.ctxt.textbaseline = 'top'; this.lx = (this.lock + '').indexof('x') + 1; this.ly = (this.lock + '').indexof('y') + 1; this.frozen = this.dx = this.dy = this.fixedanim = this.touchstate = 0; this.fixedalpha = 1; this.source = lctr || cid; this.repeattags = min(64, ~~this.repeattags); this.mintags = min(200, ~~this.mintags); if(~~this.scrollpause > 0) tagcanvas.scrollpause = ~~this.scrollpause; else this.scrollpause = 0; if(this.mintags > 0 && this.repeattags < 1 && (i = this.gettags().length)) this.repeattags = ceil(this.mintags / i) - 1; this.transform = matrix.identity(); this.starttime = this.time = timenow(); this.mx = this.my = -1; this.centreimage && centreimage(this); this.animate = this.dragcontrol ? this.animatedrag : this.animateposition; this.animtiming = (typeof tagcanvas[this.animtiming] == 'function' ? tagcanvas[this.animtiming] : tagcanvas.smooth); if(this.shadowblur || this.shadowoffset[0] || this.shadowoffset[1]) { // let the browser translate "red" into "#ff0000" this.ctxt.shadowcolor = this.shadow; this.shadow = this.ctxt.shadowcolor; this.shadowalpha = shadowalphabroken(); } else { delete this.shadow; } this.load(); if(lctr && this.hidetags) { (function(t) { if(tagcanvas.loaded) t.hidetags(); else addhandler('load', function() { t.hidetags(); }, window); })(this); } this.yaw = this.initial ? this.initial[0] * this.maxspeed : 0; this.pitch = this.initial ? this.initial[1] * this.maxspeed : 0; if(this.tooltip) { this.ctitle = c.title; c.title = ''; if(this.tooltip == 'native') { this.tooltip = this.tooltipnative; } else { this.tooltip = this.tooltipdiv; if(!this.ttdiv) { this.ttdiv = doc.createelement('div'); this.ttdiv.classname = this.tooltipclass; this.ttdiv.style.position = 'absolute'; this.ttdiv.style.zindex = c.style.zindex + 1; addhandler('mouseover',function(e){e.target.style.display='none';},this.ttdiv); doc.body.appendchild(this.ttdiv); } } } else { this.tooltip = this.tooltipnone; } if(!this.nomouse && !handlers[cid]) { handlers[cid] = [ ['mousemove', mousemove], ['mouseout', mouseout], ['mouseup', mouseup], ['touchstart', touchdown], ['touchend', touchup], ['touchcancel', touchup], ['touchmove', touchmove] ]; if(this.dragcontrol) { handlers[cid].push(['mousedown', mousedown]); handlers[cid].push(['selectstart', nop]); } if(this.wheelzoom) { handlers[cid].push(['mousewheel', mousewheel]); handlers[cid].push(['dommousescroll', mousewheel]); } if(this.scrollpause) { handlers[cid].push(['scroll', scroll, window]); } for(i = 0; i < handlers[cid].length; ++i) { p = handlers[cid][i]; addhandler(p[0], p[1], p[2] ? p[2] : c); } } if(!tagcanvas.started) { raf = window.requestanimationframe = window.requestanimationframe || window.mozrequestanimationframe || window.webkitrequestanimationframe || window.msrequestanimationframe; tagcanvas.nextframe = raf ? tagcanvas.nextframeraf : tagcanvas.nextframetimeout; tagcanvas.interval = this.interval; tagcanvas.nextframe(this.interval); tagcanvas.started = 1; } } tcproto = tagcanvas.prototype; tcproto.sourceelements = function() { if(doc.queryselectorall) return doc.queryselectorall('#' + this.source); return [doc.getelementbyid(this.source)]; }; tcproto.hidetags = function() { var el = this.sourceelements(), i; for(i = 0; i < el.length; ++i) el[i].style.display = 'none'; }; tcproto.gettags = function() { var el = this.sourceelements(), etl, tl = [], i, j, k; for(k = 0; k <= this.repeattags; ++k) { for(i = 0; i < el.length; ++i) { etl = el[i].getelementsbytagname('a'); for(j = 0; j < etl.length; ++j) { tl.push(etl[j]); } } } return tl; }; tcproto.message = function(text) { var tl = [], i, p, tc = text.split(''), a, t, x, z; for(i = 0; i < tc.length; ++i) { if(tc[i] != ' ') { p = i - tc.length / 2; a = doc.createelement('a'); a.href = '#'; a.innertext = tc[i]; x = 100 * sin(p / 9); z = -100 * cos(p / 9); t = new tag(this, tc[i], a, [x,0,z], 2, 18, '#000', '#fff', 0, 0, 0, 'monospace', 2, tc[i]); t.init(); tl.push(t); } } return tl; }; tcproto.createtag = function(e) { var im, i, t, txt, ts, font, bc, boc, p = [0, 0, 0]; if('text' != this.imagemode) { im = e.getelementsbytagname('img'); if(im.length) { i = new image; i.src = im[0].src; if(!this.imagemode) { t = new tag(this, "", e, p, 0, 0); t.setimage(i); //t.init(); addimage(i, im[0], t, this); return t; } } } if('image' != this.imagemode) { ts = new textsplitter(e); txt = ts.lines(); if(!ts.empty()) { font = this.textfont || fixfont(getproperty(e,'font-family')); if(this.splitwidth) txt = ts.splitwidth(this.splitwidth, this.ctxt, font, this.textheight); bc = this.bgcolour == 'tag' ? getproperty(e, 'background-color') : this.bgcolour; boc = this.bgoutline == 'tag' ? getproperty(e, 'color') : this.bgoutline; } else { ts = null; } } if(ts || i) { t = new tag(this, txt, e, p, 2, this.textheight + 2, this.textcolour || getproperty(e,'color'), bc, this.bgradius, boc, this.bgoutlinethickness, font, this.padding, ts && ts.original); if(i) { t.setimage(i); addimage(i, im[0], t, this); } else { t.init(); } return t; } }; tcproto.updatetag = function(t, a) { var colour = this.textcolour || getproperty(a, 'color'), font = this.textfont || fixfont(getproperty(a, 'font-family')), bc = this.bgcolour == 'tag' ? getproperty(a, 'background-color') : this.bgcolour, boc = this.bgoutline == 'tag' ? getproperty(a, 'color') : this.bgoutline; t.a = a; t.title = a.title; if(t.colour != colour || t.textfont != font || t.bgcolour != bc || t.bgoutline != boc) t.setfont(font, colour, bc, boc); }; tcproto.weight = function(tl) { var ll = tl.length, w, i, s, weights = [], valid, wfrom = this.weightfrom ? this.weightfrom.split(/[, ]/) : [null], wl = wfrom.length; for(i = 0; i < ll; ++i) { weights[i] = []; for(s = 0; s < wl; ++s) { w = findweight(tl[i].a, wfrom[s], this.textheight); if(!this.max_weight[s] || w > this.max_weight[s]) this.max_weight[s] = w; if(!this.min_weight[s] || w < this.min_weight[s]) this.min_weight[s] = w; weights[i][s] = w; } } for(s = 0; s < wl; ++s) { if(this.max_weight[s] > this.min_weight[s]) valid = 1; } if(valid) { for(i = 0; i < ll; ++i) { tl[i].setweight(weights[i]); } } }; tcproto.load = function() { var tl = this.gettags(), taglist = [], shape, t, shapeargs, rx, ry, rz, vl, i, tagmap = [], pfuncs = { sphere: pointsonsphere, vcylinder: pointsoncylinderv, hcylinder: pointsoncylinderh, vring: pointsonringv, hring: pointsonringh }; if(tl.length) { tagmap.length = tl.length; for(i = 0; i < tl.length; ++i) tagmap[i] = i; this.shuffletags && shuffle(tagmap); rx = 100 * this.radiusx; ry = 100 * this.radiusy; rz = 100 * this.radiusz; this.max_radius = max(rx, max(ry, rz)); for(i = 0; i < tl.length; ++i) { t = this.createtag(tl[tagmap[i]]); if(t) taglist.push(t); } this.weight && this.weight(taglist, true); if(this.shapeargs) { this.shapeargs[0] = taglist.length; } else { shapeargs = this.shape.tostring().split(/[(),]/); shape = shapeargs.shift(); if(typeof window[shape] === 'function') this.shape = window[shape]; else this.shape = pfuncs[shape] || pfuncs.sphere; this.shapeargs = [taglist.length, rx, ry, rz].concat(shapeargs); } vl = this.shape.apply(this, this.shapeargs); this.listlength = taglist.length; for(i = 0; i < taglist.length; ++i) taglist[i].position = new vector(vl[i][0], vl[i][1], vl[i][2]); } if(this.notagsmessage && !taglist.length) { i = (this.imagemode && this.imagemode != 'both' ? this.imagemode + ' ': ''); taglist = this.message('no ' + i + 'tags'); } this.taglist = taglist; }; tcproto.update = function() { var tl = this.gettags(), newlist = [], taglist = this.taglist, found, added = [], removed = [], vl, ol, nl, i, j; if(!this.shapeargs) return this.load(); if(tl.length) { nl = this.listlength = tl.length; ol = taglist.length; // copy existing list, populate "removed" for(i = 0; i < ol; ++i) { newlist.push(taglist[i]); removed.push(i); } // find added and removed tags for(i = 0; i < nl; ++i) { for(j = 0, found = 0; j < ol; ++j) { if(taglist[j].equalto(tl[i])) { this.updatetag(newlist[j], tl[i]); found = removed[j] = -1; } } if(!found) added.push(i); } // clean out found tags from removed list for(i = 0, j = 0; i < ol; ++i) { if(removed[j] == -1) removed.splice(j,1); else ++j; } // insert new tags in gaps where old tags removed if(removed.length) { shuffle(removed); while(removed.length && added.length) { i = removed.shift(); j = added.shift(); newlist[i] = this.createtag(tl[j]); } // remove any more (in reverse order) removed.sort(function(a,b) {return a-b}); while(removed.length) { newlist.splice(removed.pop(), 1); } } // add any extra tags j = newlist.length / (added.length + 1); i = 0; while(added.length) { newlist.splice(ceil(++i * j), 0, this.createtag(tl[added.shift()])); } // assign correct positions to tags this.shapeargs[0] = nl = newlist.length; vl = this.shape.apply(this, this.shapeargs); for(i = 0; i < nl; ++i) newlist[i].position = new vector(vl[i][0], vl[i][1], vl[i][2]); // reweight tags this.weight && this.weight(newlist); } this.taglist = newlist; }; tcproto.setshadow = function(c) { c.shadowblur = this.shadowblur; c.shadowoffsetx = this.shadowoffset[0]; c.shadowoffsety = this.shadowoffset[1]; }; tcproto.draw = function(t) { if(this.paused) return; var cv = this.canvas, cw = cv.width, ch = cv.height, max_sc = 0, tdelta = (t - this.time) * tagcanvas.interval / 1000, x = cw / 2 + this.offsetx, y = ch / 2 + this.offsety, c = this.ctxt, active, a, i, aindex = -1, tl = this.taglist, l = tl.length, frontsel = this.frontselect, centredrawn = (this.centrefunc == nop), fixed; this.time = t; if(this.frozen && this.drawn) return this.animate(cw,ch,tdelta); fixed = this.animatefixed(); c.settransform(1,0,0,1,0,0); for(i = 0; i < l; ++i) tl[i].calc(this.transform, this.fixedalpha); tl = sortlist(tl, function(a,b) {return b.z-a.z}); if(fixed && this.fixedanim.active) { active = this.fixedanim.tag.updateactive(c, x, y); } else { this.active = null; for(i = 0; i < l; ++i) { a = this.mx >= 0 && this.my >= 0 && this.taglist[i].checkactive(c, x, y); if(a && a.sc > max_sc && (!frontsel || a.z <= 0)) { active = a; aindex = i; active.tag = this.taglist[i]; max_sc = a.sc; } } this.active = active; } this.txtopt || (this.shadow && this.setshadow(c)); c.clearrect(0,0,cw,ch); for(i = 0; i < l; ++i) { if(!centredrawn && tl[i].z <= 0) { // run the centrefunc if the next tag is at the front try { this.centrefunc(c, cw, ch, x, y); } catch(e) { alert(e); // don't run it again this.centrefunc = nop; } centredrawn = true; } if(!(active && active.tag == tl[i] && active.predraw(c, tl[i], x, y))) tl[i].draw(c, x, y); active && active.tag == tl[i] && active.postdraw(c); } if(this.freezeactive && active) { this.freeze(); } else { this.unfreeze(); this.drawn = (l == this.listlength); } if(this.fixedcallback) { this.fixedcallback(this,this.fixedcallbacktag); this.fixedcallback = null; } fixed || this.animate(cw, ch, tdelta); active && active.lastdraw(c); cv.style.cursor = active ? this.activecursor : ''; this.tooltip(active,this.taglist[aindex]); }; tcproto.tooltipnone = function() { }; tcproto.tooltipnative = function(active,tag) { if(active) this.canvas.title = tag && tag.title ? tag.title : ''; else this.canvas.title = this.ctitle; }; tcproto.setttdiv = function(title, tag) { var tc = this, s = tc.ttdiv.style; if(title != tc.ttdiv.innerhtml) s.display = 'none'; tc.ttdiv.innerhtml = title; tag && (tag.title = tc.ttdiv.innerhtml); if(s.display == 'none' && ! tc.tttimer) { tc.tttimer = settimeout(function() { var p = abspos(tc.canvas.id); s.display = 'block'; s.left = p.x + tc.mx + 'px'; s.top = p.y + tc.my + 24 + 'px'; tc.tttimer = null; }, tc.tooltipdelay); } }; tcproto.tooltipdiv = function(active,tag) { if(active && tag && tag.title) { this.setttdiv(tag.title, tag); } else if(!active && this.mx != -1 && this.my != -1 && this.ctitle.length) { this.setttdiv(this.ctitle); } else { this.ttdiv.style.display = 'none'; } }; tcproto.transform = function(tc, p, y) { if(p || y) { var sp = sin(p), cp = cos(p), sy = sin(y), cy = cos(y), ym = new matrix([cy,0,sy, 0,1,0, -sy,0,cy]), pm = new matrix([1,0,0, 0,cp,-sp, 0,sp,cp]); tc.transform = tc.transform.mul(ym.mul(pm)); } }; tcproto.animatefixed = function() { var fa, t1, angle, m, d; if(this.fadein) { t1 = timenow() - this.starttime; if(t1 >= this.fadein) { this.fadein = 0; this.fixedalpha = 1; } else { this.fixedalpha = t1 / this.fadein; } } if(this.fixedanim) { if(!this.fixedanim.transform) this.fixedanim.transform = this.transform; fa = this.fixedanim, t1 = timenow() - fa.t0, angle = fa.angle, m, d = this.animtiming(fa.t, t1); this.transform = fa.transform; if(t1 >= fa.t) { this.fixedcallbacktag = fa.tag; this.fixedcallback = fa.cb; this.fixedanim = this.yaw = this.pitch = 0; } else { angle *= d; } m = matrix.rotation(angle, fa.axis); this.transform = this.transform.mul(m); return (this.fixedanim != 0); } return false; }; tcproto.animateposition = function(w, h, t) { var tc = this, x = tc.mx, y = tc.my, s, r; if(!tc.frozen && x >= 0 && y >= 0 && x < w && y < h) { s = tc.maxspeed, r = tc.reverse ? -1 : 1; tc.lx || (tc.yaw = ((x * 2 * s / w) - s) * r * t); tc.ly || (tc.pitch = ((y * 2 * s / h) - s) * -r * t); tc.initial = null; } else if(!tc.initial) { if(tc.frozen && !tc.freezedecel) tc.yaw = tc.pitch = 0; else tc.decel(tc); } this.transform(tc, tc.pitch, tc.yaw); }; tcproto.animatedrag = function(w, h, t) { var tc = this, rs = 100 * t * tc.maxspeed / tc.max_radius / tc.zoom; if(tc.dx || tc.dy) { tc.lx || (tc.yaw = tc.dx * rs / tc.stretchx); tc.ly || (tc.pitch = tc.dy * -rs / tc.stretchy); tc.dx = tc.dy = 0; tc.initial = null; } else if(!tc.initial) { tc.decel(tc); } this.transform(tc, tc.pitch, tc.yaw); }; tcproto.freeze = function() { if(!this.frozen) { this.prefreeze = [this.yaw, this.pitch]; this.frozen = 1; this.drawn = 0; } }; tcproto.unfreeze = function() { if(this.frozen) { this.yaw = this.prefreeze[0]; this.pitch = this.prefreeze[1]; this.frozen = 0; } }; tcproto.decel = function(tc) { var s = tc.minspeed, ay = abs(tc.yaw), ap = abs(tc.pitch); if(!tc.lx && ay > s) tc.yaw = ay > tc.z0 ? tc.yaw * tc.decel : 0; if(!tc.ly && ap > s) tc.pitch = ap > tc.z0 ? tc.pitch * tc.decel : 0; }; tcproto.zoom = function(r) { this.z2 = this.z1 * (1/r); this.drawn = 0; }; tcproto.clicked = function(e) { var a = this.active; try { if(a && a.tag) if(this.clicktofront === false || this.clicktofront === null) a.tag.clicked(e); else this.tagtofront(a.tag, this.clicktofront, function() { a.tag.clicked(e); }, true); } catch(ex) { } }; tcproto.wheel = function(i) { var z = this.zoom + this.zoomstep * (i ? 1 : -1); this.zoom = min(this.zoommax,max(this.zoommin,z)); this.zoom(this.zoom); }; tcproto.begindrag = function(e) { this.down = eventxy(e, this.canvas); e.cancelbubble = true; e.returnvalue = false; e.preventdefault && e.preventdefault(); }; tcproto.drag = function(e, p) { if(this.dragcontrol && this.down) { var t2 = this.dragthreshold * this.dragthreshold, dx = p.x - this.down.x, dy = p.y - this.down.y; if(this.dragging || dx * dx + dy * dy > t2) { this.dx = dx; this.dy = dy; this.dragging = 1; this.down = p; } } return this.dragging; }; tcproto.enddrag = function() { var res = this.dragging; this.dragging = this.down = null; return res; }; function pinchdistance(e) { var t1 = e.targettouches[0], t2 = e.targettouches[1]; return sqrt(pow(t2.pagex - t1.pagex, 2) + pow(t2.pagey - t1.pagey, 2)); } tcproto.beginpinch = function(e) { this.pinched = [pinchdistance(e), this.zoom]; e.preventdefault && e.preventdefault(); }; tcproto.pinch = function(e) { var z, d, p = this.pinched; if(!p) return; d = pinchdistance(e); z = p[1] * d / p[0]; this.zoom = min(this.zoommax,max(this.zoommin,z)); this.zoom(this.zoom); }; tcproto.endpinch = function(e) { this.pinched = null; }; tcproto.pause = function() { this.paused = true; }; tcproto.resume = function() { this.paused = false; }; tcproto.setspeed = function(i) { this.initial = i; this.yaw = i[0] * this.maxspeed; this.pitch = i[1] * this.maxspeed; }; tcproto.findtag = function(t) { if(!defined(t)) return null; defined(t.index) && (t = t.index); if(!isobject(t)) return this.taglist[t]; var srch, tgt, i; if(defined(t.id)) srch = 'id', tgt = t.id; else if(defined(t.text)) srch = 'innertext', tgt = t.text; for(i = 0; i < this.taglist.length; ++i) if(this.taglist[i].a[srch] == tgt) return this.taglist[i]; }; tcproto.rotatetag = function(tag, lt, lg, time, callback, active) { var t = tag.calc(this.transform, 1), v1 = new vector(t.x, t.y, t.z), v2 = makevector(lg, lt), angle = v1.angle(v2), u = v1.cross(v2).unit(); if(angle == 0) { this.fixedcallbacktag = tag; this.fixedcallback = callback; } else { this.fixedanim = { angle: -angle, axis: u, t: time, t0: timenow(), cb: callback, tag: tag, active: active }; } }; tcproto.tagtofront = function(tag, time, callback, active) { this.rotatetag(tag, 0, 0, time, callback, active); }; tagcanvas.start = function(id,l,o) { tagcanvas.delete(id); tagcanvas.tc[id] = new tagcanvas(id,l,o); }; function tccall(f,id) { tagcanvas.tc[id] && tagcanvas.tc[id][f](); } tagcanvas.linear = function(t, t0) { return t0 / t; } tagcanvas.smooth = function(t, t0) { return 0.5 - cos(t0 * math.pi / t) / 2; } tagcanvas.pause = function(id) { tccall('pause',id); }; tagcanvas.resume = function(id) { tccall('resume',id); }; tagcanvas.reload = function(id) { tccall('load',id); }; tagcanvas.update = function(id) { tccall('update',id); }; tagcanvas.setspeed = function(id, speed) { if(isobject(speed) && tagcanvas.tc[id] && !isnan(speed[0]) && !isnan(speed[1])) { tagcanvas.tc[id].setspeed(speed); return true; } return false; }; tagcanvas.tagtofront = function(id, options) { if(!isobject(options)) return false; options.lat = options.lng = 0; return tagcanvas.rotatetag(id, options); }; tagcanvas.rotatetag = function(id, options) { if(isobject(options) && tagcanvas.tc[id]) { if(isnan(options.time)) options.time = 500; var tt = tagcanvas.tc[id].findtag(options); if(tt) { tagcanvas.tc[id].rotatetag(tt, options.lat, options.lng, options.time, options.callback, options.active); return true; } } return false; }; tagcanvas.delete = function(id) { var i, c; if(handlers[id]) { c = doc.getelementbyid(id); if(c) { for(i = 0; i < handlers[id].length; ++i) removehandler(handlers[id][i][0], handlers[id][i][1], c); } } delete handlers[id]; delete tagcanvas.tc[id]; }; tagcanvas.nextframeraf = function() { requestanimationframe(drawcanvasraf); }; tagcanvas.nextframetimeout = function(iv) { settimeout(drawcanvas, iv); }; tagcanvas.tc = {}; tagcanvas.options = { z1: 20000, z2: 20000, z0: 0.0002, freezeactive: true, freezedecel: false, activecursor: 'pointer', pulsateto: 1, pulsatetime: 3, reverse: false, depth: 0.5, maxspeed: 0.05, minspeed: 0, decel: 0.95, interval: 20, minbrightness: 0.1, maxbrightness: 1, outlinecolour: '', outlinethickness: 2, outlineoffset: 5, outlinemethod: 'outline', outlineradius: 0, textcolour: ['#222', '#000'], textheight: 15, textfont: 'helvetica, arial, sans-serif', shadow: '#111', shadowblur: 1, shadowoffset: [0.1,0.1], initial: null, hidetags: false, zoom: 0, weight: false, weightmode: 'size', weightfrom: null, weightsize: 1, weightsizemin: null, weightsizemax: null, weightgradient: {0:'#f00', 0.33:'#ff0', 0.66:'#0f0', 1:'#00f'}, txtopt: true, txtscale: 2, frontselect: false, wheelzoom: true, zoommin: 0.8, zoommax: 0.8, zoomstep: 0.05, shape: 'sphere', lock: null, tooltip: null, tooltipdelay: 300, tooltipclass: 'tctooltip', radiusx: 1, radiusy: 1, radiusz: 1, stretchx: 1, stretchy: 1, offsetx: 0, offsety: 0, shuffletags: false, noselect: false, nomouse: false, imagescale: 1, paused: false, dragcontrol: false, dragthreshold: 4, centrefunc: nop, splitwidth: 0, animtiming: 'smooth', clicktofront: false, fadein: 0, padding: 0, bgcolour: null, bgradius: 0, bgoutline: null, bgoutlinethickness: 0, outlineincrease: 4, textalign: 'centre', textvalign: 'middle', imagemode: null, imageposition: null, imagepadding: 2, imagealign: 'centre', imagevalign: 'middle', notagsmessage: true, centreimage: null, pinchzoom: false, repeattags: 0, mintags: 0, imageradius: 0, scrollpause: false, outlinedash: 0, outlinedashspace: 0, outlinedashspeed: 1 }; for(i in tagcanvas.options) tagcanvas[i] = tagcanvas.options[i]; window.tagcanvas = tagcanvas; // set a flag for when the window has loaded addhandler('load',function(){tagcanvas.loaded=1},window); })();