raphael.js 240 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320
  1. // ┌─────────────────────────────────────────────────────────────────────┐ \\
  2. // │ Raphaël 2 - JavaScript Vector Library │ \\
  3. // ├─────────────────────────────────────────────────────────────────────┤ \\
  4. // │ Copyright (c) 2008-2011 Dmitry Baranovskiy (http://raphaeljs.com) │ \\
  5. // │ Copyright (c) 2008-2011 Sencha Labs (http://sencha.com) │ \\
  6. // │ Licensed under the MIT (http://raphaeljs.com/license.html) license. │ \\
  7. // └─────────────────────────────────────────────────────────────────────┘ \\
  8. (function () {
  9. /*\
  10. * Raphael
  11. [ method ]
  12. **
  13. * Creates a canvas object on which to draw.
  14. * You must do this first, as all future calls to drawing methods
  15. * from this instance will be bound to this canvas.
  16. > Parameters
  17. **
  18. - container (HTMLElement|string) DOM element or its ID which is going to be a parent for drawing surface
  19. - width (number)
  20. - height (number)
  21. - callback (function) #optional callback function which is going to be executed in the context of newly created paper
  22. * or
  23. - x (number)
  24. - y (number)
  25. - width (number)
  26. - height (number)
  27. - callback (function) #optional callback function which is going to be executed in the context of newly created paper
  28. * or
  29. - all (array) (first 3 or 4 elements in the array are equal to [containerID, width, height] or [x, y, width, height]. The rest are element descriptions in format {type: type, <attributes>})
  30. - callback (function) #optional callback function which is going to be executed in the context of newly created paper
  31. * or
  32. - onReadyCallback (function) function that is going to be called on DOM ready event. You can also subscribe to this event via Eve’s “DOMLoad” event. In this case method returns `undefined`.
  33. = (object) @Paper
  34. > Usage
  35. | // Each of the following examples create a canvas
  36. | // that is 320px wide by 200px high.
  37. | // Canvas is created at the viewport’s 10,50 coordinate.
  38. | var paper = Raphael(10, 50, 320, 200);
  39. | // Canvas is created at the top left corner of the #notepad element
  40. | // (or its top right corner in dir="rtl" elements)
  41. | var paper = Raphael(document.getElementById("notepad"), 320, 200);
  42. | // Same as above
  43. | var paper = Raphael("notepad", 320, 200);
  44. | // Image dump
  45. | var set = Raphael(["notepad", 320, 200, {
  46. | type: "rect",
  47. | x: 10,
  48. | y: 10,
  49. | width: 25,
  50. | height: 25,
  51. | stroke: "#f00"
  52. | }, {
  53. | type: "text",
  54. | x: 30,
  55. | y: 40,
  56. | text: "Dump"
  57. | }]);
  58. \*/
  59. function R(first) {
  60. if (R.is(first, "function")) {
  61. return loaded ? first() : eve.on("DOMload", first);
  62. } else if (R.is(first, array)) {
  63. var a = first,
  64. cnv = create[apply](R, a.splice(0, 3 + R.is(a[0], nu))),
  65. res = cnv.set(),
  66. i = 0,
  67. ii = a.length,
  68. j;
  69. for (; i < ii; i++) {
  70. j = a[i] || {};
  71. elements[has](j.type) && res.push(cnv[j.type]().attr(j));
  72. }
  73. return res;
  74. } else {
  75. var args = Array.prototype.slice.call(arguments, 0);
  76. if (R.is(args[args.length - 1], "function")) {
  77. var f = args.pop();
  78. return loaded ? f.call(create[apply](R, args)) : eve.on("DOMload", function () {
  79. f.call(create[apply](R, args));
  80. });
  81. } else {
  82. return create[apply](R, arguments);
  83. }
  84. }
  85. }
  86. R.version = "2.0.0";
  87. var loaded,
  88. separator = /[, ]+/,
  89. elements = {circle: 1, rect: 1, path: 1, ellipse: 1, text: 1, image: 1},
  90. formatrg = /\{(\d+)\}/g,
  91. proto = "prototype",
  92. has = "hasOwnProperty",
  93. g = {
  94. doc: document,
  95. win: window
  96. },
  97. oldRaphael = {
  98. was: Object.prototype[has].call(g.win, "Raphael"),
  99. is: g.win.Raphael
  100. },
  101. Paper = function () {},
  102. paperproto,
  103. appendChild = "appendChild",
  104. apply = "apply",
  105. concat = "concat",
  106. supportsTouch = "createTouch" in g.doc,
  107. E = "",
  108. S = " ",
  109. Str = String,
  110. split = "split",
  111. events = "click dblclick mousedown mousemove mouseout mouseover mouseup touchstart touchmove touchend orientationchange touchcancel gesturestart gesturechange gestureend".split(S),
  112. touchMap = {
  113. mousedown: "touchstart",
  114. mousemove: "touchmove",
  115. mouseup: "touchend"
  116. },
  117. lowerCase = Str.prototype.toLowerCase,
  118. math = Math,
  119. mmax = math.max,
  120. mmin = math.min,
  121. abs = math.abs,
  122. pow = math.pow,
  123. PI = math.PI,
  124. nu = "number",
  125. string = "string",
  126. array = "array",
  127. toString = "toString",
  128. fillString = "fill",
  129. objectToString = Object.prototype.toString,
  130. paper = {},
  131. push = "push",
  132. ISURL = /^url\(['"]?([^\)]+?)['"]?\)$/i,
  133. colourRegExp = /^\s*((#[a-f\d]{6})|(#[a-f\d]{3})|rgba?\(\s*([\d\.]+%?\s*,\s*[\d\.]+%?\s*,\s*[\d\.]+(?:%?\s*,\s*[\d\.]+)?)%?\s*\)|hsba?\(\s*([\d\.]+(?:deg|\xb0|%)?\s*,\s*[\d\.]+%?\s*,\s*[\d\.]+(?:%?\s*,\s*[\d\.]+)?)%?\s*\)|hsla?\(\s*([\d\.]+(?:deg|\xb0|%)?\s*,\s*[\d\.]+%?\s*,\s*[\d\.]+(?:%?\s*,\s*[\d\.]+)?)%?\s*\))\s*$/i,
  134. isnan = {"NaN": 1, "Infinity": 1, "-Infinity": 1},
  135. bezierrg = /^(?:cubic-)?bezier\(([^,]+),([^,]+),([^,]+),([^\)]+)\)/,
  136. round = math.round,
  137. setAttribute = "setAttribute",
  138. toFloat = parseFloat,
  139. toInt = parseInt,
  140. ms = " progid:DXImageTransform.Microsoft",
  141. upperCase = Str.prototype.toUpperCase,
  142. availableAttrs = {"arrow-end": "none", "arrow-start": "none", blur: 0, "clip-rect": "0 0 1e9 1e9", cursor: "default", cx: 0, cy: 0, fill: "#fff", "fill-opacity": 1, font: '10px "Arial"', "font-family": '"Arial"', "font-size": "10", "font-style": "normal", "font-weight": 400, gradient: 0, height: 0, href: "http://raphaeljs.com/", opacity: 1, path: "M0,0", r: 0, rx: 0, ry: 0, src: "", stroke: "#000", "stroke-dasharray": "", "stroke-linecap": "butt", "stroke-linejoin": "butt", "stroke-miterlimit": 0, "stroke-opacity": 1, "stroke-width": 1, target: "_blank", "text-anchor": "middle", title: "Raphael", transform: "", width: 0, x: 0, y: 0},
  143. availableAnimAttrs = {blur: nu, "clip-rect": "csv", cx: nu, cy: nu, fill: "colour", "fill-opacity": nu, "font-size": nu, height: nu, opacity: nu, path: "path", r: nu, rx: nu, ry: nu, stroke: "colour", "stroke-opacity": nu, "stroke-width": nu, transform: "transform", width: nu, x: nu, y: nu},
  144. commaSpaces = /\s*,\s*/,
  145. hsrg = {hs: 1, rg: 1},
  146. p2s = /,?([achlmqrstvxz]),?/gi,
  147. pathCommand = /([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[\-+]?\d+)?\s*,?\s*)+)/ig,
  148. tCommand = /([rstm])[\s,]*((-?\d*\.?\d*(?:e[\-+]?\d+)?\s*,?\s*)+)/ig,
  149. pathValues = /(-?\d*\.?\d*(?:e[\-+]?\d+)?)\s*,?\s*/ig,
  150. radial_gradient = /^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/,
  151. sortByKey = function (a, b) {
  152. return a.key - b.key;
  153. },
  154. sortByNumber = function (a, b) {
  155. return a - b;
  156. },
  157. fun = function () {},
  158. pipe = function (x) {
  159. return x;
  160. },
  161. rectPath = function (x, y, w, h, r) {
  162. if (r) {
  163. return [["M", x + r, y], ["l", w - r * 2, 0], ["a", r, r, 0, 0, 1, r, r], ["l", 0, h - r * 2], ["a", r, r, 0, 0, 1, -r, r], ["l", r * 2 - w, 0], ["a", r, r, 0, 0, 1, -r, -r], ["l", 0, r * 2 - h], ["a", r, r, 0, 0, 1, r, -r], ["z"]];
  164. }
  165. return [["M", x, y], ["l", w, 0], ["l", 0, h], ["l", -w, 0], ["z"]];
  166. },
  167. ellipsePath = function (x, y, rx, ry) {
  168. if (ry == null) {
  169. ry = rx;
  170. }
  171. return [["M", x, y], ["m", 0, -ry], ["a", rx, ry, 0, 1, 1, 0, 2 * ry], ["a", rx, ry, 0, 1, 1, 0, -2 * ry], ["z"]];
  172. },
  173. getPath = {
  174. path: function (el) {
  175. return el.attr("path");
  176. },
  177. circle: function (el) {
  178. var a = el.attrs;
  179. return ellipsePath(a.cx, a.cy, a.r);
  180. },
  181. ellipse: function (el) {
  182. var a = el.attrs;
  183. return ellipsePath(a.cx, a.cy, a.rx, a.ry);
  184. },
  185. rect: function (el) {
  186. var a = el.attrs;
  187. return rectPath(a.x, a.y, a.width, a.height, a.r);
  188. },
  189. image: function (el) {
  190. var a = el.attrs;
  191. return rectPath(a.x, a.y, a.width, a.height);
  192. },
  193. text: function (el) {
  194. var bbox = el._getBBox();
  195. return rectPath(bbox.x, bbox.y, bbox.width, bbox.height);
  196. }
  197. },
  198. mapPath = function (path, matrix) {
  199. if (!matrix) {
  200. return path;
  201. }
  202. var x, y, i, j, pathi;
  203. path = path2curve(path);
  204. for (i = 0, ii = path.length; i < ii; i++) {
  205. pathi = path[i];
  206. for (j = 1, jj = pathi.length; j < jj; j += 2) {
  207. x = matrix.x(pathi[j], pathi[j + 1]);
  208. y = matrix.y(pathi[j], pathi[j + 1]);
  209. pathi[j] = x;
  210. pathi[j + 1] = y;
  211. }
  212. }
  213. return path;
  214. };
  215. /*\
  216. * Raphael.type
  217. [ property (string) ]
  218. **
  219. * Can be “SVG”, “VML” or empty, depending on browser support.
  220. \*/
  221. R.type = (g.win.SVGAngle || g.doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML");
  222. if (R.type == "VML") {
  223. var d = g.doc.createElement("div"),
  224. b;
  225. d.innerHTML = '<v:shape adj="1"/>';
  226. b = d.firstChild;
  227. b.style.behavior = "url(#default#VML)";
  228. if (!(b && typeof b.adj == "object")) {
  229. return (R.type = E);
  230. }
  231. d = null;
  232. }
  233. /*\
  234. * Raphael.svg
  235. [ property (boolean) ]
  236. **
  237. * `true` if browser supports SVG.
  238. \*/
  239. /*\
  240. * Raphael.vml
  241. [ property (boolean) ]
  242. **
  243. * `true` if browser supports VML.
  244. \*/
  245. R.svg = !(R.vml = R.type == "VML");
  246. paperproto = Paper.prototype = R.prototype;
  247. /*\
  248. * Paper.customAttributes
  249. [ property (object) ]
  250. **
  251. * If you have a set of attributes that you would like to represent
  252. * as a function of some number you can do it easily with custom attributes:
  253. > Usage
  254. | paper.customAttributes.hue = function (num) {
  255. | num = num % 1;
  256. | return {fill: "hsb(" + num + ", .75, 1)"};
  257. | };
  258. | // Custom attribute “hue” will change fill
  259. | // to be given hue with fixed saturation and brightness.
  260. | // Now you can use it like this:
  261. | var c = paper.circle(10, 10, 10).attr({hue: .45});
  262. | // or even like this:
  263. | c.animate({hue: 1}, 1e3);
  264. |
  265. | // You could also create custom attribute
  266. | // with multiple parameters:
  267. | paper.customAttributes.hsb = function (h, s, b) {
  268. | return {fill: "hsb(" + [h, s, b].join(",") + ")"};
  269. | };
  270. | c.attr({hsb: ".5 .8 1"});
  271. | c.animate({hsb: "1 0 .5"}, 1e3);
  272. \*/
  273. paperproto.customAttributes = {};
  274. R._id = 0;
  275. R._oid = 0;
  276. /*\
  277. * Raphael.fn
  278. [ property (object) ]
  279. **
  280. * You can add your own method to the canvas. For example if you want to draw a pie chart,
  281. * you can create your own pie chart function and ship it as a Raphaël plugin. To do this
  282. * you need to extend the `Raphael.fn` object. Please note that you can create your own namespaces
  283. * inside the `fn` object — methods will be run in the context of canvas anyway. You should alter
  284. * the `fn` object before a Raphaël instance is created, otherwise it will take no effect.
  285. > Usage
  286. | Raphael.fn.arrow = function (x1, y1, x2, y2, size) {
  287. | return this.path( ... );
  288. | };
  289. | // or create namespace
  290. | Raphael.fn.mystuff = {
  291. | arrow: function () {…},
  292. | star: function () {…},
  293. | // etc…
  294. | };
  295. | var paper = Raphael(10, 10, 630, 480);
  296. | // then use it
  297. | paper.arrow(10, 10, 30, 30, 5).attr({fill: "#f00"});
  298. | paper.mystuff.arrow();
  299. | paper.mystuff.star();
  300. \*/
  301. R.fn = {};
  302. /*\
  303. * Raphael.is
  304. [ method ]
  305. **
  306. * Handfull replacement for `typeof` operator.
  307. > Parameters
  308. - o (…) any object or primitive
  309. - type (string) name of the type, i.e. “string”, “function”, “number”, etc.
  310. = (boolean) is given value is of given type
  311. \*/
  312. R.is = function (o, type) {
  313. type = lowerCase.call(type);
  314. if (type == "finite") {
  315. return !isnan[has](+o);
  316. }
  317. return (type == "null" && o === null) ||
  318. (type == typeof o) ||
  319. (type == "object" && o === Object(o)) ||
  320. (type == "array" && Array.isArray && Array.isArray(o)) ||
  321. objectToString.call(o).slice(8, -1).toLowerCase() == type;
  322. };
  323. /*\
  324. * Raphael.angle
  325. [ method ]
  326. **
  327. * Returns angle between two or three points
  328. > Parameters
  329. - x1 (number) x coord of first point
  330. - y1 (number) y coord of first point
  331. - x2 (number) x coord of second point
  332. - y2 (number) y coord of second point
  333. - x3 (number) #optional x coord of third point
  334. - y3 (number) #optional y coord of third point
  335. = (number) angle in degrees.
  336. \*/
  337. R.angle = function (x1, y1, x2, y2, x3, y3) {
  338. if (x3 == null) {
  339. var x = x1 - x2,
  340. y = y1 - y2;
  341. if (!x && !y) {
  342. return 0;
  343. }
  344. return (180 + math.atan2(-y, -x) * 180 / PI + 360) % 360;
  345. } else {
  346. return R.angle(x1, y1, x3, y3) - R.angle(x2, y2, x3, y3);
  347. }
  348. };
  349. /*\
  350. * Raphael.rad
  351. [ method ]
  352. **
  353. * Transform angle to radians
  354. > Parameters
  355. - deg (number) angle in degrees
  356. = (number) angle in radians.
  357. \*/
  358. R.rad = function (deg) {
  359. return deg % 360 * PI / 180;
  360. };
  361. /*\
  362. * Raphael.deg
  363. [ method ]
  364. **
  365. * Transform angle to degrees
  366. > Parameters
  367. - deg (number) angle in radians
  368. = (number) angle in degrees.
  369. \*/
  370. R.deg = function (rad) {
  371. return rad * 180 / PI % 360;
  372. };
  373. /*\
  374. * Raphael.snapTo
  375. [ method ]
  376. **
  377. * Snaps given value to given grid.
  378. > Parameters
  379. - values (array|number) given array of values or step of the grid
  380. - value (number) value to adjust
  381. - tolerance (number) #optional tolerance for snapping. Default is `10`.
  382. = (number) adjusted value.
  383. \*/
  384. R.snapTo = function (values, value, tolerance) {
  385. tolerance = R.is(tolerance, "finite") ? tolerance : 10;
  386. if (R.is(values, array)) {
  387. var i = values.length;
  388. while (i--) if (abs(values[i] - value) <= tolerance) {
  389. return values[i];
  390. }
  391. } else {
  392. values = +values;
  393. var rem = value % values;
  394. if (rem < tolerance) {
  395. return value - rem;
  396. }
  397. if (rem > values - tolerance) {
  398. return value - rem + values;
  399. }
  400. }
  401. return value;
  402. };
  403. var createUUID = (function (uuidRegEx, uuidReplacer) {
  404. return function () {
  405. return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(uuidRegEx, uuidReplacer).toUpperCase();
  406. };
  407. })(/[xy]/g, function (c) {
  408. var r = math.random() * 16 | 0,
  409. v = c == "x" ? r : (r & 3 | 8);
  410. return v.toString(16);
  411. });
  412. /*\
  413. * Raphael.setWindow
  414. [ method ]
  415. **
  416. * Used when you need to draw in `&lt;iframe>`. Switched window to the iframe one.
  417. > Parameters
  418. - newwin (window) new window object
  419. \*/
  420. R.setWindow = function (newwin) {
  421. eve("setWindow", R, g.win, newwin);
  422. g.win = newwin;
  423. g.doc = g.win.document;
  424. if (initWin) {
  425. initWin(g.win);
  426. }
  427. };
  428. // colour utilities
  429. var toHex = function (color) {
  430. if (R.vml) {
  431. // http://dean.edwards.name/weblog/2009/10/convert-any-colour-value-to-hex-in-msie/
  432. var trim = /^\s+|\s+$/g;
  433. var bod;
  434. try {
  435. var docum = new ActiveXObject("htmlfile");
  436. docum.write("<body>");
  437. docum.close();
  438. bod = docum.body;
  439. } catch(e) {
  440. bod = createPopup().document.body;
  441. }
  442. var range = bod.createTextRange();
  443. toHex = cacher(function (color) {
  444. try {
  445. bod.style.color = Str(color).replace(trim, E);
  446. var value = range.queryCommandValue("ForeColor");
  447. value = ((value & 255) << 16) | (value & 65280) | ((value & 16711680) >>> 16);
  448. return "#" + ("000000" + value.toString(16)).slice(-6);
  449. } catch(e) {
  450. return "none";
  451. }
  452. });
  453. } else {
  454. var i = g.doc.createElement("i");
  455. i.title = "Rapha\xebl Colour Picker";
  456. i.style.display = "none";
  457. g.doc.body.appendChild(i);
  458. toHex = cacher(function (color) {
  459. i.style.color = color;
  460. return g.doc.defaultView.getComputedStyle(i, E).getPropertyValue("color");
  461. });
  462. }
  463. return toHex(color);
  464. },
  465. hsbtoString = function () {
  466. return "hsb(" + [this.h, this.s, this.b] + ")";
  467. },
  468. hsltoString = function () {
  469. return "hsl(" + [this.h, this.s, this.l] + ")";
  470. },
  471. rgbtoString = function () {
  472. return this.hex;
  473. },
  474. prepareRGB = function (r, g, b) {
  475. if (g == null && R.is(r, "object") && "r" in r && "g" in r && "b" in r) {
  476. b = r.b;
  477. g = r.g;
  478. r = r.r;
  479. }
  480. if (g == null && R.is(r, string)) {
  481. var clr = R.getRGB(r);
  482. r = clr.r;
  483. g = clr.g;
  484. b = clr.b;
  485. }
  486. if (r > 1 || g > 1 || b > 1) {
  487. r /= 255;
  488. g /= 255;
  489. b /= 255;
  490. }
  491. return [r, g, b];
  492. },
  493. packageRGB = function (r, g, b, o) {
  494. r *= 255;
  495. g *= 255;
  496. b *= 255;
  497. var rgb = {
  498. r: r,
  499. g: g,
  500. b: b,
  501. hex: R.rgb(r, g, b),
  502. toString: rgbtoString
  503. };
  504. R.is(o, "finite") && (rgb.opacity = o);
  505. return rgb;
  506. };
  507. /*\
  508. * Raphael.color
  509. [ method ]
  510. **
  511. * Parses the color string and returns object with all values for the given color.
  512. > Parameters
  513. - clr (string) color string in one of the supported formasts (see @Raphael.getRGB)
  514. = (object) Combined RGB & HSB object in format:
  515. o {
  516. o r (number) red,
  517. o g (number) green,
  518. o b (number) blue,
  519. o hex (string) color in HTML/CSS format: #••••••,
  520. o error (boolean) `true` if string can’t be parsed,
  521. o h (number) hue,
  522. o s (number) saturation,
  523. o v (number) value (brightness),
  524. o l (number) lightness
  525. o }
  526. \*/
  527. R.color = function (clr) {
  528. var rgb;
  529. if (R.is(clr, "object") && "h" in clr && "s" in clr && "b" in clr) {
  530. rgb = R.hsb2rgb(clr);
  531. clr.r = rgb.r;
  532. clr.g = rgb.g;
  533. clr.b = rgb.b;
  534. clr.hex = rgb.hex;
  535. } else if (R.is(clr, "object") && "h" in clr && "s" in clr && "l" in clr) {
  536. rgb = R.hsl2rgb(clr);
  537. clr.r = rgb.r;
  538. clr.g = rgb.g;
  539. clr.b = rgb.b;
  540. clr.hex = rgb.hex;
  541. } else {
  542. if (R.is(clr, "string")) {
  543. clr = R.getRGB(clr);
  544. }
  545. if (R.is(clr, "object") && "r" in clr && "g" in clr && "b" in clr) {
  546. rgb = R.rgb2hsl(clr);
  547. clr.h = rgb.h;
  548. clr.s = rgb.s;
  549. clr.l = rgb.l;
  550. rgb = R.rgb2hsb(clr);
  551. clr.v = rgb.b;
  552. } else {
  553. clr = {hex: "none"};
  554. crl.r = clr.g = clr.b = clr.h = clr.s = clr.v = clr.l = -1;
  555. }
  556. }
  557. clr.toString = rgbtoString;
  558. return clr;
  559. };
  560. /*\
  561. * Raphael.hsb2rgb
  562. [ method ]
  563. **
  564. * Converts HSB values to RGB object.
  565. > Parameters
  566. - h (number) hue
  567. - s (number) saturation
  568. - v (number) value or brightness
  569. = (object) RGB object in format:
  570. o {
  571. o r (number) red,
  572. o g (number) green,
  573. o b (number) blue,
  574. o hex (string) color in HTML/CSS format: #••••••
  575. o }
  576. \*/
  577. R.hsb2rgb = function (h, s, v, o) {
  578. if (this.is(h, "object") && "h" in h && "s" in h && "b" in h) {
  579. v = h.b;
  580. s = h.s;
  581. h = h.h;
  582. o = h.o;
  583. }
  584. h *= 360;
  585. var R, G, B, X, C;
  586. h = (h % 360) / 60;
  587. C = v * s;
  588. X = C * (1 - abs(h % 2 - 1));
  589. R = G = B = v - C;
  590. h = ~~h;
  591. R += [C, X, 0, 0, X, C][h];
  592. G += [X, C, C, X, 0, 0][h];
  593. B += [0, 0, X, C, C, X][h];
  594. return packageRGB(R, G, B, o);
  595. };
  596. /*\
  597. * Raphael.hsl2rgb
  598. [ method ]
  599. **
  600. * Converts HSL values to RGB object.
  601. > Parameters
  602. - h (number) hue
  603. - s (number) saturation
  604. - l (number) luminosity
  605. = (object) RGB object in format:
  606. o {
  607. o r (number) red,
  608. o g (number) green,
  609. o b (number) blue,
  610. o hex (string) color in HTML/CSS format: #••••••
  611. o }
  612. \*/
  613. R.hsl2rgb = function (h, s, l, o) {
  614. if (this.is(h, "object") && "h" in h && "s" in h && "l" in h) {
  615. l = h.l;
  616. s = h.s;
  617. h = h.h;
  618. }
  619. if (h > 1 || s > 1 || l > 1) {
  620. h /= 360;
  621. s /= 100;
  622. l /= 100;
  623. }
  624. h *= 360;
  625. var R, G, B, X, C;
  626. h = (h % 360) / 60;
  627. C = 2 * s * (l < .5 ? l : 1 - l);
  628. X = C * (1 - abs(h % 2 - 1));
  629. R = G = B = l - C / 2;
  630. h = ~~h;
  631. R += [C, X, 0, 0, X, C][h];
  632. G += [X, C, C, X, 0, 0][h];
  633. B += [0, 0, X, C, C, X][h];
  634. return packageRGB(R, G, B, o);
  635. };
  636. /*\
  637. * Raphael.rgb2hsb
  638. [ method ]
  639. **
  640. * Converts RGB values to HSB object.
  641. > Parameters
  642. - r (number) red
  643. - g (number) green
  644. - b (number) blue
  645. = (object) HSB object in format:
  646. o {
  647. o h (number) hue
  648. o s (number) saturation
  649. o b (number) brightness
  650. o }
  651. \*/
  652. R.rgb2hsb = function (r, g, b) {
  653. b = prepareRGB(r, g, b);
  654. r = b[0];
  655. g = b[1];
  656. b = b[2];
  657. var H, S, V, C;
  658. V = mmax(r, g, b);
  659. C = V - mmin(r, g, b);
  660. H = (C == 0 ? null :
  661. V == r ? (g - b) / C :
  662. V == g ? (b - r) / C + 2 :
  663. (r - g) / C + 4
  664. );
  665. H = ((H + 360) % 6) * 60 / 360;
  666. S = C == 0 ? 0 : C / V;
  667. return {h: H, s: S, b: V, toString: hsbtoString};
  668. };
  669. /*\
  670. * Raphael.rgb2hsl
  671. [ method ]
  672. **
  673. * Converts RGB values to HSL object.
  674. > Parameters
  675. - r (number) red
  676. - g (number) green
  677. - b (number) blue
  678. = (object) HSL object in format:
  679. o {
  680. o h (number) hue
  681. o s (number) saturation
  682. o l (number) luminosity
  683. o }
  684. \*/
  685. R.rgb2hsl = function (r, g, b) {
  686. b = prepareRGB(r, g, b);
  687. r = b[0];
  688. g = b[1];
  689. b = b[2];
  690. var H, S, L, M, m, C;
  691. M = mmax(r, g, b);
  692. m = mmin(r, g, b);
  693. C = M - m;
  694. H = (C == 0 ? null :
  695. M == r ? (g - b) / C :
  696. M == g ? (b - r) / C + 2 :
  697. (r - g) / C + 4);
  698. H = ((H + 360) % 6) * 60 / 360;
  699. L = (M + m) / 2;
  700. S = (C == 0 ? 0 :
  701. L < .5 ? C / (2 * L) :
  702. C / (2 - 2 * L));
  703. return {h: H, s: S, l: L, toString: hsltoString};
  704. };
  705. R._path2string = function () {
  706. return this.join(",").replace(p2s, "$1");
  707. };
  708. function repush(array, item) {
  709. for (var i = 0, ii = array.length; i < ii; i++) if (array[i] === item) {
  710. return array.push(array.splice(i, 1)[0]);
  711. }
  712. }
  713. function cacher(f, scope, postprocessor) {
  714. function newf() {
  715. var arg = Array.prototype.slice.call(arguments, 0),
  716. args = arg.join("\u2400"),
  717. cache = newf.cache = newf.cache || {},
  718. count = newf.count = newf.count || [];
  719. if (cache[has](args)) {
  720. repush(count, args);
  721. return postprocessor ? postprocessor(cache[args]) : cache[args];
  722. }
  723. count.length >= 1e3 && delete cache[count.shift()];
  724. count.push(args);
  725. cache[args] = f[apply](scope, arg);
  726. return postprocessor ? postprocessor(cache[args]) : cache[args];
  727. }
  728. return newf;
  729. }
  730. function preload(src, f) {
  731. var img = g.doc.createElement("img");
  732. img.style.cssText = "position:absolute;left:-9999em;top-9999em";
  733. img.onload = function () {
  734. f.call(this);
  735. this.onload = null;
  736. g.doc.body.removeChild(this);
  737. };
  738. img.onerror = function () {
  739. g.doc.body.removeChild(this);
  740. };
  741. g.doc.body.appendChild(img);
  742. img.src = src;
  743. }
  744. function clrToString() {
  745. return this.hex;
  746. }
  747. /*\
  748. * Raphael.getRGB
  749. [ method ]
  750. **
  751. * Parses colour string as RGB object
  752. > Parameters
  753. - colour (string) colour string in one of formats:
  754. # <ul>
  755. # <li>Colour name (“<code>red</code>”, “<code>green</code>”, “<code>cornflowerblue</code>”, etc)</li>
  756. # <li>#••• — shortened HTML colour: (“<code>#000</code>”, “<code>#fc0</code>”, etc)</li>
  757. # <li>#•••••• — full length HTML colour: (“<code>#000000</code>”, “<code>#bd2300</code>”)</li>
  758. # <li>rgb(•••, •••, •••) — red, green and blue channels’ values: (“<code>rgb(200,&nbsp;100,&nbsp;0)</code>”)</li>
  759. # <li>rgb(•••%, •••%, •••%) — same as above, but in %: (“<code>rgb(100%,&nbsp;175%,&nbsp;0%)</code>”)</li>
  760. # <li>hsb(•••, •••, •••) — hue, saturation and brightness values: (“<code>hsb(0.5,&nbsp;0.25,&nbsp;1)</code>”)</li>
  761. # <li>hsb(•••%, •••%, •••%) — same as above, but in %</li>
  762. # <li>hsl(•••, •••, •••) — same as hsb</li>
  763. # <li>hsl(•••%, •••%, •••%) — same as hsb</li>
  764. # </ul>
  765. = (object) RGB object in format:
  766. o {
  767. o r (number) red,
  768. o g (number) green,
  769. o b (number) blue
  770. o hex (string) color in HTML/CSS format: #••••••,
  771. o error (boolean) true if string can’t be parsed
  772. o }
  773. \*/
  774. R.getRGB = cacher(function (colour) {
  775. if (!colour || !!((colour = Str(colour)).indexOf("-") + 1)) {
  776. return {r: -1, g: -1, b: -1, hex: "none", error: 1, toString: clrToString};
  777. }
  778. if (colour == "none") {
  779. return {r: -1, g: -1, b: -1, hex: "none", toString: clrToString};
  780. }
  781. !(hsrg[has](colour.toLowerCase().substring(0, 2)) || colour.charAt() == "#") && (colour = toHex(colour));
  782. var res,
  783. red,
  784. green,
  785. blue,
  786. opacity,
  787. t,
  788. values,
  789. rgb = colour.match(colourRegExp);
  790. if (rgb) {
  791. if (rgb[2]) {
  792. blue = toInt(rgb[2].substring(5), 16);
  793. green = toInt(rgb[2].substring(3, 5), 16);
  794. red = toInt(rgb[2].substring(1, 3), 16);
  795. }
  796. if (rgb[3]) {
  797. blue = toInt((t = rgb[3].charAt(3)) + t, 16);
  798. green = toInt((t = rgb[3].charAt(2)) + t, 16);
  799. red = toInt((t = rgb[3].charAt(1)) + t, 16);
  800. }
  801. if (rgb[4]) {
  802. values = rgb[4].split(commaSpaces);
  803. red = toFloat(values[0]);
  804. values[0].slice(-1) == "%" && (red *= 2.55);
  805. green = toFloat(values[1]);
  806. values[1].slice(-1) == "%" && (green *= 2.55);
  807. blue = toFloat(values[2]);
  808. values[2].slice(-1) == "%" && (blue *= 2.55);
  809. rgb[1].toLowerCase().slice(0, 4) == "rgba" && (opacity = toFloat(values[3]));
  810. values[3] && values[3].slice(-1) == "%" && (opacity /= 100);
  811. }
  812. if (rgb[5]) {
  813. values = rgb[5].split(commaSpaces);
  814. red = toFloat(values[0]);
  815. values[0].slice(-1) == "%" && (red *= 2.55);
  816. green = toFloat(values[1]);
  817. values[1].slice(-1) == "%" && (green *= 2.55);
  818. blue = toFloat(values[2]);
  819. values[2].slice(-1) == "%" && (blue *= 2.55);
  820. (values[0].slice(-3) == "deg" || values[0].slice(-1) == "\xb0") && (red /= 360);
  821. rgb[1].toLowerCase().slice(0, 4) == "hsba" && (opacity = toFloat(values[3]));
  822. values[3] && values[3].slice(-1) == "%" && (opacity /= 100);
  823. return R.hsb2rgb(red, green, blue, opacity);
  824. }
  825. if (rgb[6]) {
  826. values = rgb[6].split(commaSpaces);
  827. red = toFloat(values[0]);
  828. values[0].slice(-1) == "%" && (red *= 2.55);
  829. green = toFloat(values[1]);
  830. values[1].slice(-1) == "%" && (green *= 2.55);
  831. blue = toFloat(values[2]);
  832. values[2].slice(-1) == "%" && (blue *= 2.55);
  833. (values[0].slice(-3) == "deg" || values[0].slice(-1) == "\xb0") && (red /= 360);
  834. rgb[1].toLowerCase().slice(0, 4) == "hsla" && (opacity = toFloat(values[3]));
  835. values[3] && values[3].slice(-1) == "%" && (opacity /= 100);
  836. return R.hsl2rgb(red, green, blue, opacity);
  837. }
  838. rgb = {r: red, g: green, b: blue, toString: clrToString};
  839. rgb.hex = "#" + (16777216 | blue | (green << 8) | (red << 16)).toString(16).slice(1);
  840. R.is(opacity, "finite") && (rgb.opacity = opacity);
  841. return rgb;
  842. }
  843. return {r: -1, g: -1, b: -1, hex: "none", error: 1, toString: clrToString};
  844. }, R);
  845. /*\
  846. * Raphael.hsb
  847. [ method ]
  848. **
  849. * Converts HSB values to hex representation of the colour.
  850. > Parameters
  851. - h (number) hue
  852. - s (number) saturation
  853. - b (number) value or brightness
  854. = (string) hex representation of the colour.
  855. \*/
  856. R.hsb = cacher(function (h, s, b) {
  857. return R.hsb2rgb(h, s, b).hex;
  858. });
  859. /*\
  860. * Raphael.hsl
  861. [ method ]
  862. **
  863. * Converts HSL values to hex representation of the colour.
  864. > Parameters
  865. - h (number) hue
  866. - s (number) saturation
  867. - l (number) luminosity
  868. = (string) hex representation of the colour.
  869. \*/
  870. R.hsl = cacher(function (h, s, l) {
  871. return R.hsl2rgb(h, s, l).hex;
  872. });
  873. /*\
  874. * Raphael.rgb
  875. [ method ]
  876. **
  877. * Converts RGB values to hex representation of the colour.
  878. > Parameters
  879. - r (number) red
  880. - g (number) green
  881. - b (number) blue
  882. = (string) hex representation of the colour.
  883. \*/
  884. R.rgb = cacher(function (r, g, b) {
  885. return "#" + (16777216 | b | (g << 8) | (r << 16)).toString(16).slice(1);
  886. });
  887. /*\
  888. * Raphael.getColor
  889. [ method ]
  890. **
  891. * On each call returns next colour in the spectrum. To reset it back to red call @Raphael.getColor.reset
  892. > Parameters
  893. - value (number) #optional brightness, default is `0.75`
  894. = (string) hex representation of the colour.
  895. \*/
  896. R.getColor = function (value) {
  897. var start = this.getColor.start = this.getColor.start || {h: 0, s: 1, b: value || .75},
  898. rgb = this.hsb2rgb(start.h, start.s, start.b);
  899. start.h += .075;
  900. if (start.h > 1) {
  901. start.h = 0;
  902. start.s -= .2;
  903. start.s <= 0 && (this.getColor.start = {h: 0, s: 1, b: start.b});
  904. }
  905. return rgb.hex;
  906. };
  907. /*\
  908. * Raphael.getColor.reset
  909. [ method ]
  910. **
  911. * Resets spectrum position for @Raphael.getColor back to red.
  912. \*/
  913. R.getColor.reset = function () {
  914. delete this.start;
  915. };
  916. /*\
  917. * Raphael.parsePathString
  918. [ method ]
  919. **
  920. * Utility method
  921. **
  922. * Parses given path string into an array of arrays of path segments.
  923. > Parameters
  924. - pathString (string|array) path string or array of segments (in the last case it will be returned straight away)
  925. = (array) array of segments.
  926. \*/
  927. R.parsePathString = cacher(function (pathString) {
  928. if (!pathString) {
  929. return null;
  930. }
  931. var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
  932. data = [];
  933. if (R.is(pathString, array) && R.is(pathString[0], array)) { // rough assumption
  934. data = pathClone(pathString);
  935. }
  936. if (!data.length) {
  937. Str(pathString).replace(pathCommand, function (a, b, c) {
  938. var params = [],
  939. name = lowerCase.call(b);
  940. c.replace(pathValues, function (a, b) {
  941. b && params.push(+b);
  942. });
  943. if (name == "m" && params.length > 2) {
  944. data.push([b][concat](params.splice(0, 2)));
  945. name = "l";
  946. b = b == "m" ? "l" : "L";
  947. }
  948. while (params.length >= paramCounts[name]) {
  949. data.push([b][concat](params.splice(0, paramCounts[name])));
  950. if (!paramCounts[name]) {
  951. break;
  952. }
  953. }
  954. });
  955. }
  956. data.toString = R._path2string;
  957. return data;
  958. });
  959. /*\
  960. * Raphael.parseTransformString
  961. [ method ]
  962. **
  963. * Utility method
  964. **
  965. * Parses given path string into an array of transformations.
  966. > Parameters
  967. - TString (string|array) transform string or array of transformations (in the last case it will be returned straight away)
  968. = (array) array of transformations.
  969. \*/
  970. R.parseTransformString = cacher(function (TString) {
  971. if (!TString) {
  972. return null;
  973. }
  974. var paramCounts = {r: 3, s: 4, t: 2, m: 6},
  975. data = [];
  976. if (R.is(TString, array) && R.is(TString[0], array)) { // rough assumption
  977. data = pathClone(TString);
  978. }
  979. if (!data.length) {
  980. Str(TString).replace(tCommand, function (a, b, c) {
  981. var params = [],
  982. name = lowerCase.call(b);
  983. c.replace(pathValues, function (a, b) {
  984. b && params.push(+b);
  985. });
  986. data.push([name][concat](params));
  987. });
  988. }
  989. data.toString = R._path2string;
  990. return data;
  991. });
  992. /*\
  993. * Raphael.findDotsAtSegment
  994. [ method ]
  995. **
  996. * Utility method
  997. **
  998. * Find dot coordinates on the given cubic bezier curve at the given t.
  999. > Parameters
  1000. - p1x (number) x of the first point of the curve
  1001. - p1y (number) y of the first point of the curve
  1002. - c1x (number) x of the first anchor of the curve
  1003. - c1y (number) y of the first anchor of the curve
  1004. - c2x (number) x of the second anchor of the curve
  1005. - c2y (number) y of the second anchor of the curve
  1006. - p2x (number) x of the second point of the curve
  1007. - p2y (number) y of the second point of the curve
  1008. - t (number) position on the curve (0..1)
  1009. = (object) point information in format:
  1010. o {
  1011. o x: (number) x coordinate of the point
  1012. o y: (number) y coordinate of the point
  1013. o m: {
  1014. o x: (number) x coordinate of the left anchor
  1015. o y: (number) y coordinate of the left anchor
  1016. o }
  1017. o n: {
  1018. o x: (number) x coordinate of the right anchor
  1019. o y: (number) y coordinate of the right anchor
  1020. o }
  1021. o start: {
  1022. o x: (number) x coordinate of the start of the curve
  1023. o y: (number) y coordinate of the start of the curve
  1024. o }
  1025. o end: {
  1026. o x: (number) x coordinate of the end of the curve
  1027. o y: (number) y coordinate of the end of the curve
  1028. o }
  1029. o alpha: (number) angle of the curve derivative at the point
  1030. o }
  1031. \*/
  1032. R.findDotsAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
  1033. var t1 = 1 - t,
  1034. x = pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
  1035. y = pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y,
  1036. mx = p1x + 2 * t * (c1x - p1x) + t * t * (c2x - 2 * c1x + p1x),
  1037. my = p1y + 2 * t * (c1y - p1y) + t * t * (c2y - 2 * c1y + p1y),
  1038. nx = c1x + 2 * t * (c2x - c1x) + t * t * (p2x - 2 * c2x + c1x),
  1039. ny = c1y + 2 * t * (c2y - c1y) + t * t * (p2y - 2 * c2y + c1y),
  1040. ax = (1 - t) * p1x + t * c1x,
  1041. ay = (1 - t) * p1y + t * c1y,
  1042. cx = (1 - t) * c2x + t * p2x,
  1043. cy = (1 - t) * c2y + t * p2y,
  1044. alpha = (90 - math.atan2(mx - nx, my - ny) * 180 / PI);
  1045. (mx > nx || my < ny) && (alpha += 180);
  1046. return {x: x, y: y, m: {x: mx, y: my}, n: {x: nx, y: ny}, start: {x: ax, y: ay}, end: {x: cx, y: cy}, alpha: alpha};
  1047. };
  1048. var pathDimensions = cacher(function (path) {
  1049. if (!path) {
  1050. return {x: 0, y: 0, width: 0, height: 0};
  1051. }
  1052. path = path2curve(path);
  1053. var x = 0,
  1054. y = 0,
  1055. X = [],
  1056. Y = [],
  1057. p;
  1058. for (var i = 0, ii = path.length; i < ii; i++) {
  1059. p = path[i];
  1060. if (p[0] == "M") {
  1061. x = p[1];
  1062. y = p[2];
  1063. X.push(x);
  1064. Y.push(y);
  1065. } else {
  1066. var dim = curveDim(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
  1067. X = X[concat](dim.min.x, dim.max.x);
  1068. Y = Y[concat](dim.min.y, dim.max.y);
  1069. x = p[5];
  1070. y = p[6];
  1071. }
  1072. }
  1073. var xmin = mmin[apply](0, X),
  1074. ymin = mmin[apply](0, Y);
  1075. return {
  1076. x: xmin,
  1077. y: ymin,
  1078. width: mmax[apply](0, X) - xmin,
  1079. height: mmax[apply](0, Y) - ymin
  1080. };
  1081. }, null, function (o) {
  1082. return {
  1083. x: o.x,
  1084. y: o.y,
  1085. width: o.width,
  1086. height: o.height
  1087. };
  1088. }),
  1089. pathClone = function (pathArray) {
  1090. var res = [];
  1091. if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
  1092. pathArray = R.parsePathString(pathArray);
  1093. }
  1094. for (var i = 0, ii = pathArray.length; i < ii; i++) {
  1095. res[i] = [];
  1096. for (var j = 0, jj = pathArray[i].length; j < jj; j++) {
  1097. res[i][j] = pathArray[i][j];
  1098. }
  1099. }
  1100. res.toString = R._path2string;
  1101. return res;
  1102. },
  1103. pathToRelative = cacher(function (pathArray) {
  1104. if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
  1105. pathArray = R.parsePathString(pathArray);
  1106. }
  1107. var res = [],
  1108. x = 0,
  1109. y = 0,
  1110. mx = 0,
  1111. my = 0,
  1112. start = 0;
  1113. if (pathArray[0][0] == "M") {
  1114. x = pathArray[0][1];
  1115. y = pathArray[0][2];
  1116. mx = x;
  1117. my = y;
  1118. start++;
  1119. res.push(["M", x, y]);
  1120. }
  1121. for (var i = start, ii = pathArray.length; i < ii; i++) {
  1122. var r = res[i] = [],
  1123. pa = pathArray[i];
  1124. if (pa[0] != lowerCase.call(pa[0])) {
  1125. r[0] = lowerCase.call(pa[0]);
  1126. switch (r[0]) {
  1127. case "a":
  1128. r[1] = pa[1];
  1129. r[2] = pa[2];
  1130. r[3] = pa[3];
  1131. r[4] = pa[4];
  1132. r[5] = pa[5];
  1133. r[6] = +(pa[6] - x).toFixed(3);
  1134. r[7] = +(pa[7] - y).toFixed(3);
  1135. break;
  1136. case "v":
  1137. r[1] = +(pa[1] - y).toFixed(3);
  1138. break;
  1139. case "m":
  1140. mx = pa[1];
  1141. my = pa[2];
  1142. default:
  1143. for (var j = 1, jj = pa.length; j < jj; j++) {
  1144. r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
  1145. }
  1146. }
  1147. } else {
  1148. r = res[i] = [];
  1149. if (pa[0] == "m") {
  1150. mx = pa[1] + x;
  1151. my = pa[2] + y;
  1152. }
  1153. for (var k = 0, kk = pa.length; k < kk; k++) {
  1154. res[i][k] = pa[k];
  1155. }
  1156. }
  1157. var len = res[i].length;
  1158. switch (res[i][0]) {
  1159. case "z":
  1160. x = mx;
  1161. y = my;
  1162. break;
  1163. case "h":
  1164. x += +res[i][len - 1];
  1165. break;
  1166. case "v":
  1167. y += +res[i][len - 1];
  1168. break;
  1169. default:
  1170. x += +res[i][len - 2];
  1171. y += +res[i][len - 1];
  1172. }
  1173. }
  1174. res.toString = R._path2string;
  1175. return res;
  1176. }, 0, pathClone),
  1177. pathToAbsolute = cacher(function (pathArray) {
  1178. if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
  1179. pathArray = R.parsePathString(pathArray);
  1180. }
  1181. if (!pathArray || !pathArray.length) {
  1182. return [["M", 0, 0]];
  1183. }
  1184. var res = [],
  1185. x = 0,
  1186. y = 0,
  1187. mx = 0,
  1188. my = 0,
  1189. start = 0;
  1190. if (pathArray[0][0] == "M") {
  1191. x = +pathArray[0][1];
  1192. y = +pathArray[0][2];
  1193. mx = x;
  1194. my = y;
  1195. start++;
  1196. res[0] = ["M", x, y];
  1197. }
  1198. for (var i = start, ii = pathArray.length; i < ii; i++) {
  1199. var r = res[i] = [],
  1200. pa = pathArray[i];
  1201. if (pa[0] != upperCase.call(pa[0])) {
  1202. r[0] = upperCase.call(pa[0]);
  1203. switch (r[0]) {
  1204. case "A":
  1205. r[1] = pa[1];
  1206. r[2] = pa[2];
  1207. r[3] = pa[3];
  1208. r[4] = pa[4];
  1209. r[5] = pa[5];
  1210. r[6] = +(pa[6] + x);
  1211. r[7] = +(pa[7] + y);
  1212. break;
  1213. case "V":
  1214. r[1] = +pa[1] + y;
  1215. break;
  1216. case "H":
  1217. r[1] = +pa[1] + x;
  1218. break;
  1219. case "M":
  1220. mx = +pa[1] + x;
  1221. my = +pa[2] + y;
  1222. default:
  1223. for (var j = 1, jj = pa.length; j < jj; j++) {
  1224. r[j] = +pa[j] + ((j % 2) ? x : y);
  1225. }
  1226. }
  1227. } else {
  1228. for (var k = 0, kk = pa.length; k < kk; k++) {
  1229. res[i][k] = pa[k];
  1230. }
  1231. }
  1232. switch (r[0]) {
  1233. case "Z":
  1234. x = mx;
  1235. y = my;
  1236. break;
  1237. case "H":
  1238. x = r[1];
  1239. break;
  1240. case "V":
  1241. y = r[1];
  1242. break;
  1243. case "M":
  1244. mx = res[i][res[i].length - 2];
  1245. my = res[i][res[i].length - 1];
  1246. default:
  1247. x = res[i][res[i].length - 2];
  1248. y = res[i][res[i].length - 1];
  1249. }
  1250. }
  1251. res.toString = R._path2string;
  1252. return res;
  1253. }, null, pathClone),
  1254. l2c = function (x1, y1, x2, y2) {
  1255. return [x1, y1, x2, y2, x2, y2];
  1256. },
  1257. q2c = function (x1, y1, ax, ay, x2, y2) {
  1258. var _13 = 1 / 3,
  1259. _23 = 2 / 3;
  1260. return [
  1261. _13 * x1 + _23 * ax,
  1262. _13 * y1 + _23 * ay,
  1263. _13 * x2 + _23 * ax,
  1264. _13 * y2 + _23 * ay,
  1265. x2,
  1266. y2
  1267. ];
  1268. },
  1269. a2c = function (x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
  1270. // for more information of where this math came from visit:
  1271. // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
  1272. var _120 = PI * 120 / 180,
  1273. rad = PI / 180 * (+angle || 0),
  1274. res = [],
  1275. xy,
  1276. rotate = cacher(function (x, y, rad) {
  1277. var X = x * math.cos(rad) - y * math.sin(rad),
  1278. Y = x * math.sin(rad) + y * math.cos(rad);
  1279. return {x: X, y: Y};
  1280. });
  1281. if (!recursive) {
  1282. xy = rotate(x1, y1, -rad);
  1283. x1 = xy.x;
  1284. y1 = xy.y;
  1285. xy = rotate(x2, y2, -rad);
  1286. x2 = xy.x;
  1287. y2 = xy.y;
  1288. var cos = math.cos(PI / 180 * angle),
  1289. sin = math.sin(PI / 180 * angle),
  1290. x = (x1 - x2) / 2,
  1291. y = (y1 - y2) / 2;
  1292. var h = (x * x) / (rx * rx) + (y * y) / (ry * ry);
  1293. if (h > 1) {
  1294. h = math.sqrt(h);
  1295. rx = h * rx;
  1296. ry = h * ry;
  1297. }
  1298. var rx2 = rx * rx,
  1299. ry2 = ry * ry,
  1300. k = (large_arc_flag == sweep_flag ? -1 : 1) *
  1301. math.sqrt(abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))),
  1302. cx = k * rx * y / ry + (x1 + x2) / 2,
  1303. cy = k * -ry * x / rx + (y1 + y2) / 2,
  1304. f1 = math.asin(((y1 - cy) / ry).toFixed(9)),
  1305. f2 = math.asin(((y2 - cy) / ry).toFixed(9));
  1306. f1 = x1 < cx ? PI - f1 : f1;
  1307. f2 = x2 < cx ? PI - f2 : f2;
  1308. f1 < 0 && (f1 = PI * 2 + f1);
  1309. f2 < 0 && (f2 = PI * 2 + f2);
  1310. if (sweep_flag && f1 > f2) {
  1311. f1 = f1 - PI * 2;
  1312. }
  1313. if (!sweep_flag && f2 > f1) {
  1314. f2 = f2 - PI * 2;
  1315. }
  1316. } else {
  1317. f1 = recursive[0];
  1318. f2 = recursive[1];
  1319. cx = recursive[2];
  1320. cy = recursive[3];
  1321. }
  1322. var df = f2 - f1;
  1323. if (abs(df) > _120) {
  1324. var f2old = f2,
  1325. x2old = x2,
  1326. y2old = y2;
  1327. f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
  1328. x2 = cx + rx * math.cos(f2);
  1329. y2 = cy + ry * math.sin(f2);
  1330. res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
  1331. }
  1332. df = f2 - f1;
  1333. var c1 = math.cos(f1),
  1334. s1 = math.sin(f1),
  1335. c2 = math.cos(f2),
  1336. s2 = math.sin(f2),
  1337. t = math.tan(df / 4),
  1338. hx = 4 / 3 * rx * t,
  1339. hy = 4 / 3 * ry * t,
  1340. m1 = [x1, y1],
  1341. m2 = [x1 + hx * s1, y1 - hy * c1],
  1342. m3 = [x2 + hx * s2, y2 - hy * c2],
  1343. m4 = [x2, y2];
  1344. m2[0] = 2 * m1[0] - m2[0];
  1345. m2[1] = 2 * m1[1] - m2[1];
  1346. if (recursive) {
  1347. return [m2, m3, m4][concat](res);
  1348. } else {
  1349. res = [m2, m3, m4][concat](res).join().split(",");
  1350. var newres = [];
  1351. for (var i = 0, ii = res.length; i < ii; i++) {
  1352. newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
  1353. }
  1354. return newres;
  1355. }
  1356. },
  1357. findDotAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
  1358. var t1 = 1 - t;
  1359. return {
  1360. x: pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
  1361. y: pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y
  1362. };
  1363. },
  1364. curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
  1365. var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
  1366. b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
  1367. c = p1x - c1x,
  1368. t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a,
  1369. t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a,
  1370. y = [p1y, p2y],
  1371. x = [p1x, p2x],
  1372. dot;
  1373. abs(t1) > "1e12" && (t1 = .5);
  1374. abs(t2) > "1e12" && (t2 = .5);
  1375. if (t1 > 0 && t1 < 1) {
  1376. dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
  1377. x.push(dot.x);
  1378. y.push(dot.y);
  1379. }
  1380. if (t2 > 0 && t2 < 1) {
  1381. dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
  1382. x.push(dot.x);
  1383. y.push(dot.y);
  1384. }
  1385. a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y);
  1386. b = 2 * (c1y - p1y) - 2 * (c2y - c1y);
  1387. c = p1y - c1y;
  1388. t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a;
  1389. t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a;
  1390. abs(t1) > "1e12" && (t1 = .5);
  1391. abs(t2) > "1e12" && (t2 = .5);
  1392. if (t1 > 0 && t1 < 1) {
  1393. dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
  1394. x.push(dot.x);
  1395. y.push(dot.y);
  1396. }
  1397. if (t2 > 0 && t2 < 1) {
  1398. dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
  1399. x.push(dot.x);
  1400. y.push(dot.y);
  1401. }
  1402. return {
  1403. min: {x: mmin[apply](0, x), y: mmin[apply](0, y)},
  1404. max: {x: mmax[apply](0, x), y: mmax[apply](0, y)}
  1405. };
  1406. }),
  1407. path2curve = cacher(function (path, path2) {
  1408. var p = pathToAbsolute(path),
  1409. p2 = path2 && pathToAbsolute(path2),
  1410. attrs = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
  1411. attrs2 = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
  1412. processPath = function (path, d) {
  1413. var nx, ny;
  1414. if (!path) {
  1415. return ["C", d.x, d.y, d.x, d.y, d.x, d.y];
  1416. }
  1417. !(path[0] in {T:1, Q:1}) && (d.qx = d.qy = null);
  1418. switch (path[0]) {
  1419. case "M":
  1420. d.X = path[1];
  1421. d.Y = path[2];
  1422. break;
  1423. case "A":
  1424. path = ["C"][concat](a2c[apply](0, [d.x, d.y][concat](path.slice(1))));
  1425. break;
  1426. case "S":
  1427. nx = d.x + (d.x - (d.bx || d.x));
  1428. ny = d.y + (d.y - (d.by || d.y));
  1429. path = ["C", nx, ny][concat](path.slice(1));
  1430. break;
  1431. case "T":
  1432. d.qx = d.x + (d.x - (d.qx || d.x));
  1433. d.qy = d.y + (d.y - (d.qy || d.y));
  1434. path = ["C"][concat](q2c(d.x, d.y, d.qx, d.qy, path[1], path[2]));
  1435. break;
  1436. case "Q":
  1437. d.qx = path[1];
  1438. d.qy = path[2];
  1439. path = ["C"][concat](q2c(d.x, d.y, path[1], path[2], path[3], path[4]));
  1440. break;
  1441. case "L":
  1442. path = ["C"][concat](l2c(d.x, d.y, path[1], path[2]));
  1443. break;
  1444. case "H":
  1445. path = ["C"][concat](l2c(d.x, d.y, path[1], d.y));
  1446. break;
  1447. case "V":
  1448. path = ["C"][concat](l2c(d.x, d.y, d.x, path[1]));
  1449. break;
  1450. case "Z":
  1451. path = ["C"][concat](l2c(d.x, d.y, d.X, d.Y));
  1452. break;
  1453. }
  1454. return path;
  1455. },
  1456. fixArc = function (pp, i) {
  1457. if (pp[i].length > 7) {
  1458. pp[i].shift();
  1459. var pi = pp[i];
  1460. while (pi.length) {
  1461. pp.splice(i++, 0, ["C"][concat](pi.splice(0, 6)));
  1462. }
  1463. pp.splice(i, 1);
  1464. ii = mmax(p.length, p2 && p2.length || 0);
  1465. }
  1466. },
  1467. fixM = function (path1, path2, a1, a2, i) {
  1468. if (path1 && path2 && path1[i][0] == "M" && path2[i][0] != "M") {
  1469. path2.splice(i, 0, ["M", a2.x, a2.y]);
  1470. a1.bx = 0;
  1471. a1.by = 0;
  1472. a1.x = path1[i][1];
  1473. a1.y = path1[i][2];
  1474. ii = mmax(p.length, p2 && p2.length || 0);
  1475. }
  1476. };
  1477. for (var i = 0, ii = mmax(p.length, p2 && p2.length || 0); i < ii; i++) {
  1478. p[i] = processPath(p[i], attrs);
  1479. fixArc(p, i);
  1480. p2 && (p2[i] = processPath(p2[i], attrs2));
  1481. p2 && fixArc(p2, i);
  1482. fixM(p, p2, attrs, attrs2, i);
  1483. fixM(p2, p, attrs2, attrs, i);
  1484. var seg = p[i],
  1485. seg2 = p2 && p2[i],
  1486. seglen = seg.length,
  1487. seg2len = p2 && seg2.length;
  1488. attrs.x = seg[seglen - 2];
  1489. attrs.y = seg[seglen - 1];
  1490. attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
  1491. attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
  1492. attrs2.bx = p2 && (toFloat(seg2[seg2len - 4]) || attrs2.x);
  1493. attrs2.by = p2 && (toFloat(seg2[seg2len - 3]) || attrs2.y);
  1494. attrs2.x = p2 && seg2[seg2len - 2];
  1495. attrs2.y = p2 && seg2[seg2len - 1];
  1496. }
  1497. return p2 ? [p, p2] : p;
  1498. }, null, pathClone),
  1499. parseDots = cacher(function (gradient) {
  1500. var dots = [];
  1501. for (var i = 0, ii = gradient.length; i < ii; i++) {
  1502. var dot = {},
  1503. par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
  1504. dot.color = R.getRGB(par[1]);
  1505. if (dot.color.error) {
  1506. return null;
  1507. }
  1508. dot.color = dot.color.hex;
  1509. par[2] && (dot.offset = par[2] + "%");
  1510. dots.push(dot);
  1511. }
  1512. for (i = 1, ii = dots.length - 1; i < ii; i++) {
  1513. if (!dots[i].offset) {
  1514. var start = toFloat(dots[i - 1].offset || 0),
  1515. end = 0;
  1516. for (var j = i + 1; j < ii; j++) {
  1517. if (dots[j].offset) {
  1518. end = dots[j].offset;
  1519. break;
  1520. }
  1521. }
  1522. if (!end) {
  1523. end = 100;
  1524. j = ii;
  1525. }
  1526. end = toFloat(end);
  1527. var d = (end - start) / (j - i + 1);
  1528. for (; i < j; i++) {
  1529. start += d;
  1530. dots[i].offset = start + "%";
  1531. }
  1532. }
  1533. }
  1534. return dots;
  1535. }),
  1536. getContainer = function (x, y, w, h) {
  1537. var container;
  1538. container = h == null && !R.is(x, "object") ? g.doc.getElementById(x) : x;
  1539. if (container == null) {
  1540. return;
  1541. }
  1542. if (container.tagName) {
  1543. if (y == null) {
  1544. return {
  1545. container: container,
  1546. width: container.style.pixelWidth || container.offsetWidth,
  1547. height: container.style.pixelHeight || container.offsetHeight
  1548. };
  1549. } else {
  1550. return {container: container, width: y, height: w};
  1551. }
  1552. }
  1553. return {container: 1, x: x, y: y, width: w, height: h};
  1554. },
  1555. plugins = function (con, add) {
  1556. var that = this;
  1557. for (var prop in add) {
  1558. if (add[has](prop) && !(prop in con)) {
  1559. switch (typeof add[prop]) {
  1560. case "function":
  1561. (function (f) {
  1562. con[prop] = con === that ? f : function () { return f[apply](that, arguments); };
  1563. })(add[prop]);
  1564. break;
  1565. case "object":
  1566. con[prop] = con[prop] || {};
  1567. plugins.call(this, con[prop], add[prop]);
  1568. break;
  1569. default:
  1570. con[prop] = add[prop];
  1571. break;
  1572. }
  1573. }
  1574. }
  1575. },
  1576. tear = function (el, paper) {
  1577. el == paper.top && (paper.top = el.prev);
  1578. el == paper.bottom && (paper.bottom = el.next);
  1579. el.next && (el.next.prev = el.prev);
  1580. el.prev && (el.prev.next = el.next);
  1581. },
  1582. tofront = function (el, paper) {
  1583. if (paper.top === el) {
  1584. return;
  1585. }
  1586. tear(el, paper);
  1587. el.next = null;
  1588. el.prev = paper.top;
  1589. paper.top.next = el;
  1590. paper.top = el;
  1591. },
  1592. toback = function (el, paper) {
  1593. if (paper.bottom === el) {
  1594. return;
  1595. }
  1596. tear(el, paper);
  1597. el.next = paper.bottom;
  1598. el.prev = null;
  1599. paper.bottom.prev = el;
  1600. paper.bottom = el;
  1601. },
  1602. insertafter = function (el, el2, paper) {
  1603. tear(el, paper);
  1604. el2 == paper.top && (paper.top = el);
  1605. el2.next && (el2.next.prev = el);
  1606. el.next = el2.next;
  1607. el.prev = el2;
  1608. el2.next = el;
  1609. },
  1610. insertbefore = function (el, el2, paper) {
  1611. tear(el, paper);
  1612. el2 == paper.bottom && (paper.bottom = el);
  1613. el2.prev && (el2.prev.next = el);
  1614. el.prev = el2.prev;
  1615. el2.prev = el;
  1616. el.next = el2;
  1617. },
  1618. removed = function (methodname) {
  1619. return function () {
  1620. throw new Error("Rapha\xebl: you are calling to method \u201c" + methodname + "\u201d of removed object");
  1621. };
  1622. },
  1623. extractTransform = function (el, tstr) {
  1624. if (tstr == null) {
  1625. return el._.transform;
  1626. }
  1627. tstr = Str(tstr).replace(/\.{3}|\u2026/g, el._.transform || E);
  1628. var tdata = R.parseTransformString(tstr),
  1629. deg = 0,
  1630. dx = 0,
  1631. dy = 0,
  1632. sx = 1,
  1633. sy = 1,
  1634. _ = el._,
  1635. m = new Matrix;
  1636. _.transform = tdata || [];
  1637. if (tdata) {
  1638. for (var i = 0, ii = tdata.length; i < ii; i++) {
  1639. var t = tdata[i],
  1640. tlen = t.length,
  1641. bb;
  1642. t[0] = Str(t[0]).toLowerCase();
  1643. if (t[0] == "t" && tlen == 3) {
  1644. m.translate(t[1], t[2]);
  1645. } else if (t[0] == "r") {
  1646. if (tlen == 2) {
  1647. bb = bb || el.getBBox(1);
  1648. m.rotate(t[1], bb.x + bb.width / 2, bb.y + bb.height / 2);
  1649. deg += t[1];
  1650. } else if (tlen == 4) {
  1651. m.rotate(t[1], t[2], t[3]);
  1652. deg += t[1];
  1653. }
  1654. } else if (t[0] == "s") {
  1655. if (tlen == 2 || tlen == 3) {
  1656. bb = bb || el.getBBox(1);
  1657. m.scale(t[1], t[tlen - 1], bb.x + bb.width / 2, bb.y + bb.height / 2);
  1658. sx *= t[1];
  1659. sy *= t[tlen - 1];
  1660. } else if (tlen == 5) {
  1661. m.scale(t[1], t[2], t[3], t[4]);
  1662. sx *= t[1];
  1663. sy *= t[2];
  1664. }
  1665. } else if (t[0] == "m" && tlen == 7) {
  1666. m.add(t[1], t[2], t[3], t[4], t[5], t[6]);
  1667. }
  1668. _.dirtyT = 1;
  1669. el.matrix = m;
  1670. }
  1671. }
  1672. el.matrix = m;
  1673. _.sx = sx;
  1674. _.sy = sy;
  1675. _.deg = deg;
  1676. _.dx = dx = m.e;
  1677. _.dy = dy = m.f;
  1678. if (sx == 1 && sy == 1 && !deg && _.bbox) {
  1679. _.bbox.x += +dx;
  1680. _.bbox.y += +dy;
  1681. } else {
  1682. _.dirtyT = 1;
  1683. }
  1684. },
  1685. getEmpty = function (item) {
  1686. switch (item[0]) {
  1687. case "t": return ["t", 0, 0];
  1688. case "m": return ["m", 1, 0, 0, 1, 0, 0];
  1689. case "r": if (item.length == 4) {
  1690. return ["r", 0, item[2], item[3]];
  1691. } else {
  1692. return ["r", 0];
  1693. }
  1694. case "s": if (item.length == 5) {
  1695. return ["s", 1, 1, item[3], item[4]];
  1696. } else if (item.length == 3) {
  1697. return ["s", 1, 1];
  1698. } else {
  1699. return ["s", 1];
  1700. }
  1701. }
  1702. },
  1703. equaliseTransform = function (t1, t2) {
  1704. t2 = Str(t2).replace(/\.{3}|\u2026/g, t1);
  1705. t1 = R.parseTransformString(t1) || [];
  1706. t2 = R.parseTransformString(t2) || [];
  1707. var maxlength = mmax(t1.length, t2.length),
  1708. from = [],
  1709. to = [],
  1710. i = 0, j, jj,
  1711. tt1, tt2;
  1712. for (; i < maxlength; i++) {
  1713. tt1 = t1[i] || getEmpty(t2[i]);
  1714. tt2 = t2[i] || getEmpty(tt1);
  1715. if ((tt1[0] != tt2[0]) ||
  1716. (tt1[0] == "r" && (tt1[2] != tt2[2] || tt1[3] != tt2[3])) ||
  1717. (tt1[0] == "s" && (tt1[3] != tt2[3] || tt1[4] != tt2[4]))
  1718. ) {
  1719. return;
  1720. }
  1721. from[i] = [];
  1722. to[i] = [];
  1723. for (j = 0, jj = mmax(tt1.length, tt2.length); j < jj; j++) {
  1724. j in tt1 && (from[i][j] = tt1[j]);
  1725. j in tt2 && (to[i][j] = tt2[j]);
  1726. }
  1727. }
  1728. return {
  1729. from: from,
  1730. to: to
  1731. };
  1732. };
  1733. /*\
  1734. * Raphael.pathToRelative
  1735. [ method ]
  1736. **
  1737. * Utility method
  1738. **
  1739. * Converts path to relative form
  1740. > Parameters
  1741. - pathString (string|array) path string or array of segments
  1742. = (array) array of segments.
  1743. \*/
  1744. R.pathToRelative = pathToRelative;
  1745. /*\
  1746. * Raphael.path2curve
  1747. [ method ]
  1748. **
  1749. * Utility method
  1750. **
  1751. * Converts path to a new path where all segments are cubic bezier curves.
  1752. > Parameters
  1753. - pathString (string|array) path string or array of segments
  1754. = (array) array of segments.
  1755. \*/
  1756. R.path2curve = path2curve;
  1757. /*\
  1758. * Raphael.matrix
  1759. [ method ]
  1760. **
  1761. * Utility method
  1762. **
  1763. * Returns matrix based on given parameters.
  1764. > Parameters
  1765. - a (number)
  1766. - b (number)
  1767. - c (number)
  1768. - d (number)
  1769. - e (number)
  1770. - f (number)
  1771. = (object) @Matrix
  1772. \*/
  1773. R.matrix = function (a, b, c, d, e, f) {
  1774. return new Matrix(a, b, c, d, e, f);
  1775. };
  1776. function Matrix(a, b, c, d, e, f) {
  1777. if (a != null) {
  1778. this.a = +a;
  1779. this.b = +b;
  1780. this.c = +c;
  1781. this.d = +d;
  1782. this.e = +e;
  1783. this.f = +f;
  1784. } else {
  1785. this.a = 1;
  1786. this.b = 0;
  1787. this.c = 0;
  1788. this.d = 1;
  1789. this.e = 0;
  1790. this.f = 0;
  1791. }
  1792. }
  1793. (function (matrixproto) {
  1794. matrixproto.add = function (a, b, c, d, e, f) {
  1795. var out = [[], [], []],
  1796. m = [[this.a, this.c, this.e], [this.b, this.d, this.f], [0, 0, 1]],
  1797. matrix = [[a, c, e], [b, d, f], [0, 0, 1]],
  1798. x, y, z, res;
  1799. if (a && a instanceof Matrix) {
  1800. matrix = [[a.a, a.c, a.e], [a.b, a.d, a.f], [0, 0, 1]];
  1801. }
  1802. for (x = 0; x < 3; x++) {
  1803. for (y = 0; y < 3; y++) {
  1804. res = 0;
  1805. for (z = 0; z < 3; z++) {
  1806. res += m[x][z] * matrix[z][y];
  1807. }
  1808. out[x][y] = res;
  1809. }
  1810. }
  1811. this.a = out[0][0];
  1812. this.b = out[1][0];
  1813. this.c = out[0][1];
  1814. this.d = out[1][1];
  1815. this.e = out[0][2];
  1816. this.f = out[1][2];
  1817. };
  1818. matrixproto.invert = function () {
  1819. var me = this,
  1820. x = me.a * me.d - me.b * me.c;
  1821. return new Matrix(me.d / x, -me.b / x, -me.c / x, me.a / x, (me.c * me.f - me.d * me.e) / x, (me.b * me.e - me.a * me.f) / x);
  1822. };
  1823. matrixproto.clone = function () {
  1824. return new Matrix(this.a, this.b, this.c, this.d, this.e, this.f);
  1825. };
  1826. matrixproto.translate = function (x, y) {
  1827. this.add(1, 0, 0, 1, x, y);
  1828. };
  1829. matrixproto.scale = function (x, y, cx, cy) {
  1830. y == null && (y = x);
  1831. this.add(1, 0, 0, 1, cx, cy);
  1832. this.add(x, 0, 0, y, 0, 0);
  1833. this.add(1, 0, 0, 1, -cx, -cy);
  1834. };
  1835. matrixproto.rotate = function (a, x, y) {
  1836. a = R.rad(a);
  1837. var cos = +math.cos(a).toFixed(9),
  1838. sin = +math.sin(a).toFixed(9);
  1839. this.add(cos, sin, -sin, cos, x, y);
  1840. this.add(1, 0, 0, 1, -x, -y);
  1841. };
  1842. matrixproto.x = function (x, y) {
  1843. return x * this.a + y * this.c + this.e;
  1844. };
  1845. matrixproto.y = function (x, y) {
  1846. return x * this.b + y * this.d + this.f;
  1847. };
  1848. matrixproto.get = function (i) {
  1849. return +this[Str.fromCharCode(97 + i)].toFixed(4);
  1850. };
  1851. matrixproto.toString = function () {
  1852. return R.svg ?
  1853. "matrix(" + [this.get(0), this.get(1), this.get(2), this.get(3), this.get(4), this.get(5)].join() + ")" :
  1854. [this.get(0), this.get(2), this.get(1), this.get(3), 0, 0].join();
  1855. };
  1856. matrixproto.toFilter = function () {
  1857. return "progid:DXImageTransform.Microsoft.Matrix(M11=" + this.get(0) +
  1858. ", M12=" + this.get(2) + ", M21=" + this.get(1) + ", M22=" + this.get(3) +
  1859. ", Dx=" + this.get(4) + ", Dy=" + this.get(5) + ", sizingmethod='auto expand')";
  1860. };
  1861. matrixproto.offset = function () {
  1862. return [this.e.toFixed(4), this.f.toFixed(4)];
  1863. };
  1864. function norm(a) {
  1865. return a[0] * a[0] + a[1] * a[1];
  1866. }
  1867. function normalize(a) {
  1868. var mag = math.sqrt(norm(a));
  1869. a[0] /= mag;
  1870. a[1] /= mag;
  1871. }
  1872. matrixproto.split = function () {
  1873. var out = {};
  1874. // translation
  1875. out.dx = this.e;
  1876. out.dy = this.f;
  1877. // scale and shear
  1878. var row = [[this.a, this.c], [this.b, this.d]];
  1879. out.scalex = math.sqrt(norm(row[0]));
  1880. normalize(row[0]);
  1881. out.shear = row[0][0] * row[1][0] + row[0][1] * row[1][1];
  1882. row[1] = [row[1][0] - row[0][0] * out.shear, row[1][1] - row[0][1] * out.shear];
  1883. out.scaley = math.sqrt(norm(row[1]));
  1884. normalize(row[1]);
  1885. out.shear /= out.scaley;
  1886. // rotation
  1887. var sin = -row[0][1],
  1888. cos = row[1][1];
  1889. if (cos < 0) {
  1890. out.rotate = R.deg(math.acos(cos));
  1891. if (sin < 0) {
  1892. out.rotate = 360 - out.rotate;
  1893. }
  1894. } else {
  1895. out.rotate = R.deg(math.asin(sin));
  1896. }
  1897. out.isSimple = !+out.shear.toFixed(9) && (out.scalex.toFixed(9) == out.scaley.toFixed(9) || !out.rotate);
  1898. out.isSuperSimple = !+out.shear.toFixed(9) && out.scalex.toFixed(9) == out.scaley.toFixed(9) && !out.rotate;
  1899. return out;
  1900. };
  1901. matrixproto.toTransformString = function () {
  1902. var s = this.split();
  1903. if (s.isSimple) {
  1904. return "t" + [s.dx, s.dy] + "s" + [s.scalex, s.scaley, 0, 0] + "r" + [s.rotate, 0, 0];
  1905. } else {
  1906. return "m" + [this.get(0), this.get(1), this.get(2), this.get(3), this.get(4), this.get(5)];
  1907. }
  1908. };
  1909. })(Matrix.prototype);
  1910. // SVG
  1911. if (R.svg) {
  1912. var xlink = "http://www.w3.org/1999/xlink",
  1913. markers = {
  1914. block: "M5,0 0,2.5 5,5z",
  1915. classic: "M5,0 0,2.5 5,5 3.5,3 3.5,2z",
  1916. diamond: "M2.5,0 5,2.5 2.5,5 0,2.5z",
  1917. open: "M6,1 1,3.5 6,6",
  1918. oval: "M2.5,0A2.5,2.5,0,0,1,2.5,5 2.5,2.5,0,0,1,2.5,0z"
  1919. },
  1920. markerCounter = {};
  1921. R.toString = function () {
  1922. return "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version;
  1923. };
  1924. var $ = function (el, attr) {
  1925. if (attr) {
  1926. if (typeof el == "string") {
  1927. el = $(el);
  1928. }
  1929. for (var key in attr) if (attr[has](key)) {
  1930. if (key.substring(0, 6) == "xlink:") {
  1931. el.setAttributeNS(xlink, key.substring(6), Str(attr[key]));
  1932. } else {
  1933. el[setAttribute](key, Str(attr[key]));
  1934. }
  1935. }
  1936. } else {
  1937. el = g.doc.createElementNS("http://www.w3.org/2000/svg", el);
  1938. el.style && (el.style.webkitTapHighlightColor = "rgba(0,0,0,0)");
  1939. }
  1940. return el;
  1941. },
  1942. thePath = function (pathString, SVG) {
  1943. var el = $("path");
  1944. SVG.canvas && SVG.canvas.appendChild(el);
  1945. var p = new Element(el, SVG);
  1946. p.type = "path";
  1947. setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
  1948. return p;
  1949. },
  1950. gradients = {},
  1951. rgGrad = /^url\(#(.*)\)$/,
  1952. removeGradientFill = function (node, paper) {
  1953. var oid = node.getAttribute(fillString);
  1954. oid = oid && oid.match(rgGrad);
  1955. if (oid && !--gradients[oid[1]]) {
  1956. delete gradients[oid[1]];
  1957. paper.defs.removeChild(g.doc.getElementById(oid[1]));
  1958. }
  1959. },
  1960. addGradientFill = function (element, gradient) {
  1961. var type = "linear",
  1962. id = element.id + gradient,
  1963. fx = .5, fy = .5,
  1964. o = element.node,
  1965. SVG = element.paper,
  1966. s = o.style,
  1967. el = g.doc.getElementById(id);
  1968. if (!el) {
  1969. gradient = Str(gradient).replace(radial_gradient, function (all, _fx, _fy) {
  1970. type = "radial";
  1971. if (_fx && _fy) {
  1972. fx = toFloat(_fx);
  1973. fy = toFloat(_fy);
  1974. var dir = ((fy > .5) * 2 - 1);
  1975. pow(fx - .5, 2) + pow(fy - .5, 2) > .25 &&
  1976. (fy = math.sqrt(.25 - pow(fx - .5, 2)) * dir + .5) &&
  1977. fy != .5 &&
  1978. (fy = fy.toFixed(5) - 1e-5 * dir);
  1979. }
  1980. return E;
  1981. });
  1982. gradient = gradient.split(/\s*\-\s*/);
  1983. if (type == "linear") {
  1984. var angle = gradient.shift();
  1985. angle = -toFloat(angle);
  1986. if (isNaN(angle)) {
  1987. return null;
  1988. }
  1989. var vector = [0, 0, math.cos(R.rad(angle)), math.sin(R.rad(angle))],
  1990. max = 1 / (mmax(abs(vector[2]), abs(vector[3])) || 1);
  1991. vector[2] *= max;
  1992. vector[3] *= max;
  1993. if (vector[2] < 0) {
  1994. vector[0] = -vector[2];
  1995. vector[2] = 0;
  1996. }
  1997. if (vector[3] < 0) {
  1998. vector[1] = -vector[3];
  1999. vector[3] = 0;
  2000. }
  2001. }
  2002. var dots = parseDots(gradient);
  2003. if (!dots) {
  2004. return null;
  2005. }
  2006. if (element.gradient) {
  2007. SVG.defs.removeChild(element.gradient);
  2008. delete element.gradient;
  2009. }
  2010. id = id.replace(/[\(\)\s,\xb0#]/g, "-");
  2011. el = $(type + "Gradient", {id: id});
  2012. element.gradient = el;
  2013. $(el, type == "radial" ? {
  2014. fx: fx,
  2015. fy: fy
  2016. } : {
  2017. x1: vector[0],
  2018. y1: vector[1],
  2019. x2: vector[2],
  2020. y2: vector[3],
  2021. gradientTransform: element.matrix.invert()
  2022. });
  2023. SVG.defs.appendChild(el);
  2024. for (var i = 0, ii = dots.length; i < ii; i++) {
  2025. el.appendChild($("stop", {
  2026. offset: dots[i].offset ? dots[i].offset : i ? "100%" : "0%",
  2027. "stop-color": dots[i].color || "#fff"
  2028. }));
  2029. }
  2030. }
  2031. $(o, {
  2032. fill: "url(#" + id + ")",
  2033. opacity: 1,
  2034. "fill-opacity": 1
  2035. });
  2036. s.fill = E;
  2037. s.opacity = 1;
  2038. s.fillOpacity = 1;
  2039. return 1;
  2040. },
  2041. updatePosition = function (o) {
  2042. var bbox = o.getBBox(1);
  2043. $(o.pattern, {patternTransform: o.matrix.invert() + " translate(" + bbox.x + "," + bbox.y + ")"});
  2044. },
  2045. addArrow = function (o, value, isEnd) {
  2046. if (o.type == "path") {
  2047. var values = Str(value).toLowerCase().split("-"),
  2048. p = o.paper,
  2049. se = isEnd ? "end" : "start",
  2050. node = o.node,
  2051. attrs = o.attrs,
  2052. stroke = attrs["stroke-width"],
  2053. i = values.length,
  2054. type = "classic",
  2055. from,
  2056. to,
  2057. dx,
  2058. refX,
  2059. attr,
  2060. w = 3,
  2061. h = 3,
  2062. t = 5;
  2063. while (i--) {
  2064. switch (values[i]) {
  2065. case "block":
  2066. case "classic":
  2067. case "oval":
  2068. case "diamond":
  2069. case "open":
  2070. case "none":
  2071. type = values[i];
  2072. break;
  2073. case "wide": h = 5; break;
  2074. case "narrow": h = 2; break;
  2075. case "long": w = 5; break;
  2076. case "short": w = 2; break;
  2077. }
  2078. }
  2079. if (type == "open") {
  2080. w += 2;
  2081. h += 2;
  2082. t += 2;
  2083. dx = 1;
  2084. refX = isEnd ? 4 : 1;
  2085. attr = {
  2086. fill: "none",
  2087. stroke: attrs.stroke
  2088. };
  2089. } else {
  2090. refX = dx = w / 2;
  2091. attr = {
  2092. fill: attrs.stroke,
  2093. stroke: "none"
  2094. };
  2095. }
  2096. if (o._.arrows) {
  2097. if (isEnd) {
  2098. o._.arrows.endPath && markerCounter[o._.arrows.endPath]--;
  2099. o._.arrows.endMarker && markerCounter[o._.arrows.endMarker]--;
  2100. } else {
  2101. o._.arrows.startPath && markerCounter[o._.arrows.startPath]--;
  2102. o._.arrows.startMarker && markerCounter[o._.arrows.startMarker]--;
  2103. }
  2104. } else {
  2105. o._.arrows = {};
  2106. }
  2107. if (type != "none") {
  2108. var pathId = "raphael-marker-" + type,
  2109. markerId = "raphael-marker-" + se + type + w + h;
  2110. if (!g.doc.getElementById(pathId)) {
  2111. p.defs.appendChild($($("path"), {
  2112. "stroke-linecap": "round",
  2113. d: markers[type],
  2114. id: pathId
  2115. }));
  2116. markerCounter[pathId] = 1;
  2117. } else {
  2118. markerCounter[pathId]++;
  2119. }
  2120. var marker = g.doc.getElementById(markerId),
  2121. use;
  2122. if (!marker) {
  2123. marker = $($("marker"), {
  2124. id: markerId,
  2125. markerHeight: h,
  2126. markerWidth: w,
  2127. orient: "auto",
  2128. refX: refX,
  2129. refY: h / 2
  2130. });
  2131. use = $($("use"), {
  2132. "xlink:href": "#" + pathId,
  2133. transform: (isEnd ? " rotate(180 " + w / 2 + " " + h / 2 + ") " : S) + "scale(" + w / t + "," + h / t + ")",
  2134. "stroke-width": 1 / ((w / t + h / t) / 2)
  2135. });
  2136. marker.appendChild(use);
  2137. p.defs.appendChild(marker);
  2138. markerCounter[markerId] = 1;
  2139. } else {
  2140. markerCounter[markerId]++;
  2141. use = marker.getElementsByTagName("use")[0];
  2142. }
  2143. $(use, attr);
  2144. var delta = dx * (type != "diamond" && type != "oval");
  2145. if (isEnd) {
  2146. from = o._.arrows.startdx * stroke || 0;
  2147. to = R.getTotalLength(attrs.path) - delta * stroke;
  2148. } else {
  2149. from = delta * stroke;
  2150. to = R.getTotalLength(attrs.path) - (o._.arrows.enddx * stroke || 0);
  2151. }
  2152. attr = {};
  2153. attr["marker-" + se] = "url(#" + markerId + ")";
  2154. if (to || from) {
  2155. attr.d = Raphael.getSubpath(attrs.path, from, to);
  2156. }
  2157. $(node, attr);
  2158. o._.arrows[se + "Path"] = pathId;
  2159. o._.arrows[se + "Marker"] = markerId;
  2160. o._.arrows[se + "dx"] = delta;
  2161. o._.arrows[se + "Type"] = type;
  2162. o._.arrows[se + "String"] = value;
  2163. } else {
  2164. if (isEnd) {
  2165. from = o._.arrows.startdx * stroke || 0;
  2166. to = R.getTotalLength(attrs.path) - from;
  2167. } else {
  2168. from = 0;
  2169. to = R.getTotalLength(attrs.path) - (o._.arrows.enddx * stroke || 0);
  2170. }
  2171. o._.arrows[se + "Path"] && $(node, {d: Raphael.getSubpath(attrs.path, from, to)});
  2172. delete o._.arrows[se + "Path"];
  2173. delete o._.arrows[se + "Marker"];
  2174. delete o._.arrows[se + "dx"];
  2175. delete o._.arrows[se + "Type"];
  2176. delete o._.arrows[se + "String"];
  2177. }
  2178. for (attr in markerCounter) if (markerCounter[has](attr) && !markerCounter[attr]) {
  2179. var item = g.doc.getElementById(attr);
  2180. item && item.parentNode.removeChild(item);
  2181. }
  2182. }
  2183. },
  2184. setFillAndStroke = function (o, params) {
  2185. var dasharray = {
  2186. "": [0],
  2187. "none": [0],
  2188. "-": [3, 1],
  2189. ".": [1, 1],
  2190. "-.": [3, 1, 1, 1],
  2191. "-..": [3, 1, 1, 1, 1, 1],
  2192. ". ": [1, 3],
  2193. "- ": [4, 3],
  2194. "--": [8, 3],
  2195. "- .": [4, 3, 1, 3],
  2196. "--.": [8, 3, 1, 3],
  2197. "--..": [8, 3, 1, 3, 1, 3]
  2198. },
  2199. node = o.node,
  2200. attrs = o.attrs,
  2201. addDashes = function (o, value) {
  2202. value = dasharray[lowerCase.call(value)];
  2203. if (value) {
  2204. var width = o.attrs["stroke-width"] || "1",
  2205. butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
  2206. dashes = [],
  2207. i = value.length;
  2208. while (i--) {
  2209. dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
  2210. }
  2211. $(node, {"stroke-dasharray": dashes.join(",")});
  2212. }
  2213. };
  2214. for (var att in params) {
  2215. if (params[has](att)) {
  2216. if (!availableAttrs[has](att)) {
  2217. continue;
  2218. }
  2219. var value = params[att];
  2220. attrs[att] = value;
  2221. switch (att) {
  2222. case "blur":
  2223. o.blur(value);
  2224. break;
  2225. case "href":
  2226. case "title":
  2227. case "target":
  2228. var pn = node.parentNode;
  2229. if (lowerCase.call(pn.tagName) != "a") {
  2230. var hl = $("a");
  2231. pn.insertBefore(hl, node);
  2232. hl.appendChild(node);
  2233. pn = hl;
  2234. }
  2235. if (att == "target" && value == "blank") {
  2236. pn.setAttributeNS(xlink, "show", "new");
  2237. } else {
  2238. pn.setAttributeNS(xlink, att, value);
  2239. }
  2240. break;
  2241. case "cursor":
  2242. node.style.cursor = value;
  2243. break;
  2244. case "transform":
  2245. o.transform(value);
  2246. break;
  2247. case "arrow-start":
  2248. addArrow(o, value);
  2249. break;
  2250. case "arrow-end":
  2251. addArrow(o, value, 1);
  2252. break;
  2253. case "clip-rect":
  2254. var rect = Str(value).split(separator);
  2255. if (rect.length == 4) {
  2256. o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
  2257. var el = $("clipPath"),
  2258. rc = $("rect");
  2259. el.id = createUUID();
  2260. $(rc, {
  2261. x: rect[0],
  2262. y: rect[1],
  2263. width: rect[2],
  2264. height: rect[3]
  2265. });
  2266. el.appendChild(rc);
  2267. o.paper.defs.appendChild(el);
  2268. $(node, {"clip-path": "url(#" + el.id + ")"});
  2269. o.clip = rc;
  2270. }
  2271. if (!value) {
  2272. var clip = g.doc.getElementById(node.getAttribute("clip-path").replace(/(^url\(#|\)$)/g, E));
  2273. clip && clip.parentNode.removeChild(clip);
  2274. $(node, {"clip-path": E});
  2275. delete o.clip;
  2276. }
  2277. break;
  2278. case "path":
  2279. if (o.type == "path") {
  2280. $(node, {d: value ? attrs.path = pathToAbsolute(value) : "M0,0"});
  2281. o._.dirty = 1;
  2282. if (o._.arrows) {
  2283. "startString" in o._.arrows && addArrow(o, o._.arrows.startString);
  2284. "endString" in o._.arrows && addArrow(o, o._.arrows.endString, 1);
  2285. }
  2286. }
  2287. break;
  2288. case "width":
  2289. node[setAttribute](att, value);
  2290. o._.dirty = 1;
  2291. if (attrs.fx) {
  2292. att = "x";
  2293. value = attrs.x;
  2294. } else {
  2295. break;
  2296. }
  2297. case "x":
  2298. if (attrs.fx) {
  2299. value = -attrs.x - (attrs.width || 0);
  2300. }
  2301. case "rx":
  2302. if (att == "rx" && o.type == "rect") {
  2303. break;
  2304. }
  2305. case "cx":
  2306. node[setAttribute](att, value);
  2307. o.pattern && updatePosition(o);
  2308. o._.dirty = 1;
  2309. break;
  2310. case "height":
  2311. node[setAttribute](att, value);
  2312. o._.dirty = 1;
  2313. if (attrs.fy) {
  2314. att = "y";
  2315. value = attrs.y;
  2316. } else {
  2317. break;
  2318. }
  2319. case "y":
  2320. if (attrs.fy) {
  2321. value = -attrs.y - (attrs.height || 0);
  2322. }
  2323. case "ry":
  2324. if (att == "ry" && o.type == "rect") {
  2325. break;
  2326. }
  2327. case "cy":
  2328. node[setAttribute](att, value);
  2329. o.pattern && updatePosition(o);
  2330. o._.dirty = 1;
  2331. break;
  2332. case "r":
  2333. if (o.type == "rect") {
  2334. $(node, {rx: value, ry: value});
  2335. } else {
  2336. node[setAttribute](att, value);
  2337. }
  2338. o._.dirty = 1;
  2339. break;
  2340. case "src":
  2341. if (o.type == "image") {
  2342. node.setAttributeNS(xlink, "href", value);
  2343. }
  2344. break;
  2345. case "stroke-width":
  2346. if (o._.sx != 1 || o._.sy != 1) {
  2347. value /= mmax(abs(o._.sx), abs(o._.sy)) || 1;
  2348. }
  2349. if (o.paper._vbSize) {
  2350. value *= o.paper._vbSize;
  2351. }
  2352. node[setAttribute](att, value);
  2353. if (attrs["stroke-dasharray"]) {
  2354. addDashes(o, attrs["stroke-dasharray"]);
  2355. }
  2356. if (o._.arrows) {
  2357. "startString" in o._.arrows && addArrow(o, o._.arrows.startString);
  2358. "endString" in o._.arrows && addArrow(o, o._.arrows.endString, 1);
  2359. }
  2360. break;
  2361. case "stroke-dasharray":
  2362. addDashes(o, value);
  2363. break;
  2364. case fillString:
  2365. var isURL = Str(value).match(ISURL);
  2366. if (isURL) {
  2367. el = $("pattern");
  2368. var ig = $("image");
  2369. el.id = createUUID();
  2370. $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse", height: 1, width: 1});
  2371. $(ig, {x: 0, y: 0, "xlink:href": isURL[1]});
  2372. el.appendChild(ig);
  2373. (function (el) {
  2374. preload(isURL[1], function () {
  2375. var w = this.offsetWidth,
  2376. h = this.offsetHeight;
  2377. $(el, {width: w, height: h});
  2378. $(ig, {width: w, height: h});
  2379. o.paper.safari();
  2380. });
  2381. })(el);
  2382. o.paper.defs.appendChild(el);
  2383. node.style.fill = "url(#" + el.id + ")";
  2384. $(node, {fill: "url(#" + el.id + ")"});
  2385. o.pattern = el;
  2386. o.pattern && updatePosition(o);
  2387. break;
  2388. }
  2389. var clr = R.getRGB(value);
  2390. if (!clr.error) {
  2391. delete params.gradient;
  2392. delete attrs.gradient;
  2393. !R.is(attrs.opacity, "undefined") &&
  2394. R.is(params.opacity, "undefined") &&
  2395. $(node, {opacity: attrs.opacity});
  2396. !R.is(attrs["fill-opacity"], "undefined") &&
  2397. R.is(params["fill-opacity"], "undefined") &&
  2398. $(node, {"fill-opacity": attrs["fill-opacity"]});
  2399. } else if ((o.type == "circle" || o.type == "ellipse" || Str(value).charAt() != "r") && addGradientFill(o, value)) {
  2400. if ("opacity" in attrs || "fill-opacity" in attrs) {
  2401. var gradient = g.doc.getElementById(node.getAttribute(fillString).replace(/^url\(#|\)$/g, E));
  2402. if (gradient) {
  2403. var stops = gradient.getElementsByTagName("stop");
  2404. $(stops[stops.length - 1], {"stop-opacity": ("opacity" in attrs ? attrs.opacity : 1) * ("fill-opacity" in attrs ? attrs["fill-opacity"] : 1)});
  2405. }
  2406. }
  2407. attrs.gradient = value;
  2408. attrs.fill = "none";
  2409. break;
  2410. }
  2411. clr[has]("opacity") && $(node, {"fill-opacity": clr.opacity > 1 ? clr.opacity / 100 : clr.opacity});
  2412. case "stroke":
  2413. clr = R.getRGB(value);
  2414. node[setAttribute](att, clr.hex);
  2415. att == "stroke" && clr[has]("opacity") && $(node, {"stroke-opacity": clr.opacity > 1 ? clr.opacity / 100 : clr.opacity});
  2416. if (att == "stroke" && o._.arrows) {
  2417. "startString" in o._.arrows && addArrow(o, o._.arrows.startString);
  2418. "endString" in o._.arrows && addArrow(o, o._.arrows.endString, 1);
  2419. }
  2420. break;
  2421. case "gradient":
  2422. (o.type == "circle" || o.type == "ellipse" || Str(value).charAt() != "r") && addGradientFill(o, value);
  2423. break;
  2424. case "opacity":
  2425. if (attrs.gradient && !attrs[has]("stroke-opacity")) {
  2426. $(node, {"stroke-opacity": value > 1 ? value / 100 : value});
  2427. }
  2428. // fall
  2429. case "fill-opacity":
  2430. if (attrs.gradient) {
  2431. gradient = g.doc.getElementById(node.getAttribute(fillString).replace(/^url\(#|\)$/g, E));
  2432. if (gradient) {
  2433. stops = gradient.getElementsByTagName("stop");
  2434. $(stops[stops.length - 1], {"stop-opacity": value});
  2435. }
  2436. break;
  2437. }
  2438. default:
  2439. att == "font-size" && (value = toInt(value, 10) + "px");
  2440. var cssrule = att.replace(/(\-.)/g, function (w) {
  2441. return upperCase.call(w.substring(1));
  2442. });
  2443. node.style[cssrule] = value;
  2444. o._.dirty = 1;
  2445. node[setAttribute](att, value);
  2446. break;
  2447. }
  2448. }
  2449. }
  2450. tuneText(o, params);
  2451. },
  2452. leading = 1.2,
  2453. tuneText = function (el, params) {
  2454. if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
  2455. return;
  2456. }
  2457. var a = el.attrs,
  2458. node = el.node,
  2459. fontSize = node.firstChild ? toInt(g.doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
  2460. if (params[has]("text")) {
  2461. a.text = params.text;
  2462. while (node.firstChild) {
  2463. node.removeChild(node.firstChild);
  2464. }
  2465. var texts = Str(params.text).split("\n"),
  2466. tspans = [],
  2467. tspan;
  2468. for (var i = 0, ii = texts.length; i < ii; i++) {
  2469. tspan = $("tspan");
  2470. i && $(tspan, {dy: fontSize * leading, x: a.x});
  2471. tspan.appendChild(g.doc.createTextNode(texts[i]));
  2472. node.appendChild(tspan);
  2473. tspans[i] = tspan;
  2474. }
  2475. } else {
  2476. tspans = node.getElementsByTagName("tspan");
  2477. for (i = 0, ii = tspans.length; i < ii; i++) if (i) {
  2478. $(tspans[i], {dy: fontSize * leading, x: a.x});
  2479. } else {
  2480. $(tspans[0], {dy: 0});
  2481. }
  2482. }
  2483. $(node, {x: a.x, y: a.y});
  2484. el._.dirty = 1;
  2485. var bb = el._getBBox(),
  2486. dif = a.y - (bb.y + bb.height / 2);
  2487. dif && R.is(dif, "finite") && $(tspans[0], {dy: dif});
  2488. },
  2489. Element = function (node, svg) {
  2490. var X = 0,
  2491. Y = 0;
  2492. /*\
  2493. * Element.node
  2494. [ property (object) ]
  2495. **
  2496. * Gives you a reference to the DOM object, so you can assign event handlers or just mess around.
  2497. > Usage
  2498. | // draw a circle at coordinate 10,10 with radius of 10
  2499. | var c = paper.circle(10, 10, 10);
  2500. | c.node.onclick = function () {
  2501. | c.attr("fill", "red");
  2502. | };
  2503. \*/
  2504. this[0] = this.node = node;
  2505. /*\
  2506. * Element.raphael
  2507. [ property (object) ]
  2508. **
  2509. * Internal reference to @Raphael object. In case it is not available.
  2510. > Usage
  2511. | Raphael.el.red = function () {
  2512. | var hsb = this.paper.raphael.rgb2hsb(this.attr("fill"));
  2513. | hsb.h = 1;
  2514. | this.attr({fill: this.paper.raphael.hsb2rgb(hsb).hex});
  2515. | }
  2516. \*/
  2517. node.raphael = true;
  2518. /*\
  2519. * Element.id
  2520. [ property (number) ]
  2521. **
  2522. * Unique id of the element. Especially usesful when you want to listen to events of the element,
  2523. * because all events are fired in format `<module>.<action>.<id>`. Also useful for @Paper.getById method.
  2524. \*/
  2525. this.id = R._oid++;
  2526. node.raphaelid = this.id;
  2527. this.matrix = new Matrix;
  2528. this.realPath = null;
  2529. /*\
  2530. * Element.paper
  2531. [ property (object) ]
  2532. **
  2533. * Internal reference to “paper” where object drawn. Mainly for use in plugins and element extensions.
  2534. > Usage
  2535. | Raphael.el.cross = function () {
  2536. | this.attr({fill: "red"});
  2537. | this.paper.path("M10,10L50,50M50,10L10,50")
  2538. | .attr({stroke: "red"});
  2539. | }
  2540. \*/
  2541. this.paper = svg;
  2542. this.attrs = this.attrs || {};
  2543. this._ = {
  2544. transform: [],
  2545. sx: 1,
  2546. sy: 1,
  2547. deg: 0,
  2548. dx: 0,
  2549. dy: 0,
  2550. dirty: 1
  2551. };
  2552. !svg.bottom && (svg.bottom = this);
  2553. /*\
  2554. * Element.prev
  2555. [ property (object) ]
  2556. **
  2557. * Reference to the previous element in the hierarchy.
  2558. \*/
  2559. this.prev = svg.top;
  2560. svg.top && (svg.top.next = this);
  2561. svg.top = this;
  2562. /*\
  2563. * Element.next
  2564. [ property (object) ]
  2565. **
  2566. * Reference to the next element in the hierarchy.
  2567. \*/
  2568. this.next = null;
  2569. },
  2570. elproto = Element.prototype;
  2571. /*\
  2572. * Element.rotate
  2573. [ method ]
  2574. **
  2575. * Adds rotation by given angle around given point to the list of
  2576. * transformations of the element.
  2577. > Parameters
  2578. - deg (number) angle in degrees
  2579. - cx (number) #optional x coordinate of the centre of rotation
  2580. - cy (number) #optional y coordinate of the centre of rotation
  2581. * If cx & cy aren’t specified centre of the shape is used as a point of rotation.
  2582. = (object) @Element
  2583. \*/
  2584. elproto.rotate = function (deg, cx, cy) {
  2585. if (this.removed) {
  2586. return this;
  2587. }
  2588. deg = Str(deg).split(separator);
  2589. if (deg.length - 1) {
  2590. cx = toFloat(deg[1]);
  2591. cy = toFloat(deg[2]);
  2592. }
  2593. deg = toFloat(deg[0]);
  2594. (cy == null) && (cx = cy);
  2595. if (cx == null || cy == null) {
  2596. var bbox = this.getBBox(1);
  2597. cx = bbox.x + bbox.width / 2;
  2598. cy = bbox.y + bbox.height / 2;
  2599. }
  2600. this.transform(this._.transform.concat([["r", deg, cx, cy]]));
  2601. return this;
  2602. };
  2603. /*\
  2604. * Element.scale
  2605. [ method ]
  2606. **
  2607. * Adds scale by given amount relative to given point to the list of
  2608. * transformations of the element.
  2609. > Parameters
  2610. - sx (number) horisontal scale amount
  2611. - sy (number) vertical scale amount
  2612. - cx (number) #optional x coordinate of the centre of scale
  2613. - cy (number) #optional y coordinate of the centre of scale
  2614. * If cx & cy aren’t specified centre of the shape is used instead.
  2615. = (object) @Element
  2616. \*/
  2617. elproto.scale = function (sx, sy, cx, cy) {
  2618. if (this.removed) {
  2619. return this;
  2620. }
  2621. sx = Str(sx).split(separator);
  2622. if (sx.length - 1) {
  2623. sy = toFloat(sx[1]);
  2624. cx = toFloat(sx[2]);
  2625. cy = toFloat(sx[3]);
  2626. }
  2627. sx = toFloat(sx[0]);
  2628. (sy == null) && (sy = sx);
  2629. (cy == null) && (cx = cy);
  2630. if (cx == null || cy == null) {
  2631. var bbox = this.getBBox(1);
  2632. }
  2633. cx = cx == null ? bbox.x + bbox.width / 2 : cx;
  2634. cy = cy == null ? bbox.y + bbox.height / 2 : cy;
  2635. this.transform(this._.transform.concat([["s", sx, sy, cx, cy]]));
  2636. return this;
  2637. };
  2638. /*\
  2639. * Element.translate
  2640. [ method ]
  2641. **
  2642. * Adds translation by given amount to the list of transformations of the element.
  2643. > Parameters
  2644. - dx (number) horisontal shift
  2645. - dy (number) vertical shift
  2646. = (object) @Element
  2647. \*/
  2648. elproto.translate = function (dx, dy) {
  2649. if (this.removed) {
  2650. return this;
  2651. }
  2652. dx = Str(dx).split(separator);
  2653. if (dx.length - 1) {
  2654. dy = toFloat(dx[1]);
  2655. }
  2656. dx = toFloat(dx[0]) || 0;
  2657. dy = +dy || 0;
  2658. this.transform(this._.transform.concat([["t", dx, dy]]));
  2659. return this;
  2660. };
  2661. /*\
  2662. * Element.transform
  2663. [ method ]
  2664. **
  2665. * Adds transformation to the element which is separate to other attributes,
  2666. * i.e. translation doesn’t change `x` or `y` of the rectange. The format
  2667. * of transformation string is similar to the path string syntax:
  2668. | "t100,100r30,100,100s2,2,100,100r45s1.5"
  2669. * Each letter is a command. There are four commands: `t` is for translate, `r` is for rotate, `s` is for
  2670. * scale and `m` is for matrix.
  2671. *
  2672. * So, the example line above could be read like “translate by 100, 100; rotate 30° around 100, 100; scale twice around 100, 100;
  2673. * rotate 45° around centre; scale 1.5 times relative to centre”. As you can see rotate and scale commands have origin
  2674. * coordinates as optional parameters, the default is the centre point of the element.
  2675. * Matrix accepts six parameters.
  2676. > Usage
  2677. | var el = paper.rect(10, 20, 300, 200);
  2678. | // translate 100, 100, rotate 45°, translate -100, 0
  2679. | el.transform("t100,100r45t-100,0");
  2680. | // if you want you can append or prepend transformations
  2681. | el.transform("...t50,50");
  2682. | el.transform("s2...");
  2683. | // or even wrap
  2684. | el.transform("t50,50...t-50-50");
  2685. | // to reset transformation call method with empty string
  2686. | el.transform("");
  2687. | // to get current value call it without parameters
  2688. | console.log(el.transform());
  2689. > Parameters
  2690. - tstr (string) #optional transformation string
  2691. * If tstr isn’t specified
  2692. = (string) current transformation string
  2693. * else
  2694. = (object) @Element
  2695. \*/
  2696. elproto.transform = function (tstr) {
  2697. var _ = this._;
  2698. if (tstr == null) {
  2699. return _.transform;
  2700. }
  2701. extractTransform(this, tstr);
  2702. this.clip && $(this.clip, {transform: this.matrix.invert()});
  2703. this.pattern && updatePosition(this);
  2704. this.node && $(this.node, {transform: this.matrix});
  2705. if (_.sx != 1 || _.sy != 1) {
  2706. var sw = this.attrs[has]("stroke-width") ? this.attrs["stroke-width"] : 1;
  2707. this.attr({"stroke-width": sw});
  2708. }
  2709. return this;
  2710. };
  2711. /*\
  2712. * Element.hide
  2713. [ method ]
  2714. **
  2715. * Makes element invisible. See @Element.show.
  2716. = (object) @Element
  2717. \*/
  2718. elproto.hide = function () {
  2719. !this.removed && this.paper.safari(this.node.style.display = "none");
  2720. return this;
  2721. };
  2722. /*\
  2723. * Element.show
  2724. [ method ]
  2725. **
  2726. * Makes element visible. See @Element.hide.
  2727. = (object) @Element
  2728. \*/
  2729. elproto.show = function () {
  2730. !this.removed && this.paper.safari(this.node.style.display = "");
  2731. return this;
  2732. };
  2733. /*\
  2734. * Element.remove
  2735. [ method ]
  2736. **
  2737. * Removes element form the paper.
  2738. \*/
  2739. elproto.remove = function () {
  2740. if (this.removed) {
  2741. return;
  2742. }
  2743. eve.unbind("*.*." + this.id);
  2744. tear(this, this.paper);
  2745. this.node.parentNode.removeChild(this.node);
  2746. for (var i in this) {
  2747. delete this[i];
  2748. }
  2749. this.removed = true;
  2750. };
  2751. elproto._getBBox = function () {
  2752. if (this.node.style.display == "none") {
  2753. this.show();
  2754. var hide = true;
  2755. }
  2756. var bbox = {};
  2757. try {
  2758. bbox = this.node.getBBox();
  2759. } catch(e) {
  2760. // Firefox 3.0.x plays badly here
  2761. } finally {
  2762. bbox = bbox || {};
  2763. }
  2764. hide && this.hide();
  2765. return bbox;
  2766. };
  2767. /*\
  2768. * Element.attr
  2769. [ method ]
  2770. **
  2771. * Sets the attributes of the element.
  2772. > Parameters
  2773. - attrName (string) attribute’s name
  2774. - value (string) value
  2775. * or
  2776. - params (object) object of name/value pairs
  2777. * or
  2778. - attrName (string) attribute’s name
  2779. * or
  2780. - attrNames (array) in this case method returns array of current values for given attribute names
  2781. = (object) @Element if attrsName & value or params are passed in.
  2782. = (...) value of the attribute if only attrsName is passed in.
  2783. = (array) array of values of the attribute if attrsNames is passed in.
  2784. = (object) object of attributes if nothing is passed in.
  2785. > Possible parameters
  2786. # <p>Please refer to the <a href="http://www.w3.org/TR/SVG/" title="The W3C Recommendation for the SVG language describes these properties in detail.">SVG specification</a> for an explanation of these parameters.</p>
  2787. o arrow-end (string) arrowhead on the end of the path. The format for string is `<type>[-<width>[-<length>]]`. Possible types: `classic`, `block`, `open`, `oval`, `diamond`, `none`, width: `wide`, `narrow`, `midium`, length: `long`, `short`, `midium`.
  2788. o clip-rect (string) comma or space separated values: x, y, width and height
  2789. o cursor (string) CSS type of the cursor
  2790. o cx (number)
  2791. o cy (number)
  2792. o fill (string) colour, gradient or image
  2793. o fill-opacity (number)
  2794. o font (string)
  2795. o font-family (string)
  2796. o font-size (number) font size in pixels
  2797. o font-weight (string)
  2798. o height (number)
  2799. o href (string) URL, if specified element behaves as hyperlink
  2800. o opacity (number)
  2801. o path (string) SVG path string format
  2802. o r (number)
  2803. o rx (number)
  2804. o ry (number)
  2805. o src (string) image URL, only works for @Element.image element
  2806. o stroke (string) stroke colour
  2807. o stroke-dasharray (string) [“”, “`-`”, “`.`”, “`-.`”, “`-..`”, “`. `”, “`- `”, “`--`”, “`- .`”, “`--.`”, “`--..`”]
  2808. o stroke-linecap (string) [“`butt`”, “`square`”, “`round`”]
  2809. o stroke-linejoin (string) [“`bevel`”, “`round`”, “`miter`”]
  2810. o stroke-miterlimit (number)
  2811. o stroke-opacity (number)
  2812. o stroke-width (number) stroke width in pixels, default is '1'
  2813. o target (string) used with href
  2814. o text (string) contents of the text element. Use `\n` for multiline text
  2815. o text-anchor (string) [“`start`”, “`middle`”, “`end`”], default is “`middle`”
  2816. o title (string) will create tooltip with a given text
  2817. o transform (string) see @Element.transform
  2818. o width (number)
  2819. o x (number)
  2820. o y (number)
  2821. > Gradients
  2822. * Linear gradient format: “`‹angle›-‹colour›[-‹colour›[:‹offset›]]*-‹colour›`”, example: “`90-#fff-#000`” – 90°
  2823. * gradient from white to black or “`0-#fff-#f00:20-#000`” – 0° gradient from white via red (at 20%) to black.
  2824. *
  2825. * radial gradient: “`r[(‹fx›, ‹fy›)]‹colour›[-‹colour›[:‹offset›]]*-‹colour›`”, example: “`r#fff-#000`” –
  2826. * gradient from white to black or “`r(0.25, 0.75)#fff-#000`” – gradient from white to black with focus point
  2827. * at 0.25, 0.75. Focus point coordinates are in 0..1 range. Radial gradients can only be applied to circles and ellipses.
  2828. > Path String
  2829. # <p>Please refer to <a href="http://www.w3.org/TR/SVG/paths.html#PathData" title="Details of a path’s data attribute’s format are described in the SVG specification.">SVG documentation regarding path string</a>. Raphaël fully supports it.</p>
  2830. > Colour Parsing
  2831. # <ul>
  2832. # <li>Colour name (“<code>red</code>”, “<code>green</code>”, “<code>cornflowerblue</code>”, etc)</li>
  2833. # <li>#••• — shortened HTML colour: (“<code>#000</code>”, “<code>#fc0</code>”, etc)</li>
  2834. # <li>#•••••• — full length HTML colour: (“<code>#000000</code>”, “<code>#bd2300</code>”)</li>
  2835. # <li>rgb(•••, •••, •••) — red, green and blue channels’ values: (“<code>rgb(200,&nbsp;100,&nbsp;0)</code>”)</li>
  2836. # <li>rgb(•••%, •••%, •••%) — same as above, but in %: (“<code>rgb(100%,&nbsp;175%,&nbsp;0%)</code>”)</li>
  2837. # <li>rgba(•••, •••, •••, •••) — red, green and blue channels’ values: (“<code>rgba(200,&nbsp;100,&nbsp;0, .5)</code>”)</li>
  2838. # <li>rgba(•••%, •••%, •••%, •••%) — same as above, but in %: (“<code>rgba(100%,&nbsp;175%,&nbsp;0%, 50%)</code>”)</li>
  2839. # <li>hsb(•••, •••, •••) — hue, saturation and brightness values: (“<code>hsb(0.5,&nbsp;0.25,&nbsp;1)</code>”)</li>
  2840. # <li>hsb(•••%, •••%, •••%) — same as above, but in %</li>
  2841. # <li>hsba(•••, •••, •••, •••) — same as above, but with opacity</li>
  2842. # <li>hsl(•••, •••, •••) — almost the same as hsb, see <a href="http://en.wikipedia.org/wiki/HSL_and_HSV" title="HSL and HSV - Wikipedia, the free encyclopedia">Wikipedia page</a></li>
  2843. # <li>hsl(•••%, •••%, •••%) — same as above, but in %</li>
  2844. # <li>hsla(•••, •••, •••) — same as above, but with opacity</li>
  2845. # <li>Optionally for hsb and hsl you could specify hue as a degree: “<code>hsl(240deg,&nbsp;1,&nbsp;.5)</code>” or, if you want to go fancy, “<code>hsl(240°,&nbsp;1,&nbsp;.5)</code>”</li>
  2846. # </ul>
  2847. \*/
  2848. elproto.attr = function (name, value) {
  2849. if (this.removed) {
  2850. return this;
  2851. }
  2852. if (name == null) {
  2853. var res = {};
  2854. for (var a in this.attrs) if (this.attrs[has](a)) {
  2855. res[a] = this.attrs[a];
  2856. }
  2857. res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
  2858. res.transform = this._.transform;
  2859. return res;
  2860. }
  2861. if (value == null && R.is(name, string)) {
  2862. if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
  2863. return this.attrs.gradient;
  2864. }
  2865. if (name == "transform") {
  2866. return this._.transform;
  2867. }
  2868. var names = name.split(separator),
  2869. out = {};
  2870. for (var i = 0, ii = names.length; i < ii; i++) {
  2871. name = names[i];
  2872. if (name in this.attrs) {
  2873. out[name] = this.attrs[name];
  2874. } else if (R.is(this.paper.customAttributes[name], "function")) {
  2875. out[name] = this.paper.customAttributes[name].def;
  2876. } else {
  2877. out[name] = availableAttrs[name];
  2878. }
  2879. }
  2880. return ii - 1 ? out : out[names[0]];
  2881. }
  2882. if (value == null && R.is(name, array)) {
  2883. out = {};
  2884. for (i = 0, ii = name.length; i < ii; i++) {
  2885. out[name[i]] = this.attr(name[i]);
  2886. }
  2887. return out;
  2888. }
  2889. if (value != null) {
  2890. var params = {};
  2891. params[name] = value;
  2892. } else if (name != null && R.is(name, "object")) {
  2893. params = name;
  2894. }
  2895. for (var key in this.paper.customAttributes) if (this.paper.customAttributes[has](key) && params[has](key) && R.is(this.paper.customAttributes[key], "function")) {
  2896. var par = this.paper.customAttributes[key].apply(this, [][concat](params[key]));
  2897. this.attrs[key] = params[key];
  2898. for (var subkey in par) if (par[has](subkey)) {
  2899. params[subkey] = par[subkey];
  2900. }
  2901. }
  2902. setFillAndStroke(this, params);
  2903. return this;
  2904. };
  2905. /*\
  2906. * Element.toFront
  2907. [ method ]
  2908. **
  2909. * Moves the element so it is the closest to the viewer’s eyes, on top of other elements.
  2910. = (object) @Element
  2911. \*/
  2912. elproto.toFront = function () {
  2913. if (this.removed) {
  2914. return this;
  2915. }
  2916. this.node.parentNode.appendChild(this.node);
  2917. var svg = this.paper;
  2918. svg.top != this && tofront(this, svg);
  2919. return this;
  2920. };
  2921. /*\
  2922. * Element.toBack
  2923. [ method ]
  2924. **
  2925. * Moves the element so it is the furthest from the viewer’s eyes, behind other elements.
  2926. = (object) @Element
  2927. \*/
  2928. elproto.toBack = function () {
  2929. if (this.removed) {
  2930. return this;
  2931. }
  2932. if (this.node.parentNode.firstChild != this.node) {
  2933. this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
  2934. toback(this, this.paper);
  2935. var svg = this.paper;
  2936. }
  2937. return this;
  2938. };
  2939. /*\
  2940. * Element.insertAfter
  2941. [ method ]
  2942. **
  2943. * Inserts current object after the given one.
  2944. = (object) @Element
  2945. \*/
  2946. elproto.insertAfter = function (element) {
  2947. if (this.removed) {
  2948. return this;
  2949. }
  2950. var node = element.node || element[element.length - 1].node;
  2951. if (node.nextSibling) {
  2952. node.parentNode.insertBefore(this.node, node.nextSibling);
  2953. } else {
  2954. node.parentNode.appendChild(this.node);
  2955. }
  2956. insertafter(this, element, this.paper);
  2957. return this;
  2958. };
  2959. /*\
  2960. * Element.insertBefore
  2961. [ method ]
  2962. **
  2963. * Inserts current object before the given one.
  2964. = (object) @Element
  2965. \*/
  2966. elproto.insertBefore = function (element) {
  2967. if (this.removed) {
  2968. return this;
  2969. }
  2970. var node = element.node || element[0].node;
  2971. node.parentNode.insertBefore(this.node, node);
  2972. insertbefore(this, element, this.paper);
  2973. return this;
  2974. };
  2975. elproto.blur = function (size) {
  2976. // Experimental. No Safari support. Use it on your own risk.
  2977. var t = this;
  2978. if (+size !== 0) {
  2979. var fltr = $("filter"),
  2980. blur = $("feGaussianBlur");
  2981. t.attrs.blur = size;
  2982. fltr.id = createUUID();
  2983. $(blur, {stdDeviation: +size || 1.5});
  2984. fltr.appendChild(blur);
  2985. t.paper.defs.appendChild(fltr);
  2986. t._blur = fltr;
  2987. $(t.node, {filter: "url(#" + fltr.id + ")"});
  2988. } else {
  2989. if (t._blur) {
  2990. t._blur.parentNode.removeChild(t._blur);
  2991. delete t._blur;
  2992. delete t.attrs.blur;
  2993. }
  2994. t.node.removeAttribute("filter");
  2995. }
  2996. };
  2997. var theCircle = function (svg, x, y, r) {
  2998. var el = $("circle");
  2999. svg.canvas && svg.canvas.appendChild(el);
  3000. var res = new Element(el, svg);
  3001. res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
  3002. res.type = "circle";
  3003. $(el, res.attrs);
  3004. return res;
  3005. },
  3006. theRect = function (svg, x, y, w, h, r) {
  3007. var el = $("rect");
  3008. svg.canvas && svg.canvas.appendChild(el);
  3009. var res = new Element(el, svg);
  3010. res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
  3011. res.type = "rect";
  3012. $(el, res.attrs);
  3013. return res;
  3014. },
  3015. theEllipse = function (svg, x, y, rx, ry) {
  3016. var el = $("ellipse");
  3017. svg.canvas && svg.canvas.appendChild(el);
  3018. var res = new Element(el, svg);
  3019. res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
  3020. res.type = "ellipse";
  3021. $(el, res.attrs);
  3022. return res;
  3023. },
  3024. theImage = function (svg, src, x, y, w, h) {
  3025. var el = $("image");
  3026. $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
  3027. el.setAttributeNS(xlink, "href", src);
  3028. svg.canvas && svg.canvas.appendChild(el);
  3029. var res = new Element(el, svg);
  3030. res.attrs = {x: x, y: y, width: w, height: h, src: src};
  3031. res.type = "image";
  3032. return res;
  3033. },
  3034. theText = function (svg, x, y, text) {
  3035. var el = $("text");
  3036. // $(el, {x: x, y: y, "text-anchor": "middle"});
  3037. svg.canvas && svg.canvas.appendChild(el);
  3038. var res = new Element(el, svg);
  3039. res.attrs = {
  3040. x: x,
  3041. y: y,
  3042. "text-anchor": "middle",
  3043. text: text,
  3044. font: availableAttrs.font,
  3045. stroke: "none",
  3046. fill: "#000"
  3047. };
  3048. res.type = "text";
  3049. setFillAndStroke(res, res.attrs);
  3050. return res;
  3051. },
  3052. setSize = function (width, height) {
  3053. this.width = width || this.width;
  3054. this.height = height || this.height;
  3055. this.canvas[setAttribute]("width", this.width);
  3056. this.canvas[setAttribute]("height", this.height);
  3057. if (this._viewBox) {
  3058. this.setViewBox.apply(this, this._viewBox);
  3059. }
  3060. return this;
  3061. },
  3062. create = function () {
  3063. var con = getContainer[apply](0, arguments),
  3064. container = con && con.container,
  3065. x = con.x,
  3066. y = con.y,
  3067. width = con.width,
  3068. height = con.height;
  3069. if (!container) {
  3070. throw new Error("SVG container not found.");
  3071. }
  3072. var cnvs = $("svg"),
  3073. css = "overflow:hidden;",
  3074. isFloating;
  3075. x = x || 0;
  3076. y = y || 0;
  3077. width = width || 512;
  3078. height = height || 342;
  3079. $(cnvs, {
  3080. height: height,
  3081. version: 1.1,
  3082. width: width,
  3083. xmlns: "http://www.w3.org/2000/svg"
  3084. });
  3085. if (container == 1) {
  3086. cnvs.style.cssText = css + "position:absolute;left:" + x + "px;top:" + y + "px";
  3087. g.doc.body.appendChild(cnvs);
  3088. isFloating = 1;
  3089. } else {
  3090. cnvs.style.cssText = css + "position:relative";
  3091. if (container.firstChild) {
  3092. container.insertBefore(cnvs, container.firstChild);
  3093. } else {
  3094. container.appendChild(cnvs);
  3095. }
  3096. }
  3097. container = new Paper;
  3098. container.width = width;
  3099. container.height = height;
  3100. container.canvas = cnvs;
  3101. plugins.call(container, container, R.fn);
  3102. container.clear();
  3103. container._left = container._top = 0;
  3104. isFloating && (container.renderfix = fun);
  3105. container.renderfix();
  3106. return container;
  3107. },
  3108. setViewBox = function (x, y, w, h, fit) {
  3109. eve("setViewBox", this, this._viewBox, [x, y, w, h, fit]);
  3110. var size = mmax(w / this.width, h / this.height),
  3111. top = this.top,
  3112. aspectRatio = fit ? "meet" : "xMinYMin",
  3113. vb,
  3114. sw;
  3115. if (x == null) {
  3116. if (this._vbSize) {
  3117. size = 1;
  3118. }
  3119. delete this._vbSize;
  3120. vb = "0 0 " + this.width + S + this.height;
  3121. } else {
  3122. this._vbSize = size;
  3123. vb = x + S + y + S + w + S + h;
  3124. }
  3125. $(this.canvas, {
  3126. viewBox: vb,
  3127. preserveAspectRatio: aspectRatio
  3128. });
  3129. while (size && top) {
  3130. sw = "stroke-width" in top.attrs ? top.attrs["stroke-width"] : 1;
  3131. top.attr({"stroke-width": sw});
  3132. top._.dirty = 1;
  3133. top._.dirtyT = 1;
  3134. top = top.prev;
  3135. }
  3136. this._viewBox = [x, y, w, h, !!fit];
  3137. return this;
  3138. };
  3139. /*\
  3140. * Paper.renderfix
  3141. [ method ]
  3142. **
  3143. * Fixes the issue of Firefox and IE9 regarding subpixel rendering. If paper is dependant
  3144. * on other elements after reflow it could shift half pixel which cause for lines to lost their crispness.
  3145. * This method fixes the issue.
  3146. **
  3147. Special thanks to Mariusz Nowak (http://www.medikoo.com/) for this method.
  3148. \*/
  3149. paperproto.renderfix = function () {
  3150. var cnvs = this.canvas,
  3151. s = cnvs.style,
  3152. pos = cnvs.getScreenCTM(),
  3153. left = -pos.e % 1,
  3154. top = -pos.f % 1;
  3155. if (left || top) {
  3156. if (left) {
  3157. this._left = (this._left + left) % 1;
  3158. s.left = this._left + "px";
  3159. }
  3160. if (top) {
  3161. this._top = (this._top + top) % 1;
  3162. s.top = this._top + "px";
  3163. }
  3164. }
  3165. };
  3166. /*\
  3167. * Paper.clear
  3168. [ method ]
  3169. **
  3170. * Clears the paper, i.e. removes all the elements.
  3171. \*/
  3172. paperproto.clear = function () {
  3173. eve("clear", this);
  3174. var c = this.canvas;
  3175. while (c.firstChild) {
  3176. c.removeChild(c.firstChild);
  3177. }
  3178. this.bottom = this.top = null;
  3179. (this.desc = $("desc")).appendChild(g.doc.createTextNode("Created with Rapha\xebl " + R.version));
  3180. c.appendChild(this.desc);
  3181. c.appendChild(this.defs = $("defs"));
  3182. };
  3183. /*\
  3184. * Paper.remove
  3185. [ method ]
  3186. **
  3187. * Removes the paper from the DOM.
  3188. \*/
  3189. paperproto.remove = function () {
  3190. eve("remove", this);
  3191. this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
  3192. for (var i in this) {
  3193. this[i] = removed(i);
  3194. }
  3195. };
  3196. }
  3197. // VML
  3198. if (R.vml) {
  3199. var map = {M: "m", L: "l", C: "c", Z: "x", m: "t", l: "r", c: "v", z: "x"},
  3200. bites = /([clmz]),?([^clmz]*)/gi,
  3201. blurregexp = / progid:\S+Blur\([^\)]+\)/g,
  3202. val = /-?[^,\s-]+/g,
  3203. cssDot = "position:absolute;left:0;top:0;width:1px;height:1px",
  3204. zoom = 21600,
  3205. pathTypes = {path: 1, rect: 1},
  3206. ovalTypes = {circle: 1, ellipse: 1},
  3207. path2vml = function (path) {
  3208. var total = /[ahqstv]/ig,
  3209. command = pathToAbsolute;
  3210. Str(path).match(total) && (command = path2curve);
  3211. total = /[clmz]/g;
  3212. if (command == pathToAbsolute && !Str(path).match(total)) {
  3213. var res = Str(path).replace(bites, function (all, command, args) {
  3214. var vals = [],
  3215. isMove = lowerCase.call(command) == "m",
  3216. res = map[command];
  3217. args.replace(val, function (value) {
  3218. if (isMove && vals.length == 2) {
  3219. res += vals + map[command == "m" ? "l" : "L"];
  3220. vals = [];
  3221. }
  3222. vals.push(round(value * zoom));
  3223. });
  3224. return res + vals;
  3225. });
  3226. return res;
  3227. }
  3228. var pa = command(path), p, r;
  3229. res = [];
  3230. for (var i = 0, ii = pa.length; i < ii; i++) {
  3231. p = pa[i];
  3232. r = lowerCase.call(pa[i][0]);
  3233. r == "z" && (r = "x");
  3234. for (var j = 1, jj = p.length; j < jj; j++) {
  3235. r += round(p[j] * zoom) + (j != jj - 1 ? "," : E);
  3236. }
  3237. res.push(r);
  3238. }
  3239. return res.join(S);
  3240. },
  3241. compensation = function (deg, dx, dy) {
  3242. var m = new Matrix;
  3243. m.rotate(-deg, .5, .5);
  3244. return {
  3245. dx: m.x(dx, dy),
  3246. dy: m.y(dx, dy)
  3247. };
  3248. },
  3249. setCoords = function (p, sx, sy, dx, dy, deg) {
  3250. var _ = p._,
  3251. m = p.matrix,
  3252. // sp = m.split(),
  3253. // sx = sp.scalex,
  3254. // sy = sp.scaley,
  3255. // deg = sp.rotate,
  3256. // dx = sp.dx,
  3257. // dy = sp.dy,
  3258. fillpos = _.fillpos,
  3259. o = p.node,
  3260. s = o.style,
  3261. y = 1,
  3262. flip = "",
  3263. dxdy,
  3264. kx = zoom / sx,
  3265. ky = zoom / sy;
  3266. s.visibility = "hidden";
  3267. o.coordsize = abs(kx) + S + abs(ky);
  3268. s.rotation = deg * (sx * sy < 0 ? -1 : 1);
  3269. if (deg) {
  3270. var c = compensation(deg, dx, dy);
  3271. dx = c.dx;
  3272. dy = c.dy;
  3273. }
  3274. sx < 0 && (flip += "x");
  3275. sy < 0 && (flip += " y") && (y = -1);
  3276. s.flip = flip;
  3277. o.coordorigin = (dx * -kx) + S + (dy * -ky);
  3278. if (fillpos || _.fillsize) {
  3279. var fill = o.getElementsByTagName(fillString);
  3280. fill = fill && fill[0];
  3281. o.removeChild(fill);
  3282. if (fillpos) {
  3283. c = compensation(deg, m.x(fillpos[0], fillpos[1]), m.y(fillpos[0], fillpos[1]));
  3284. fill.position = c.dx * y + S + c.dy * y;
  3285. }
  3286. if (_.fillsize) {
  3287. fill.size = _.fillsize[0] * abs(sx) + S + _.fillsize[1] * abs(sy);
  3288. }
  3289. o.appendChild(fill);
  3290. }
  3291. s.visibility = "visible";
  3292. };
  3293. R.toString = function () {
  3294. return "Your browser doesn\u2019t support SVG. Falling down to VML.\nYou are running Rapha\xebl " + this.version;
  3295. };
  3296. addArrow = function (o, value, isEnd) {
  3297. var values = Str(value).toLowerCase().split("-"),
  3298. se = isEnd ? "end" : "start",
  3299. i = values.length,
  3300. type = "classic",
  3301. w = "medium",
  3302. h = "medium";
  3303. while (i--) {
  3304. switch (values[i]) {
  3305. case "block":
  3306. case "classic":
  3307. case "oval":
  3308. case "diamond":
  3309. case "open":
  3310. case "none":
  3311. type = values[i];
  3312. break;
  3313. case "wide":
  3314. case "narrow": h = values[i]; break;
  3315. case "long":
  3316. case "short": w = values[i]; break;
  3317. }
  3318. }
  3319. var stroke = o.node.getElementsByTagName("stroke")[0];
  3320. stroke[se + "arrow"] = type;
  3321. stroke[se + "arrowlength"] = w;
  3322. stroke[se + "arrowwidth"] = h;
  3323. };
  3324. setFillAndStroke = function (o, params) {
  3325. o.paper.canvas.style.display = "none";
  3326. o.attrs = o.attrs || {};
  3327. var node = o.node,
  3328. a = o.attrs,
  3329. s = node.style,
  3330. xy,
  3331. newpath = pathTypes[o.type] && (params.x != a.x || params.y != a.y || params.width != a.width || params.height != a.height || params.cx != a.cx || params.cy != a.cy || params.rx != a.rx || params.ry != a.ry || params.r != a.r),
  3332. isOval = ovalTypes[o.type] && (a.cx != params.cx || a.cy != params.cy || a.r != params.r || a.rx != params.rx || a.ry != params.ry),
  3333. res = o;
  3334. for (var par in params) if (params[has](par)) {
  3335. a[par] = params[par];
  3336. }
  3337. if (newpath) {
  3338. a.path = getPath[o.type](o);
  3339. o._.dirty = 1;
  3340. }
  3341. params.href && (node.href = params.href);
  3342. params.title && (node.title = params.title);
  3343. params.target && (node.target = params.target);
  3344. params.cursor && (s.cursor = params.cursor);
  3345. "blur" in params && o.blur(params.blur);
  3346. "transform" in params && o.transform(params.transform);
  3347. if (params.path && o.type == "path" || newpath) {
  3348. node.path = path2vml(a.path);
  3349. }
  3350. if (isOval) {
  3351. var cx = a.cx,
  3352. cy = a.cy,
  3353. rx = a.rx || a.r || 0,
  3354. ry = a.ry || a.r || 0;
  3355. node.path = R.format("ar{0},{1},{2},{3},{4},{1},{4},{1}x", round((cx - rx) * zoom), round((cy - ry) * zoom), round((cx + rx) * zoom), round((cy + ry) * zoom), round(cx * zoom));
  3356. }
  3357. if ("clip-rect" in params) {
  3358. var rect = Str(params["clip-rect"]).split(separator);
  3359. if (rect.length == 4) {
  3360. rect[2] = +rect[2] + (+rect[0]);
  3361. rect[3] = +rect[3] + (+rect[1]);
  3362. var div = node.clipRect || g.doc.createElement("div"),
  3363. dstyle = div.style,
  3364. group = node.parentNode;
  3365. dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
  3366. if (!node.clipRect) {
  3367. dstyle.position = "absolute";
  3368. dstyle.top = 0;
  3369. dstyle.left = 0;
  3370. dstyle.width = o.paper.width + "px";
  3371. dstyle.height = o.paper.height + "px";
  3372. group.parentNode.insertBefore(div, group);
  3373. div.appendChild(group);
  3374. node.clipRect = div;
  3375. }
  3376. }
  3377. if (!params["clip-rect"]) {
  3378. node.clipRect && (node.clipRect.style.clip = E);
  3379. }
  3380. }
  3381. if (o.textpath) {
  3382. var textpathStyle = o.textpath.style;
  3383. params.font && (textpathStyle.font = params.font);
  3384. params["font-family"] && (textpathStyle.fontFamily = '"' + params["font-family"].split(",")[0].replace(/^['"]+|['"]+$/g, E) + '"');
  3385. params["font-size"] && (textpathStyle.fontSize = params["font-size"]);
  3386. params["font-weight"] && (textpathStyle.fontWeight = params["font-weight"]);
  3387. params["font-style"] && (textpathStyle.fontStyle = params["font-style"]);
  3388. }
  3389. if ("arrow-start" in params) {
  3390. addArrow(res, params["arrow-start"]);
  3391. }
  3392. if ("arrow-end" in params) {
  3393. addArrow(res, params["arrow-end"], 1);
  3394. }
  3395. if (params.opacity != null ||
  3396. params["stroke-width"] != null ||
  3397. params.fill != null ||
  3398. params.src != null ||
  3399. params.stroke != null ||
  3400. params["stroke-width"] != null ||
  3401. params["stroke-opacity"] != null ||
  3402. params["fill-opacity"] != null ||
  3403. params["stroke-dasharray"] != null ||
  3404. params["stroke-miterlimit"] != null ||
  3405. params["stroke-linejoin"] != null ||
  3406. params["stroke-linecap"] != null) {
  3407. var fill = node.getElementsByTagName(fillString),
  3408. newfill = false;
  3409. fill = fill && fill[0];
  3410. !fill && (newfill = fill = createNode(fillString));
  3411. if (o.type == "image" && params.src) {
  3412. fill.src = params.src;
  3413. }
  3414. if ("fill-opacity" in params || "opacity" in params) {
  3415. var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+R.getRGB(params.fill).o + 1 || 2) - 1);
  3416. opacity = mmin(mmax(opacity, 0), 1);
  3417. fill.opacity = opacity;
  3418. }
  3419. params.fill && (fill.on = true);
  3420. if (fill.on == null || params.fill == "none" || params.fill === null) {
  3421. fill.on = false;
  3422. }
  3423. if (fill.on && params.fill) {
  3424. var isURL = Str(params.fill).match(ISURL);
  3425. if (isURL) {
  3426. fill.parentNode == node && node.removeChild(fill);
  3427. fill.rotate = true;
  3428. fill.src = isURL[1];
  3429. fill.type = "tile";
  3430. var bbox = o.getBBox(1);
  3431. fill.position = bbox.x + S + bbox.y;
  3432. o._.fillpos = [bbox.x, bbox.y];
  3433. preload(isURL[1], function () {
  3434. o._.fillsize = [this.offsetWidth, this.offsetHeight];
  3435. });
  3436. } else {
  3437. fill.color = R.getRGB(params.fill).hex;
  3438. fill.src = E;
  3439. fill.type = "solid";
  3440. if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || Str(params.fill).charAt() != "r") && addGradientFill(res, params.fill, fill)) {
  3441. a.fill = "none";
  3442. a.gradient = params.fill;
  3443. fill.rotate = false;
  3444. }
  3445. }
  3446. }
  3447. node.appendChild(fill);
  3448. var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
  3449. newstroke = false;
  3450. !stroke && (newstroke = stroke = createNode("stroke"));
  3451. if ((params.stroke && params.stroke != "none") ||
  3452. params["stroke-width"] ||
  3453. params["stroke-opacity"] != null ||
  3454. params["stroke-dasharray"] ||
  3455. params["stroke-miterlimit"] ||
  3456. params["stroke-linejoin"] ||
  3457. params["stroke-linecap"]) {
  3458. stroke.on = true;
  3459. }
  3460. (params.stroke == "none" || params.stroke === null || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
  3461. var strokeColor = R.getRGB(params.stroke);
  3462. stroke.on && params.stroke && (stroke.color = strokeColor.hex);
  3463. opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+strokeColor.o + 1 || 2) - 1);
  3464. var width = (toFloat(params["stroke-width"]) || 1) * .75;
  3465. opacity = mmin(mmax(opacity, 0), 1);
  3466. params["stroke-width"] == null && (width = a["stroke-width"]);
  3467. params["stroke-width"] && (stroke.weight = width);
  3468. width && width < 1 && (opacity *= width) && (stroke.weight = 1);
  3469. stroke.opacity = opacity;
  3470. params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
  3471. stroke.miterlimit = params["stroke-miterlimit"] || 8;
  3472. params["stroke-linecap"] && (stroke.endcap = params["stroke-linecap"] == "butt" ? "flat" : params["stroke-linecap"] == "square" ? "square" : "round");
  3473. if (params["stroke-dasharray"]) {
  3474. var dasharray = {
  3475. "-": "shortdash",
  3476. ".": "shortdot",
  3477. "-.": "shortdashdot",
  3478. "-..": "shortdashdotdot",
  3479. ". ": "dot",
  3480. "- ": "dash",
  3481. "--": "longdash",
  3482. "- .": "dashdot",
  3483. "--.": "longdashdot",
  3484. "--..": "longdashdotdot"
  3485. };
  3486. stroke.dashstyle = dasharray[has](params["stroke-dasharray"]) ? dasharray[params["stroke-dasharray"]] : E;
  3487. }
  3488. newstroke && node.appendChild(stroke);
  3489. }
  3490. if (res.type == "text") {
  3491. res.paper.canvas.style.display = E;
  3492. var span = res.paper.span,
  3493. m = 100,
  3494. fontSize = a.font && a.font.match(/\d+(?:\.\d*)?(?=px)/);
  3495. s = span.style;
  3496. a.font && (s.font = a.font);
  3497. a["font-family"] && (s.fontFamily = a["font-family"]);
  3498. a["font-weight"] && (s.fontWeight = a["font-weight"]);
  3499. a["font-style"] && (s.fontStyle = a["font-style"]);
  3500. fontSize = toFloat(fontSize ? fontSize[0] : a["font-size"]);
  3501. s.fontSize = fontSize * m + "px";
  3502. res.textpath.string && (span.innerHTML = Str(res.textpath.string).replace(/</g, "&#60;").replace(/&/g, "&#38;").replace(/\n/g, "<br>"));
  3503. var brect = span.getBoundingClientRect();
  3504. res.W = a.w = (brect.right - brect.left) / m;
  3505. res.H = a.h = (brect.bottom - brect.top) / m;
  3506. res.paper.canvas.style.display = "none";
  3507. res.X = a.x;
  3508. res.Y = a.y + res.H / 2;
  3509. ("x" in params || "y" in params) && (res.path.v = R.format("m{0},{1}l{2},{1}", round(a.x * zoom), round(a.y * zoom), round(a.x * zoom) + 1));
  3510. var dirtyattrs = ["x", "y", "text", "font", "font-family", "font-weight", "font-style", "font-size"];
  3511. for (var d = 0, dd = dirtyattrs.length; d < dd; d++) if (dirtyattrs[d] in params) {
  3512. res._.dirty = 1;
  3513. break;
  3514. }
  3515. // text-anchor emulation
  3516. switch (a["text-anchor"]) {
  3517. case "start":
  3518. res.textpath.style["v-text-align"] = "left";
  3519. res.bbx = res.W / 2;
  3520. break;
  3521. case "end":
  3522. res.textpath.style["v-text-align"] = "right";
  3523. res.bbx = -res.W / 2;
  3524. break;
  3525. default:
  3526. res.textpath.style["v-text-align"] = "center";
  3527. res.bbx = 0;
  3528. break;
  3529. }
  3530. res.textpath.style["v-text-kern"] = true;
  3531. }
  3532. res.paper.canvas.style.display = E;
  3533. };
  3534. addGradientFill = function (o, gradient, fill) {
  3535. o.attrs = o.attrs || {};
  3536. var attrs = o.attrs,
  3537. opacity,
  3538. oindex,
  3539. type = "linear",
  3540. fxfy = ".5 .5";
  3541. o.attrs.gradient = gradient;
  3542. gradient = Str(gradient).replace(radial_gradient, function (all, fx, fy) {
  3543. type = "radial";
  3544. if (fx && fy) {
  3545. fx = toFloat(fx);
  3546. fy = toFloat(fy);
  3547. pow(fx - .5, 2) + pow(fy - .5, 2) > .25 && (fy = math.sqrt(.25 - pow(fx - .5, 2)) * ((fy > .5) * 2 - 1) + .5);
  3548. fxfy = fx + S + fy;
  3549. }
  3550. return E;
  3551. });
  3552. gradient = gradient.split(/\s*\-\s*/);
  3553. if (type == "linear") {
  3554. var angle = gradient.shift();
  3555. angle = -toFloat(angle);
  3556. if (isNaN(angle)) {
  3557. return null;
  3558. }
  3559. }
  3560. var dots = parseDots(gradient);
  3561. if (!dots) {
  3562. return null;
  3563. }
  3564. o = o.shape || o.node;
  3565. if (dots.length) {
  3566. o.removeChild(fill);
  3567. fill.on = true;
  3568. fill.method = "none";
  3569. fill.color = dots[0].color;
  3570. fill.color2 = dots[dots.length - 1].color;
  3571. var clrs = [];
  3572. for (var i = 0, ii = dots.length; i < ii; i++) {
  3573. dots[i].offset && clrs.push(dots[i].offset + S + dots[i].color);
  3574. }
  3575. fill.colors = clrs.length ? clrs.join() : "0% " + fill.color;
  3576. if (type == "radial") {
  3577. fill.type = "gradientTitle";
  3578. fill.focus = "100%";
  3579. fill.focussize = "0 0";
  3580. fill.focusposition = fxfy;
  3581. fill.angle = 0;
  3582. } else {
  3583. // fill.rotate= true;
  3584. fill.type = "gradient";
  3585. fill.angle = (270 - angle) % 360;
  3586. }
  3587. o.appendChild(fill);
  3588. }
  3589. return 1;
  3590. };
  3591. Element = function (node, vml) {
  3592. this[0] = this.node = node;
  3593. node.raphael = true;
  3594. this.id = R._oid++;
  3595. node.raphaelid = this.id;
  3596. this.X = 0;
  3597. this.Y = 0;
  3598. this.attrs = {};
  3599. this.paper = vml;
  3600. this.matrix = new Matrix;
  3601. this._ = {
  3602. transform: [],
  3603. sx: 1,
  3604. sy: 1,
  3605. dx: 0,
  3606. dy: 0,
  3607. deg: 0,
  3608. dirty: 1,
  3609. dirtyT: 1
  3610. };
  3611. !vml.bottom && (vml.bottom = this);
  3612. this.prev = vml.top;
  3613. vml.top && (vml.top.next = this);
  3614. vml.top = this;
  3615. this.next = null;
  3616. };
  3617. elproto = Element.prototype;
  3618. elproto.transform = function (tstr) {
  3619. if (tstr == null) {
  3620. return this._.transform;
  3621. }
  3622. extractTransform(this, tstr);
  3623. var matrix = this.matrix.clone(),
  3624. vbs = this.paper._viewBoxShift,
  3625. skew = this.skew,
  3626. o = this.node,
  3627. split,
  3628. isGrad = Str(this.attrs.fill).indexOf("-") + 1;
  3629. matrix.translate(-.5, -.5);
  3630. if (vbs) {
  3631. matrix.scale(vbs.scale, vbs.scale, -1, -1);
  3632. matrix.translate(vbs.dx, vbs.dy);
  3633. }
  3634. if (isGrad || this.type == "image") {
  3635. skew.matrix = "1 0 0 1";
  3636. skew.offset = "0 0";
  3637. // just translation?
  3638. if (isGrad || !(split = matrix.split()).isSimple) {
  3639. o.style.filter = matrix.toFilter();
  3640. var bb = this.getBBox(),
  3641. bbt = this.getBBox(1),
  3642. dx = bb.x - bbt.x,
  3643. dy = bb.y - bbt.y;
  3644. o.coordorigin = (dx * -zoom) + S + (dy * -zoom);
  3645. setCoords(this, 1, 1, dx, dy, 0);
  3646. } else {
  3647. o.style.filter = E;
  3648. setCoords(this, split.scalex, split.scaley, split.dx, split.dy, split.rotate);
  3649. }
  3650. } else {
  3651. o.style.filter = E;
  3652. skew.matrix = Str(matrix);
  3653. skew.offset = matrix.offset();
  3654. }
  3655. return this;
  3656. };
  3657. elproto.rotate = function (deg, cx, cy) {
  3658. if (this.removed) {
  3659. return this;
  3660. }
  3661. if (deg == null) {
  3662. return;
  3663. }
  3664. deg = Str(deg).split(separator);
  3665. if (deg.length - 1) {
  3666. cx = toFloat(deg[1]);
  3667. cy = toFloat(deg[2]);
  3668. }
  3669. deg = toFloat(deg[0]);
  3670. (cy == null) && (cx = cy);
  3671. if (cx == null || cy == null) {
  3672. var bbox = this.getBBox(1);
  3673. cx = bbox.x + bbox.width / 2;
  3674. cy = bbox.y + bbox.height / 2;
  3675. }
  3676. this._.dirtyT = 1;
  3677. this.transform(this._.transform.concat([["r", deg, cx, cy]]));
  3678. return this;
  3679. };
  3680. elproto.translate = function (dx, dy) {
  3681. if (this.removed) {
  3682. return this;
  3683. }
  3684. dx = Str(dx).split(separator);
  3685. if (dx.length - 1) {
  3686. dy = toFloat(dx[1]);
  3687. }
  3688. dx = toFloat(dx[0]) || 0;
  3689. dy = +dy || 0;
  3690. if (this._.bbox) {
  3691. this._.bbox.x += dx;
  3692. this._.bbox.y += dy;
  3693. }
  3694. this.transform(this._.transform.concat([["t", dx, dy]]));
  3695. return this;
  3696. };
  3697. elproto.scale = function (sx, sy, cx, cy) {
  3698. if (this.removed) {
  3699. return this;
  3700. }
  3701. sx = Str(sx).split(separator);
  3702. if (sx.length - 1) {
  3703. sy = toFloat(sx[1]);
  3704. cx = toFloat(sx[2]);
  3705. cy = toFloat(sx[3]);
  3706. isNaN(cx) && (cx = null);
  3707. isNaN(cy) && (cy = null);
  3708. }
  3709. sx = toFloat(sx[0]);
  3710. (sy == null) && (sy = sx);
  3711. (cy == null) && (cx = cy);
  3712. if (cx == null || cy == null) {
  3713. var bbox = this.getBBox(1);
  3714. }
  3715. cx = cx == null ? bbox.x + bbox.width / 2 : cx;
  3716. cy = cy == null ? bbox.y + bbox.height / 2 : cy;
  3717. this.transform(this._.transform.concat([["s", sx, sy, cx, cy]]));
  3718. this._.dirtyT = 1;
  3719. return this;
  3720. };
  3721. elproto.hide = function () {
  3722. !this.removed && (this.node.style.display = "none");
  3723. return this;
  3724. };
  3725. elproto.show = function () {
  3726. !this.removed && (this.node.style.display = E);
  3727. return this;
  3728. };
  3729. elproto._getBBox = function () {
  3730. if (this.removed) {
  3731. return {};
  3732. }
  3733. if (this.type == "text") {
  3734. return {
  3735. x: this.X + (this.bbx || 0) - this.W / 2,
  3736. y: this.Y - this.H,
  3737. width: this.W,
  3738. height: this.H
  3739. };
  3740. } else {
  3741. return pathDimensions(this.attrs.path);
  3742. }
  3743. };
  3744. elproto.remove = function () {
  3745. if (this.removed) {
  3746. return;
  3747. }
  3748. eve.unbind("*.*." + this.id);
  3749. tear(this, this.paper);
  3750. this.node.parentNode.removeChild(this.node);
  3751. this.shape && this.shape.parentNode.removeChild(this.shape);
  3752. for (var i in this) {
  3753. delete this[i];
  3754. }
  3755. this.removed = true;
  3756. };
  3757. elproto.attr = function (name, value) {
  3758. if (this.removed) {
  3759. return this;
  3760. }
  3761. if (name == null) {
  3762. var res = {};
  3763. for (var a in this.attrs) if (this.attrs[has](a)) {
  3764. res[a] = this.attrs[a];
  3765. }
  3766. res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
  3767. res.transform = this._.transform;
  3768. return res;
  3769. }
  3770. if (value == null && R.is(name, "string")) {
  3771. if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
  3772. return this.attrs.gradient;
  3773. }
  3774. var names = name.split(separator),
  3775. out = {};
  3776. for (var i = 0, ii = names.length; i < ii; i++) {
  3777. name = names[i];
  3778. if (name in this.attrs) {
  3779. out[name] = this.attrs[name];
  3780. } else if (R.is(this.paper.customAttributes[name], "function")) {
  3781. out[name] = this.paper.customAttributes[name].def;
  3782. } else {
  3783. out[name] = availableAttrs[name];
  3784. }
  3785. }
  3786. return ii - 1 ? out : out[names[0]];
  3787. }
  3788. if (this.attrs && value == null && R.is(name, array)) {
  3789. out = {};
  3790. for (i = 0, ii = name.length; i < ii; i++) {
  3791. out[name[i]] = this.attr(name[i]);
  3792. }
  3793. return out;
  3794. }
  3795. var params;
  3796. if (value != null) {
  3797. params = {};
  3798. params[name] = value;
  3799. }
  3800. value == null && R.is(name, "object") && (params = name);
  3801. for (var key in params) {
  3802. eve("attr." + key + "." + this.id, this, params[key]);
  3803. }
  3804. if (params) {
  3805. for (key in this.paper.customAttributes) if (this.paper.customAttributes[has](key) && params[has](key) && R.is(this.paper.customAttributes[key], "function")) {
  3806. var par = this.paper.customAttributes[key].apply(this, [][concat](params[key]));
  3807. this.attrs[key] = params[key];
  3808. for (var subkey in par) if (par[has](subkey)) {
  3809. params[subkey] = par[subkey];
  3810. }
  3811. }
  3812. // this.paper.canvas.style.display = "none";
  3813. if (params.text && this.type == "text") {
  3814. this.textpath.string = params.text;
  3815. }
  3816. setFillAndStroke(this, params);
  3817. // this.paper.canvas.style.display = E;
  3818. }
  3819. return this;
  3820. };
  3821. elproto.toFront = function () {
  3822. !this.removed && this.node.parentNode.appendChild(this.node);
  3823. this.paper && this.paper.top != this && tofront(this, this.paper);
  3824. return this;
  3825. };
  3826. elproto.toBack = function () {
  3827. if (this.removed) {
  3828. return this;
  3829. }
  3830. if (this.node.parentNode.firstChild != this.node) {
  3831. this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
  3832. toback(this, this.paper);
  3833. }
  3834. return this;
  3835. };
  3836. elproto.insertAfter = function (element) {
  3837. if (this.removed) {
  3838. return this;
  3839. }
  3840. if (element.constructor == Set) {
  3841. element = element[element.length - 1];
  3842. }
  3843. if (element.node.nextSibling) {
  3844. element.node.parentNode.insertBefore(this.node, element.node.nextSibling);
  3845. } else {
  3846. element.node.parentNode.appendChild(this.node);
  3847. }
  3848. insertafter(this, element, this.paper);
  3849. return this;
  3850. };
  3851. elproto.insertBefore = function (element) {
  3852. if (this.removed) {
  3853. return this;
  3854. }
  3855. if (element.constructor == Set) {
  3856. element = element[0];
  3857. }
  3858. element.node.parentNode.insertBefore(this.node, element.node);
  3859. insertbefore(this, element, this.paper);
  3860. return this;
  3861. };
  3862. elproto.blur = function (size) {
  3863. var s = this.node.runtimeStyle,
  3864. f = s.filter;
  3865. f = f.replace(blurregexp, E);
  3866. if (+size !== 0) {
  3867. this.attrs.blur = size;
  3868. s.filter = f + S + ms + ".Blur(pixelradius=" + (+size || 1.5) + ")";
  3869. s.margin = R.format("-{0}px 0 0 -{0}px", round(+size || 1.5));
  3870. } else {
  3871. s.filter = f;
  3872. s.margin = 0;
  3873. delete this.attrs.blur;
  3874. }
  3875. };
  3876. thePath = function (pathString, vml) {
  3877. var el = createNode("shape");
  3878. el.style.cssText = cssDot;
  3879. el.coordsize = zoom + S + zoom;
  3880. el.coordorigin = vml.coordorigin;
  3881. var p = new Element(el, vml),
  3882. attr = {fill: "none", stroke: "#000"};
  3883. pathString && (attr.path = pathString);
  3884. p.type = "path";
  3885. p.path = [];
  3886. p.Path = E;
  3887. setFillAndStroke(p, attr);
  3888. vml.canvas.appendChild(el);
  3889. var skew = createNode("skew");
  3890. skew.on = true;
  3891. el.appendChild(skew);
  3892. p.skew = skew;
  3893. p.transform(E);
  3894. return p;
  3895. };
  3896. theRect = function (vml, x, y, w, h, r) {
  3897. var path = rectPath(x, y, w, h, r),
  3898. res = vml.path(path),
  3899. a = res.attrs;
  3900. res.X = a.x = x;
  3901. res.Y = a.y = y;
  3902. res.W = a.width = w;
  3903. res.H = a.height = h;
  3904. a.r = r;
  3905. a.path = path;
  3906. res.type = "rect";
  3907. return res;
  3908. };
  3909. theEllipse = function (vml, x, y, rx, ry) {
  3910. var res = vml.path(),
  3911. a = res.attrs;
  3912. res.X = x - rx;
  3913. res.Y = y - ry;
  3914. res.W = rx * 2;
  3915. res.H = ry * 2;
  3916. res.type = "ellipse";
  3917. setFillAndStroke(res, {
  3918. cx: x,
  3919. cy: y,
  3920. rx: rx,
  3921. ry: ry
  3922. });
  3923. return res;
  3924. };
  3925. theCircle = function (vml, x, y, r) {
  3926. var res = vml.path(),
  3927. a = res.attrs;
  3928. res.X = x - r;
  3929. res.Y = y - r;
  3930. res.W = res.H = r * 2;
  3931. res.type = "circle";
  3932. setFillAndStroke(res, {
  3933. cx: x,
  3934. cy: y,
  3935. r: r
  3936. });
  3937. return res;
  3938. };
  3939. theImage = function (vml, src, x, y, w, h) {
  3940. var path = rectPath(x, y, w, h),
  3941. res = vml.path(path).attr({stroke: "none"}),
  3942. a = res.attrs,
  3943. node = res.node,
  3944. fill = node.getElementsByTagName(fillString)[0];
  3945. a.src = src;
  3946. res.X = a.x = x;
  3947. res.Y = a.y = y;
  3948. res.W = a.width = w;
  3949. res.H = a.height = h;
  3950. a.path = path;
  3951. res.type = "image";
  3952. fill.parentNode == node && node.removeChild(fill);
  3953. fill.rotate = true;
  3954. fill.src = src;
  3955. fill.type = "tile";
  3956. res._.fillpos = [x, y];
  3957. res._.fillsize = [w, h];
  3958. node.appendChild(fill);
  3959. setCoords(res, 1, 1, 0, 0, 0);
  3960. return res;
  3961. };
  3962. theText = function (vml, x, y, text) {
  3963. var el = createNode("shape"),
  3964. path = createNode("path"),
  3965. o = createNode("textpath");
  3966. x = x || 0;
  3967. y = y || 0;
  3968. text = text || "";
  3969. path.v = R.format("m{0},{1}l{2},{1}", round(x * zoom), round(y * zoom), round(x * zoom) + 1);
  3970. path.textpathok = true;
  3971. o.string = Str(text);
  3972. o.on = true;
  3973. el.style.cssText = "position:absolute;left:0;top:0;width:1px;height:1px";
  3974. el.coordsize = zoom + S + zoom;
  3975. el.coordorigin = "0 0";
  3976. var p = new Element(el, vml),
  3977. attr = {fill: "#000", stroke: "none", font: availableAttrs.font, text: text};
  3978. p.shape = el;
  3979. p.path = path;
  3980. p.textpath = o;
  3981. p.type = "text";
  3982. p.attrs.text = Str(text);
  3983. p.attrs.x = x;
  3984. p.attrs.y = y;
  3985. p.attrs.w = 1;
  3986. p.attrs.h = 1;
  3987. setFillAndStroke(p, attr);
  3988. el.appendChild(o);
  3989. el.appendChild(path);
  3990. vml.canvas.appendChild(el);
  3991. var skew = createNode("skew");
  3992. skew.on = true;
  3993. el.appendChild(skew);
  3994. p.skew = skew;
  3995. p.transform(E);
  3996. return p;
  3997. };
  3998. setSize = function (width, height) {
  3999. var cs = this.canvas.style;
  4000. this.width = width;
  4001. this.height = height;
  4002. width == +width && (width += "px");
  4003. height == +height && (height += "px");
  4004. cs.width = width;
  4005. cs.height = height;
  4006. cs.clip = "rect(0 " + width + " " + height + " 0)";
  4007. if (this._viewBox) {
  4008. setViewBox.apply(this, this._viewBox);
  4009. }
  4010. return this;
  4011. };
  4012. setViewBox = function (x, y, w, h, fit) {
  4013. eve("setViewBox", this, this._viewBox, [x, y, w, h, fit]);
  4014. var width = this.width,
  4015. height = this.height,
  4016. size = 1 / mmax(w / width, h / height),
  4017. H, W;
  4018. if (fit) {
  4019. H = height / h;
  4020. W = width / w;
  4021. if (w * H < width) {
  4022. x -= (width - w * H) / 2 / H;
  4023. }
  4024. if (h * W < height) {
  4025. y -= (height - h * W) / 2 / W;
  4026. }
  4027. }
  4028. this._viewBox = [x, y, w, h, !!fit];
  4029. this._viewBoxShift = {
  4030. dx: -x,
  4031. dy: -y,
  4032. scale: size
  4033. };
  4034. this.forEach(function (el) {
  4035. el.transform("...");
  4036. });
  4037. return this;
  4038. };
  4039. var createNode,
  4040. initWin = function (win) {
  4041. var doc = win.document;
  4042. doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
  4043. try {
  4044. !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
  4045. createNode = function (tagName) {
  4046. return doc.createElement('<rvml:' + tagName + ' class="rvml">');
  4047. };
  4048. } catch (e) {
  4049. createNode = function (tagName) {
  4050. return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
  4051. };
  4052. }
  4053. };
  4054. initWin(g.win);
  4055. create = function () {
  4056. var con = getContainer[apply](0, arguments),
  4057. container = con.container,
  4058. height = con.height,
  4059. s,
  4060. width = con.width,
  4061. x = con.x,
  4062. y = con.y;
  4063. if (!container) {
  4064. throw new Error("VML container not found.");
  4065. }
  4066. var res = new Paper,
  4067. c = res.canvas = g.doc.createElement("div"),
  4068. cs = c.style;
  4069. x = x || 0;
  4070. y = y || 0;
  4071. width = width || 512;
  4072. height = height || 342;
  4073. res.width = width;
  4074. res.height = height;
  4075. width == +width && (width += "px");
  4076. height == +height && (height += "px");
  4077. res.coordsize = zoom * 1e3 + S + zoom * 1e3;
  4078. res.coordorigin = "0 0";
  4079. res.span = g.doc.createElement("span");
  4080. res.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;";
  4081. c.appendChild(res.span);
  4082. cs.cssText = R.format("top:0;left:0;width:{0};height:{1};display:inline-block;position:relative;clip:rect(0 {0} {1} 0);overflow:hidden", width, height);
  4083. if (container == 1) {
  4084. g.doc.body.appendChild(c);
  4085. cs.left = x + "px";
  4086. cs.top = y + "px";
  4087. cs.position = "absolute";
  4088. } else {
  4089. if (container.firstChild) {
  4090. container.insertBefore(c, container.firstChild);
  4091. } else {
  4092. container.appendChild(c);
  4093. }
  4094. }
  4095. plugins.call(res, res, R.fn);
  4096. res.renderfix = fun;
  4097. return res;
  4098. };
  4099. paperproto.clear = function () {
  4100. eve("clear", this);
  4101. this.canvas.innerHTML = E;
  4102. this.span = g.doc.createElement("span");
  4103. this.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
  4104. this.canvas.appendChild(this.span);
  4105. this.bottom = this.top = null;
  4106. };
  4107. paperproto.remove = function () {
  4108. eve("remove", this);
  4109. this.canvas.parentNode.removeChild(this.canvas);
  4110. for (var i in this) {
  4111. this[i] = removed(i);
  4112. }
  4113. return true;
  4114. };
  4115. }
  4116. // WebKit rendering bug workaround method
  4117. var version = navigator.userAgent.match(/Version\/(.*?)\s/) || navigator.userAgent.match(/Chrome\/(\d+)/);
  4118. if ((navigator.vendor == "Apple Computer, Inc.") && (version && version[1] < 4 || navigator.platform.slice(0, 2) == "iP") ||
  4119. (navigator.vendor == "Google Inc." && version && version[1] < 8)) {
  4120. /*\
  4121. * Paper.safari
  4122. [ method ]
  4123. **
  4124. * There is an inconvenient rendering bug in Safari (WebKit):
  4125. * sometimes the rendering should be forced.
  4126. * This method should help with dealing with this bug.
  4127. \*/
  4128. paperproto.safari = function () {
  4129. var rect = this.rect(-99, -99, this.width + 99, this.height + 99).attr({stroke: "none"});
  4130. setTimeout(function () {rect.remove();});
  4131. };
  4132. } else {
  4133. paperproto.safari = fun;
  4134. }
  4135. // Events
  4136. var preventDefault = function () {
  4137. this.returnValue = false;
  4138. },
  4139. preventTouch = function () {
  4140. return this.originalEvent.preventDefault();
  4141. },
  4142. stopPropagation = function () {
  4143. this.cancelBubble = true;
  4144. },
  4145. stopTouch = function () {
  4146. return this.originalEvent.stopPropagation();
  4147. },
  4148. addEvent = (function () {
  4149. if (g.doc.addEventListener) {
  4150. return function (obj, type, fn, element) {
  4151. var realName = supportsTouch && touchMap[type] ? touchMap[type] : type,
  4152. f = function (e) {
  4153. var scrollY = g.doc.documentElement.scrollTop || g.doc.body.scrollTop,
  4154. scrollX = g.doc.documentElement.scrollLeft || g.doc.body.scrollLeft,
  4155. x = e.clientX + scrollX,
  4156. y = e.clientY + scrollY;
  4157. if (supportsTouch && touchMap[has](type)) {
  4158. for (var i = 0, ii = e.targetTouches && e.targetTouches.length; i < ii; i++) {
  4159. if (e.targetTouches[i].target == obj) {
  4160. var olde = e;
  4161. e = e.targetTouches[i];
  4162. e.originalEvent = olde;
  4163. e.preventDefault = preventTouch;
  4164. e.stopPropagation = stopTouch;
  4165. break;
  4166. }
  4167. }
  4168. }
  4169. return fn.call(element, e, x, y);
  4170. };
  4171. obj.addEventListener(realName, f, false);
  4172. return function () {
  4173. obj.removeEventListener(realName, f, false);
  4174. return true;
  4175. };
  4176. };
  4177. } else if (g.doc.attachEvent) {
  4178. return function (obj, type, fn, element) {
  4179. var f = function (e) {
  4180. e = e || g.win.event;
  4181. var scrollY = g.doc.documentElement.scrollTop || g.doc.body.scrollTop,
  4182. scrollX = g.doc.documentElement.scrollLeft || g.doc.body.scrollLeft,
  4183. x = e.clientX + scrollX,
  4184. y = e.clientY + scrollY;
  4185. e.preventDefault = e.preventDefault || preventDefault;
  4186. e.stopPropagation = e.stopPropagation || stopPropagation;
  4187. return fn.call(element, e, x, y);
  4188. };
  4189. obj.attachEvent("on" + type, f);
  4190. var detacher = function () {
  4191. obj.detachEvent("on" + type, f);
  4192. return true;
  4193. };
  4194. return detacher;
  4195. };
  4196. }
  4197. })(),
  4198. drag = [],
  4199. dragMove = function (e) {
  4200. var x = e.clientX,
  4201. y = e.clientY,
  4202. scrollY = g.doc.documentElement.scrollTop || g.doc.body.scrollTop,
  4203. scrollX = g.doc.documentElement.scrollLeft || g.doc.body.scrollLeft,
  4204. dragi,
  4205. j = drag.length;
  4206. while (j--) {
  4207. dragi = drag[j];
  4208. if (supportsTouch) {
  4209. var i = e.touches.length,
  4210. touch;
  4211. while (i--) {
  4212. touch = e.touches[i];
  4213. if (touch.identifier == dragi.el._drag.id) {
  4214. x = touch.clientX;
  4215. y = touch.clientY;
  4216. (e.originalEvent ? e.originalEvent : e).preventDefault();
  4217. break;
  4218. }
  4219. }
  4220. } else {
  4221. e.preventDefault();
  4222. }
  4223. var node = dragi.el.node,
  4224. o,
  4225. next = node.nextSibling,
  4226. parent = node.parentNode,
  4227. display = node.style.display;
  4228. g.win.opera && parent.removeChild(node);
  4229. node.style.display = "none";
  4230. o = dragi.el.paper.getElementByPoint(x, y);
  4231. node.style.display = display;
  4232. g.win.opera && (next ? parent.insertBefore(node, next) : parent.appendChild(node));
  4233. o && eve("drag.over." + dragi.el.id, dragi.el, o);
  4234. x += scrollX;
  4235. y += scrollY;
  4236. eve("drag.move." + dragi.el.id, dragi.move_scope || dragi.el, x - dragi.el._drag.x, y - dragi.el._drag.y, x, y, e);
  4237. }
  4238. },
  4239. dragUp = function (e) {
  4240. R.unmousemove(dragMove).unmouseup(dragUp);
  4241. var i = drag.length,
  4242. dragi;
  4243. while (i--) {
  4244. dragi = drag[i];
  4245. dragi.el._drag = {};
  4246. eve("drag.end." + dragi.el.id, dragi.end_scope || dragi.start_scope || dragi.move_scope || dragi.el, e);
  4247. }
  4248. drag = [];
  4249. };
  4250. for (var i = events.length; i--;) {
  4251. (function (eventName) {
  4252. R[eventName] = Element.prototype[eventName] = function (fn, scope) {
  4253. if (R.is(fn, "function")) {
  4254. this.events = this.events || [];
  4255. this.events.push({name: eventName, f: fn, unbind: addEvent(this.shape || this.node || g.doc, eventName, fn, scope || this)});
  4256. }
  4257. return this;
  4258. };
  4259. R["un" + eventName] = Element.prototype["un" + eventName] = function (fn) {
  4260. var events = this.events,
  4261. l = events.length;
  4262. while (l--) if (events[l].name == eventName && events[l].f == fn) {
  4263. events[l].unbind();
  4264. events.splice(l, 1);
  4265. !events.length && delete this.events;
  4266. return this;
  4267. }
  4268. return this;
  4269. };
  4270. })(events[i]);
  4271. }
  4272. /*\
  4273. * Element.hover
  4274. [ method ]
  4275. **
  4276. * Adds event handlers for hover for the element.
  4277. > Parameters
  4278. - f_in (function) handler for hover in
  4279. - f_out (function) handler for hover out
  4280. - icontext (object) #optional context for hover in handler
  4281. - ocontext (object) #optional context for hover out handler
  4282. = (object) @Element
  4283. \*/
  4284. elproto.hover = function (f_in, f_out, scope_in, scope_out) {
  4285. return this.mouseover(f_in, scope_in).mouseout(f_out, scope_out || scope_in);
  4286. };
  4287. /*\
  4288. * Element.unhover
  4289. [ method ]
  4290. **
  4291. * Removes event handlers for hover for the element.
  4292. > Parameters
  4293. - f_in (function) handler for hover in
  4294. - f_out (function) handler for hover out
  4295. = (object) @Element
  4296. \*/
  4297. elproto.unhover = function (f_in, f_out) {
  4298. return this.unmouseover(f_in).unmouseout(f_out);
  4299. };
  4300. /*\
  4301. * Element.drag
  4302. [ method ]
  4303. **
  4304. * Adds event handlers for drag of the element.
  4305. > Parameters
  4306. - onmove (function) handler for moving
  4307. - onstart (function) handler for drag start
  4308. - onend (function) handler for drag end
  4309. - mcontext (object) #optional context for moving handler
  4310. - scontext (object) #optional context for drag start handler
  4311. - econtext (object) #optional context for drag end handler
  4312. * Additionaly following `drag` events will be triggered: `drag.start.<id>` on start,
  4313. * `drag.end.<id>` on end and `drag.move.<id>` on every move. When element will be dragged over another element
  4314. * `drag.over.<id>` will be fired as well.
  4315. *
  4316. * Start event and start handler will be called in specified context or in context of the element with following parameters:
  4317. o x (number) x position of the mouse
  4318. o y (number) y position of the mouse
  4319. o event (object) DOM event object
  4320. * Move event and move handler will be called in specified context or in context of the element with following parameters:
  4321. o dx (number) shift by x from the start point
  4322. o dy (number) shift by y from the start point
  4323. o x (number) x position of the mouse
  4324. o y (number) y position of the mouse
  4325. o event (object) DOM event object
  4326. * End event and end handler will be called in specified context or in context of the element with following parameters:
  4327. o event (object) DOM event object
  4328. = (object) @Element
  4329. \*/
  4330. elproto.drag = function (onmove, onstart, onend, move_scope, start_scope, end_scope) {
  4331. function start(e) {
  4332. (e.originalEvent || e).preventDefault();
  4333. var scrollY = g.doc.documentElement.scrollTop || g.doc.body.scrollTop,
  4334. scrollX = g.doc.documentElement.scrollLeft || g.doc.body.scrollLeft;
  4335. this._drag.x = e.clientX + scrollX;
  4336. this._drag.y = e.clientY + scrollY;
  4337. this._drag.id = e.identifier;
  4338. !drag.length && R.mousemove(dragMove).mouseup(dragUp);
  4339. drag.push({el: this, move_scope: move_scope, start_scope: start_scope, end_scope: end_scope});
  4340. onstart && eve.on("drag.start." + this.id, onstart);
  4341. onmove && eve.on("drag.move." + this.id, onmove);
  4342. onend && eve.on("drag.end." + this.id, onend);
  4343. eve("drag.start." + this.id, start_scope || move_scope || this, e.clientX + scrollX, e.clientY + scrollY, e);
  4344. }
  4345. this._drag = {};
  4346. this.mousedown(start);
  4347. return this;
  4348. };
  4349. /*\
  4350. * Element.onDragOver
  4351. [ method ]
  4352. **
  4353. * Shortcut for assigning event handler for `drag.over.<id>` event, where id is id of the element (see @Element.id).
  4354. > Parameters
  4355. - f (function) handler for event
  4356. \*/
  4357. elproto.onDragOver = function (f) {
  4358. f ? eve.on("drag.over." + this.id, f) : eve.unbind("drag.over." + this.id);
  4359. };
  4360. /*\
  4361. * Element.undrag
  4362. [ method ]
  4363. **
  4364. * Removes all drag event handlers from given element.
  4365. \*/
  4366. elproto.undrag = function () {
  4367. var i = drag.length;
  4368. while (i--) if (drag[i].el == this) {
  4369. R.unmousedown(drag[i].start);
  4370. drag.splice(i++, 1);
  4371. eve.unbind("drag.*." + this.id);
  4372. }
  4373. !drag.length && R.unmousemove(dragMove).unmouseup(dragUp);
  4374. };
  4375. /*\
  4376. * Paper.circle
  4377. [ method ]
  4378. **
  4379. * Draws a circle.
  4380. **
  4381. > Parameters
  4382. **
  4383. - x (number) x coordinate of the centre
  4384. - y (number) y coordinate of the centre
  4385. - r (number) radius
  4386. = (object) Raphaël element object with type “circle”
  4387. **
  4388. > Usage
  4389. | var c = paper.circle(50, 50, 40);
  4390. \*/
  4391. paperproto.circle = function (x, y, r) {
  4392. return theCircle(this, x || 0, y || 0, r || 0);
  4393. };
  4394. /*\
  4395. * Paper.rect
  4396. [ method ]
  4397. *
  4398. * Draws a rectangle.
  4399. **
  4400. > Parameters
  4401. **
  4402. - x (number) x coordinate of the top left corner
  4403. - y (number) y coordinate of the top left corner
  4404. - width (number) width
  4405. - height (number) height
  4406. - r (number) #optional radius for rounded corners, default is 0
  4407. = (object) Raphaël element object with type “rect”
  4408. **
  4409. > Usage
  4410. | // regular rectangle
  4411. | var c = paper.rect(10, 10, 50, 50);
  4412. | // rectangle with rounded corners
  4413. | var c = paper.rect(40, 40, 50, 50, 10);
  4414. \*/
  4415. paperproto.rect = function (x, y, w, h, r) {
  4416. return theRect(this, x || 0, y || 0, w || 0, h || 0, r || 0);
  4417. };
  4418. /*\
  4419. * Paper.ellipse
  4420. [ method ]
  4421. **
  4422. * Draws an ellipse.
  4423. **
  4424. > Parameters
  4425. **
  4426. - x (number) x coordinate of the centre
  4427. - y (number) y coordinate of the centre
  4428. - rx (number) horizontal radius
  4429. - ry (number) vertical radius
  4430. = (object) Raphaël element object with type “ellipse”
  4431. **
  4432. > Usage
  4433. | var c = paper.ellipse(50, 50, 40, 20);
  4434. \*/
  4435. paperproto.ellipse = function (x, y, rx, ry) {
  4436. return theEllipse(this, x || 0, y || 0, rx || 0, ry || 0);
  4437. };
  4438. /*\
  4439. * Paper.path
  4440. [ method ]
  4441. **
  4442. * Creates a path element by given path data string.
  4443. **
  4444. > Parameters
  4445. **
  4446. - pathString (string) path data in SVG path string format.
  4447. = (object) Raphaël element object with type “ellipse”
  4448. # Details of a path's data attribute's format are described in the <a href="http://www.w3.org/TR/SVG/paths.html#PathData">SVG specification</a>.
  4449. **
  4450. > Usage
  4451. | var c = paper.path("M10 10L90 90");
  4452. | // draw a diagonal line:
  4453. | // move to 10,10, line to 90,90
  4454. \*/
  4455. paperproto.path = function (pathString) {
  4456. pathString && !R.is(pathString, string) && !R.is(pathString[0], array) && (pathString += E);
  4457. return thePath(R.format[apply](R, arguments), this);
  4458. };
  4459. /*\
  4460. * Paper.image
  4461. [ method ]
  4462. **
  4463. * Embeds an image into the surface.
  4464. **
  4465. > Parameters
  4466. **
  4467. - src (string) URI of the source image
  4468. - x (number) x coordinate position
  4469. - y (number) y coordinate position
  4470. - width (number) width of the image
  4471. - height (number) height of the image
  4472. = (object) Raphaël element object with type “image”
  4473. **
  4474. > Usage
  4475. | var c = paper.image("apple.png", 10, 10, 80, 80);
  4476. \*/
  4477. paperproto.image = function (src, x, y, w, h) {
  4478. return theImage(this, src || "about:blank", x || 0, y || 0, w || 0, h || 0);
  4479. };
  4480. /*\
  4481. * Paper.text
  4482. [ method ]
  4483. **
  4484. * Draws a text string. If you need line breaks, put “\n” in the string.
  4485. **
  4486. > Parameters
  4487. **
  4488. - x (number) x coordinate position
  4489. - y (number) y coordinate position
  4490. - text (string) The text string to draw
  4491. = (object) Raphaël element object with type “text”
  4492. **
  4493. > Usage
  4494. | var t = paper.text(50, 50, "Raphaël\nkicks\nbutt!");
  4495. \*/
  4496. paperproto.text = function (x, y, text) {
  4497. return theText(this, x || 0, y || 0, Str(text));
  4498. };
  4499. /*\
  4500. * Paper.set
  4501. [ method ]
  4502. **
  4503. * Creates array-like object to keep and operate several elements at once.
  4504. * Warning: it doesn’t create any elements for itself in the page, it just groups existing elements.
  4505. * Sets act as pseudo elements — all methods available to an element can be used on a set.
  4506. = (object) array-like object that represents set of elements
  4507. **
  4508. > Usage
  4509. | var st = paper.set();
  4510. | st.push(
  4511. | paper.circle(10, 10, 5),
  4512. | paper.circle(30, 10, 5)
  4513. | );
  4514. | st.attr({fill: "red"});
  4515. \*/
  4516. paperproto.set = function (itemsArray) {
  4517. !R.is(itemsArray, "array") && (itemsArray = Array.prototype.splice.call(arguments, 0, arguments.length));
  4518. return new Set(itemsArray);
  4519. };
  4520. /*\
  4521. * Paper.setSize
  4522. [ method ]
  4523. **
  4524. * If you need to change dimensions of the canvas call this method
  4525. **
  4526. > Parameters
  4527. **
  4528. - width (number) new width of the canvas
  4529. - height (number) new height of the canvas
  4530. > Usage
  4531. | var st = paper.set();
  4532. | st.push(
  4533. | paper.circle(10, 10, 5),
  4534. | paper.circle(30, 10, 5)
  4535. | );
  4536. | st.attr({fill: "red"});
  4537. \*/
  4538. paperproto.setSize = setSize;
  4539. /*\
  4540. * Paper.setViewBox
  4541. [ method ]
  4542. **
  4543. * Sets the view box of the paper. Practically it gives you ability to zoom and pan whole paper surface by
  4544. * specifying new boundaries.
  4545. **
  4546. > Parameters
  4547. **
  4548. x, y, w, h, fit
  4549. - x (number) new x position, default is `0`
  4550. - y (number) new y position, default is `0`
  4551. - w (number) new width of the canvas
  4552. - h (number) new height of the canvas
  4553. - fit (boolean) `true` if you want graphics to fit into new boundary box
  4554. \*/
  4555. paperproto.setViewBox = setViewBox;
  4556. /*\
  4557. * Paper.top
  4558. [ property ]
  4559. **
  4560. * Points to the topmost element on the paper
  4561. \*/
  4562. /*\
  4563. * Paper.bottom
  4564. [ property ]
  4565. **
  4566. * Points to the bottom element on the paper
  4567. \*/
  4568. paperproto.top = paperproto.bottom = null;
  4569. /*\
  4570. * Paper.raphael
  4571. [ property ]
  4572. **
  4573. * Points to the @Raphael object/function
  4574. \*/
  4575. paperproto.raphael = R;
  4576. var getOffset = function (elem) {
  4577. var box = elem.getBoundingClientRect(),
  4578. doc = elem.ownerDocument,
  4579. body = doc.body,
  4580. docElem = doc.documentElement,
  4581. clientTop = docElem.clientTop || body.clientTop || 0, clientLeft = docElem.clientLeft || body.clientLeft || 0,
  4582. top = box.top + (g.win.pageYOffset || docElem.scrollTop || body.scrollTop ) - clientTop,
  4583. left = box.left + (g.win.pageXOffset || docElem.scrollLeft || body.scrollLeft) - clientLeft;
  4584. return {
  4585. y: top,
  4586. x: left
  4587. };
  4588. };
  4589. /*\
  4590. * Paper.getElementByPoint
  4591. [ method ]
  4592. **
  4593. * Returns you topmost element under given point.
  4594. **
  4595. = (object) Raphaël element object
  4596. > Parameters
  4597. **
  4598. - x (number) x coordinate from the top left corner of the window
  4599. - y (number) y coordinate from the top left corner of the window
  4600. > Usage
  4601. | paper.getElementByPoint(mouseX, mouseY).attr({stroke: "#f00"});
  4602. \*/
  4603. paperproto.getElementByPoint = function (x, y) {
  4604. var paper = this,
  4605. svg = paper.canvas,
  4606. target = g.doc.elementFromPoint(x, y);
  4607. if (g.win.opera && target.tagName == "svg") {
  4608. var so = getOffset(svg),
  4609. sr = svg.createSVGRect();
  4610. sr.x = x - so.x;
  4611. sr.y = y - so.y;
  4612. sr.width = sr.height = 1;
  4613. var hits = svg.getIntersectionList(sr, null);
  4614. if (hits.length) {
  4615. target = hits[hits.length - 1];
  4616. }
  4617. }
  4618. if (!target) {
  4619. return null;
  4620. }
  4621. while (target.parentNode && target != svg.parentNode && !target.raphael) {
  4622. target = target.parentNode;
  4623. }
  4624. target == paper.canvas.parentNode && (target = svg);
  4625. target = target && target.raphael ? paper.getById(target.raphaelid) : null;
  4626. return target;
  4627. };
  4628. /*\
  4629. * Paper.getById
  4630. [ method ]
  4631. **
  4632. * Returns you element by its internal ID.
  4633. **
  4634. > Parameters
  4635. **
  4636. - id (number) id
  4637. = (object) Raphaël element object
  4638. \*/
  4639. paperproto.getById = function (id) {
  4640. var bot = this.bottom;
  4641. while (bot) {
  4642. if (bot.id == id) {
  4643. return bot;
  4644. }
  4645. bot = bot.next;
  4646. }
  4647. return null;
  4648. };
  4649. /*\
  4650. * Paper.forEach
  4651. [ method ]
  4652. **
  4653. * Executes given function for each element on the paper
  4654. *
  4655. * If callback function returns `false` it will stop loop running.
  4656. **
  4657. > Parameters
  4658. **
  4659. - callback (function) function to run
  4660. - thisArg (object) context object for the callback
  4661. = (object) Paper object
  4662. \*/
  4663. paperproto.forEach = function (callback, thisArg) {
  4664. var bot = this.bottom;
  4665. while (bot) {
  4666. if (callback.call(thisArg, bot) === false) {
  4667. return this;
  4668. }
  4669. bot = bot.next;
  4670. }
  4671. return this;
  4672. };
  4673. function x_y() {
  4674. return this.x + S + this.y;
  4675. }
  4676. function x_y_w_h() {
  4677. return this.x + S + this.y + S + this.width + "\xd7" + this.height;
  4678. }
  4679. /*\
  4680. * Element.getBBox
  4681. [ method ]
  4682. **
  4683. * Return bounding box for a given element
  4684. **
  4685. > Parameters
  4686. **
  4687. - isWithoutTransform (boolean) flag, `true` if you want to have bounding box before transformations. Default is `false`.
  4688. = (object) Bounding box object:
  4689. o {
  4690. o x: (number) top left corner x
  4691. o y: (number) top left corner y
  4692. o width: (number) width
  4693. o height: (number) height
  4694. o }
  4695. \*/
  4696. elproto.getBBox = function (isWithoutTransform) {
  4697. if (this.removed) {
  4698. return {};
  4699. }
  4700. var _ = this._;
  4701. if (isWithoutTransform) {
  4702. if (_.dirty || !_.bboxwt) {
  4703. this.realPath = getPath[this.type](this);
  4704. _.bboxwt = pathDimensions(this.realPath);
  4705. _.bboxwt.toString = x_y_w_h;
  4706. _.dirty = 0;
  4707. }
  4708. return _.bboxwt;
  4709. }
  4710. if (_.dirty || _.dirtyT || !_.bbox) {
  4711. if (_.dirty || !this.realPath) {
  4712. _.bboxwt = 0;
  4713. this.realPath = getPath[this.type](this);
  4714. }
  4715. _.bbox = pathDimensions(mapPath(this.realPath, this.matrix));
  4716. _.bbox.toString = x_y_w_h;
  4717. _.dirty = _.dirtyT = 0;
  4718. }
  4719. return _.bbox;
  4720. };
  4721. /*\
  4722. * Element.clone
  4723. [ method ]
  4724. **
  4725. = (object) clone of a given element
  4726. **
  4727. \*/
  4728. elproto.clone = function () {
  4729. if (this.removed) {
  4730. return null;
  4731. }
  4732. return this.paper[this.type]().attr(this.attr());
  4733. };
  4734. /*\
  4735. * Element.glow
  4736. [ method ]
  4737. **
  4738. * Return set of elements that create glow-like effect around given element. See @Paper.set.
  4739. *
  4740. * Note: Glow is not connected to the element. If you change element attributes it won’t adjust itself.
  4741. **
  4742. > Parameters
  4743. **
  4744. - glow (object) #optional parameters object with all properties optional:
  4745. o {
  4746. o width (number) size of the glow, default is `10`
  4747. o fill (boolean) will it be filled, default is `false`
  4748. o opacity: opacity, default is `0.5`
  4749. o offsetx: horizontal offset, default is `0`
  4750. o offsety: vertical offset, default is `0`
  4751. o color: glow colour, default is `black`
  4752. o }
  4753. = (object) @Paper.set of elements that represents glow
  4754. \*/
  4755. elproto.glow = function (glow) {
  4756. if (this.type == "text") {
  4757. return null;
  4758. }
  4759. glow = glow || {};
  4760. var s = {
  4761. width: (glow.width || 10) + (+this.attr("stroke-width") || 1),
  4762. fill: glow.fill || false,
  4763. opacity: glow.opacity || .5,
  4764. offsetx: glow.offsetx || 0,
  4765. offsety: glow.offsety || 0,
  4766. color: glow.color || "#000"
  4767. },
  4768. c = s.width / 2,
  4769. r = this.paper,
  4770. out = r.set(),
  4771. path = this.realPath || getPath[this.type](this);
  4772. path = this.matrix ? mapPath(path, this.matrix) : path;
  4773. for (var i = 1; i < c + 1; i++) {
  4774. out.push(r.path(path).attr({
  4775. stroke: s.color,
  4776. fill: s.fill ? s.color : "none",
  4777. "stroke-linejoin": "round",
  4778. "stroke-linecap": "round",
  4779. "stroke-width": +(s.width / c * i).toFixed(3),
  4780. opacity: +(s.opacity / c).toFixed(3)
  4781. }));
  4782. }
  4783. return out.insertBefore(this).translate(s.offsetx, s.offsety);
  4784. };
  4785. var curveslengths = {},
  4786. getPointAtSegmentLength = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, length) {
  4787. var len = 0,
  4788. precision = 100,
  4789. name = [p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y].join(),
  4790. cache = curveslengths[name],
  4791. old, dot;
  4792. !cache && (curveslengths[name] = cache = {data: []});
  4793. cache.timer && clearTimeout(cache.timer);
  4794. cache.timer = setTimeout(function () {delete curveslengths[name];}, 2e3);
  4795. if (length != null && !cache.precision) {
  4796. var total = getPointAtSegmentLength(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y);
  4797. cache.precision = ~~total * 10;
  4798. cache.data = [];
  4799. }
  4800. precision = cache.precision || precision;
  4801. for (var i = 0; i < precision + 1; i++) {
  4802. if (cache.data[i * precision]) {
  4803. dot = cache.data[i * precision];
  4804. } else {
  4805. dot = R.findDotsAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i / precision);
  4806. cache.data[i * precision] = dot;
  4807. }
  4808. i && (len += pow(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2), .5));
  4809. if (length != null && len >= length) {
  4810. return dot;
  4811. }
  4812. old = dot;
  4813. }
  4814. if (length == null) {
  4815. return len;
  4816. }
  4817. },
  4818. getLengthFactory = function (istotal, subpath) {
  4819. return function (path, length, onlystart) {
  4820. path = path2curve(path);
  4821. var x, y, p, l, sp = "", subpaths = {}, point,
  4822. len = 0;
  4823. for (var i = 0, ii = path.length; i < ii; i++) {
  4824. p = path[i];
  4825. if (p[0] == "M") {
  4826. x = +p[1];
  4827. y = +p[2];
  4828. } else {
  4829. l = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
  4830. if (len + l > length) {
  4831. if (subpath && !subpaths.start) {
  4832. point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
  4833. sp += ["C" + point.start.x, point.start.y, point.m.x, point.m.y, point.x, point.y];
  4834. if (onlystart) {return sp;}
  4835. subpaths.start = sp;
  4836. sp = ["M" + point.x, point.y + "C" + point.n.x, point.n.y, point.end.x, point.end.y, p[5], p[6]].join();
  4837. len += l;
  4838. x = +p[5];
  4839. y = +p[6];
  4840. continue;
  4841. }
  4842. if (!istotal && !subpath) {
  4843. point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
  4844. return {x: point.x, y: point.y, alpha: point.alpha};
  4845. }
  4846. }
  4847. len += l;
  4848. x = +p[5];
  4849. y = +p[6];
  4850. }
  4851. sp += p.shift() + p;
  4852. }
  4853. subpaths.end = sp;
  4854. point = istotal ? len : subpath ? subpaths : R.findDotsAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], 1);
  4855. point.alpha && (point = {x: point.x, y: point.y, alpha: point.alpha});
  4856. return point;
  4857. };
  4858. };
  4859. var getTotalLength = getLengthFactory(1),
  4860. getPointAtLength = getLengthFactory(),
  4861. getSubpathsAtLength = getLengthFactory(0, 1);
  4862. /*\
  4863. * Raphael.getTotalLength
  4864. [ method ]
  4865. **
  4866. * Returns length of the given path in pixels.
  4867. **
  4868. > Parameters
  4869. **
  4870. - path (string) SVG path string.
  4871. **
  4872. = (number) length.
  4873. \*/
  4874. R.getTotalLength = getTotalLength;
  4875. /*\
  4876. * Raphael.getPointAtLength
  4877. [ method ]
  4878. **
  4879. * Return coordinates of the point located at the given length on the given path.
  4880. **
  4881. > Parameters
  4882. **
  4883. - path (string) SVG path string
  4884. - length (number)
  4885. **
  4886. = (object) representation of the point:
  4887. o {
  4888. o x: (number) x coordinate
  4889. o y: (number) y coordinate
  4890. o alpha: (number) angle of derivative
  4891. o }
  4892. \*/
  4893. R.getPointAtLength = getPointAtLength;
  4894. /*\
  4895. * Raphael.getSubpath
  4896. [ method ]
  4897. **
  4898. * Return subpath of a given path from given length to given length.
  4899. **
  4900. > Parameters
  4901. **
  4902. - path (string) SVG path string
  4903. - from (number) position of the start of the segment
  4904. - to (number) position of the end of the segment
  4905. **
  4906. = (string) pathstring for the segment
  4907. \*/
  4908. R.getSubpath = function (path, from, to) {
  4909. if (abs(this.getTotalLength(path) - to) < 1e-6) {
  4910. return getSubpathsAtLength(path, from).end;
  4911. }
  4912. var a = getSubpathsAtLength(path, to, 1);
  4913. return from ? getSubpathsAtLength(a, from).end : a;
  4914. };
  4915. /*\
  4916. * Element.getTotalLength
  4917. [ method ]
  4918. **
  4919. * Returns length of the path in pixels. Only works for element of “path” type.
  4920. = (number) length.
  4921. \*/
  4922. elproto.getTotalLength = function () {
  4923. if (this.type != "path") {return;}
  4924. if (this.node.getTotalLength) {
  4925. return this.node.getTotalLength();
  4926. }
  4927. return getTotalLength(this.attrs.path);
  4928. };
  4929. /*\
  4930. * Element.getPointAtLength
  4931. [ method ]
  4932. **
  4933. * Return coordinates of the point located at the given length on the given path. Only works for element of “path” type.
  4934. **
  4935. > Parameters
  4936. **
  4937. - length (number)
  4938. **
  4939. = (object) representation of the point:
  4940. o {
  4941. o x: (number) x coordinate
  4942. o y: (number) y coordinate
  4943. o alpha: (number) angle of derivative
  4944. o }
  4945. \*/
  4946. elproto.getPointAtLength = function (length) {
  4947. if (this.type != "path") {return;}
  4948. return getPointAtLength(this.attrs.path, length);
  4949. };
  4950. /*\
  4951. * Element.getSubpath
  4952. [ method ]
  4953. **
  4954. * Return subpath of a given element from given length to given length. Only works for element of “path” type.
  4955. **
  4956. > Parameters
  4957. **
  4958. - from (number) position of the start of the segment
  4959. - to (number) position of the end of the segment
  4960. **
  4961. = (string) pathstring for the segment
  4962. \*/
  4963. elproto.getSubpath = function (from, to) {
  4964. if (this.type != "path") {return;}
  4965. return R.getSubpath(this.attrs.path, from, to);
  4966. };
  4967. /*\
  4968. * Raphael.easing_formulas
  4969. [ property ]
  4970. **
  4971. * Object that contains easing formulas for animation. You could extend it with your own. By default it has following list of easing:
  4972. # <ul>
  4973. # <li>“linear”</li>
  4974. # <li>“&lt;” or “easeIn” or “ease-in”</li>
  4975. # <li>“>” or “easeOut” or “ease-out”</li>
  4976. # <li>“&lt;>” or “easeInOut” or “ease-in-out”</li>
  4977. # <li>“backIn” or “back-in”</li>
  4978. # <li>“backOut” or “back-out”</li>
  4979. # <li>“elastic”</li>
  4980. # <li>“bounce”</li>
  4981. # </ul>
  4982. # <p>See also <a href="http://raphaeljs.com/easing.html">Easing demo</a>.</p>
  4983. \*/
  4984. var ef = R.easing_formulas = {
  4985. linear: function (n) {
  4986. return n;
  4987. },
  4988. "<": function (n) {
  4989. return pow(n, 1.7);
  4990. },
  4991. ">": function (n) {
  4992. return pow(n, .48);
  4993. },
  4994. "<>": function (n) {
  4995. var q = .48 - n / 1.04,
  4996. Q = math.sqrt(.1734 + q * q),
  4997. x = Q - q,
  4998. X = pow(abs(x), 1 / 3) * (x < 0 ? -1 : 1),
  4999. y = -Q - q,
  5000. Y = pow(abs(y), 1 / 3) * (y < 0 ? -1 : 1),
  5001. t = X + Y + .5;
  5002. return (1 - t) * 3 * t * t + t * t * t;
  5003. },
  5004. backIn: function (n) {
  5005. var s = 1.70158;
  5006. return n * n * ((s + 1) * n - s);
  5007. },
  5008. backOut: function (n) {
  5009. n = n - 1;
  5010. var s = 1.70158;
  5011. return n * n * ((s + 1) * n + s) + 1;
  5012. },
  5013. elastic: function (n) {
  5014. if (n == !!n) {
  5015. return n;
  5016. }
  5017. return pow(2, -10 * n) * math.sin((n - .075) * (2 * PI) / .3) + 1;
  5018. },
  5019. bounce: function (n) {
  5020. var s = 7.5625,
  5021. p = 2.75,
  5022. l;
  5023. if (n < (1 / p)) {
  5024. l = s * n * n;
  5025. } else {
  5026. if (n < (2 / p)) {
  5027. n -= (1.5 / p);
  5028. l = s * n * n + .75;
  5029. } else {
  5030. if (n < (2.5 / p)) {
  5031. n -= (2.25 / p);
  5032. l = s * n * n + .9375;
  5033. } else {
  5034. n -= (2.625 / p);
  5035. l = s * n * n + .984375;
  5036. }
  5037. }
  5038. }
  5039. return l;
  5040. }
  5041. };
  5042. ef.easeIn = ef["ease-in"] = ef["<"];
  5043. ef.easeOut = ef["ease-out"] = ef[">"];
  5044. ef.easeInOut = ef["ease-in-out"] = ef["<>"];
  5045. ef["back-in"] = ef.backIn;
  5046. ef["back-out"] = ef.backOut;
  5047. var animationElements = [],
  5048. requestAnimFrame = window.requestAnimationFrame ||
  5049. window.webkitRequestAnimationFrame ||
  5050. window.mozRequestAnimationFrame ||
  5051. window.oRequestAnimationFrame ||
  5052. window.msRequestAnimationFrame ||
  5053. function (callback) {
  5054. setTimeout(callback, 16);
  5055. },
  5056. animation = function () {
  5057. var Now = +new Date,
  5058. l = 0;
  5059. for (; l < animationElements.length; l++) {
  5060. var e = animationElements[l];
  5061. if (e.el.removed || e.paused) {
  5062. continue;
  5063. }
  5064. var time = Now - e.start,
  5065. ms = e.ms,
  5066. easing = e.easing,
  5067. from = e.from,
  5068. diff = e.diff,
  5069. to = e.to,
  5070. t = e.t,
  5071. that = e.el,
  5072. set = {},
  5073. now;
  5074. if (e.initstatus) {
  5075. time = (e.initstatus * e.anim.top - e.prev) / (e.percent - e.prev) * ms;
  5076. e.status = e.initstatus;
  5077. delete e.initstatus;
  5078. e.stop && animationElements.splice(l--, 1);
  5079. } else {
  5080. e.status = (e.prev + (e.percent - e.prev) * (time / ms)) / e.anim.top;
  5081. }
  5082. if (time < 0) {
  5083. continue;
  5084. }
  5085. if (time < ms) {
  5086. var pos = easing(time / ms);
  5087. for (var attr in from) if (from[has](attr)) {
  5088. switch (availableAnimAttrs[attr]) {
  5089. case nu:
  5090. now = +from[attr] + pos * ms * diff[attr];
  5091. break;
  5092. case "colour":
  5093. now = "rgb(" + [
  5094. upto255(round(from[attr].r + pos * ms * diff[attr].r)),
  5095. upto255(round(from[attr].g + pos * ms * diff[attr].g)),
  5096. upto255(round(from[attr].b + pos * ms * diff[attr].b))
  5097. ].join(",") + ")";
  5098. break;
  5099. case "path":
  5100. now = [];
  5101. for (var i = 0, ii = from[attr].length; i < ii; i++) {
  5102. now[i] = [from[attr][i][0]];
  5103. for (var j = 1, jj = from[attr][i].length; j < jj; j++) {
  5104. now[i][j] = +from[attr][i][j] + pos * ms * diff[attr][i][j];
  5105. }
  5106. now[i] = now[i].join(S);
  5107. }
  5108. now = now.join(S);
  5109. break;
  5110. case "transform":
  5111. if (diff[attr].real) {
  5112. now = [];
  5113. for (i = 0, ii = from[attr].length; i < ii; i++) {
  5114. now[i] = [from[attr][i][0]];
  5115. for (j = 1, jj = from[attr][i].length; j < jj; j++) {
  5116. now[i][j] = from[attr][i][j] + pos * ms * diff[attr][i][j];
  5117. }
  5118. }
  5119. } else {
  5120. var get = function (i) {
  5121. return +from[attr][i] + pos * ms * diff[attr][i];
  5122. };
  5123. // now = [["r", get(2), 0, 0], ["t", get(3), get(4)], ["s", get(0), get(1), 0, 0]];
  5124. now = [["m", get(0), get(1), get(2), get(3), get(4), get(5)]];
  5125. }
  5126. break;
  5127. case "csv":
  5128. if (attr == "clip-rect") {
  5129. now = [];
  5130. i = 4;
  5131. while (i--) {
  5132. now[i] = +from[attr][i] + pos * ms * diff[attr][i];
  5133. }
  5134. }
  5135. break;
  5136. default:
  5137. var from2 = [].concat(from[attr]);
  5138. now = [];
  5139. i = that.paper.customAttributes[attr].length;
  5140. while (i--) {
  5141. now[i] = +from2[i] + pos * ms * diff[attr][i];
  5142. }
  5143. break;
  5144. }
  5145. set[attr] = now;
  5146. }
  5147. that.attr(set);
  5148. (function (id, that, anim) {
  5149. setTimeout(function () {
  5150. eve("anim.frame." + id, that, anim);
  5151. });
  5152. })(that.id, that, e.anim);
  5153. } else {
  5154. (function(f, el, a) {
  5155. setTimeout(function() {
  5156. eve("anim.frame." + el.id, el, a);
  5157. eve("anim.finish." + el.id, el, a);
  5158. R.is(f, "function") && f.call(el);
  5159. });
  5160. })(e.callback, that, e.anim);
  5161. that.attr(to);
  5162. animationElements.splice(l--, 1);
  5163. if (e.repeat > 1 && !e.next) {
  5164. runAnimation(e.anim, e.el, e.anim.percents[0], null, e.totalOrigin, e.repeat - 1);
  5165. }
  5166. if (e.next && !e.stop) {
  5167. runAnimation(e.anim, e.el, e.next, null, e.totalOrigin, e.repeat);
  5168. }
  5169. }
  5170. }
  5171. R.svg && that && that.paper && that.paper.safari();
  5172. animationElements.length && requestAnimFrame(animation);
  5173. },
  5174. upto255 = function (color) {
  5175. return color > 255 ? 255 : color < 0 ? 0 : color;
  5176. };
  5177. /*\
  5178. * Element.animateWith
  5179. [ method ]
  5180. **
  5181. * Acts similar to @Element.animate, but ensure that given animation runs in sync with another given element.
  5182. **
  5183. > Parameters
  5184. **
  5185. - params (object) final attributes for the element, see also @Element.attr
  5186. - ms (number) number of milliseconds for animation to run
  5187. - easing (string) #optional easing type. Accept on of @Raphael.easing_formulas or CSS format: `cubic&#x2010;bezier(XX,&#160;XX,&#160;XX,&#160;XX)`
  5188. - callback (function) #optional callback function. Will be called at the end of animation.
  5189. * or
  5190. - animation (object) animation object, see @Raphael.animation
  5191. **
  5192. = (object) original element
  5193. \*/
  5194. elproto.animateWith = function (element, params, ms, easing, callback) {
  5195. this.animate(params, ms, easing, callback);
  5196. var start, el;
  5197. for (var i = 0, ii = animationElements.length; i < ii; i++) {
  5198. el = animationElements[i];
  5199. if (el.el.id == element.id) {
  5200. start = el.timestamp;
  5201. } else if (el.el.id == this.id) {
  5202. el.start = start;
  5203. }
  5204. }
  5205. return this.animate(params, ms, easing, callback);
  5206. };
  5207. function CubicBezierAtTime(t, p1x, p1y, p2x, p2y, duration) {
  5208. var cx = 3 * p1x,
  5209. bx = 3 * (p2x - p1x) - cx,
  5210. ax = 1 - cx - bx,
  5211. cy = 3 * p1y,
  5212. by = 3 * (p2y - p1y) - cy,
  5213. ay = 1 - cy - by;
  5214. function sampleCurveX(t) {
  5215. return ((ax * t + bx) * t + cx) * t;
  5216. }
  5217. function solve(x, epsilon) {
  5218. var t = solveCurveX(x, epsilon);
  5219. return ((ay * t + by) * t + cy) * t;
  5220. }
  5221. function solveCurveX(x, epsilon) {
  5222. var t0, t1, t2, x2, d2, i;
  5223. for(t2 = x, i = 0; i < 8; i++) {
  5224. x2 = sampleCurveX(t2) - x;
  5225. if (abs(x2) < epsilon) {
  5226. return t2;
  5227. }
  5228. d2 = (3 * ax * t2 + 2 * bx) * t2 + cx;
  5229. if (abs(d2) < 1e-6) {
  5230. break;
  5231. }
  5232. t2 = t2 - x2 / d2;
  5233. }
  5234. t0 = 0;
  5235. t1 = 1;
  5236. t2 = x;
  5237. if (t2 < t0) {
  5238. return t0;
  5239. }
  5240. if (t2 > t1) {
  5241. return t1;
  5242. }
  5243. while (t0 < t1) {
  5244. x2 = sampleCurveX(t2);
  5245. if (abs(x2 - x) < epsilon) {
  5246. return t2;
  5247. }
  5248. if (x > x2) {
  5249. t0 = t2;
  5250. } else {
  5251. t1 = t2;
  5252. }
  5253. t2 = (t1 - t0) / 2 + t0;
  5254. }
  5255. return t2;
  5256. }
  5257. return solve(t, 1 / (200 * duration));
  5258. }
  5259. elproto.onAnimation = function (f) {
  5260. f ? eve.on("anim.frame." + this.id, f) : eve.unbind("anim.frame." + this.id);
  5261. return this;
  5262. };
  5263. function Animation(anim, ms) {
  5264. var percents = [];
  5265. this.anim = anim;
  5266. this.ms = ms;
  5267. this.times = 1;
  5268. if (this.anim) {
  5269. for (var attr in this.anim) if (this.anim[has](attr)) {
  5270. percents.push(+attr);
  5271. }
  5272. percents.sort(sortByNumber);
  5273. }
  5274. this.top = percents[percents.length - 1];
  5275. this.percents = percents;
  5276. }
  5277. /*\
  5278. * Animation.delay
  5279. [ method ]
  5280. **
  5281. * Creates a copy of existing animation object with given delay.
  5282. **
  5283. > Parameters
  5284. **
  5285. - delay (number) number of ms to pass between animation start and actual animation
  5286. **
  5287. = (object) new altered Animation object
  5288. \*/
  5289. Animation.prototype.delay = function (delay) {
  5290. var a = new Animation(this.anim, this.ms);
  5291. a.times = this.times;
  5292. a.del = +delay || 0;
  5293. return a;
  5294. };
  5295. /*\
  5296. * Animation.repeat
  5297. [ method ]
  5298. **
  5299. * Creates a copy of existing animation object with given repetition.
  5300. **
  5301. > Parameters
  5302. **
  5303. - repeat (number) number iterations of animation. For infinite animation pass `Infinity`
  5304. **
  5305. = (object) new altered Animation object
  5306. \*/
  5307. Animation.prototype.repeat = function (times) {
  5308. var a = new Animation(this.anim, this.ms);
  5309. a.del = this.del;
  5310. a.times = math.floor(mmax(times, 0)) || 1;
  5311. return a;
  5312. };
  5313. function runAnimation(anim, element, percent, status, totalOrigin, times) {
  5314. percent = toFloat(percent);
  5315. var params,
  5316. isInAnim,
  5317. isInAnimSet,
  5318. percents = [],
  5319. next,
  5320. prev,
  5321. timestamp,
  5322. ms = anim.ms,
  5323. from = {},
  5324. to = {},
  5325. diff = {};
  5326. if (status) {
  5327. for (i = 0, ii = animationElements.length; i < ii; i++) {
  5328. var e = animationElements[i];
  5329. if (e.el.id == element.id && e.anim == anim) {
  5330. if (e.percent != percent) {
  5331. animationElements.splice(i, 1);
  5332. isInAnimSet = 1;
  5333. } else {
  5334. isInAnim = e;
  5335. }
  5336. element.attr(e.totalOrigin);
  5337. break;
  5338. }
  5339. }
  5340. } else {
  5341. status = +to; // NaN
  5342. }
  5343. for (var i = 0, ii = anim.percents.length; i < ii; i++) {
  5344. if (anim.percents[i] == percent || anim.percents[i] > status * anim.top) {
  5345. percent = anim.percents[i];
  5346. prev = anim.percents[i - 1] || 0;
  5347. ms = ms / anim.top * (percent - prev);
  5348. next = anim.percents[i + 1];
  5349. params = anim.anim[percent];
  5350. break;
  5351. } else if (status) {
  5352. element.attr(anim.anim[anim.percents[i]]);
  5353. }
  5354. }
  5355. if (!params) {
  5356. return;
  5357. }
  5358. if (!isInAnim) {
  5359. for (attr in params) if (params[has](attr)) {
  5360. if (availableAnimAttrs[has](attr) || element.paper.customAttributes[has](attr)) {
  5361. from[attr] = element.attr(attr);
  5362. (from[attr] == null) && (from[attr] = availableAttrs[attr]);
  5363. to[attr] = params[attr];
  5364. switch (availableAnimAttrs[attr]) {
  5365. case nu:
  5366. diff[attr] = (to[attr] - from[attr]) / ms;
  5367. break;
  5368. case "colour":
  5369. from[attr] = R.getRGB(from[attr]);
  5370. var toColour = R.getRGB(to[attr]);
  5371. diff[attr] = {
  5372. r: (toColour.r - from[attr].r) / ms,
  5373. g: (toColour.g - from[attr].g) / ms,
  5374. b: (toColour.b - from[attr].b) / ms
  5375. };
  5376. break;
  5377. case "path":
  5378. var pathes = path2curve(from[attr], to[attr]),
  5379. toPath = pathes[1];
  5380. from[attr] = pathes[0];
  5381. diff[attr] = [];
  5382. for (i = 0, ii = from[attr].length; i < ii; i++) {
  5383. diff[attr][i] = [0];
  5384. for (var j = 1, jj = from[attr][i].length; j < jj; j++) {
  5385. diff[attr][i][j] = (toPath[i][j] - from[attr][i][j]) / ms;
  5386. }
  5387. }
  5388. break;
  5389. case "transform":
  5390. var _ = element._,
  5391. eq = equaliseTransform(_[attr], to[attr]);
  5392. if (eq) {
  5393. from[attr] = eq.from;
  5394. to[attr] = eq.to;
  5395. diff[attr] = [];
  5396. diff[attr].real = true;
  5397. for (i = 0, ii = from[attr].length; i < ii; i++) {
  5398. diff[attr][i] = [from[attr][i][0]];
  5399. for (j = 1, jj = from[attr][i].length; j < jj; j++) {
  5400. diff[attr][i][j] = (to[attr][i][j] - from[attr][i][j]) / ms;
  5401. }
  5402. }
  5403. } else {
  5404. var m = (element.matrix || new Matrix),
  5405. to2 = {_:{transform: _.transform}, getBBox: function () { return element.getBBox(); }};
  5406. from[attr] = [
  5407. m.a,
  5408. m.b,
  5409. m.c,
  5410. m.d,
  5411. m.e,
  5412. m.f
  5413. ];
  5414. extractTransform(to2, to[attr]);
  5415. to[attr] = to2._.transform;
  5416. diff[attr] = [
  5417. (to2.matrix.a - m.a) / ms,
  5418. (to2.matrix.b - m.b) / ms,
  5419. (to2.matrix.c - m.c) / ms,
  5420. (to2.matrix.d - m.d) / ms,
  5421. (to2.matrix.e - m.e) / ms,
  5422. (to2.matrix.e - m.f) / ms
  5423. ];
  5424. // from[attr] = [_.sx, _.sy, _.deg, _.dx, _.dy];
  5425. // var to2 = {_:{}, getBBox: function () { return element.getBBox(); }};
  5426. // extractTransform(to2, to[attr]);
  5427. // diff[attr] = [
  5428. // (to2._.sx - _.sx) / ms,
  5429. // (to2._.sy - _.sy) / ms,
  5430. // (to2._.deg - _.deg) / ms,
  5431. // (to2._.dx - _.dx) / ms,
  5432. // (to2._.dy - _.dy) / ms
  5433. // ];
  5434. }
  5435. break;
  5436. case "csv":
  5437. var values = Str(params[attr]).split(separator),
  5438. from2 = Str(from[attr]).split(separator);
  5439. if (attr == "clip-rect") {
  5440. from[attr] = from2;
  5441. diff[attr] = [];
  5442. i = from2.length;
  5443. while (i--) {
  5444. diff[attr][i] = (values[i] - from[attr][i]) / ms;
  5445. }
  5446. }
  5447. to[attr] = values;
  5448. break;
  5449. default:
  5450. values = [].concat(params[attr]);
  5451. from2 = [].concat(from[attr]);
  5452. diff[attr] = [];
  5453. i = element.paper.customAttributes[attr].length;
  5454. while (i--) {
  5455. diff[attr][i] = ((values[i] || 0) - (from2[i] || 0)) / ms;
  5456. }
  5457. break;
  5458. }
  5459. }
  5460. }
  5461. var easing = params.easing,
  5462. easyeasy = R.easing_formulas[easing];
  5463. if (!easyeasy) {
  5464. easyeasy = Str(easing).match(bezierrg);
  5465. if (easyeasy && easyeasy.length == 5) {
  5466. var curve = easyeasy;
  5467. easyeasy = function (t) {
  5468. return CubicBezierAtTime(t, +curve[1], +curve[2], +curve[3], +curve[4], ms);
  5469. };
  5470. } else {
  5471. easyeasy = pipe;
  5472. }
  5473. }
  5474. timestamp = params.start || anim.start || +new Date;
  5475. e = {
  5476. anim: anim,
  5477. percent: percent,
  5478. timestamp: timestamp,
  5479. start: timestamp + (anim.del || 0),
  5480. status: 0,
  5481. initstatus: status || 0,
  5482. stop: false,
  5483. ms: ms,
  5484. easing: easyeasy,
  5485. from: from,
  5486. diff: diff,
  5487. to: to,
  5488. el: element,
  5489. callback: params.callback,
  5490. prev: prev,
  5491. next: next,
  5492. repeat: times || anim.times,
  5493. origin: element.attr(),
  5494. totalOrigin: totalOrigin
  5495. };
  5496. animationElements.push(e);
  5497. if (status && !isInAnim) {
  5498. e.stop = true;
  5499. e.start = new Date - ms * status;
  5500. if (animationElements.length == 1) {
  5501. return animation();
  5502. }
  5503. }
  5504. animationElements.length == 1 && requestAnimFrame(animation);
  5505. } else {
  5506. isInAnim.initstatus = status;
  5507. isInAnim.start = new Date - isInAnim.ms * status;
  5508. }
  5509. eve("anim.start." + element.id, element, anim);
  5510. }
  5511. /*\
  5512. * Raphael.animation
  5513. [ method ]
  5514. **
  5515. * Creates an animation object that can be passed to the @Element.animate or @Element.animateWith methods.
  5516. * See also @Animation.delay and @Animation.repeat methods.
  5517. **
  5518. > Parameters
  5519. **
  5520. - params (object) final attributes for the element, see also @Element.attr
  5521. - ms (number) number of milliseconds for animation to run
  5522. - easing (string) #optional easing type. Accept one of @Raphael.easing_formulas or CSS format: `cubic&#x2010;bezier(XX,&#160;XX,&#160;XX,&#160;XX)`
  5523. - callback (function) #optional callback function. Will be called at the end of animation.
  5524. **
  5525. = (object) @Animation
  5526. \*/
  5527. R.animation = function (params, ms, easing, callback) {
  5528. if (R.is(easing, "function") || !easing) {
  5529. callback = callback || easing || null;
  5530. easing = null;
  5531. }
  5532. params = Object(params);
  5533. ms = +ms || 0;
  5534. var p = {},
  5535. json,
  5536. attr;
  5537. for (attr in params) if (params[has](attr) && toFloat(attr) != attr) {
  5538. json = true;
  5539. p[attr] = params[attr];
  5540. }
  5541. if (!json) {
  5542. return new Animation(params, ms);
  5543. } else {
  5544. easing && (p.easing = easing);
  5545. callback && (p.callback = callback);
  5546. return new Animation({100: p}, ms);
  5547. }
  5548. };
  5549. /*\
  5550. * Element.animate
  5551. [ method ]
  5552. **
  5553. * Creates and starts animation for given element.
  5554. **
  5555. > Parameters
  5556. **
  5557. - params (object) final attributes for the element, see also @Element.attr
  5558. - ms (number) number of milliseconds for animation to run
  5559. - easing (string) #optional easing type. Accept one of @Raphael.easing_formulas or CSS format: `cubic&#x2010;bezier(XX,&#160;XX,&#160;XX,&#160;XX)`
  5560. - callback (function) #optional callback function. Will be called at the end of animation.
  5561. * or
  5562. - animation (object) animation object, see @Raphael.animation
  5563. **
  5564. = (object) original element
  5565. \*/
  5566. elproto.animate = function (params, ms, easing, callback) {
  5567. var element = this;
  5568. if (element.removed) {
  5569. callback && callback.call(element);
  5570. return element;
  5571. }
  5572. var anim = params instanceof Animation ? params : R.animation(params, ms, easing, callback);
  5573. runAnimation(anim, element, anim.percents[0], null, element.attr());
  5574. return element;
  5575. };
  5576. /*\
  5577. * Element.setTime
  5578. [ method ]
  5579. **
  5580. * Sets the status of animation of the element in milliseconds. Similar to @Element.status method.
  5581. **
  5582. > Parameters
  5583. **
  5584. - anim (object) animation object
  5585. - value (number) number of milliseconds from the beginning of the animation
  5586. **
  5587. = (object) original element if `value` is specified
  5588. * Note, that during animation following events are triggered:
  5589. *
  5590. * On each animation frame event `anim.frame.<id>`, on start `anim.start.<id>` and on end `anim.finish.<id>`.
  5591. \*/
  5592. elproto.setTime = function (anim, value) {
  5593. if (anim && value != null) {
  5594. this.status(anim, mmin(value, anim.ms) / anim.ms);
  5595. }
  5596. return this;
  5597. };
  5598. /*\
  5599. * Element.status
  5600. [ method ]
  5601. **
  5602. * Gets or sets the status of animation of the element.
  5603. **
  5604. > Parameters
  5605. **
  5606. - anim (object) #optional animation object
  5607. - value (number) #optional 0 – 1. If specified, method works like a setter and sets the status of a given animation to the value. This will cause animation to jump to the given position.
  5608. **
  5609. = (number) status
  5610. * or
  5611. = (array) status if `anim` is not specified. Array of objects in format:
  5612. o {
  5613. o anim: (object) animation object
  5614. o status: (number) status
  5615. o }
  5616. * or
  5617. = (object) original element if `value` is specified
  5618. \*/
  5619. elproto.status = function (anim, value) {
  5620. var out = [],
  5621. i = 0,
  5622. len,
  5623. e;
  5624. if (value != null) {
  5625. runAnimation(anim, this, -1, mmin(value, 1));
  5626. return this;
  5627. } else {
  5628. len = animationElements.length;
  5629. for (; i < len; i++) {
  5630. e = animationElements[i];
  5631. if (e.el.id == this.id && (!anim || e.anim == anim)) {
  5632. if (anim) {
  5633. return e.status;
  5634. }
  5635. out.push({anim: e.anim, status: e.status});
  5636. }
  5637. }
  5638. if (anim) {
  5639. return 0;
  5640. }
  5641. return out;
  5642. }
  5643. };
  5644. /*\
  5645. * Element.pause
  5646. [ method ]
  5647. **
  5648. * Stops animation of the element with ability to resume it later on.
  5649. **
  5650. > Parameters
  5651. **
  5652. - anim (object) #optional animation object
  5653. **
  5654. = (object) original element
  5655. \*/
  5656. elproto.pause = function (anim) {
  5657. for (var i = 0; i < animationElements.length; i++) if (animationElements[i].el.id == this.id && (!anim || animationElements[i].anim == anim)) {
  5658. if (eve("anim.pause." + this.id, this, animationElements[i].anim) !== false) {
  5659. animationElements[i].paused = true;
  5660. }
  5661. }
  5662. return this;
  5663. };
  5664. /*\
  5665. * Element.resume
  5666. [ method ]
  5667. **
  5668. * Resumes animation if it was paused with @Element.pause method.
  5669. **
  5670. > Parameters
  5671. **
  5672. - anim (object) #optional animation object
  5673. **
  5674. = (object) original element
  5675. \*/
  5676. elproto.resume = function (anim) {
  5677. for (var i = 0; i < animationElements.length; i++) if (animationElements[i].el.id == this.id && (!anim || animationElements[i].anim == anim)) {
  5678. var e = animationElements[i];
  5679. if (eve("anim.resume." + this.id, this, e.anim) !== false) {
  5680. delete e.paused;
  5681. this.status(e.anim, e.status);
  5682. }
  5683. }
  5684. return this;
  5685. };
  5686. /*\
  5687. * Element.stop
  5688. [ method ]
  5689. **
  5690. * Stops animation of the element.
  5691. **
  5692. > Parameters
  5693. **
  5694. - anim (object) #optional animation object
  5695. **
  5696. = (object) original element
  5697. \*/
  5698. elproto.stop = function (anim) {
  5699. for (var i = 0; i < animationElements.length; i++) if (animationElements[i].el.id == this.id && (!anim || animationElements[i].anim == anim)) {
  5700. if (eve("anim.stop." + this.id, this, animationElements[i].anim) !== false) {
  5701. animationElements.splice(i--, 1);
  5702. }
  5703. }
  5704. return this;
  5705. };
  5706. elproto.toString = function () {
  5707. return "Rapha\xebl\u2019s object";
  5708. };
  5709. // Set
  5710. var Set = function (items) {
  5711. this.items = [];
  5712. this.length = 0;
  5713. this.type = "set";
  5714. if (items) {
  5715. for (var i = 0, ii = items.length; i < ii; i++) {
  5716. if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) {
  5717. this[this.items.length] = this.items[this.items.length] = items[i];
  5718. this.length++;
  5719. }
  5720. }
  5721. }
  5722. },
  5723. setproto = Set.prototype;
  5724. /*\
  5725. * Set.push
  5726. [ method ]
  5727. **
  5728. * Adds each argument to the current set.
  5729. = (object) original element
  5730. \*/
  5731. setproto.push = function () {
  5732. var item,
  5733. len;
  5734. for (var i = 0, ii = arguments.length; i < ii; i++) {
  5735. item = arguments[i];
  5736. if (item && (item.constructor == Element || item.constructor == Set)) {
  5737. len = this.items.length;
  5738. this[len] = this.items[len] = item;
  5739. this.length++;
  5740. }
  5741. }
  5742. return this;
  5743. };
  5744. /*\
  5745. * Set.pop
  5746. [ method ]
  5747. **
  5748. * Removes last element and returns it.
  5749. = (object) element
  5750. \*/
  5751. setproto.pop = function () {
  5752. this.length && delete this[this.length--];
  5753. return this.items.pop();
  5754. };
  5755. /*\
  5756. * Set.forEach
  5757. [ method ]
  5758. **
  5759. * Executes given function for each element in the set.
  5760. *
  5761. * If function returns `false` it will stop loop running.
  5762. **
  5763. > Parameters
  5764. **
  5765. - callback (function) function to run
  5766. - thisArg (object) context object for the callback
  5767. = (object) Set object
  5768. \*/
  5769. setproto.forEach = function (callback, thisArg) {
  5770. for (var i = 0, ii = this.items.length; i < ii; i++) {
  5771. if (callback.call(thisArg, this.items[i]) === false) {
  5772. return this;
  5773. }
  5774. }
  5775. return this;
  5776. };
  5777. for (var method in elproto) if (elproto[has](method)) {
  5778. setproto[method] = (function (methodname) {
  5779. return function () {
  5780. var arg = arguments;
  5781. return this.forEach(function (el) {
  5782. el[methodname][apply](el, arg);
  5783. });
  5784. };
  5785. })(method);
  5786. }
  5787. setproto.attr = function (name, value) {
  5788. if (name && R.is(name, array) && R.is(name[0], "object")) {
  5789. for (var j = 0, jj = name.length; j < jj; j++) {
  5790. this.items[j].attr(name[j]);
  5791. }
  5792. } else {
  5793. for (var i = 0, ii = this.items.length; i < ii; i++) {
  5794. this.items[i].attr(name, value);
  5795. }
  5796. }
  5797. return this;
  5798. };
  5799. setproto.clear = function () {
  5800. while (this.length) {
  5801. this.pop();
  5802. }
  5803. };
  5804. setproto.animate = function (params, ms, easing, callback) {
  5805. (R.is(easing, "function") || !easing) && (callback = easing || null);
  5806. var len = this.items.length,
  5807. i = len,
  5808. item,
  5809. set = this,
  5810. collector;
  5811. if (!len) {
  5812. return this;
  5813. }
  5814. callback && (collector = function () {
  5815. !--len && callback.call(set);
  5816. });
  5817. easing = R.is(easing, string) ? easing : collector;
  5818. var anim = params instanceof Animation ? params : R.animation(params, ms, easing, collector);
  5819. item = this.items[--i].animate(anim);
  5820. while (i--) {
  5821. this.items[i] && !this.items[i].removed && this.items[i].animateWith(item, anim);
  5822. }
  5823. return this;
  5824. };
  5825. setproto.insertAfter = function (el) {
  5826. var i = this.items.length;
  5827. while (i--) {
  5828. this.items[i].insertAfter(el);
  5829. }
  5830. return this;
  5831. };
  5832. setproto.getBBox = function () {
  5833. var x = [],
  5834. y = [],
  5835. w = [],
  5836. h = [];
  5837. for (var i = this.items.length; i--;) if (!this.items[i].removed) {
  5838. var box = this.items[i].getBBox();
  5839. x.push(box.x);
  5840. y.push(box.y);
  5841. w.push(box.x + box.width);
  5842. h.push(box.y + box.height);
  5843. }
  5844. x = mmin[apply](0, x);
  5845. y = mmin[apply](0, y);
  5846. return {
  5847. x: x,
  5848. y: y,
  5849. width: mmax[apply](0, w) - x,
  5850. height: mmax[apply](0, h) - y
  5851. };
  5852. };
  5853. setproto.clone = function (s) {
  5854. s = new Set;
  5855. for (var i = 0, ii = this.items.length; i < ii; i++) {
  5856. s.push(this.items[i].clone());
  5857. }
  5858. return s;
  5859. };
  5860. setproto.toString = function () {
  5861. return "Rapha\xebl\u2018s set";
  5862. };
  5863. R.registerFont = function (font) {
  5864. if (!font.face) {
  5865. return font;
  5866. }
  5867. this.fonts = this.fonts || {};
  5868. var fontcopy = {
  5869. w: font.w,
  5870. face: {},
  5871. glyphs: {}
  5872. },
  5873. family = font.face["font-family"];
  5874. for (var prop in font.face) if (font.face[has](prop)) {
  5875. fontcopy.face[prop] = font.face[prop];
  5876. }
  5877. if (this.fonts[family]) {
  5878. this.fonts[family].push(fontcopy);
  5879. } else {
  5880. this.fonts[family] = [fontcopy];
  5881. }
  5882. if (!font.svg) {
  5883. fontcopy.face["units-per-em"] = toInt(font.face["units-per-em"], 10);
  5884. for (var glyph in font.glyphs) if (font.glyphs[has](glyph)) {
  5885. var path = font.glyphs[glyph];
  5886. fontcopy.glyphs[glyph] = {
  5887. w: path.w,
  5888. k: {},
  5889. d: path.d && "M" + path.d.replace(/[mlcxtrv]/g, function (command) {
  5890. return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
  5891. }) + "z"
  5892. };
  5893. if (path.k) {
  5894. for (var k in path.k) if (path[has](k)) {
  5895. fontcopy.glyphs[glyph].k[k] = path.k[k];
  5896. }
  5897. }
  5898. }
  5899. }
  5900. return font;
  5901. };
  5902. paperproto.getFont = function (family, weight, style, stretch) {
  5903. stretch = stretch || "normal";
  5904. style = style || "normal";
  5905. weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
  5906. if (!R.fonts) {
  5907. return;
  5908. }
  5909. var font = R.fonts[family];
  5910. if (!font) {
  5911. var name = new RegExp("(^|\\s)" + family.replace(/[^\w\d\s+!~.:_-]/g, E) + "(\\s|$)", "i");
  5912. for (var fontName in R.fonts) if (R.fonts[has](fontName)) {
  5913. if (name.test(fontName)) {
  5914. font = R.fonts[fontName];
  5915. break;
  5916. }
  5917. }
  5918. }
  5919. var thefont;
  5920. if (font) {
  5921. for (var i = 0, ii = font.length; i < ii; i++) {
  5922. thefont = font[i];
  5923. if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
  5924. break;
  5925. }
  5926. }
  5927. }
  5928. return thefont;
  5929. };
  5930. paperproto.print = function (x, y, string, font, size, origin, letter_spacing) {
  5931. origin = origin || "middle"; // baseline|middle
  5932. letter_spacing = mmax(mmin(letter_spacing || 0, 1), -1);
  5933. var out = this.set(),
  5934. letters = Str(string).split(E),
  5935. shift = 0,
  5936. path = E,
  5937. scale;
  5938. R.is(font, string) && (font = this.getFont(font));
  5939. if (font) {
  5940. scale = (size || 16) / font.face["units-per-em"];
  5941. var bb = font.face.bbox.split(separator),
  5942. top = +bb[0],
  5943. height = +bb[1] + (origin == "baseline" ? bb[3] - bb[1] + (+font.face.descent) : (bb[3] - bb[1]) / 2);
  5944. for (var i = 0, ii = letters.length; i < ii; i++) {
  5945. var prev = i && font.glyphs[letters[i - 1]] || {},
  5946. curr = font.glyphs[letters[i]];
  5947. shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) + (font.w * letter_spacing) : 0;
  5948. curr && curr.d && out.push(this.path(curr.d).attr({fill: "#000", stroke: "none", transform: [["t", shift, 0]]}));
  5949. }
  5950. out.scale(scale, scale, top, height).translate(x - top, y - height);
  5951. }
  5952. return out;
  5953. };
  5954. R.format = function (token, params) {
  5955. var args = R.is(params, array) ? [0][concat](params) : arguments;
  5956. token && R.is(token, string) && args.length - 1 && (token = token.replace(formatrg, function (str, i) {
  5957. return args[++i] == null ? E : args[i];
  5958. }));
  5959. return token || E;
  5960. };
  5961. R.ninja = function () {
  5962. oldRaphael.was ? (g.win.Raphael = oldRaphael.is) : delete Raphael;
  5963. return R;
  5964. };
  5965. /*\
  5966. * Raphael.el
  5967. [ property (object) ]
  5968. **
  5969. * You can add your own method to elements. This is usefull when you want to hack default functionality or
  5970. * want to wrap some common transformation or attributes in one method. In difference to canvas methods,
  5971. * you can redefine element method at any time. Expending element methods wouldn’t affect set.
  5972. > Usage
  5973. | Raphael.el.red = function () {
  5974. | this.attr({fill: "#f00"});
  5975. | };
  5976. | // then use it
  5977. | paper.circle(100, 100, 20).red();
  5978. \*/
  5979. R.el = elproto;
  5980. R.st = setproto;
  5981. // Firefox <3.6 fix: http://webreflection.blogspot.com/2009/11/195-chars-to-help-lazy-loading.html
  5982. (function (doc, loaded, f) {
  5983. if (doc.readyState == null && doc.addEventListener){
  5984. doc.addEventListener(loaded, f = function () {
  5985. doc.removeEventListener(loaded, f, false);
  5986. doc.readyState = "complete";
  5987. }, false);
  5988. doc.readyState = "loading";
  5989. }
  5990. function isLoaded() {
  5991. (/in/).test(doc.readyState) ? setTimeout(isLoaded, 9) : eve("DOMload");
  5992. }
  5993. isLoaded();
  5994. })(document, "DOMContentLoaded");
  5995. oldRaphael.was ? (g.win.Raphael = R) : (Raphael = R);
  5996. /*
  5997. * Eve 0.2.3 - JavaScript Events Library
  5998. *
  5999. * Copyright (c) 2010 Dmitry Baranovskiy (http://dmitry.baranovskiy.com/)
  6000. * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
  6001. */
  6002. var eve = R.eve = (function () {
  6003. var version = "0.2.3",
  6004. has = "hasOwnProperty",
  6005. separator = /[\.\/]/,
  6006. wildcard = "*",
  6007. fun = function () {},
  6008. numsort = function (a, b) {
  6009. return a - b;
  6010. },
  6011. current_event,
  6012. events = {n: {}},
  6013. /*\
  6014. * eve
  6015. [ method ]
  6016. **
  6017. * Fires event with given `name`, given scope and other parameters.
  6018. **
  6019. > Arguments
  6020. **
  6021. - name (string) name of the event, dot (`.`) or slash (`/`) separated
  6022. - scope (object) context for the event handlers
  6023. - varargs (...) the rest of arguments will be sent to event handlers
  6024. **
  6025. = (array) array of errors, if any. Each element of the array is in format:
  6026. o {
  6027. o error (string) error message
  6028. o func (function) handler that caused error
  6029. o }
  6030. \*/
  6031. eve = function (name, scope) {
  6032. var e = events,
  6033. args = Array.prototype.slice.call(arguments, 2),
  6034. listeners = eve.listeners(name),
  6035. z = 0,
  6036. f = false,
  6037. l,
  6038. indexed = [],
  6039. queue = {},
  6040. errors = [];
  6041. current_event = name;
  6042. for (var i = 0, ii = listeners.length; i < ii; i++) if ("zIndex" in listeners[i]) {
  6043. indexed.push(listeners[i].zIndex);
  6044. if (listeners[i].zIndex < 0) {
  6045. queue[listeners[i].zIndex] = listeners[i];
  6046. }
  6047. }
  6048. indexed.sort(numsort);
  6049. while (indexed[z] < 0) {
  6050. l = queue[indexed[z++]];
  6051. if (l.apply(scope, args) === f) {
  6052. return f;
  6053. }
  6054. }
  6055. for (i = 0; i < ii; i++) {
  6056. l = listeners[i];
  6057. if ("zIndex" in l) {
  6058. if (l.zIndex == indexed[z]) {
  6059. if (l.apply(scope, args) === f) {
  6060. return f;
  6061. }
  6062. do {
  6063. z++;
  6064. l = queue[indexed[z]];
  6065. if (l) {
  6066. if (l.apply(scope, args) === f) {
  6067. return f;
  6068. }
  6069. }
  6070. } while (l)
  6071. } else {
  6072. queue[l.zIndex] = l;
  6073. }
  6074. } else {
  6075. if (l.apply(scope, args) === f) {
  6076. return f;
  6077. }
  6078. }
  6079. }
  6080. };
  6081. /*\
  6082. * eve.listeners
  6083. [ method ]
  6084. **
  6085. * Internal method which gives you array of all event handlers that will be triggered by the given `name`.
  6086. **
  6087. > Arguments
  6088. **
  6089. - name (string) name of the event, dot (`.`) or slash (`/`) separated
  6090. **
  6091. = (array) array of event handlers
  6092. \*/
  6093. eve.listeners = function (name) {
  6094. var names = name.split(separator),
  6095. e = events,
  6096. item,
  6097. items,
  6098. k,
  6099. i,
  6100. ii,
  6101. j,
  6102. jj,
  6103. nes,
  6104. es = [e],
  6105. out = [];
  6106. for (i = 0, ii = names.length; i < ii; i++) {
  6107. nes = [];
  6108. for (j = 0, jj = es.length; j < jj; j++) {
  6109. e = es[j].n;
  6110. items = [e[names[i]], e[wildcard]];
  6111. k = 2;
  6112. while (k--) {
  6113. item = items[k];
  6114. if (item) {
  6115. nes.push(item);
  6116. out = out.concat(item.f || []);
  6117. }
  6118. }
  6119. }
  6120. es = nes;
  6121. }
  6122. return out;
  6123. };
  6124. /*\
  6125. * eve.on
  6126. [ method ]
  6127. **
  6128. * Binds given event handler with a given name. You can use wildcards “`*`” for the names:
  6129. | eve.on("*.under.*", f);
  6130. | eve("mouse.under.floor"); // triggers f
  6131. * Use @eve to trigger the listener.
  6132. **
  6133. > Arguments
  6134. **
  6135. - name (string) name of the event, dot (`.`) or slash (`/`) separated, with optional wildcards
  6136. - f (function) event handler function
  6137. **
  6138. = (function) returned function accept one number parameter that represents z-index of the handler. It is optional feature and only used when you need to ensure that some subset of handlers will be invoked in a given order, despite of the order of assignment.
  6139. > Example:
  6140. | eve.on("mouse", eat)(2);
  6141. | eve.on("mouse", scream);
  6142. | eve.on("mouse", catch)(1);
  6143. * This will ensure that `catch` function will be called before `eat`.
  6144. * If you want to put you hadler before not indexed handlers specify negative value.
  6145. * Note: I assume most of the time you don’t need to worry about z-index, but it’s nice to have this feature “just in case”.
  6146. \*/
  6147. eve.on = function (name, f) {
  6148. var names = name.split(separator),
  6149. e = events;
  6150. for (var i = 0, ii = names.length; i < ii; i++) {
  6151. e = e.n;
  6152. !e[names[i]] && (e[names[i]] = {n: {}});
  6153. e = e[names[i]];
  6154. }
  6155. e.f = e.f || [];
  6156. for (i = 0, ii = e.f.length; i < ii; i++) if (e.f[i] == f) {
  6157. return fun;
  6158. }
  6159. e.f.push(f);
  6160. return function (zIndex) {
  6161. if (+zIndex == +zIndex) {
  6162. f.zIndex = +zIndex;
  6163. }
  6164. };
  6165. };
  6166. /*\
  6167. * eve.nt
  6168. [ method ]
  6169. **
  6170. * Could be used inside event handler to figure out actual name of the event.
  6171. **
  6172. > Arguments
  6173. **
  6174. - subname (string) #optional subname of the event
  6175. **
  6176. = (string) name of the event, if `subname` is not specified
  6177. * or
  6178. = (boolean) `true`, if current event’s name contains `subname`
  6179. \*/
  6180. eve.nt = function (subname) {
  6181. if (subname) {
  6182. return new RegExp("(?:\\.|\\/|^)" + subname + "(?:\\.|\\/|$)").test(current_event);
  6183. }
  6184. return current_event;
  6185. };
  6186. /*\
  6187. * eve.unbind
  6188. [ method ]
  6189. **
  6190. * Removes given function from the list of event listeners assigned to given name.
  6191. **
  6192. > Arguments
  6193. **
  6194. - name (string) name of the event, dot (`.`) or slash (`/`) separated, with optional wildcards
  6195. - f (function) event handler function
  6196. \*/
  6197. eve.unbind = function (name, f) {
  6198. var names = name.split(separator),
  6199. e,
  6200. key,
  6201. splice,
  6202. cur = [events];
  6203. for (var i = 0, ii = names.length; i < ii; i++) {
  6204. for (var j = 0; j < cur.length; j += splice.length - 2) {
  6205. splice = [j, 1];
  6206. e = cur[j].n;
  6207. if (names[i] != wildcard) {
  6208. if (e[names[i]]) {
  6209. splice.push(e[names[i]]);
  6210. }
  6211. } else {
  6212. for (key in e) if (e[has](key)) {
  6213. splice.push(e[key]);
  6214. }
  6215. }
  6216. cur.splice.apply(cur, splice);
  6217. }
  6218. }
  6219. for (i = 0, ii = cur.length; i < ii; i++) {
  6220. e = cur[i];
  6221. while (e.n) {
  6222. if (f) {
  6223. if (e.f) {
  6224. for (i = 0, ii = e.f.length; i < ii; i++) if (e.f[i] == f) {
  6225. e.f.splice(i, 1);
  6226. break;
  6227. }
  6228. !e.f.length && delete e.f;
  6229. }
  6230. for (key in e.n) if (e.n[has](key) && e.n[key].f) {
  6231. var funcs = e.n[key].f;
  6232. for (i = 0, ii = funcs.length; i < ii; i++) if (funcs[i] == f) {
  6233. funcs.splice(i, 1);
  6234. break;
  6235. }
  6236. !funcs.length && delete e.n[key].f;
  6237. }
  6238. } else {
  6239. delete e.f;
  6240. for (key in e.n) if (e.n[has](key) && e.n[key].f) {
  6241. delete e.n[key].f;
  6242. }
  6243. }
  6244. e = e.n;
  6245. }
  6246. }
  6247. };
  6248. /*\
  6249. * eve.version
  6250. [ property (string) ]
  6251. **
  6252. * Current version of the library.
  6253. \*/
  6254. eve.version = version;
  6255. eve.toString = function () {
  6256. return "You are running Eve " + version;
  6257. };
  6258. return eve;
  6259. })();
  6260. // Eve finished
  6261. eve.on("DOMload", function () {
  6262. loaded = true;
  6263. });
  6264. })();