raphael.core.js 189 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416
  1. define(["eve"], function(eve) {
  2. /*\
  3. * Raphael
  4. [ method ]
  5. **
  6. * Creates a canvas object on which to draw.
  7. * You must do this first, as all future calls to drawing methods
  8. * from this instance will be bound to this canvas.
  9. > Parameters
  10. **
  11. - container (HTMLElement|string) DOM element or its ID which is going to be a parent for drawing surface
  12. - width (number)
  13. - height (number)
  14. - callback (function) #optional callback function which is going to be executed in the context of newly created paper
  15. * or
  16. - x (number)
  17. - y (number)
  18. - width (number)
  19. - height (number)
  20. - callback (function) #optional callback function which is going to be executed in the context of newly created paper
  21. * or
  22. - 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>}). See @Paper.add.
  23. - callback (function) #optional callback function which is going to be executed in the context of newly created paper
  24. * or
  25. - 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`.
  26. = (object) @Paper
  27. > Usage
  28. | // Each of the following examples create a canvas
  29. | // that is 320px wide by 200px high.
  30. | // Canvas is created at the viewport’s 10,50 coordinate.
  31. | var paper = Raphael(10, 50, 320, 200);
  32. | // Canvas is created at the top left corner of the #notepad element
  33. | // (or its top right corner in dir="rtl" elements)
  34. | var paper = Raphael(document.getElementById("notepad"), 320, 200);
  35. | // Same as above
  36. | var paper = Raphael("notepad", 320, 200);
  37. | // Image dump
  38. | var set = Raphael(["notepad", 320, 200, {
  39. | type: "rect",
  40. | x: 10,
  41. | y: 10,
  42. | width: 25,
  43. | height: 25,
  44. | stroke: "#f00"
  45. | }, {
  46. | type: "text",
  47. | x: 30,
  48. | y: 40,
  49. | text: "Dump"
  50. | }]);
  51. \*/
  52. function R(first) {
  53. if (R.is(first, "function")) {
  54. return loaded ? first() : eve.on("raphael.DOMload", first);
  55. } else if (R.is(first, array)) {
  56. return R._engine.create[apply](R, first.splice(0, 3 + R.is(first[0], nu))).add(first);
  57. } else {
  58. var args = Array.prototype.slice.call(arguments, 0);
  59. if (R.is(args[args.length - 1], "function")) {
  60. var f = args.pop();
  61. return loaded ? f.call(R._engine.create[apply](R, args)) : eve.on("raphael.DOMload", function () {
  62. f.call(R._engine.create[apply](R, args));
  63. });
  64. } else {
  65. return R._engine.create[apply](R, arguments);
  66. }
  67. }
  68. }
  69. R.version = "2.3.0";
  70. R.eve = eve;
  71. var loaded,
  72. separator = /[, ]+/,
  73. elements = {circle: 1, rect: 1, path: 1, ellipse: 1, text: 1, image: 1},
  74. formatrg = /\{(\d+)\}/g,
  75. proto = "prototype",
  76. has = "hasOwnProperty",
  77. g = {
  78. doc: document,
  79. win: window
  80. },
  81. oldRaphael = {
  82. was: Object.prototype[has].call(g.win, "Raphael"),
  83. is: g.win.Raphael
  84. },
  85. Paper = function () {
  86. /*\
  87. * Paper.ca
  88. [ property (object) ]
  89. **
  90. * Shortcut for @Paper.customAttributes
  91. \*/
  92. /*\
  93. * Paper.customAttributes
  94. [ property (object) ]
  95. **
  96. * If you have a set of attributes that you would like to represent
  97. * as a function of some number you can do it easily with custom attributes:
  98. > Usage
  99. | paper.customAttributes.hue = function (num) {
  100. | num = num % 1;
  101. | return {fill: "hsb(" + num + ", 0.75, 1)"};
  102. | };
  103. | // Custom attribute “hue” will change fill
  104. | // to be given hue with fixed saturation and brightness.
  105. | // Now you can use it like this:
  106. | var c = paper.circle(10, 10, 10).attr({hue: .45});
  107. | // or even like this:
  108. | c.animate({hue: 1}, 1e3);
  109. |
  110. | // You could also create custom attribute
  111. | // with multiple parameters:
  112. | paper.customAttributes.hsb = function (h, s, b) {
  113. | return {fill: "hsb(" + [h, s, b].join(",") + ")"};
  114. | };
  115. | c.attr({hsb: "0.5 .8 1"});
  116. | c.animate({hsb: [1, 0, 0.5]}, 1e3);
  117. \*/
  118. this.ca = this.customAttributes = {};
  119. },
  120. paperproto,
  121. appendChild = "appendChild",
  122. apply = "apply",
  123. concat = "concat",
  124. //taken from Modernizr touch test: https://github.com/Modernizr/Modernizr/blob/master/feature-detects/touchevents.js#L40
  125. supportsTouch = ('ontouchstart' in window) || window.TouchEvent || window.DocumentTouch && document instanceof DocumentTouch,
  126. E = "",
  127. S = " ",
  128. Str = String,
  129. split = "split",
  130. events = "click dblclick mousedown mousemove mouseout mouseover mouseup touchstart touchmove touchend touchcancel"[split](S),
  131. touchMap = {
  132. mousedown: "touchstart",
  133. mousemove: "touchmove",
  134. mouseup: "touchend"
  135. },
  136. lowerCase = Str.prototype.toLowerCase,
  137. math = Math,
  138. mmax = math.max,
  139. mmin = math.min,
  140. abs = math.abs,
  141. pow = math.pow,
  142. PI = math.PI,
  143. nu = "number",
  144. string = "string",
  145. array = "array",
  146. toString = "toString",
  147. fillString = "fill",
  148. objectToString = Object.prototype.toString,
  149. paper = {},
  150. push = "push",
  151. ISURL = R._ISURL = /^url\(['"]?(.+?)['"]?\)$/i,
  152. 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,
  153. isnan = {"NaN": 1, "Infinity": 1, "-Infinity": 1},
  154. bezierrg = /^(?:cubic-)?bezier\(([^,]+),([^,]+),([^,]+),([^\)]+)\)/,
  155. round = math.round,
  156. setAttribute = "setAttribute",
  157. toFloat = parseFloat,
  158. toInt = parseInt,
  159. upperCase = Str.prototype.toUpperCase,
  160. availableAttrs = R._availableAttrs = {
  161. "arrow-end": "none",
  162. "arrow-start": "none",
  163. blur: 0,
  164. "clip-rect": "0 0 1e9 1e9",
  165. cursor: "default",
  166. cx: 0,
  167. cy: 0,
  168. fill: "#fff",
  169. "fill-opacity": 1,
  170. font: '10px "Arial"',
  171. "font-family": '"Arial"',
  172. "font-size": "10",
  173. "font-style": "normal",
  174. "font-weight": 400,
  175. gradient: 0,
  176. height: 0,
  177. href: "http://raphaeljs.com/",
  178. "letter-spacing": 0,
  179. opacity: 1,
  180. path: "M0,0",
  181. r: 0,
  182. rx: 0,
  183. ry: 0,
  184. src: "",
  185. stroke: "#000",
  186. "stroke-dasharray": "",
  187. "stroke-linecap": "butt",
  188. "stroke-linejoin": "butt",
  189. "stroke-miterlimit": 0,
  190. "stroke-opacity": 1,
  191. "stroke-width": 1,
  192. target: "_blank",
  193. "text-anchor": "middle",
  194. title: "Raphael",
  195. transform: "",
  196. width: 0,
  197. x: 0,
  198. y: 0,
  199. "class": ""
  200. },
  201. availableAnimAttrs = R._availableAnimAttrs = {
  202. blur: nu,
  203. "clip-rect": "csv",
  204. cx: nu,
  205. cy: nu,
  206. fill: "colour",
  207. "fill-opacity": nu,
  208. "font-size": nu,
  209. height: nu,
  210. opacity: nu,
  211. path: "path",
  212. r: nu,
  213. rx: nu,
  214. ry: nu,
  215. stroke: "colour",
  216. "stroke-opacity": nu,
  217. "stroke-width": nu,
  218. transform: "transform",
  219. width: nu,
  220. x: nu,
  221. y: nu
  222. },
  223. whitespace = /[\x09\x0a\x0b\x0c\x0d\x20\xa0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\u2028\u2029]/g,
  224. commaSpaces = /[\x09\x0a\x0b\x0c\x0d\x20\xa0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\u2028\u2029]*,[\x09\x0a\x0b\x0c\x0d\x20\xa0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\u2028\u2029]*/,
  225. hsrg = {hs: 1, rg: 1},
  226. p2s = /,?([achlmqrstvxz]),?/gi,
  227. pathCommand = /([achlmrqstvz])[\x09\x0a\x0b\x0c\x0d\x20\xa0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\u2028\u2029,]*((-?\d*\.?\d*(?:e[\-+]?\d+)?[\x09\x0a\x0b\x0c\x0d\x20\xa0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\u2028\u2029]*,?[\x09\x0a\x0b\x0c\x0d\x20\xa0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\u2028\u2029]*)+)/ig,
  228. tCommand = /([rstm])[\x09\x0a\x0b\x0c\x0d\x20\xa0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\u2028\u2029,]*((-?\d*\.?\d*(?:e[\-+]?\d+)?[\x09\x0a\x0b\x0c\x0d\x20\xa0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\u2028\u2029]*,?[\x09\x0a\x0b\x0c\x0d\x20\xa0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\u2028\u2029]*)+)/ig,
  229. pathValues = /(-?\d*\.?\d*(?:e[\-+]?\d+)?)[\x09\x0a\x0b\x0c\x0d\x20\xa0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\u2028\u2029]*,?[\x09\x0a\x0b\x0c\x0d\x20\xa0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\u2028\u2029]*/ig,
  230. radial_gradient = R._radial_gradient = /^r(?:\(([^,]+?)[\x09\x0a\x0b\x0c\x0d\x20\xa0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\u2028\u2029]*,[\x09\x0a\x0b\x0c\x0d\x20\xa0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\u2028\u2029]*([^\)]+?)\))?/,
  231. eldata = {},
  232. sortByKey = function (a, b) {
  233. return a.key - b.key;
  234. },
  235. sortByNumber = function (a, b) {
  236. return toFloat(a) - toFloat(b);
  237. },
  238. fun = function () {},
  239. pipe = function (x) {
  240. return x;
  241. },
  242. rectPath = R._rectPath = function (x, y, w, h, r) {
  243. if (r) {
  244. 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"]];
  245. }
  246. return [["M", x, y], ["l", w, 0], ["l", 0, h], ["l", -w, 0], ["z"]];
  247. },
  248. ellipsePath = function (x, y, rx, ry) {
  249. if (ry == null) {
  250. ry = rx;
  251. }
  252. 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"]];
  253. },
  254. getPath = R._getPath = {
  255. path: function (el) {
  256. return el.attr("path");
  257. },
  258. circle: function (el) {
  259. var a = el.attrs;
  260. return ellipsePath(a.cx, a.cy, a.r);
  261. },
  262. ellipse: function (el) {
  263. var a = el.attrs;
  264. return ellipsePath(a.cx, a.cy, a.rx, a.ry);
  265. },
  266. rect: function (el) {
  267. var a = el.attrs;
  268. return rectPath(a.x, a.y, a.width, a.height, a.r);
  269. },
  270. image: function (el) {
  271. var a = el.attrs;
  272. return rectPath(a.x, a.y, a.width, a.height);
  273. },
  274. text: function (el) {
  275. var bbox = el._getBBox();
  276. return rectPath(bbox.x, bbox.y, bbox.width, bbox.height);
  277. },
  278. set : function(el) {
  279. var bbox = el._getBBox();
  280. return rectPath(bbox.x, bbox.y, bbox.width, bbox.height);
  281. }
  282. },
  283. /*\
  284. * Raphael.mapPath
  285. [ method ]
  286. **
  287. * Transform the path string with given matrix.
  288. > Parameters
  289. - path (string) path string
  290. - matrix (object) see @Matrix
  291. = (string) transformed path string
  292. \*/
  293. mapPath = R.mapPath = function (path, matrix) {
  294. if (!matrix) {
  295. return path;
  296. }
  297. var x, y, i, j, ii, jj, pathi;
  298. path = path2curve(path);
  299. for (i = 0, ii = path.length; i < ii; i++) {
  300. pathi = path[i];
  301. for (j = 1, jj = pathi.length; j < jj; j += 2) {
  302. x = matrix.x(pathi[j], pathi[j + 1]);
  303. y = matrix.y(pathi[j], pathi[j + 1]);
  304. pathi[j] = x;
  305. pathi[j + 1] = y;
  306. }
  307. }
  308. return path;
  309. };
  310. R._g = g;
  311. /*\
  312. * Raphael.type
  313. [ property (string) ]
  314. **
  315. * Can be “SVG”, “VML” or empty, depending on browser support.
  316. \*/
  317. R.type = (g.win.SVGAngle || g.doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML");
  318. if (R.type == "VML") {
  319. var d = g.doc.createElement("div"),
  320. b;
  321. d.innerHTML = '<v:shape adj="1"/>';
  322. b = d.firstChild;
  323. b.style.behavior = "url(#default#VML)";
  324. if (!(b && typeof b.adj == "object")) {
  325. return (R.type = E);
  326. }
  327. d = null;
  328. }
  329. /*\
  330. * Raphael.svg
  331. [ property (boolean) ]
  332. **
  333. * `true` if browser supports SVG.
  334. \*/
  335. /*\
  336. * Raphael.vml
  337. [ property (boolean) ]
  338. **
  339. * `true` if browser supports VML.
  340. \*/
  341. R.svg = !(R.vml = R.type == "VML");
  342. R._Paper = Paper;
  343. /*\
  344. * Raphael.fn
  345. [ property (object) ]
  346. **
  347. * You can add your own method to the canvas. For example if you want to draw a pie chart,
  348. * you can create your own pie chart function and ship it as a Raphaël plugin. To do this
  349. * you need to extend the `Raphael.fn` object. You should modify the `fn` object before a
  350. * Raphaël instance is created, otherwise it will take no effect. Please note that the
  351. * ability for namespaced plugins was removed in Raphael 2.0. It is up to the plugin to
  352. * ensure any namespacing ensures proper context.
  353. > Usage
  354. | Raphael.fn.arrow = function (x1, y1, x2, y2, size) {
  355. | return this.path( ... );
  356. | };
  357. | // or create namespace
  358. | Raphael.fn.mystuff = {
  359. | arrow: function () {…},
  360. | star: function () {…},
  361. | // etc…
  362. | };
  363. | var paper = Raphael(10, 10, 630, 480);
  364. | // then use it
  365. | paper.arrow(10, 10, 30, 30, 5).attr({fill: "#f00"});
  366. | paper.mystuff.arrow();
  367. | paper.mystuff.star();
  368. \*/
  369. R.fn = paperproto = Paper.prototype = R.prototype;
  370. R._id = 0;
  371. /*\
  372. * Raphael.is
  373. [ method ]
  374. **
  375. * Handful of replacements for `typeof` operator.
  376. > Parameters
  377. - o (…) any object or primitive
  378. - type (string) name of the type, i.e. “string”, “function”, “number”, etc.
  379. = (boolean) is given value is of given type
  380. \*/
  381. R.is = function (o, type) {
  382. type = lowerCase.call(type);
  383. if (type == "finite") {
  384. return !isnan[has](+o);
  385. }
  386. if (type == "array") {
  387. return o instanceof Array;
  388. }
  389. return (type == "null" && o === null) ||
  390. (type == typeof o && o !== null) ||
  391. (type == "object" && o === Object(o)) ||
  392. (type == "array" && Array.isArray && Array.isArray(o)) ||
  393. objectToString.call(o).slice(8, -1).toLowerCase() == type;
  394. };
  395. function clone(obj) {
  396. if (typeof obj == "function" || Object(obj) !== obj) {
  397. return obj;
  398. }
  399. var res = new obj.constructor;
  400. for (var key in obj) if (obj[has](key)) {
  401. res[key] = clone(obj[key]);
  402. }
  403. return res;
  404. }
  405. /*\
  406. * Raphael.angle
  407. [ method ]
  408. **
  409. * Returns angle between two or three points
  410. > Parameters
  411. - x1 (number) x coord of first point
  412. - y1 (number) y coord of first point
  413. - x2 (number) x coord of second point
  414. - y2 (number) y coord of second point
  415. - x3 (number) #optional x coord of third point
  416. - y3 (number) #optional y coord of third point
  417. = (number) angle in degrees.
  418. \*/
  419. R.angle = function (x1, y1, x2, y2, x3, y3) {
  420. if (x3 == null) {
  421. var x = x1 - x2,
  422. y = y1 - y2;
  423. if (!x && !y) {
  424. return 0;
  425. }
  426. return (180 + math.atan2(-y, -x) * 180 / PI + 360) % 360;
  427. } else {
  428. return R.angle(x1, y1, x3, y3) - R.angle(x2, y2, x3, y3);
  429. }
  430. };
  431. /*\
  432. * Raphael.rad
  433. [ method ]
  434. **
  435. * Transform angle to radians
  436. > Parameters
  437. - deg (number) angle in degrees
  438. = (number) angle in radians.
  439. \*/
  440. R.rad = function (deg) {
  441. return deg % 360 * PI / 180;
  442. };
  443. /*\
  444. * Raphael.deg
  445. [ method ]
  446. **
  447. * Transform angle to degrees
  448. > Parameters
  449. - rad (number) angle in radians
  450. = (number) angle in degrees.
  451. \*/
  452. R.deg = function (rad) {
  453. return Math.round ((rad * 180 / PI% 360)* 1000) / 1000;
  454. };
  455. /*\
  456. * Raphael.snapTo
  457. [ method ]
  458. **
  459. * Snaps given value to given grid.
  460. > Parameters
  461. - values (array|number) given array of values or step of the grid
  462. - value (number) value to adjust
  463. - tolerance (number) #optional tolerance for snapping. Default is `10`.
  464. = (number) adjusted value.
  465. \*/
  466. R.snapTo = function (values, value, tolerance) {
  467. tolerance = R.is(tolerance, "finite") ? tolerance : 10;
  468. if (R.is(values, array)) {
  469. var i = values.length;
  470. while (i--) if (abs(values[i] - value) <= tolerance) {
  471. return values[i];
  472. }
  473. } else {
  474. values = +values;
  475. var rem = value % values;
  476. if (rem < tolerance) {
  477. return value - rem;
  478. }
  479. if (rem > values - tolerance) {
  480. return value - rem + values;
  481. }
  482. }
  483. return value;
  484. };
  485. /*\
  486. * Raphael.createUUID
  487. [ method ]
  488. **
  489. * Returns RFC4122, version 4 ID
  490. \*/
  491. var createUUID = R.createUUID = (function (uuidRegEx, uuidReplacer) {
  492. return function () {
  493. return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(uuidRegEx, uuidReplacer).toUpperCase();
  494. };
  495. })(/[xy]/g, function (c) {
  496. var r = math.random() * 16 | 0,
  497. v = c == "x" ? r : (r & 3 | 8);
  498. return v.toString(16);
  499. });
  500. /*\
  501. * Raphael.setWindow
  502. [ method ]
  503. **
  504. * Used when you need to draw in `&lt;iframe>`. Switched window to the iframe one.
  505. > Parameters
  506. - newwin (window) new window object
  507. \*/
  508. R.setWindow = function (newwin) {
  509. eve("raphael.setWindow", R, g.win, newwin);
  510. g.win = newwin;
  511. g.doc = g.win.document;
  512. if (R._engine.initWin) {
  513. R._engine.initWin(g.win);
  514. }
  515. };
  516. var toHex = function (color) {
  517. if (R.vml) {
  518. // http://dean.edwards.name/weblog/2009/10/convert-any-colour-value-to-hex-in-msie/
  519. var trim = /^\s+|\s+$/g;
  520. var bod;
  521. try {
  522. var docum = new ActiveXObject("htmlfile");
  523. docum.write("<body>");
  524. docum.close();
  525. bod = docum.body;
  526. } catch(e) {
  527. bod = createPopup().document.body;
  528. }
  529. var range = bod.createTextRange();
  530. toHex = cacher(function (color) {
  531. try {
  532. bod.style.color = Str(color).replace(trim, E);
  533. var value = range.queryCommandValue("ForeColor");
  534. value = ((value & 255) << 16) | (value & 65280) | ((value & 16711680) >>> 16);
  535. return "#" + ("000000" + value.toString(16)).slice(-6);
  536. } catch(e) {
  537. return "none";
  538. }
  539. });
  540. } else {
  541. var i = g.doc.createElement("i");
  542. i.title = "Rapha\xebl Colour Picker";
  543. i.style.display = "none";
  544. g.doc.body.appendChild(i);
  545. toHex = cacher(function (color) {
  546. i.style.color = color;
  547. return g.doc.defaultView.getComputedStyle(i, E).getPropertyValue("color");
  548. });
  549. }
  550. return toHex(color);
  551. },
  552. hsbtoString = function () {
  553. return "hsb(" + [this.h, this.s, this.b] + ")";
  554. },
  555. hsltoString = function () {
  556. return "hsl(" + [this.h, this.s, this.l] + ")";
  557. },
  558. rgbtoString = function () {
  559. return this.hex;
  560. },
  561. prepareRGB = function (r, g, b) {
  562. if (g == null && R.is(r, "object") && "r" in r && "g" in r && "b" in r) {
  563. b = r.b;
  564. g = r.g;
  565. r = r.r;
  566. }
  567. if (g == null && R.is(r, string)) {
  568. var clr = R.getRGB(r);
  569. r = clr.r;
  570. g = clr.g;
  571. b = clr.b;
  572. }
  573. if (r > 1 || g > 1 || b > 1) {
  574. r /= 255;
  575. g /= 255;
  576. b /= 255;
  577. }
  578. return [r, g, b];
  579. },
  580. packageRGB = function (r, g, b, o) {
  581. r *= 255;
  582. g *= 255;
  583. b *= 255;
  584. var rgb = {
  585. r: r,
  586. g: g,
  587. b: b,
  588. hex: R.rgb(r, g, b),
  589. toString: rgbtoString
  590. };
  591. R.is(o, "finite") && (rgb.opacity = o);
  592. return rgb;
  593. };
  594. /*\
  595. * Raphael.color
  596. [ method ]
  597. **
  598. * Parses the color string and returns object with all values for the given color.
  599. > Parameters
  600. - clr (string) color string in one of the supported formats (see @Raphael.getRGB)
  601. = (object) Combined RGB & HSB object in format:
  602. o {
  603. o r (number) red,
  604. o g (number) green,
  605. o b (number) blue,
  606. o hex (string) color in HTML/CSS format: #••••••,
  607. o error (boolean) `true` if string can’t be parsed,
  608. o h (number) hue,
  609. o s (number) saturation,
  610. o v (number) value (brightness),
  611. o l (number) lightness
  612. o }
  613. \*/
  614. R.color = function (clr) {
  615. var rgb;
  616. if (R.is(clr, "object") && "h" in clr && "s" in clr && "b" in clr) {
  617. rgb = R.hsb2rgb(clr);
  618. clr.r = rgb.r;
  619. clr.g = rgb.g;
  620. clr.b = rgb.b;
  621. clr.hex = rgb.hex;
  622. } else if (R.is(clr, "object") && "h" in clr && "s" in clr && "l" in clr) {
  623. rgb = R.hsl2rgb(clr);
  624. clr.r = rgb.r;
  625. clr.g = rgb.g;
  626. clr.b = rgb.b;
  627. clr.hex = rgb.hex;
  628. } else {
  629. if (R.is(clr, "string")) {
  630. clr = R.getRGB(clr);
  631. }
  632. if (R.is(clr, "object") && "r" in clr && "g" in clr && "b" in clr) {
  633. rgb = R.rgb2hsl(clr);
  634. clr.h = rgb.h;
  635. clr.s = rgb.s;
  636. clr.l = rgb.l;
  637. rgb = R.rgb2hsb(clr);
  638. clr.v = rgb.b;
  639. } else {
  640. clr = {hex: "none"};
  641. clr.r = clr.g = clr.b = clr.h = clr.s = clr.v = clr.l = -1;
  642. }
  643. }
  644. clr.toString = rgbtoString;
  645. return clr;
  646. };
  647. /*\
  648. * Raphael.hsb2rgb
  649. [ method ]
  650. **
  651. * Converts HSB values to RGB object.
  652. > Parameters
  653. - h (number) hue
  654. - s (number) saturation
  655. - v (number) value or brightness
  656. = (object) RGB object in format:
  657. o {
  658. o r (number) red,
  659. o g (number) green,
  660. o b (number) blue,
  661. o hex (string) color in HTML/CSS format: #••••••
  662. o }
  663. \*/
  664. R.hsb2rgb = function (h, s, v, o) {
  665. if (this.is(h, "object") && "h" in h && "s" in h && "b" in h) {
  666. v = h.b;
  667. s = h.s;
  668. o = h.o;
  669. h = h.h;
  670. }
  671. h *= 360;
  672. var R, G, B, X, C;
  673. h = (h % 360) / 60;
  674. C = v * s;
  675. X = C * (1 - abs(h % 2 - 1));
  676. R = G = B = v - C;
  677. h = ~~h;
  678. R += [C, X, 0, 0, X, C][h];
  679. G += [X, C, C, X, 0, 0][h];
  680. B += [0, 0, X, C, C, X][h];
  681. return packageRGB(R, G, B, o);
  682. };
  683. /*\
  684. * Raphael.hsl2rgb
  685. [ method ]
  686. **
  687. * Converts HSL values to RGB object.
  688. > Parameters
  689. - h (number) hue
  690. - s (number) saturation
  691. - l (number) luminosity
  692. = (object) RGB object in format:
  693. o {
  694. o r (number) red,
  695. o g (number) green,
  696. o b (number) blue,
  697. o hex (string) color in HTML/CSS format: #••••••
  698. o }
  699. \*/
  700. R.hsl2rgb = function (h, s, l, o) {
  701. if (this.is(h, "object") && "h" in h && "s" in h && "l" in h) {
  702. l = h.l;
  703. s = h.s;
  704. h = h.h;
  705. }
  706. if (h > 1 || s > 1 || l > 1) {
  707. h /= 360;
  708. s /= 100;
  709. l /= 100;
  710. }
  711. h *= 360;
  712. var R, G, B, X, C;
  713. h = (h % 360) / 60;
  714. C = 2 * s * (l < .5 ? l : 1 - l);
  715. X = C * (1 - abs(h % 2 - 1));
  716. R = G = B = l - C / 2;
  717. h = ~~h;
  718. R += [C, X, 0, 0, X, C][h];
  719. G += [X, C, C, X, 0, 0][h];
  720. B += [0, 0, X, C, C, X][h];
  721. return packageRGB(R, G, B, o);
  722. };
  723. /*\
  724. * Raphael.rgb2hsb
  725. [ method ]
  726. **
  727. * Converts RGB values to HSB object.
  728. > Parameters
  729. - r (number) red
  730. - g (number) green
  731. - b (number) blue
  732. = (object) HSB object in format:
  733. o {
  734. o h (number) hue
  735. o s (number) saturation
  736. o b (number) brightness
  737. o }
  738. \*/
  739. R.rgb2hsb = function (r, g, b) {
  740. b = prepareRGB(r, g, b);
  741. r = b[0];
  742. g = b[1];
  743. b = b[2];
  744. var H, S, V, C;
  745. V = mmax(r, g, b);
  746. C = V - mmin(r, g, b);
  747. H = (C == 0 ? null :
  748. V == r ? (g - b) / C :
  749. V == g ? (b - r) / C + 2 :
  750. (r - g) / C + 4
  751. );
  752. H = ((H + 360) % 6) * 60 / 360;
  753. S = C == 0 ? 0 : C / V;
  754. return {h: H, s: S, b: V, toString: hsbtoString};
  755. };
  756. /*\
  757. * Raphael.rgb2hsl
  758. [ method ]
  759. **
  760. * Converts RGB values to HSL object.
  761. > Parameters
  762. - r (number) red
  763. - g (number) green
  764. - b (number) blue
  765. = (object) HSL object in format:
  766. o {
  767. o h (number) hue
  768. o s (number) saturation
  769. o l (number) luminosity
  770. o }
  771. \*/
  772. R.rgb2hsl = function (r, g, b) {
  773. b = prepareRGB(r, g, b);
  774. r = b[0];
  775. g = b[1];
  776. b = b[2];
  777. var H, S, L, M, m, C;
  778. M = mmax(r, g, b);
  779. m = mmin(r, g, b);
  780. C = M - m;
  781. H = (C == 0 ? null :
  782. M == r ? (g - b) / C :
  783. M == g ? (b - r) / C + 2 :
  784. (r - g) / C + 4);
  785. H = ((H + 360) % 6) * 60 / 360;
  786. L = (M + m) / 2;
  787. S = (C == 0 ? 0 :
  788. L < .5 ? C / (2 * L) :
  789. C / (2 - 2 * L));
  790. return {h: H, s: S, l: L, toString: hsltoString};
  791. };
  792. R._path2string = function () {
  793. return this.join(",").replace(p2s, "$1");
  794. };
  795. function repush(array, item) {
  796. for (var i = 0, ii = array.length; i < ii; i++) if (array[i] === item) {
  797. return array.push(array.splice(i, 1)[0]);
  798. }
  799. }
  800. function cacher(f, scope, postprocessor) {
  801. function newf() {
  802. var arg = Array.prototype.slice.call(arguments, 0),
  803. args = arg.join("\u2400"),
  804. cache = newf.cache = newf.cache || {},
  805. count = newf.count = newf.count || [];
  806. if (cache[has](args)) {
  807. repush(count, args);
  808. return postprocessor ? postprocessor(cache[args]) : cache[args];
  809. }
  810. count.length >= 1e3 && delete cache[count.shift()];
  811. count.push(args);
  812. cache[args] = f[apply](scope, arg);
  813. return postprocessor ? postprocessor(cache[args]) : cache[args];
  814. }
  815. return newf;
  816. }
  817. var preload = R._preload = function (src, f) {
  818. var img = g.doc.createElement("img");
  819. img.style.cssText = "position:absolute;left:-9999em;top:-9999em";
  820. img.onload = function () {
  821. f.call(this);
  822. this.onload = null;
  823. g.doc.body.removeChild(this);
  824. };
  825. img.onerror = function () {
  826. g.doc.body.removeChild(this);
  827. };
  828. g.doc.body.appendChild(img);
  829. img.src = src;
  830. };
  831. function clrToString() {
  832. return this.hex;
  833. }
  834. /*\
  835. * Raphael.getRGB
  836. [ method ]
  837. **
  838. * Parses colour string as RGB object
  839. > Parameters
  840. - colour (string) colour string in one of formats:
  841. # <ul>
  842. # <li>Colour name (“<code>red</code>”, “<code>green</code>”, “<code>cornflowerblue</code>”, etc)</li>
  843. # <li>#••• — shortened HTML colour: (“<code>#000</code>”, “<code>#fc0</code>”, etc)</li>
  844. # <li>#•••••• — full length HTML colour: (“<code>#000000</code>”, “<code>#bd2300</code>”)</li>
  845. # <li>rgb(•••, •••, •••) — red, green and blue channels’ values: (“<code>rgb(200,&nbsp;100,&nbsp;0)</code>”)</li>
  846. # <li>rgb(•••%, •••%, •••%) — same as above, but in %: (“<code>rgb(100%,&nbsp;175%,&nbsp;0%)</code>”)</li>
  847. # <li>hsb(•••, •••, •••) — hue, saturation and brightness values: (“<code>hsb(0.5,&nbsp;0.25,&nbsp;1)</code>”)</li>
  848. # <li>hsb(•••%, •••%, •••%) — same as above, but in %</li>
  849. # <li>hsl(•••, •••, •••) — same as hsb</li>
  850. # <li>hsl(•••%, •••%, •••%) — same as hsb</li>
  851. # </ul>
  852. = (object) RGB object in format:
  853. o {
  854. o r (number) red,
  855. o g (number) green,
  856. o b (number) blue
  857. o hex (string) color in HTML/CSS format: #••••••,
  858. o error (boolean) true if string can’t be parsed
  859. o }
  860. \*/
  861. R.getRGB = cacher(function (colour) {
  862. if (!colour || !!((colour = Str(colour)).indexOf("-") + 1)) {
  863. return {r: -1, g: -1, b: -1, hex: "none", error: 1, toString: clrToString};
  864. }
  865. if (colour == "none") {
  866. return {r: -1, g: -1, b: -1, hex: "none", toString: clrToString};
  867. }
  868. !(hsrg[has](colour.toLowerCase().substring(0, 2)) || colour.charAt() == "#") && (colour = toHex(colour));
  869. var res,
  870. red,
  871. green,
  872. blue,
  873. opacity,
  874. t,
  875. values,
  876. rgb = colour.match(colourRegExp);
  877. if (rgb) {
  878. if (rgb[2]) {
  879. blue = toInt(rgb[2].substring(5), 16);
  880. green = toInt(rgb[2].substring(3, 5), 16);
  881. red = toInt(rgb[2].substring(1, 3), 16);
  882. }
  883. if (rgb[3]) {
  884. blue = toInt((t = rgb[3].charAt(3)) + t, 16);
  885. green = toInt((t = rgb[3].charAt(2)) + t, 16);
  886. red = toInt((t = rgb[3].charAt(1)) + t, 16);
  887. }
  888. if (rgb[4]) {
  889. values = rgb[4][split](commaSpaces);
  890. red = toFloat(values[0]);
  891. values[0].slice(-1) == "%" && (red *= 2.55);
  892. green = toFloat(values[1]);
  893. values[1].slice(-1) == "%" && (green *= 2.55);
  894. blue = toFloat(values[2]);
  895. values[2].slice(-1) == "%" && (blue *= 2.55);
  896. rgb[1].toLowerCase().slice(0, 4) == "rgba" && (opacity = toFloat(values[3]));
  897. values[3] && values[3].slice(-1) == "%" && (opacity /= 100);
  898. }
  899. if (rgb[5]) {
  900. values = rgb[5][split](commaSpaces);
  901. red = toFloat(values[0]);
  902. values[0].slice(-1) == "%" && (red *= 2.55);
  903. green = toFloat(values[1]);
  904. values[1].slice(-1) == "%" && (green *= 2.55);
  905. blue = toFloat(values[2]);
  906. values[2].slice(-1) == "%" && (blue *= 2.55);
  907. (values[0].slice(-3) == "deg" || values[0].slice(-1) == "\xb0") && (red /= 360);
  908. rgb[1].toLowerCase().slice(0, 4) == "hsba" && (opacity = toFloat(values[3]));
  909. values[3] && values[3].slice(-1) == "%" && (opacity /= 100);
  910. return R.hsb2rgb(red, green, blue, opacity);
  911. }
  912. if (rgb[6]) {
  913. values = rgb[6][split](commaSpaces);
  914. red = toFloat(values[0]);
  915. values[0].slice(-1) == "%" && (red *= 2.55);
  916. green = toFloat(values[1]);
  917. values[1].slice(-1) == "%" && (green *= 2.55);
  918. blue = toFloat(values[2]);
  919. values[2].slice(-1) == "%" && (blue *= 2.55);
  920. (values[0].slice(-3) == "deg" || values[0].slice(-1) == "\xb0") && (red /= 360);
  921. rgb[1].toLowerCase().slice(0, 4) == "hsla" && (opacity = toFloat(values[3]));
  922. values[3] && values[3].slice(-1) == "%" && (opacity /= 100);
  923. return R.hsl2rgb(red, green, blue, opacity);
  924. }
  925. rgb = {r: red, g: green, b: blue, toString: clrToString};
  926. rgb.hex = "#" + (16777216 | blue | (green << 8) | (red << 16)).toString(16).slice(1);
  927. R.is(opacity, "finite") && (rgb.opacity = opacity);
  928. return rgb;
  929. }
  930. return {r: -1, g: -1, b: -1, hex: "none", error: 1, toString: clrToString};
  931. }, R);
  932. /*\
  933. * Raphael.hsb
  934. [ method ]
  935. **
  936. * Converts HSB values to hex representation of the colour.
  937. > Parameters
  938. - h (number) hue
  939. - s (number) saturation
  940. - b (number) value or brightness
  941. = (string) hex representation of the colour.
  942. \*/
  943. R.hsb = cacher(function (h, s, b) {
  944. return R.hsb2rgb(h, s, b).hex;
  945. });
  946. /*\
  947. * Raphael.hsl
  948. [ method ]
  949. **
  950. * Converts HSL values to hex representation of the colour.
  951. > Parameters
  952. - h (number) hue
  953. - s (number) saturation
  954. - l (number) luminosity
  955. = (string) hex representation of the colour.
  956. \*/
  957. R.hsl = cacher(function (h, s, l) {
  958. return R.hsl2rgb(h, s, l).hex;
  959. });
  960. /*\
  961. * Raphael.rgb
  962. [ method ]
  963. **
  964. * Converts RGB values to hex representation of the colour.
  965. > Parameters
  966. - r (number) red
  967. - g (number) green
  968. - b (number) blue
  969. = (string) hex representation of the colour.
  970. \*/
  971. R.rgb = cacher(function (r, g, b) {
  972. function round(x) { return (x + 0.5) | 0; }
  973. return "#" + (16777216 | round(b) | (round(g) << 8) | (round(r) << 16)).toString(16).slice(1);
  974. });
  975. /*\
  976. * Raphael.getColor
  977. [ method ]
  978. **
  979. * On each call returns next colour in the spectrum. To reset it back to red call @Raphael.getColor.reset
  980. > Parameters
  981. - value (number) #optional brightness, default is `0.75`
  982. = (string) hex representation of the colour.
  983. \*/
  984. R.getColor = function (value) {
  985. var start = this.getColor.start = this.getColor.start || {h: 0, s: 1, b: value || .75},
  986. rgb = this.hsb2rgb(start.h, start.s, start.b);
  987. start.h += .075;
  988. if (start.h > 1) {
  989. start.h = 0;
  990. start.s -= .2;
  991. start.s <= 0 && (this.getColor.start = {h: 0, s: 1, b: start.b});
  992. }
  993. return rgb.hex;
  994. };
  995. /*\
  996. * Raphael.getColor.reset
  997. [ method ]
  998. **
  999. * Resets spectrum position for @Raphael.getColor back to red.
  1000. \*/
  1001. R.getColor.reset = function () {
  1002. delete this.start;
  1003. };
  1004. // http://schepers.cc/getting-to-the-point
  1005. function catmullRom2bezier(crp, z) {
  1006. var d = [];
  1007. for (var i = 0, iLen = crp.length; iLen - 2 * !z > i; i += 2) {
  1008. var p = [
  1009. {x: +crp[i - 2], y: +crp[i - 1]},
  1010. {x: +crp[i], y: +crp[i + 1]},
  1011. {x: +crp[i + 2], y: +crp[i + 3]},
  1012. {x: +crp[i + 4], y: +crp[i + 5]}
  1013. ];
  1014. if (z) {
  1015. if (!i) {
  1016. p[0] = {x: +crp[iLen - 2], y: +crp[iLen - 1]};
  1017. } else if (iLen - 4 == i) {
  1018. p[3] = {x: +crp[0], y: +crp[1]};
  1019. } else if (iLen - 2 == i) {
  1020. p[2] = {x: +crp[0], y: +crp[1]};
  1021. p[3] = {x: +crp[2], y: +crp[3]};
  1022. }
  1023. } else {
  1024. if (iLen - 4 == i) {
  1025. p[3] = p[2];
  1026. } else if (!i) {
  1027. p[0] = {x: +crp[i], y: +crp[i + 1]};
  1028. }
  1029. }
  1030. d.push(["C",
  1031. (-p[0].x + 6 * p[1].x + p[2].x) / 6,
  1032. (-p[0].y + 6 * p[1].y + p[2].y) / 6,
  1033. (p[1].x + 6 * p[2].x - p[3].x) / 6,
  1034. (p[1].y + 6*p[2].y - p[3].y) / 6,
  1035. p[2].x,
  1036. p[2].y
  1037. ]);
  1038. }
  1039. return d;
  1040. }
  1041. /*\
  1042. * Raphael.parsePathString
  1043. [ method ]
  1044. **
  1045. * Utility method
  1046. **
  1047. * Parses given path string into an array of arrays of path segments.
  1048. > Parameters
  1049. - pathString (string|array) path string or array of segments (in the last case it will be returned straight away)
  1050. = (array) array of segments.
  1051. \*/
  1052. R.parsePathString = function (pathString) {
  1053. if (!pathString) {
  1054. return null;
  1055. }
  1056. var pth = paths(pathString);
  1057. if (pth.arr) {
  1058. return pathClone(pth.arr);
  1059. }
  1060. var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, r: 4, q: 4, s: 4, t: 2, v: 1, z: 0},
  1061. data = [];
  1062. if (R.is(pathString, array) && R.is(pathString[0], array)) { // rough assumption
  1063. data = pathClone(pathString);
  1064. }
  1065. if (!data.length) {
  1066. Str(pathString).replace(pathCommand, function (a, b, c) {
  1067. var params = [],
  1068. name = b.toLowerCase();
  1069. c.replace(pathValues, function (a, b) {
  1070. b && params.push(+b);
  1071. });
  1072. if (name == "m" && params.length > 2) {
  1073. data.push([b][concat](params.splice(0, 2)));
  1074. name = "l";
  1075. b = b == "m" ? "l" : "L";
  1076. }
  1077. if (name == "r") {
  1078. data.push([b][concat](params));
  1079. } else while (params.length >= paramCounts[name]) {
  1080. data.push([b][concat](params.splice(0, paramCounts[name])));
  1081. if (!paramCounts[name]) {
  1082. break;
  1083. }
  1084. }
  1085. });
  1086. }
  1087. data.toString = R._path2string;
  1088. pth.arr = pathClone(data);
  1089. return data;
  1090. };
  1091. /*\
  1092. * Raphael.parseTransformString
  1093. [ method ]
  1094. **
  1095. * Utility method
  1096. **
  1097. * Parses given path string into an array of transformations.
  1098. > Parameters
  1099. - TString (string|array) transform string or array of transformations (in the last case it will be returned straight away)
  1100. = (array) array of transformations.
  1101. \*/
  1102. R.parseTransformString = cacher(function (TString) {
  1103. if (!TString) {
  1104. return null;
  1105. }
  1106. var paramCounts = {r: 3, s: 4, t: 2, m: 6},
  1107. data = [];
  1108. if (R.is(TString, array) && R.is(TString[0], array)) { // rough assumption
  1109. data = pathClone(TString);
  1110. }
  1111. if (!data.length) {
  1112. Str(TString).replace(tCommand, function (a, b, c) {
  1113. var params = [],
  1114. name = lowerCase.call(b);
  1115. c.replace(pathValues, function (a, b) {
  1116. b && params.push(+b);
  1117. });
  1118. data.push([b][concat](params));
  1119. });
  1120. }
  1121. data.toString = R._path2string;
  1122. return data;
  1123. }, this, function(elem) {
  1124. if (!elem) return elem;
  1125. var newData = [];
  1126. for (var i = 0; i < elem.length; i++) {
  1127. var newLevel = [];
  1128. for (var j = 0; j < elem[i].length; j++) {
  1129. newLevel.push(elem[i][j]);
  1130. }
  1131. newData.push(newLevel);
  1132. }
  1133. return newData; } );
  1134. // PATHS
  1135. var paths = function (ps) {
  1136. var p = paths.ps = paths.ps || {};
  1137. if (p[ps]) {
  1138. p[ps].sleep = 100;
  1139. } else {
  1140. p[ps] = {
  1141. sleep: 100
  1142. };
  1143. }
  1144. setTimeout(function () {
  1145. for (var key in p) if (p[has](key) && key != ps) {
  1146. p[key].sleep--;
  1147. !p[key].sleep && delete p[key];
  1148. }
  1149. });
  1150. return p[ps];
  1151. };
  1152. /*\
  1153. * Raphael.findDotsAtSegment
  1154. [ method ]
  1155. **
  1156. * Utility method
  1157. **
  1158. * Find dot coordinates on the given cubic bezier curve at the given t.
  1159. > Parameters
  1160. - p1x (number) x of the first point of the curve
  1161. - p1y (number) y of the first point of the curve
  1162. - c1x (number) x of the first anchor of the curve
  1163. - c1y (number) y of the first anchor of the curve
  1164. - c2x (number) x of the second anchor of the curve
  1165. - c2y (number) y of the second anchor of the curve
  1166. - p2x (number) x of the second point of the curve
  1167. - p2y (number) y of the second point of the curve
  1168. - t (number) position on the curve (0..1)
  1169. = (object) point information in format:
  1170. o {
  1171. o x: (number) x coordinate of the point
  1172. o y: (number) y coordinate of the point
  1173. o m: {
  1174. o x: (number) x coordinate of the left anchor
  1175. o y: (number) y coordinate of the left anchor
  1176. o }
  1177. o n: {
  1178. o x: (number) x coordinate of the right anchor
  1179. o y: (number) y coordinate of the right anchor
  1180. o }
  1181. o start: {
  1182. o x: (number) x coordinate of the start of the curve
  1183. o y: (number) y coordinate of the start of the curve
  1184. o }
  1185. o end: {
  1186. o x: (number) x coordinate of the end of the curve
  1187. o y: (number) y coordinate of the end of the curve
  1188. o }
  1189. o alpha: (number) angle of the curve derivative at the point
  1190. o }
  1191. \*/
  1192. R.findDotsAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
  1193. var t1 = 1 - t,
  1194. t13 = pow(t1, 3),
  1195. t12 = pow(t1, 2),
  1196. t2 = t * t,
  1197. t3 = t2 * t,
  1198. x = t13 * p1x + t12 * 3 * t * c1x + t1 * 3 * t * t * c2x + t3 * p2x,
  1199. y = t13 * p1y + t12 * 3 * t * c1y + t1 * 3 * t * t * c2y + t3 * p2y,
  1200. mx = p1x + 2 * t * (c1x - p1x) + t2 * (c2x - 2 * c1x + p1x),
  1201. my = p1y + 2 * t * (c1y - p1y) + t2 * (c2y - 2 * c1y + p1y),
  1202. nx = c1x + 2 * t * (c2x - c1x) + t2 * (p2x - 2 * c2x + c1x),
  1203. ny = c1y + 2 * t * (c2y - c1y) + t2 * (p2y - 2 * c2y + c1y),
  1204. ax = t1 * p1x + t * c1x,
  1205. ay = t1 * p1y + t * c1y,
  1206. cx = t1 * c2x + t * p2x,
  1207. cy = t1 * c2y + t * p2y,
  1208. alpha = (90 - math.atan2(mx - nx, my - ny) * 180 / PI);
  1209. (mx > nx || my < ny) && (alpha += 180);
  1210. return {
  1211. x: x,
  1212. y: y,
  1213. m: {x: mx, y: my},
  1214. n: {x: nx, y: ny},
  1215. start: {x: ax, y: ay},
  1216. end: {x: cx, y: cy},
  1217. alpha: alpha
  1218. };
  1219. };
  1220. /*\
  1221. * Raphael.bezierBBox
  1222. [ method ]
  1223. **
  1224. * Utility method
  1225. **
  1226. * Return bounding box of a given cubic bezier curve
  1227. > Parameters
  1228. - p1x (number) x of the first point of the curve
  1229. - p1y (number) y of the first point of the curve
  1230. - c1x (number) x of the first anchor of the curve
  1231. - c1y (number) y of the first anchor of the curve
  1232. - c2x (number) x of the second anchor of the curve
  1233. - c2y (number) y of the second anchor of the curve
  1234. - p2x (number) x of the second point of the curve
  1235. - p2y (number) y of the second point of the curve
  1236. * or
  1237. - bez (array) array of six points for bezier curve
  1238. = (object) point information in format:
  1239. o {
  1240. o min: {
  1241. o x: (number) x coordinate of the left point
  1242. o y: (number) y coordinate of the top point
  1243. o }
  1244. o max: {
  1245. o x: (number) x coordinate of the right point
  1246. o y: (number) y coordinate of the bottom point
  1247. o }
  1248. o }
  1249. \*/
  1250. R.bezierBBox = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
  1251. if (!R.is(p1x, "array")) {
  1252. p1x = [p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y];
  1253. }
  1254. var bbox = curveDim.apply(null, p1x);
  1255. return {
  1256. x: bbox.min.x,
  1257. y: bbox.min.y,
  1258. x2: bbox.max.x,
  1259. y2: bbox.max.y,
  1260. width: bbox.max.x - bbox.min.x,
  1261. height: bbox.max.y - bbox.min.y
  1262. };
  1263. };
  1264. /*\
  1265. * Raphael.isPointInsideBBox
  1266. [ method ]
  1267. **
  1268. * Utility method
  1269. **
  1270. * Returns `true` if given point is inside bounding boxes.
  1271. > Parameters
  1272. - bbox (string) bounding box
  1273. - x (string) x coordinate of the point
  1274. - y (string) y coordinate of the point
  1275. = (boolean) `true` if point inside
  1276. \*/
  1277. R.isPointInsideBBox = function (bbox, x, y) {
  1278. return x >= bbox.x && x <= bbox.x2 && y >= bbox.y && y <= bbox.y2;
  1279. };
  1280. /*\
  1281. * Raphael.isBBoxIntersect
  1282. [ method ]
  1283. **
  1284. * Utility method
  1285. **
  1286. * Returns `true` if two bounding boxes intersect
  1287. > Parameters
  1288. - bbox1 (string) first bounding box
  1289. - bbox2 (string) second bounding box
  1290. = (boolean) `true` if they intersect
  1291. \*/
  1292. R.isBBoxIntersect = function (bbox1, bbox2) {
  1293. var i = R.isPointInsideBBox;
  1294. return i(bbox2, bbox1.x, bbox1.y)
  1295. || i(bbox2, bbox1.x2, bbox1.y)
  1296. || i(bbox2, bbox1.x, bbox1.y2)
  1297. || i(bbox2, bbox1.x2, bbox1.y2)
  1298. || i(bbox1, bbox2.x, bbox2.y)
  1299. || i(bbox1, bbox2.x2, bbox2.y)
  1300. || i(bbox1, bbox2.x, bbox2.y2)
  1301. || i(bbox1, bbox2.x2, bbox2.y2)
  1302. || (bbox1.x < bbox2.x2 && bbox1.x > bbox2.x || bbox2.x < bbox1.x2 && bbox2.x > bbox1.x)
  1303. && (bbox1.y < bbox2.y2 && bbox1.y > bbox2.y || bbox2.y < bbox1.y2 && bbox2.y > bbox1.y);
  1304. };
  1305. function base3(t, p1, p2, p3, p4) {
  1306. var t1 = -3 * p1 + 9 * p2 - 9 * p3 + 3 * p4,
  1307. t2 = t * t1 + 6 * p1 - 12 * p2 + 6 * p3;
  1308. return t * t2 - 3 * p1 + 3 * p2;
  1309. }
  1310. function bezlen(x1, y1, x2, y2, x3, y3, x4, y4, z) {
  1311. if (z == null) {
  1312. z = 1;
  1313. }
  1314. z = z > 1 ? 1 : z < 0 ? 0 : z;
  1315. var z2 = z / 2,
  1316. n = 12,
  1317. Tvalues = [-0.1252,0.1252,-0.3678,0.3678,-0.5873,0.5873,-0.7699,0.7699,-0.9041,0.9041,-0.9816,0.9816],
  1318. Cvalues = [0.2491,0.2491,0.2335,0.2335,0.2032,0.2032,0.1601,0.1601,0.1069,0.1069,0.0472,0.0472],
  1319. sum = 0;
  1320. for (var i = 0; i < n; i++) {
  1321. var ct = z2 * Tvalues[i] + z2,
  1322. xbase = base3(ct, x1, x2, x3, x4),
  1323. ybase = base3(ct, y1, y2, y3, y4),
  1324. comb = xbase * xbase + ybase * ybase;
  1325. sum += Cvalues[i] * math.sqrt(comb);
  1326. }
  1327. return z2 * sum;
  1328. }
  1329. function getTatLen(x1, y1, x2, y2, x3, y3, x4, y4, ll) {
  1330. if (ll < 0 || bezlen(x1, y1, x2, y2, x3, y3, x4, y4) < ll) {
  1331. return;
  1332. }
  1333. var t = 1,
  1334. step = t / 2,
  1335. t2 = t - step,
  1336. l,
  1337. e = .01;
  1338. l = bezlen(x1, y1, x2, y2, x3, y3, x4, y4, t2);
  1339. while (abs(l - ll) > e) {
  1340. step /= 2;
  1341. t2 += (l < ll ? 1 : -1) * step;
  1342. l = bezlen(x1, y1, x2, y2, x3, y3, x4, y4, t2);
  1343. }
  1344. return t2;
  1345. }
  1346. function intersect(x1, y1, x2, y2, x3, y3, x4, y4) {
  1347. if (
  1348. mmax(x1, x2) < mmin(x3, x4) ||
  1349. mmin(x1, x2) > mmax(x3, x4) ||
  1350. mmax(y1, y2) < mmin(y3, y4) ||
  1351. mmin(y1, y2) > mmax(y3, y4)
  1352. ) {
  1353. return;
  1354. }
  1355. var nx = (x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4),
  1356. ny = (x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4),
  1357. denominator = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
  1358. if (!denominator) {
  1359. return;
  1360. }
  1361. var px = nx / denominator,
  1362. py = ny / denominator,
  1363. px2 = +px.toFixed(2),
  1364. py2 = +py.toFixed(2);
  1365. if (
  1366. px2 < +mmin(x1, x2).toFixed(2) ||
  1367. px2 > +mmax(x1, x2).toFixed(2) ||
  1368. px2 < +mmin(x3, x4).toFixed(2) ||
  1369. px2 > +mmax(x3, x4).toFixed(2) ||
  1370. py2 < +mmin(y1, y2).toFixed(2) ||
  1371. py2 > +mmax(y1, y2).toFixed(2) ||
  1372. py2 < +mmin(y3, y4).toFixed(2) ||
  1373. py2 > +mmax(y3, y4).toFixed(2)
  1374. ) {
  1375. return;
  1376. }
  1377. return {x: px, y: py};
  1378. }
  1379. function inter(bez1, bez2) {
  1380. return interHelper(bez1, bez2);
  1381. }
  1382. function interCount(bez1, bez2) {
  1383. return interHelper(bez1, bez2, 1);
  1384. }
  1385. function interHelper(bez1, bez2, justCount) {
  1386. var bbox1 = R.bezierBBox(bez1),
  1387. bbox2 = R.bezierBBox(bez2);
  1388. if (!R.isBBoxIntersect(bbox1, bbox2)) {
  1389. return justCount ? 0 : [];
  1390. }
  1391. var l1 = bezlen.apply(0, bez1),
  1392. l2 = bezlen.apply(0, bez2),
  1393. n1 = mmax(~~(l1 / 5), 1),
  1394. n2 = mmax(~~(l2 / 5), 1),
  1395. dots1 = [],
  1396. dots2 = [],
  1397. xy = {},
  1398. res = justCount ? 0 : [];
  1399. for (var i = 0; i < n1 + 1; i++) {
  1400. var p = R.findDotsAtSegment.apply(R, bez1.concat(i / n1));
  1401. dots1.push({x: p.x, y: p.y, t: i / n1});
  1402. }
  1403. for (i = 0; i < n2 + 1; i++) {
  1404. p = R.findDotsAtSegment.apply(R, bez2.concat(i / n2));
  1405. dots2.push({x: p.x, y: p.y, t: i / n2});
  1406. }
  1407. for (i = 0; i < n1; i++) {
  1408. for (var j = 0; j < n2; j++) {
  1409. var di = dots1[i],
  1410. di1 = dots1[i + 1],
  1411. dj = dots2[j],
  1412. dj1 = dots2[j + 1],
  1413. ci = abs(di1.x - di.x) < .001 ? "y" : "x",
  1414. cj = abs(dj1.x - dj.x) < .001 ? "y" : "x",
  1415. is = intersect(di.x, di.y, di1.x, di1.y, dj.x, dj.y, dj1.x, dj1.y);
  1416. if (is) {
  1417. if (xy[is.x.toFixed(4)] == is.y.toFixed(4)) {
  1418. continue;
  1419. }
  1420. xy[is.x.toFixed(4)] = is.y.toFixed(4);
  1421. var t1 = di.t + abs((is[ci] - di[ci]) / (di1[ci] - di[ci])) * (di1.t - di.t),
  1422. t2 = dj.t + abs((is[cj] - dj[cj]) / (dj1[cj] - dj[cj])) * (dj1.t - dj.t);
  1423. if (t1 >= 0 && t1 <= 1.001 && t2 >= 0 && t2 <= 1.001) {
  1424. if (justCount) {
  1425. res++;
  1426. } else {
  1427. res.push({
  1428. x: is.x,
  1429. y: is.y,
  1430. t1: mmin(t1, 1),
  1431. t2: mmin(t2, 1)
  1432. });
  1433. }
  1434. }
  1435. }
  1436. }
  1437. }
  1438. return res;
  1439. }
  1440. /*\
  1441. * Raphael.pathIntersection
  1442. [ method ]
  1443. **
  1444. * Utility method
  1445. **
  1446. * Finds intersections of two paths
  1447. > Parameters
  1448. - path1 (string) path string
  1449. - path2 (string) path string
  1450. = (array) dots of intersection
  1451. o [
  1452. o {
  1453. o x: (number) x coordinate of the point
  1454. o y: (number) y coordinate of the point
  1455. o t1: (number) t value for segment of path1
  1456. o t2: (number) t value for segment of path2
  1457. o segment1: (number) order number for segment of path1
  1458. o segment2: (number) order number for segment of path2
  1459. o bez1: (array) eight coordinates representing beziér curve for the segment of path1
  1460. o bez2: (array) eight coordinates representing beziér curve for the segment of path2
  1461. o }
  1462. o ]
  1463. \*/
  1464. R.pathIntersection = function (path1, path2) {
  1465. return interPathHelper(path1, path2);
  1466. };
  1467. R.pathIntersectionNumber = function (path1, path2) {
  1468. return interPathHelper(path1, path2, 1);
  1469. };
  1470. function interPathHelper(path1, path2, justCount) {
  1471. path1 = R._path2curve(path1);
  1472. path2 = R._path2curve(path2);
  1473. var x1, y1, x2, y2, x1m, y1m, x2m, y2m, bez1, bez2,
  1474. res = justCount ? 0 : [];
  1475. for (var i = 0, ii = path1.length; i < ii; i++) {
  1476. var pi = path1[i];
  1477. if (pi[0] == "M") {
  1478. x1 = x1m = pi[1];
  1479. y1 = y1m = pi[2];
  1480. } else {
  1481. if (pi[0] == "C") {
  1482. bez1 = [x1, y1].concat(pi.slice(1));
  1483. x1 = bez1[6];
  1484. y1 = bez1[7];
  1485. } else {
  1486. bez1 = [x1, y1, x1, y1, x1m, y1m, x1m, y1m];
  1487. x1 = x1m;
  1488. y1 = y1m;
  1489. }
  1490. for (var j = 0, jj = path2.length; j < jj; j++) {
  1491. var pj = path2[j];
  1492. if (pj[0] == "M") {
  1493. x2 = x2m = pj[1];
  1494. y2 = y2m = pj[2];
  1495. } else {
  1496. if (pj[0] == "C") {
  1497. bez2 = [x2, y2].concat(pj.slice(1));
  1498. x2 = bez2[6];
  1499. y2 = bez2[7];
  1500. } else {
  1501. bez2 = [x2, y2, x2, y2, x2m, y2m, x2m, y2m];
  1502. x2 = x2m;
  1503. y2 = y2m;
  1504. }
  1505. var intr = interHelper(bez1, bez2, justCount);
  1506. if (justCount) {
  1507. res += intr;
  1508. } else {
  1509. for (var k = 0, kk = intr.length; k < kk; k++) {
  1510. intr[k].segment1 = i;
  1511. intr[k].segment2 = j;
  1512. intr[k].bez1 = bez1;
  1513. intr[k].bez2 = bez2;
  1514. }
  1515. res = res.concat(intr);
  1516. }
  1517. }
  1518. }
  1519. }
  1520. }
  1521. return res;
  1522. }
  1523. /*\
  1524. * Raphael.isPointInsidePath
  1525. [ method ]
  1526. **
  1527. * Utility method
  1528. **
  1529. * Returns `true` if given point is inside a given closed path.
  1530. > Parameters
  1531. - path (string) path string
  1532. - x (number) x of the point
  1533. - y (number) y of the point
  1534. = (boolean) true, if point is inside the path
  1535. \*/
  1536. R.isPointInsidePath = function (path, x, y) {
  1537. var bbox = R.pathBBox(path);
  1538. return R.isPointInsideBBox(bbox, x, y) &&
  1539. interPathHelper(path, [["M", x, y], ["H", bbox.x2 + 10]], 1) % 2 == 1;
  1540. };
  1541. R._removedFactory = function (methodname) {
  1542. return function () {
  1543. eve("raphael.log", null, "Rapha\xebl: you are calling to method \u201c" + methodname + "\u201d of removed object", methodname);
  1544. };
  1545. };
  1546. /*\
  1547. * Raphael.pathBBox
  1548. [ method ]
  1549. **
  1550. * Utility method
  1551. **
  1552. * Return bounding box of a given path
  1553. > Parameters
  1554. - path (string) path string
  1555. = (object) bounding box
  1556. o {
  1557. o x: (number) x coordinate of the left top point of the box
  1558. o y: (number) y coordinate of the left top point of the box
  1559. o x2: (number) x coordinate of the right bottom point of the box
  1560. o y2: (number) y coordinate of the right bottom point of the box
  1561. o width: (number) width of the box
  1562. o height: (number) height of the box
  1563. o cx: (number) x coordinate of the center of the box
  1564. o cy: (number) y coordinate of the center of the box
  1565. o }
  1566. \*/
  1567. var pathDimensions = R.pathBBox = function (path) {
  1568. var pth = paths(path);
  1569. if (pth.bbox) {
  1570. return clone(pth.bbox);
  1571. }
  1572. if (!path) {
  1573. return {x: 0, y: 0, width: 0, height: 0, x2: 0, y2: 0};
  1574. }
  1575. path = path2curve(path);
  1576. var x = 0,
  1577. y = 0,
  1578. X = [],
  1579. Y = [],
  1580. p;
  1581. for (var i = 0, ii = path.length; i < ii; i++) {
  1582. p = path[i];
  1583. if (p[0] == "M") {
  1584. x = p[1];
  1585. y = p[2];
  1586. X.push(x);
  1587. Y.push(y);
  1588. } else {
  1589. var dim = curveDim(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
  1590. X = X[concat](dim.min.x, dim.max.x);
  1591. Y = Y[concat](dim.min.y, dim.max.y);
  1592. x = p[5];
  1593. y = p[6];
  1594. }
  1595. }
  1596. var xmin = mmin[apply](0, X),
  1597. ymin = mmin[apply](0, Y),
  1598. xmax = mmax[apply](0, X),
  1599. ymax = mmax[apply](0, Y),
  1600. width = xmax - xmin,
  1601. height = ymax - ymin,
  1602. bb = {
  1603. x: xmin,
  1604. y: ymin,
  1605. x2: xmax,
  1606. y2: ymax,
  1607. width: width,
  1608. height: height,
  1609. cx: xmin + width / 2,
  1610. cy: ymin + height / 2
  1611. };
  1612. pth.bbox = clone(bb);
  1613. return bb;
  1614. },
  1615. pathClone = function (pathArray) {
  1616. var res = clone(pathArray);
  1617. res.toString = R._path2string;
  1618. return res;
  1619. },
  1620. pathToRelative = R._pathToRelative = function (pathArray) {
  1621. var pth = paths(pathArray);
  1622. if (pth.rel) {
  1623. return pathClone(pth.rel);
  1624. }
  1625. if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
  1626. pathArray = R.parsePathString(pathArray);
  1627. }
  1628. var res = [],
  1629. x = 0,
  1630. y = 0,
  1631. mx = 0,
  1632. my = 0,
  1633. start = 0;
  1634. if (pathArray[0][0] == "M") {
  1635. x = pathArray[0][1];
  1636. y = pathArray[0][2];
  1637. mx = x;
  1638. my = y;
  1639. start++;
  1640. res.push(["M", x, y]);
  1641. }
  1642. for (var i = start, ii = pathArray.length; i < ii; i++) {
  1643. var r = res[i] = [],
  1644. pa = pathArray[i];
  1645. if (pa[0] != lowerCase.call(pa[0])) {
  1646. r[0] = lowerCase.call(pa[0]);
  1647. switch (r[0]) {
  1648. case "a":
  1649. r[1] = pa[1];
  1650. r[2] = pa[2];
  1651. r[3] = pa[3];
  1652. r[4] = pa[4];
  1653. r[5] = pa[5];
  1654. r[6] = +(pa[6] - x).toFixed(3);
  1655. r[7] = +(pa[7] - y).toFixed(3);
  1656. break;
  1657. case "v":
  1658. r[1] = +(pa[1] - y).toFixed(3);
  1659. break;
  1660. case "m":
  1661. mx = pa[1];
  1662. my = pa[2];
  1663. default:
  1664. for (var j = 1, jj = pa.length; j < jj; j++) {
  1665. r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
  1666. }
  1667. }
  1668. } else {
  1669. r = res[i] = [];
  1670. if (pa[0] == "m") {
  1671. mx = pa[1] + x;
  1672. my = pa[2] + y;
  1673. }
  1674. for (var k = 0, kk = pa.length; k < kk; k++) {
  1675. res[i][k] = pa[k];
  1676. }
  1677. }
  1678. var len = res[i].length;
  1679. switch (res[i][0]) {
  1680. case "z":
  1681. x = mx;
  1682. y = my;
  1683. break;
  1684. case "h":
  1685. x += +res[i][len - 1];
  1686. break;
  1687. case "v":
  1688. y += +res[i][len - 1];
  1689. break;
  1690. default:
  1691. x += +res[i][len - 2];
  1692. y += +res[i][len - 1];
  1693. }
  1694. }
  1695. res.toString = R._path2string;
  1696. pth.rel = pathClone(res);
  1697. return res;
  1698. },
  1699. pathToAbsolute = R._pathToAbsolute = function (pathArray) {
  1700. var pth = paths(pathArray);
  1701. if (pth.abs) {
  1702. return pathClone(pth.abs);
  1703. }
  1704. if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
  1705. pathArray = R.parsePathString(pathArray);
  1706. }
  1707. if (!pathArray || !pathArray.length) {
  1708. return [["M", 0, 0]];
  1709. }
  1710. var res = [],
  1711. x = 0,
  1712. y = 0,
  1713. mx = 0,
  1714. my = 0,
  1715. start = 0;
  1716. if (pathArray[0][0] == "M") {
  1717. x = +pathArray[0][1];
  1718. y = +pathArray[0][2];
  1719. mx = x;
  1720. my = y;
  1721. start++;
  1722. res[0] = ["M", x, y];
  1723. }
  1724. var crz = pathArray.length == 3 && pathArray[0][0] == "M" && pathArray[1][0].toUpperCase() == "R" && pathArray[2][0].toUpperCase() == "Z";
  1725. for (var r, pa, i = start, ii = pathArray.length; i < ii; i++) {
  1726. res.push(r = []);
  1727. pa = pathArray[i];
  1728. if (pa[0] != upperCase.call(pa[0])) {
  1729. r[0] = upperCase.call(pa[0]);
  1730. switch (r[0]) {
  1731. case "A":
  1732. r[1] = pa[1];
  1733. r[2] = pa[2];
  1734. r[3] = pa[3];
  1735. r[4] = pa[4];
  1736. r[5] = pa[5];
  1737. r[6] = +(pa[6] + x);
  1738. r[7] = +(pa[7] + y);
  1739. break;
  1740. case "V":
  1741. r[1] = +pa[1] + y;
  1742. break;
  1743. case "H":
  1744. r[1] = +pa[1] + x;
  1745. break;
  1746. case "R":
  1747. var dots = [x, y][concat](pa.slice(1));
  1748. for (var j = 2, jj = dots.length; j < jj; j++) {
  1749. dots[j] = +dots[j] + x;
  1750. dots[++j] = +dots[j] + y;
  1751. }
  1752. res.pop();
  1753. res = res[concat](catmullRom2bezier(dots, crz));
  1754. break;
  1755. case "M":
  1756. mx = +pa[1] + x;
  1757. my = +pa[2] + y;
  1758. default:
  1759. for (j = 1, jj = pa.length; j < jj; j++) {
  1760. r[j] = +pa[j] + ((j % 2) ? x : y);
  1761. }
  1762. }
  1763. } else if (pa[0] == "R") {
  1764. dots = [x, y][concat](pa.slice(1));
  1765. res.pop();
  1766. res = res[concat](catmullRom2bezier(dots, crz));
  1767. r = ["R"][concat](pa.slice(-2));
  1768. } else {
  1769. for (var k = 0, kk = pa.length; k < kk; k++) {
  1770. r[k] = pa[k];
  1771. }
  1772. }
  1773. switch (r[0]) {
  1774. case "Z":
  1775. x = mx;
  1776. y = my;
  1777. break;
  1778. case "H":
  1779. x = r[1];
  1780. break;
  1781. case "V":
  1782. y = r[1];
  1783. break;
  1784. case "M":
  1785. mx = r[r.length - 2];
  1786. my = r[r.length - 1];
  1787. default:
  1788. x = r[r.length - 2];
  1789. y = r[r.length - 1];
  1790. }
  1791. }
  1792. res.toString = R._path2string;
  1793. pth.abs = pathClone(res);
  1794. return res;
  1795. },
  1796. l2c = function (x1, y1, x2, y2) {
  1797. return [x1, y1, x2, y2, x2, y2];
  1798. },
  1799. q2c = function (x1, y1, ax, ay, x2, y2) {
  1800. var _13 = 1 / 3,
  1801. _23 = 2 / 3;
  1802. return [
  1803. _13 * x1 + _23 * ax,
  1804. _13 * y1 + _23 * ay,
  1805. _13 * x2 + _23 * ax,
  1806. _13 * y2 + _23 * ay,
  1807. x2,
  1808. y2
  1809. ];
  1810. },
  1811. a2c = function (x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
  1812. // for more information of where this math came from visit:
  1813. // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
  1814. var _120 = PI * 120 / 180,
  1815. rad = PI / 180 * (+angle || 0),
  1816. res = [],
  1817. xy,
  1818. rotate = cacher(function (x, y, rad) {
  1819. var X = x * math.cos(rad) - y * math.sin(rad),
  1820. Y = x * math.sin(rad) + y * math.cos(rad);
  1821. return {x: X, y: Y};
  1822. });
  1823. if (!recursive) {
  1824. xy = rotate(x1, y1, -rad);
  1825. x1 = xy.x;
  1826. y1 = xy.y;
  1827. xy = rotate(x2, y2, -rad);
  1828. x2 = xy.x;
  1829. y2 = xy.y;
  1830. var cos = math.cos(PI / 180 * angle),
  1831. sin = math.sin(PI / 180 * angle),
  1832. x = (x1 - x2) / 2,
  1833. y = (y1 - y2) / 2;
  1834. var h = (x * x) / (rx * rx) + (y * y) / (ry * ry);
  1835. if (h > 1) {
  1836. h = math.sqrt(h);
  1837. rx = h * rx;
  1838. ry = h * ry;
  1839. }
  1840. var rx2 = rx * rx,
  1841. ry2 = ry * ry,
  1842. k = (large_arc_flag == sweep_flag ? -1 : 1) *
  1843. math.sqrt(abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))),
  1844. cx = k * rx * y / ry + (x1 + x2) / 2,
  1845. cy = k * -ry * x / rx + (y1 + y2) / 2,
  1846. f1 = math.asin(((y1 - cy) / ry).toFixed(9)),
  1847. f2 = math.asin(((y2 - cy) / ry).toFixed(9));
  1848. f1 = x1 < cx ? PI - f1 : f1;
  1849. f2 = x2 < cx ? PI - f2 : f2;
  1850. f1 < 0 && (f1 = PI * 2 + f1);
  1851. f2 < 0 && (f2 = PI * 2 + f2);
  1852. if (sweep_flag && f1 > f2) {
  1853. f1 = f1 - PI * 2;
  1854. }
  1855. if (!sweep_flag && f2 > f1) {
  1856. f2 = f2 - PI * 2;
  1857. }
  1858. } else {
  1859. f1 = recursive[0];
  1860. f2 = recursive[1];
  1861. cx = recursive[2];
  1862. cy = recursive[3];
  1863. }
  1864. var df = f2 - f1;
  1865. if (abs(df) > _120) {
  1866. var f2old = f2,
  1867. x2old = x2,
  1868. y2old = y2;
  1869. f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
  1870. x2 = cx + rx * math.cos(f2);
  1871. y2 = cy + ry * math.sin(f2);
  1872. res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
  1873. }
  1874. df = f2 - f1;
  1875. var c1 = math.cos(f1),
  1876. s1 = math.sin(f1),
  1877. c2 = math.cos(f2),
  1878. s2 = math.sin(f2),
  1879. t = math.tan(df / 4),
  1880. hx = 4 / 3 * rx * t,
  1881. hy = 4 / 3 * ry * t,
  1882. m1 = [x1, y1],
  1883. m2 = [x1 + hx * s1, y1 - hy * c1],
  1884. m3 = [x2 + hx * s2, y2 - hy * c2],
  1885. m4 = [x2, y2];
  1886. m2[0] = 2 * m1[0] - m2[0];
  1887. m2[1] = 2 * m1[1] - m2[1];
  1888. if (recursive) {
  1889. return [m2, m3, m4][concat](res);
  1890. } else {
  1891. res = [m2, m3, m4][concat](res).join()[split](",");
  1892. var newres = [];
  1893. for (var i = 0, ii = res.length; i < ii; i++) {
  1894. newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
  1895. }
  1896. return newres;
  1897. }
  1898. },
  1899. findDotAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
  1900. var t1 = 1 - t;
  1901. return {
  1902. x: pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
  1903. y: pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y
  1904. };
  1905. },
  1906. curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
  1907. var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
  1908. b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
  1909. c = p1x - c1x,
  1910. t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a,
  1911. t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a,
  1912. y = [p1y, p2y],
  1913. x = [p1x, p2x],
  1914. dot;
  1915. abs(t1) > "1e12" && (t1 = .5);
  1916. abs(t2) > "1e12" && (t2 = .5);
  1917. if (t1 > 0 && t1 < 1) {
  1918. dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
  1919. x.push(dot.x);
  1920. y.push(dot.y);
  1921. }
  1922. if (t2 > 0 && t2 < 1) {
  1923. dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
  1924. x.push(dot.x);
  1925. y.push(dot.y);
  1926. }
  1927. a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y);
  1928. b = 2 * (c1y - p1y) - 2 * (c2y - c1y);
  1929. c = p1y - c1y;
  1930. t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a;
  1931. t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a;
  1932. abs(t1) > "1e12" && (t1 = .5);
  1933. abs(t2) > "1e12" && (t2 = .5);
  1934. if (t1 > 0 && t1 < 1) {
  1935. dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
  1936. x.push(dot.x);
  1937. y.push(dot.y);
  1938. }
  1939. if (t2 > 0 && t2 < 1) {
  1940. dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
  1941. x.push(dot.x);
  1942. y.push(dot.y);
  1943. }
  1944. return {
  1945. min: {x: mmin[apply](0, x), y: mmin[apply](0, y)},
  1946. max: {x: mmax[apply](0, x), y: mmax[apply](0, y)}
  1947. };
  1948. }),
  1949. path2curve = R._path2curve = cacher(function (path, path2) {
  1950. var pth = !path2 && paths(path);
  1951. if (!path2 && pth.curve) {
  1952. return pathClone(pth.curve);
  1953. }
  1954. var p = pathToAbsolute(path),
  1955. p2 = path2 && pathToAbsolute(path2),
  1956. attrs = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
  1957. attrs2 = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
  1958. processPath = function (path, d, pcom) {
  1959. var nx, ny, tq = {T:1, Q:1};
  1960. if (!path) {
  1961. return ["C", d.x, d.y, d.x, d.y, d.x, d.y];
  1962. }
  1963. !(path[0] in tq) && (d.qx = d.qy = null);
  1964. switch (path[0]) {
  1965. case "M":
  1966. d.X = path[1];
  1967. d.Y = path[2];
  1968. break;
  1969. case "A":
  1970. path = ["C"][concat](a2c[apply](0, [d.x, d.y][concat](path.slice(1))));
  1971. break;
  1972. case "S":
  1973. if (pcom == "C" || pcom == "S") { // In "S" case we have to take into account, if the previous command is C/S.
  1974. nx = d.x * 2 - d.bx; // And reflect the previous
  1975. ny = d.y * 2 - d.by; // command's control point relative to the current point.
  1976. }
  1977. else { // or some else or nothing
  1978. nx = d.x;
  1979. ny = d.y;
  1980. }
  1981. path = ["C", nx, ny][concat](path.slice(1));
  1982. break;
  1983. case "T":
  1984. if (pcom == "Q" || pcom == "T") { // In "T" case we have to take into account, if the previous command is Q/T.
  1985. d.qx = d.x * 2 - d.qx; // And make a reflection similar
  1986. d.qy = d.y * 2 - d.qy; // to case "S".
  1987. }
  1988. else { // or something else or nothing
  1989. d.qx = d.x;
  1990. d.qy = d.y;
  1991. }
  1992. path = ["C"][concat](q2c(d.x, d.y, d.qx, d.qy, path[1], path[2]));
  1993. break;
  1994. case "Q":
  1995. d.qx = path[1];
  1996. d.qy = path[2];
  1997. path = ["C"][concat](q2c(d.x, d.y, path[1], path[2], path[3], path[4]));
  1998. break;
  1999. case "L":
  2000. path = ["C"][concat](l2c(d.x, d.y, path[1], path[2]));
  2001. break;
  2002. case "H":
  2003. path = ["C"][concat](l2c(d.x, d.y, path[1], d.y));
  2004. break;
  2005. case "V":
  2006. path = ["C"][concat](l2c(d.x, d.y, d.x, path[1]));
  2007. break;
  2008. case "Z":
  2009. path = ["C"][concat](l2c(d.x, d.y, d.X, d.Y));
  2010. break;
  2011. }
  2012. return path;
  2013. },
  2014. fixArc = function (pp, i) {
  2015. if (pp[i].length > 7) {
  2016. pp[i].shift();
  2017. var pi = pp[i];
  2018. while (pi.length) {
  2019. pcoms1[i]="A"; // if created multiple C:s, their original seg is saved
  2020. p2 && (pcoms2[i]="A"); // the same as above
  2021. pp.splice(i++, 0, ["C"][concat](pi.splice(0, 6)));
  2022. }
  2023. pp.splice(i, 1);
  2024. ii = mmax(p.length, p2 && p2.length || 0);
  2025. }
  2026. },
  2027. fixM = function (path1, path2, a1, a2, i) {
  2028. if (path1 && path2 && path1[i][0] == "M" && path2[i][0] != "M") {
  2029. path2.splice(i, 0, ["M", a2.x, a2.y]);
  2030. a1.bx = 0;
  2031. a1.by = 0;
  2032. a1.x = path1[i][1];
  2033. a1.y = path1[i][2];
  2034. ii = mmax(p.length, p2 && p2.length || 0);
  2035. }
  2036. },
  2037. pcoms1 = [], // path commands of original path p
  2038. pcoms2 = [], // path commands of original path p2
  2039. pfirst = "", // temporary holder for original path command
  2040. pcom = ""; // holder for previous path command of original path
  2041. for (var i = 0, ii = mmax(p.length, p2 && p2.length || 0); i < ii; i++) {
  2042. p[i] && (pfirst = p[i][0]); // save current path command
  2043. if (pfirst != "C") // C is not saved yet, because it may be result of conversion
  2044. {
  2045. pcoms1[i] = pfirst; // Save current path command
  2046. i && ( pcom = pcoms1[i-1]); // Get previous path command pcom
  2047. }
  2048. p[i] = processPath(p[i], attrs, pcom); // Previous path command is inputted to processPath
  2049. if (pcoms1[i] != "A" && pfirst == "C") pcoms1[i] = "C"; // A is the only command
  2050. // which may produce multiple C:s
  2051. // so we have to make sure that C is also C in original path
  2052. fixArc(p, i); // fixArc adds also the right amount of A:s to pcoms1
  2053. if (p2) { // the same procedures is done to p2
  2054. p2[i] && (pfirst = p2[i][0]);
  2055. if (pfirst != "C")
  2056. {
  2057. pcoms2[i] = pfirst;
  2058. i && (pcom = pcoms2[i-1]);
  2059. }
  2060. p2[i] = processPath(p2[i], attrs2, pcom);
  2061. if (pcoms2[i]!="A" && pfirst=="C") pcoms2[i]="C";
  2062. fixArc(p2, i);
  2063. }
  2064. fixM(p, p2, attrs, attrs2, i);
  2065. fixM(p2, p, attrs2, attrs, i);
  2066. var seg = p[i],
  2067. seg2 = p2 && p2[i],
  2068. seglen = seg.length,
  2069. seg2len = p2 && seg2.length;
  2070. attrs.x = seg[seglen - 2];
  2071. attrs.y = seg[seglen - 1];
  2072. attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
  2073. attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
  2074. attrs2.bx = p2 && (toFloat(seg2[seg2len - 4]) || attrs2.x);
  2075. attrs2.by = p2 && (toFloat(seg2[seg2len - 3]) || attrs2.y);
  2076. attrs2.x = p2 && seg2[seg2len - 2];
  2077. attrs2.y = p2 && seg2[seg2len - 1];
  2078. }
  2079. if (!p2) {
  2080. pth.curve = pathClone(p);
  2081. }
  2082. return p2 ? [p, p2] : p;
  2083. }, null, pathClone),
  2084. parseDots = R._parseDots = cacher(function (gradient) {
  2085. var dots = [];
  2086. for (var i = 0, ii = gradient.length; i < ii; i++) {
  2087. var dot = {},
  2088. par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
  2089. dot.color = R.getRGB(par[1]);
  2090. if (dot.color.error) {
  2091. return null;
  2092. }
  2093. dot.opacity = dot.color.opacity;
  2094. dot.color = dot.color.hex;
  2095. par[2] && (dot.offset = par[2] + "%");
  2096. dots.push(dot);
  2097. }
  2098. for (i = 1, ii = dots.length - 1; i < ii; i++) {
  2099. if (!dots[i].offset) {
  2100. var start = toFloat(dots[i - 1].offset || 0),
  2101. end = 0;
  2102. for (var j = i + 1; j < ii; j++) {
  2103. if (dots[j].offset) {
  2104. end = dots[j].offset;
  2105. break;
  2106. }
  2107. }
  2108. if (!end) {
  2109. end = 100;
  2110. j = ii;
  2111. }
  2112. end = toFloat(end);
  2113. var d = (end - start) / (j - i + 1);
  2114. for (; i < j; i++) {
  2115. start += d;
  2116. dots[i].offset = start + "%";
  2117. }
  2118. }
  2119. }
  2120. return dots;
  2121. }),
  2122. tear = R._tear = function (el, paper) {
  2123. el == paper.top && (paper.top = el.prev);
  2124. el == paper.bottom && (paper.bottom = el.next);
  2125. el.next && (el.next.prev = el.prev);
  2126. el.prev && (el.prev.next = el.next);
  2127. },
  2128. tofront = R._tofront = function (el, paper) {
  2129. if (paper.top === el) {
  2130. return;
  2131. }
  2132. tear(el, paper);
  2133. el.next = null;
  2134. el.prev = paper.top;
  2135. paper.top.next = el;
  2136. paper.top = el;
  2137. },
  2138. toback = R._toback = function (el, paper) {
  2139. if (paper.bottom === el) {
  2140. return;
  2141. }
  2142. tear(el, paper);
  2143. el.next = paper.bottom;
  2144. el.prev = null;
  2145. paper.bottom.prev = el;
  2146. paper.bottom = el;
  2147. },
  2148. insertafter = R._insertafter = function (el, el2, paper) {
  2149. tear(el, paper);
  2150. el2 == paper.top && (paper.top = el);
  2151. el2.next && (el2.next.prev = el);
  2152. el.next = el2.next;
  2153. el.prev = el2;
  2154. el2.next = el;
  2155. },
  2156. insertbefore = R._insertbefore = function (el, el2, paper) {
  2157. tear(el, paper);
  2158. el2 == paper.bottom && (paper.bottom = el);
  2159. el2.prev && (el2.prev.next = el);
  2160. el.prev = el2.prev;
  2161. el2.prev = el;
  2162. el.next = el2;
  2163. },
  2164. /*\
  2165. * Raphael.toMatrix
  2166. [ method ]
  2167. **
  2168. * Utility method
  2169. **
  2170. * Returns matrix of transformations applied to a given path
  2171. > Parameters
  2172. - path (string) path string
  2173. - transform (string|array) transformation string
  2174. = (object) @Matrix
  2175. \*/
  2176. toMatrix = R.toMatrix = function (path, transform) {
  2177. var bb = pathDimensions(path),
  2178. el = {
  2179. _: {
  2180. transform: E
  2181. },
  2182. getBBox: function () {
  2183. return bb;
  2184. }
  2185. };
  2186. extractTransform(el, transform);
  2187. return el.matrix;
  2188. },
  2189. /*\
  2190. * Raphael.transformPath
  2191. [ method ]
  2192. **
  2193. * Utility method
  2194. **
  2195. * Returns path transformed by a given transformation
  2196. > Parameters
  2197. - path (string) path string
  2198. - transform (string|array) transformation string
  2199. = (string) path
  2200. \*/
  2201. transformPath = R.transformPath = function (path, transform) {
  2202. return mapPath(path, toMatrix(path, transform));
  2203. },
  2204. extractTransform = R._extractTransform = function (el, tstr) {
  2205. if (tstr == null) {
  2206. return el._.transform;
  2207. }
  2208. tstr = Str(tstr).replace(/\.{3}|\u2026/g, el._.transform || E);
  2209. var tdata = R.parseTransformString(tstr),
  2210. deg = 0,
  2211. dx = 0,
  2212. dy = 0,
  2213. sx = 1,
  2214. sy = 1,
  2215. _ = el._,
  2216. m = new Matrix;
  2217. _.transform = tdata || [];
  2218. if (tdata) {
  2219. for (var i = 0, ii = tdata.length; i < ii; i++) {
  2220. var t = tdata[i],
  2221. tlen = t.length,
  2222. command = Str(t[0]).toLowerCase(),
  2223. absolute = t[0] != command,
  2224. inver = absolute ? m.invert() : 0,
  2225. x1,
  2226. y1,
  2227. x2,
  2228. y2,
  2229. bb;
  2230. if (command == "t" && tlen == 3) {
  2231. if (absolute) {
  2232. x1 = inver.x(0, 0);
  2233. y1 = inver.y(0, 0);
  2234. x2 = inver.x(t[1], t[2]);
  2235. y2 = inver.y(t[1], t[2]);
  2236. m.translate(x2 - x1, y2 - y1);
  2237. } else {
  2238. m.translate(t[1], t[2]);
  2239. }
  2240. } else if (command == "r") {
  2241. if (tlen == 2) {
  2242. bb = bb || el.getBBox(1);
  2243. m.rotate(t[1], bb.x + bb.width / 2, bb.y + bb.height / 2);
  2244. deg += t[1];
  2245. } else if (tlen == 4) {
  2246. if (absolute) {
  2247. x2 = inver.x(t[2], t[3]);
  2248. y2 = inver.y(t[2], t[3]);
  2249. m.rotate(t[1], x2, y2);
  2250. } else {
  2251. m.rotate(t[1], t[2], t[3]);
  2252. }
  2253. deg += t[1];
  2254. }
  2255. } else if (command == "s") {
  2256. if (tlen == 2 || tlen == 3) {
  2257. bb = bb || el.getBBox(1);
  2258. m.scale(t[1], t[tlen - 1], bb.x + bb.width / 2, bb.y + bb.height / 2);
  2259. sx *= t[1];
  2260. sy *= t[tlen - 1];
  2261. } else if (tlen == 5) {
  2262. if (absolute) {
  2263. x2 = inver.x(t[3], t[4]);
  2264. y2 = inver.y(t[3], t[4]);
  2265. m.scale(t[1], t[2], x2, y2);
  2266. } else {
  2267. m.scale(t[1], t[2], t[3], t[4]);
  2268. }
  2269. sx *= t[1];
  2270. sy *= t[2];
  2271. }
  2272. } else if (command == "m" && tlen == 7) {
  2273. m.add(t[1], t[2], t[3], t[4], t[5], t[6]);
  2274. }
  2275. _.dirtyT = 1;
  2276. el.matrix = m;
  2277. }
  2278. }
  2279. /*\
  2280. * Element.matrix
  2281. [ property (object) ]
  2282. **
  2283. * Keeps @Matrix object, which represents element transformation
  2284. \*/
  2285. el.matrix = m;
  2286. _.sx = sx;
  2287. _.sy = sy;
  2288. _.deg = deg;
  2289. _.dx = dx = m.e;
  2290. _.dy = dy = m.f;
  2291. if (sx == 1 && sy == 1 && !deg && _.bbox) {
  2292. _.bbox.x += +dx;
  2293. _.bbox.y += +dy;
  2294. } else {
  2295. _.dirtyT = 1;
  2296. }
  2297. },
  2298. getEmpty = function (item) {
  2299. var l = item[0];
  2300. switch (l.toLowerCase()) {
  2301. case "t": return [l, 0, 0];
  2302. case "m": return [l, 1, 0, 0, 1, 0, 0];
  2303. case "r": if (item.length == 4) {
  2304. return [l, 0, item[2], item[3]];
  2305. } else {
  2306. return [l, 0];
  2307. }
  2308. case "s": if (item.length == 5) {
  2309. return [l, 1, 1, item[3], item[4]];
  2310. } else if (item.length == 3) {
  2311. return [l, 1, 1];
  2312. } else {
  2313. return [l, 1];
  2314. }
  2315. }
  2316. },
  2317. equaliseTransform = R._equaliseTransform = function (t1, t2) {
  2318. t2 = Str(t2).replace(/\.{3}|\u2026/g, t1);
  2319. t1 = R.parseTransformString(t1) || [];
  2320. t2 = R.parseTransformString(t2) || [];
  2321. var maxlength = mmax(t1.length, t2.length),
  2322. from = [],
  2323. to = [],
  2324. i = 0, j, jj,
  2325. tt1, tt2;
  2326. for (; i < maxlength; i++) {
  2327. tt1 = t1[i] || getEmpty(t2[i]);
  2328. tt2 = t2[i] || getEmpty(tt1);
  2329. if ((tt1[0] != tt2[0]) ||
  2330. (tt1[0].toLowerCase() == "r" && (tt1[2] != tt2[2] || tt1[3] != tt2[3])) ||
  2331. (tt1[0].toLowerCase() == "s" && (tt1[3] != tt2[3] || tt1[4] != tt2[4]))
  2332. ) {
  2333. return;
  2334. }
  2335. from[i] = [];
  2336. to[i] = [];
  2337. for (j = 0, jj = mmax(tt1.length, tt2.length); j < jj; j++) {
  2338. j in tt1 && (from[i][j] = tt1[j]);
  2339. j in tt2 && (to[i][j] = tt2[j]);
  2340. }
  2341. }
  2342. return {
  2343. from: from,
  2344. to: to
  2345. };
  2346. };
  2347. R._getContainer = function (x, y, w, h) {
  2348. var container;
  2349. container = h == null && !R.is(x, "object") ? g.doc.getElementById(x) : x;
  2350. if (container == null) {
  2351. return;
  2352. }
  2353. if (container.tagName) {
  2354. if (y == null) {
  2355. return {
  2356. container: container,
  2357. width: container.style.pixelWidth || container.offsetWidth,
  2358. height: container.style.pixelHeight || container.offsetHeight
  2359. };
  2360. } else {
  2361. return {
  2362. container: container,
  2363. width: y,
  2364. height: w
  2365. };
  2366. }
  2367. }
  2368. return {
  2369. container: 1,
  2370. x: x,
  2371. y: y,
  2372. width: w,
  2373. height: h
  2374. };
  2375. };
  2376. /*\
  2377. * Raphael.pathToRelative
  2378. [ method ]
  2379. **
  2380. * Utility method
  2381. **
  2382. * Converts path to relative form
  2383. > Parameters
  2384. - pathString (string|array) path string or array of segments
  2385. = (array) array of segments.
  2386. \*/
  2387. R.pathToRelative = pathToRelative;
  2388. R._engine = {};
  2389. /*\
  2390. * Raphael.path2curve
  2391. [ method ]
  2392. **
  2393. * Utility method
  2394. **
  2395. * Converts path to a new path where all segments are cubic bezier curves.
  2396. > Parameters
  2397. - pathString (string|array) path string or array of segments
  2398. = (array) array of segments.
  2399. \*/
  2400. R.path2curve = path2curve;
  2401. /*\
  2402. * Raphael.matrix
  2403. [ method ]
  2404. **
  2405. * Utility method
  2406. **
  2407. * Returns matrix based on given parameters.
  2408. > Parameters
  2409. - a (number)
  2410. - b (number)
  2411. - c (number)
  2412. - d (number)
  2413. - e (number)
  2414. - f (number)
  2415. = (object) @Matrix
  2416. \*/
  2417. R.matrix = function (a, b, c, d, e, f) {
  2418. return new Matrix(a, b, c, d, e, f);
  2419. };
  2420. function Matrix(a, b, c, d, e, f) {
  2421. if (a != null) {
  2422. this.a = +a;
  2423. this.b = +b;
  2424. this.c = +c;
  2425. this.d = +d;
  2426. this.e = +e;
  2427. this.f = +f;
  2428. } else {
  2429. this.a = 1;
  2430. this.b = 0;
  2431. this.c = 0;
  2432. this.d = 1;
  2433. this.e = 0;
  2434. this.f = 0;
  2435. }
  2436. }
  2437. (function (matrixproto) {
  2438. /*\
  2439. * Matrix.add
  2440. [ method ]
  2441. **
  2442. * Adds given matrix to existing one.
  2443. > Parameters
  2444. - a (number)
  2445. - b (number)
  2446. - c (number)
  2447. - d (number)
  2448. - e (number)
  2449. - f (number)
  2450. or
  2451. - matrix (object) @Matrix
  2452. \*/
  2453. matrixproto.add = function (a, b, c, d, e, f) {
  2454. var out = [[], [], []],
  2455. m = [[this.a, this.c, this.e], [this.b, this.d, this.f], [0, 0, 1]],
  2456. matrix = [[a, c, e], [b, d, f], [0, 0, 1]],
  2457. x, y, z, res;
  2458. if (a && a instanceof Matrix) {
  2459. matrix = [[a.a, a.c, a.e], [a.b, a.d, a.f], [0, 0, 1]];
  2460. }
  2461. for (x = 0; x < 3; x++) {
  2462. for (y = 0; y < 3; y++) {
  2463. res = 0;
  2464. for (z = 0; z < 3; z++) {
  2465. res += m[x][z] * matrix[z][y];
  2466. }
  2467. out[x][y] = res;
  2468. }
  2469. }
  2470. this.a = out[0][0];
  2471. this.b = out[1][0];
  2472. this.c = out[0][1];
  2473. this.d = out[1][1];
  2474. this.e = out[0][2];
  2475. this.f = out[1][2];
  2476. };
  2477. /*\
  2478. * Matrix.invert
  2479. [ method ]
  2480. **
  2481. * Returns inverted version of the matrix
  2482. = (object) @Matrix
  2483. \*/
  2484. matrixproto.invert = function () {
  2485. var me = this,
  2486. x = me.a * me.d - me.b * me.c;
  2487. 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);
  2488. };
  2489. /*\
  2490. * Matrix.clone
  2491. [ method ]
  2492. **
  2493. * Returns copy of the matrix
  2494. = (object) @Matrix
  2495. \*/
  2496. matrixproto.clone = function () {
  2497. return new Matrix(this.a, this.b, this.c, this.d, this.e, this.f);
  2498. };
  2499. /*\
  2500. * Matrix.translate
  2501. [ method ]
  2502. **
  2503. * Translate the matrix
  2504. > Parameters
  2505. - x (number)
  2506. - y (number)
  2507. \*/
  2508. matrixproto.translate = function (x, y) {
  2509. this.add(1, 0, 0, 1, x, y);
  2510. };
  2511. /*\
  2512. * Matrix.scale
  2513. [ method ]
  2514. **
  2515. * Scales the matrix
  2516. > Parameters
  2517. - x (number)
  2518. - y (number) #optional
  2519. - cx (number) #optional
  2520. - cy (number) #optional
  2521. \*/
  2522. matrixproto.scale = function (x, y, cx, cy) {
  2523. y == null && (y = x);
  2524. (cx || cy) && this.add(1, 0, 0, 1, cx, cy);
  2525. this.add(x, 0, 0, y, 0, 0);
  2526. (cx || cy) && this.add(1, 0, 0, 1, -cx, -cy);
  2527. };
  2528. /*\
  2529. * Matrix.rotate
  2530. [ method ]
  2531. **
  2532. * Rotates the matrix
  2533. > Parameters
  2534. - a (number)
  2535. - x (number)
  2536. - y (number)
  2537. \*/
  2538. matrixproto.rotate = function (a, x, y) {
  2539. a = R.rad(a);
  2540. x = x || 0;
  2541. y = y || 0;
  2542. var cos = +math.cos(a).toFixed(9),
  2543. sin = +math.sin(a).toFixed(9);
  2544. this.add(cos, sin, -sin, cos, x, y);
  2545. this.add(1, 0, 0, 1, -x, -y);
  2546. };
  2547. /*\
  2548. * Matrix.x
  2549. [ method ]
  2550. **
  2551. * Return x coordinate for given point after transformation described by the matrix. See also @Matrix.y
  2552. > Parameters
  2553. - x (number)
  2554. - y (number)
  2555. = (number) x
  2556. \*/
  2557. matrixproto.x = function (x, y) {
  2558. return x * this.a + y * this.c + this.e;
  2559. };
  2560. /*\
  2561. * Matrix.y
  2562. [ method ]
  2563. **
  2564. * Return y coordinate for given point after transformation described by the matrix. See also @Matrix.x
  2565. > Parameters
  2566. - x (number)
  2567. - y (number)
  2568. = (number) y
  2569. \*/
  2570. matrixproto.y = function (x, y) {
  2571. return x * this.b + y * this.d + this.f;
  2572. };
  2573. matrixproto.get = function (i) {
  2574. return +this[Str.fromCharCode(97 + i)].toFixed(4);
  2575. };
  2576. matrixproto.toString = function () {
  2577. return R.svg ?
  2578. "matrix(" + [this.get(0), this.get(1), this.get(2), this.get(3), this.get(4), this.get(5)].join() + ")" :
  2579. [this.get(0), this.get(2), this.get(1), this.get(3), 0, 0].join();
  2580. };
  2581. matrixproto.toFilter = function () {
  2582. return "progid:DXImageTransform.Microsoft.Matrix(M11=" + this.get(0) +
  2583. ", M12=" + this.get(2) + ", M21=" + this.get(1) + ", M22=" + this.get(3) +
  2584. ", Dx=" + this.get(4) + ", Dy=" + this.get(5) + ", sizingmethod='auto expand')";
  2585. };
  2586. matrixproto.offset = function () {
  2587. return [this.e.toFixed(4), this.f.toFixed(4)];
  2588. };
  2589. function norm(a) {
  2590. return a[0] * a[0] + a[1] * a[1];
  2591. }
  2592. function normalize(a) {
  2593. var mag = math.sqrt(norm(a));
  2594. a[0] && (a[0] /= mag);
  2595. a[1] && (a[1] /= mag);
  2596. }
  2597. /*\
  2598. * Matrix.split
  2599. [ method ]
  2600. **
  2601. * Splits matrix into primitive transformations
  2602. = (object) in format:
  2603. o dx (number) translation by x
  2604. o dy (number) translation by y
  2605. o scalex (number) scale by x
  2606. o scaley (number) scale by y
  2607. o shear (number) shear
  2608. o rotate (number) rotation in deg
  2609. o isSimple (boolean) could it be represented via simple transformations
  2610. \*/
  2611. matrixproto.split = function () {
  2612. var out = {};
  2613. // translation
  2614. out.dx = this.e;
  2615. out.dy = this.f;
  2616. // scale and shear
  2617. var row = [[this.a, this.c], [this.b, this.d]];
  2618. out.scalex = math.sqrt(norm(row[0]));
  2619. normalize(row[0]);
  2620. out.shear = row[0][0] * row[1][0] + row[0][1] * row[1][1];
  2621. row[1] = [row[1][0] - row[0][0] * out.shear, row[1][1] - row[0][1] * out.shear];
  2622. out.scaley = math.sqrt(norm(row[1]));
  2623. normalize(row[1]);
  2624. out.shear /= out.scaley;
  2625. // rotation
  2626. var sin = -row[0][1],
  2627. cos = row[1][1];
  2628. if (cos < 0) {
  2629. out.rotate = R.deg(math.acos(cos));
  2630. if (sin < 0) {
  2631. out.rotate = 360 - out.rotate;
  2632. }
  2633. } else {
  2634. out.rotate = R.deg(math.asin(sin));
  2635. }
  2636. out.isSimple = !+out.shear.toFixed(9) && (out.scalex.toFixed(9) == out.scaley.toFixed(9) || !out.rotate);
  2637. out.isSuperSimple = !+out.shear.toFixed(9) && out.scalex.toFixed(9) == out.scaley.toFixed(9) && !out.rotate;
  2638. out.noRotation = !+out.shear.toFixed(9) && !out.rotate;
  2639. return out;
  2640. };
  2641. /*\
  2642. * Matrix.toTransformString
  2643. [ method ]
  2644. **
  2645. * Return transform string that represents given matrix
  2646. = (string) transform string
  2647. \*/
  2648. matrixproto.toTransformString = function (shorter) {
  2649. var s = shorter || this[split]();
  2650. if (s.isSimple) {
  2651. s.scalex = +s.scalex.toFixed(4);
  2652. s.scaley = +s.scaley.toFixed(4);
  2653. s.rotate = +s.rotate.toFixed(4);
  2654. return (s.dx || s.dy ? "t" + [s.dx, s.dy] : E) +
  2655. (s.scalex != 1 || s.scaley != 1 ? "s" + [s.scalex, s.scaley, 0, 0] : E) +
  2656. (s.rotate ? "r" + [s.rotate, 0, 0] : E);
  2657. } else {
  2658. return "m" + [this.get(0), this.get(1), this.get(2), this.get(3), this.get(4), this.get(5)];
  2659. }
  2660. };
  2661. })(Matrix.prototype);
  2662. var preventDefault = function () {
  2663. this.returnValue = false;
  2664. },
  2665. preventTouch = function () {
  2666. return this.originalEvent.preventDefault();
  2667. },
  2668. stopPropagation = function () {
  2669. this.cancelBubble = true;
  2670. },
  2671. stopTouch = function () {
  2672. return this.originalEvent.stopPropagation();
  2673. },
  2674. getEventPosition = function (e) {
  2675. var scrollY = g.doc.documentElement.scrollTop || g.doc.body.scrollTop,
  2676. scrollX = g.doc.documentElement.scrollLeft || g.doc.body.scrollLeft;
  2677. return {
  2678. x: e.clientX + scrollX,
  2679. y: e.clientY + scrollY
  2680. };
  2681. },
  2682. addEvent = (function () {
  2683. if (g.doc.addEventListener) {
  2684. return function (obj, type, fn, element) {
  2685. var f = function (e) {
  2686. var pos = getEventPosition(e);
  2687. return fn.call(element, e, pos.x, pos.y);
  2688. };
  2689. obj.addEventListener(type, f, false);
  2690. if (supportsTouch && touchMap[type]) {
  2691. var _f = function (e) {
  2692. var pos = getEventPosition(e),
  2693. olde = e;
  2694. for (var i = 0, ii = e.targetTouches && e.targetTouches.length; i < ii; i++) {
  2695. if (e.targetTouches[i].target == obj) {
  2696. e = e.targetTouches[i];
  2697. e.originalEvent = olde;
  2698. e.preventDefault = preventTouch;
  2699. e.stopPropagation = stopTouch;
  2700. break;
  2701. }
  2702. }
  2703. return fn.call(element, e, pos.x, pos.y);
  2704. };
  2705. obj.addEventListener(touchMap[type], _f, false);
  2706. }
  2707. return function () {
  2708. obj.removeEventListener(type, f, false);
  2709. if (supportsTouch && touchMap[type])
  2710. obj.removeEventListener(touchMap[type], _f, false);
  2711. return true;
  2712. };
  2713. };
  2714. } else if (g.doc.attachEvent) {
  2715. return function (obj, type, fn, element) {
  2716. var f = function (e) {
  2717. e = e || g.win.event;
  2718. var scrollY = g.doc.documentElement.scrollTop || g.doc.body.scrollTop,
  2719. scrollX = g.doc.documentElement.scrollLeft || g.doc.body.scrollLeft,
  2720. x = e.clientX + scrollX,
  2721. y = e.clientY + scrollY;
  2722. e.preventDefault = e.preventDefault || preventDefault;
  2723. e.stopPropagation = e.stopPropagation || stopPropagation;
  2724. return fn.call(element, e, x, y);
  2725. };
  2726. obj.attachEvent("on" + type, f);
  2727. var detacher = function () {
  2728. obj.detachEvent("on" + type, f);
  2729. return true;
  2730. };
  2731. return detacher;
  2732. };
  2733. }
  2734. })(),
  2735. drag = [],
  2736. dragMove = function (e) {
  2737. var x = e.clientX,
  2738. y = e.clientY,
  2739. scrollY = g.doc.documentElement.scrollTop || g.doc.body.scrollTop,
  2740. scrollX = g.doc.documentElement.scrollLeft || g.doc.body.scrollLeft,
  2741. dragi,
  2742. j = drag.length;
  2743. while (j--) {
  2744. dragi = drag[j];
  2745. if (supportsTouch && e.touches) {
  2746. var i = e.touches.length,
  2747. touch;
  2748. while (i--) {
  2749. touch = e.touches[i];
  2750. if (touch.identifier == dragi.el._drag.id) {
  2751. x = touch.clientX;
  2752. y = touch.clientY;
  2753. (e.originalEvent ? e.originalEvent : e).preventDefault();
  2754. break;
  2755. }
  2756. }
  2757. } else {
  2758. e.preventDefault();
  2759. }
  2760. var node = dragi.el.node,
  2761. o,
  2762. next = node.nextSibling,
  2763. parent = node.parentNode,
  2764. display = node.style.display;
  2765. g.win.opera && parent.removeChild(node);
  2766. node.style.display = "none";
  2767. o = dragi.el.paper.getElementByPoint(x, y);
  2768. node.style.display = display;
  2769. g.win.opera && (next ? parent.insertBefore(node, next) : parent.appendChild(node));
  2770. o && eve("raphael.drag.over." + dragi.el.id, dragi.el, o);
  2771. x += scrollX;
  2772. y += scrollY;
  2773. eve("raphael.drag.move." + dragi.el.id, dragi.move_scope || dragi.el, x - dragi.el._drag.x, y - dragi.el._drag.y, x, y, e);
  2774. }
  2775. },
  2776. dragUp = function (e) {
  2777. R.unmousemove(dragMove).unmouseup(dragUp);
  2778. var i = drag.length,
  2779. dragi;
  2780. while (i--) {
  2781. dragi = drag[i];
  2782. dragi.el._drag = {};
  2783. eve("raphael.drag.end." + dragi.el.id, dragi.end_scope || dragi.start_scope || dragi.move_scope || dragi.el, e);
  2784. }
  2785. drag = [];
  2786. },
  2787. /*\
  2788. * Raphael.el
  2789. [ property (object) ]
  2790. **
  2791. * You can add your own method to elements. This is useful when you want to hack default functionality or
  2792. * want to wrap some common transformation or attributes in one method. In difference to canvas methods,
  2793. * you can redefine element method at any time. Expending element methods wouldn’t affect set.
  2794. > Usage
  2795. | Raphael.el.red = function () {
  2796. | this.attr({fill: "#f00"});
  2797. | };
  2798. | // then use it
  2799. | paper.circle(100, 100, 20).red();
  2800. \*/
  2801. elproto = R.el = {};
  2802. /*\
  2803. * Element.click
  2804. [ method ]
  2805. **
  2806. * Adds event handler for click for the element.
  2807. > Parameters
  2808. - handler (function) handler for the event
  2809. = (object) @Element
  2810. \*/
  2811. /*\
  2812. * Element.unclick
  2813. [ method ]
  2814. **
  2815. * Removes event handler for click for the element.
  2816. > Parameters
  2817. - handler (function) #optional handler for the event
  2818. = (object) @Element
  2819. \*/
  2820. /*\
  2821. * Element.dblclick
  2822. [ method ]
  2823. **
  2824. * Adds event handler for double click for the element.
  2825. > Parameters
  2826. - handler (function) handler for the event
  2827. = (object) @Element
  2828. \*/
  2829. /*\
  2830. * Element.undblclick
  2831. [ method ]
  2832. **
  2833. * Removes event handler for double click for the element.
  2834. > Parameters
  2835. - handler (function) #optional handler for the event
  2836. = (object) @Element
  2837. \*/
  2838. /*\
  2839. * Element.mousedown
  2840. [ method ]
  2841. **
  2842. * Adds event handler for mousedown for the element.
  2843. > Parameters
  2844. - handler (function) handler for the event
  2845. = (object) @Element
  2846. \*/
  2847. /*\
  2848. * Element.unmousedown
  2849. [ method ]
  2850. **
  2851. * Removes event handler for mousedown for the element.
  2852. > Parameters
  2853. - handler (function) #optional handler for the event
  2854. = (object) @Element
  2855. \*/
  2856. /*\
  2857. * Element.mousemove
  2858. [ method ]
  2859. **
  2860. * Adds event handler for mousemove for the element.
  2861. > Parameters
  2862. - handler (function) handler for the event
  2863. = (object) @Element
  2864. \*/
  2865. /*\
  2866. * Element.unmousemove
  2867. [ method ]
  2868. **
  2869. * Removes event handler for mousemove for the element.
  2870. > Parameters
  2871. - handler (function) #optional handler for the event
  2872. = (object) @Element
  2873. \*/
  2874. /*\
  2875. * Element.mouseout
  2876. [ method ]
  2877. **
  2878. * Adds event handler for mouseout for the element.
  2879. > Parameters
  2880. - handler (function) handler for the event
  2881. = (object) @Element
  2882. \*/
  2883. /*\
  2884. * Element.unmouseout
  2885. [ method ]
  2886. **
  2887. * Removes event handler for mouseout for the element.
  2888. > Parameters
  2889. - handler (function) #optional handler for the event
  2890. = (object) @Element
  2891. \*/
  2892. /*\
  2893. * Element.mouseover
  2894. [ method ]
  2895. **
  2896. * Adds event handler for mouseover for the element.
  2897. > Parameters
  2898. - handler (function) handler for the event
  2899. = (object) @Element
  2900. \*/
  2901. /*\
  2902. * Element.unmouseover
  2903. [ method ]
  2904. **
  2905. * Removes event handler for mouseover for the element.
  2906. > Parameters
  2907. - handler (function) #optional handler for the event
  2908. = (object) @Element
  2909. \*/
  2910. /*\
  2911. * Element.mouseup
  2912. [ method ]
  2913. **
  2914. * Adds event handler for mouseup for the element.
  2915. > Parameters
  2916. - handler (function) handler for the event
  2917. = (object) @Element
  2918. \*/
  2919. /*\
  2920. * Element.unmouseup
  2921. [ method ]
  2922. **
  2923. * Removes event handler for mouseup for the element.
  2924. > Parameters
  2925. - handler (function) #optional handler for the event
  2926. = (object) @Element
  2927. \*/
  2928. /*\
  2929. * Element.touchstart
  2930. [ method ]
  2931. **
  2932. * Adds event handler for touchstart for the element.
  2933. > Parameters
  2934. - handler (function) handler for the event
  2935. = (object) @Element
  2936. \*/
  2937. /*\
  2938. * Element.untouchstart
  2939. [ method ]
  2940. **
  2941. * Removes event handler for touchstart for the element.
  2942. > Parameters
  2943. - handler (function) #optional handler for the event
  2944. = (object) @Element
  2945. \*/
  2946. /*\
  2947. * Element.touchmove
  2948. [ method ]
  2949. **
  2950. * Adds event handler for touchmove for the element.
  2951. > Parameters
  2952. - handler (function) handler for the event
  2953. = (object) @Element
  2954. \*/
  2955. /*\
  2956. * Element.untouchmove
  2957. [ method ]
  2958. **
  2959. * Removes event handler for touchmove for the element.
  2960. > Parameters
  2961. - handler (function) #optional handler for the event
  2962. = (object) @Element
  2963. \*/
  2964. /*\
  2965. * Element.touchend
  2966. [ method ]
  2967. **
  2968. * Adds event handler for touchend for the element.
  2969. > Parameters
  2970. - handler (function) handler for the event
  2971. = (object) @Element
  2972. \*/
  2973. /*\
  2974. * Element.untouchend
  2975. [ method ]
  2976. **
  2977. * Removes event handler for touchend for the element.
  2978. > Parameters
  2979. - handler (function) #optional handler for the event
  2980. = (object) @Element
  2981. \*/
  2982. /*\
  2983. * Element.touchcancel
  2984. [ method ]
  2985. **
  2986. * Adds event handler for touchcancel for the element.
  2987. > Parameters
  2988. - handler (function) handler for the event
  2989. = (object) @Element
  2990. \*/
  2991. /*\
  2992. * Element.untouchcancel
  2993. [ method ]
  2994. **
  2995. * Removes event handler for touchcancel for the element.
  2996. > Parameters
  2997. - handler (function) #optional handler for the event
  2998. = (object) @Element
  2999. \*/
  3000. for (var i = events.length; i--;) {
  3001. (function (eventName) {
  3002. R[eventName] = elproto[eventName] = function (fn, scope) {
  3003. if (R.is(fn, "function")) {
  3004. this.events = this.events || [];
  3005. this.events.push({name: eventName, f: fn, unbind: addEvent(this.shape || this.node || g.doc, eventName, fn, scope || this)});
  3006. }
  3007. return this;
  3008. };
  3009. R["un" + eventName] = elproto["un" + eventName] = function (fn) {
  3010. var events = this.events || [],
  3011. l = events.length;
  3012. while (l--){
  3013. if (events[l].name == eventName && (R.is(fn, "undefined") || events[l].f == fn)) {
  3014. events[l].unbind();
  3015. events.splice(l, 1);
  3016. !events.length && delete this.events;
  3017. }
  3018. }
  3019. return this;
  3020. };
  3021. })(events[i]);
  3022. }
  3023. /*\
  3024. * Element.data
  3025. [ method ]
  3026. **
  3027. * Adds or retrieves given value associated with given key.
  3028. **
  3029. * See also @Element.removeData
  3030. > Parameters
  3031. - key (string) key to store data
  3032. - value (any) #optional value to store
  3033. = (object) @Element
  3034. * or, if value is not specified:
  3035. = (any) value
  3036. * or, if key and value are not specified:
  3037. = (object) Key/value pairs for all the data associated with the element.
  3038. > Usage
  3039. | for (var i = 0, i < 5, i++) {
  3040. | paper.circle(10 + 15 * i, 10, 10)
  3041. | .attr({fill: "#000"})
  3042. | .data("i", i)
  3043. | .click(function () {
  3044. | alert(this.data("i"));
  3045. | });
  3046. | }
  3047. \*/
  3048. elproto.data = function (key, value) {
  3049. var data = eldata[this.id] = eldata[this.id] || {};
  3050. if (arguments.length == 0) {
  3051. return data;
  3052. }
  3053. if (arguments.length == 1) {
  3054. if (R.is(key, "object")) {
  3055. for (var i in key) if (key[has](i)) {
  3056. this.data(i, key[i]);
  3057. }
  3058. return this;
  3059. }
  3060. eve("raphael.data.get." + this.id, this, data[key], key);
  3061. return data[key];
  3062. }
  3063. data[key] = value;
  3064. eve("raphael.data.set." + this.id, this, value, key);
  3065. return this;
  3066. };
  3067. /*\
  3068. * Element.removeData
  3069. [ method ]
  3070. **
  3071. * Removes value associated with an element by given key.
  3072. * If key is not provided, removes all the data of the element.
  3073. > Parameters
  3074. - key (string) #optional key
  3075. = (object) @Element
  3076. \*/
  3077. elproto.removeData = function (key) {
  3078. if (key == null) {
  3079. delete eldata[this.id];
  3080. } else {
  3081. eldata[this.id] && delete eldata[this.id][key];
  3082. }
  3083. return this;
  3084. };
  3085. /*\
  3086. * Element.getData
  3087. [ method ]
  3088. **
  3089. * Retrieves the element data
  3090. = (object) data
  3091. \*/
  3092. elproto.getData = function () {
  3093. return clone(eldata[this.id] || {});
  3094. };
  3095. /*\
  3096. * Element.hover
  3097. [ method ]
  3098. **
  3099. * Adds event handlers for hover for the element.
  3100. > Parameters
  3101. - f_in (function) handler for hover in
  3102. - f_out (function) handler for hover out
  3103. - icontext (object) #optional context for hover in handler
  3104. - ocontext (object) #optional context for hover out handler
  3105. = (object) @Element
  3106. \*/
  3107. elproto.hover = function (f_in, f_out, scope_in, scope_out) {
  3108. return this.mouseover(f_in, scope_in).mouseout(f_out, scope_out || scope_in);
  3109. };
  3110. /*\
  3111. * Element.unhover
  3112. [ method ]
  3113. **
  3114. * Removes event handlers for hover for the element.
  3115. > Parameters
  3116. - f_in (function) handler for hover in
  3117. - f_out (function) handler for hover out
  3118. = (object) @Element
  3119. \*/
  3120. elproto.unhover = function (f_in, f_out) {
  3121. return this.unmouseover(f_in).unmouseout(f_out);
  3122. };
  3123. var draggable = [];
  3124. /*\
  3125. * Element.drag
  3126. [ method ]
  3127. **
  3128. * Adds event handlers for drag of the element.
  3129. > Parameters
  3130. - onmove (function) handler for moving
  3131. - onstart (function) handler for drag start
  3132. - onend (function) handler for drag end
  3133. - mcontext (object) #optional context for moving handler
  3134. - scontext (object) #optional context for drag start handler
  3135. - econtext (object) #optional context for drag end handler
  3136. * Additionally following `drag` events will be triggered: `drag.start.<id>` on start,
  3137. * `drag.end.<id>` on end and `drag.move.<id>` on every move. When element will be dragged over another element
  3138. * `drag.over.<id>` will be fired as well.
  3139. *
  3140. * Start event and start handler will be called in specified context or in context of the element with following parameters:
  3141. o x (number) x position of the mouse
  3142. o y (number) y position of the mouse
  3143. o event (object) DOM event object
  3144. * Move event and move handler will be called in specified context or in context of the element with following parameters:
  3145. o dx (number) shift by x from the start point
  3146. o dy (number) shift by y from the start point
  3147. o x (number) x position of the mouse
  3148. o y (number) y position of the mouse
  3149. o event (object) DOM event object
  3150. * End event and end handler will be called in specified context or in context of the element with following parameters:
  3151. o event (object) DOM event object
  3152. = (object) @Element
  3153. \*/
  3154. elproto.drag = function (onmove, onstart, onend, move_scope, start_scope, end_scope) {
  3155. function start(e) {
  3156. (e.originalEvent || e).preventDefault();
  3157. var x = e.clientX,
  3158. y = e.clientY,
  3159. scrollY = g.doc.documentElement.scrollTop || g.doc.body.scrollTop,
  3160. scrollX = g.doc.documentElement.scrollLeft || g.doc.body.scrollLeft;
  3161. this._drag.id = e.identifier;
  3162. if (supportsTouch && e.touches) {
  3163. var i = e.touches.length, touch;
  3164. while (i--) {
  3165. touch = e.touches[i];
  3166. this._drag.id = touch.identifier;
  3167. if (touch.identifier == this._drag.id) {
  3168. x = touch.clientX;
  3169. y = touch.clientY;
  3170. break;
  3171. }
  3172. }
  3173. }
  3174. this._drag.x = x + scrollX;
  3175. this._drag.y = y + scrollY;
  3176. !drag.length && R.mousemove(dragMove).mouseup(dragUp);
  3177. drag.push({el: this, move_scope: move_scope, start_scope: start_scope, end_scope: end_scope});
  3178. onstart && eve.on("raphael.drag.start." + this.id, onstart);
  3179. onmove && eve.on("raphael.drag.move." + this.id, onmove);
  3180. onend && eve.on("raphael.drag.end." + this.id, onend);
  3181. eve("raphael.drag.start." + this.id, start_scope || move_scope || this, this._drag.x, this._drag.y, e);
  3182. }
  3183. this._drag = {};
  3184. draggable.push({el: this, start: start});
  3185. this.mousedown(start);
  3186. return this;
  3187. };
  3188. /*\
  3189. * Element.onDragOver
  3190. [ method ]
  3191. **
  3192. * Shortcut for assigning event handler for `drag.over.<id>` event, where id is id of the element (see @Element.id).
  3193. > Parameters
  3194. - f (function) handler for event, first argument would be the element you are dragging over
  3195. \*/
  3196. elproto.onDragOver = function (f) {
  3197. f ? eve.on("raphael.drag.over." + this.id, f) : eve.unbind("raphael.drag.over." + this.id);
  3198. };
  3199. /*\
  3200. * Element.undrag
  3201. [ method ]
  3202. **
  3203. * Removes all drag event handlers from given element.
  3204. \*/
  3205. elproto.undrag = function () {
  3206. var i = draggable.length;
  3207. while (i--) if (draggable[i].el == this) {
  3208. this.unmousedown(draggable[i].start);
  3209. draggable.splice(i, 1);
  3210. eve.unbind("raphael.drag.*." + this.id);
  3211. }
  3212. !draggable.length && R.unmousemove(dragMove).unmouseup(dragUp);
  3213. drag = [];
  3214. };
  3215. /*\
  3216. * Paper.circle
  3217. [ method ]
  3218. **
  3219. * Draws a circle.
  3220. **
  3221. > Parameters
  3222. **
  3223. - x (number) x coordinate of the centre
  3224. - y (number) y coordinate of the centre
  3225. - r (number) radius
  3226. = (object) Raphaël element object with type “circle”
  3227. **
  3228. > Usage
  3229. | var c = paper.circle(50, 50, 40);
  3230. \*/
  3231. paperproto.circle = function (x, y, r) {
  3232. var out = R._engine.circle(this, x || 0, y || 0, r || 0);
  3233. this.__set__ && this.__set__.push(out);
  3234. return out;
  3235. };
  3236. /*\
  3237. * Paper.rect
  3238. [ method ]
  3239. *
  3240. * Draws a rectangle.
  3241. **
  3242. > Parameters
  3243. **
  3244. - x (number) x coordinate of the top left corner
  3245. - y (number) y coordinate of the top left corner
  3246. - width (number) width
  3247. - height (number) height
  3248. - r (number) #optional radius for rounded corners, default is 0
  3249. = (object) Raphaël element object with type “rect”
  3250. **
  3251. > Usage
  3252. | // regular rectangle
  3253. | var c = paper.rect(10, 10, 50, 50);
  3254. | // rectangle with rounded corners
  3255. | var c = paper.rect(40, 40, 50, 50, 10);
  3256. \*/
  3257. paperproto.rect = function (x, y, w, h, r) {
  3258. var out = R._engine.rect(this, x || 0, y || 0, w || 0, h || 0, r || 0);
  3259. this.__set__ && this.__set__.push(out);
  3260. return out;
  3261. };
  3262. /*\
  3263. * Paper.ellipse
  3264. [ method ]
  3265. **
  3266. * Draws an ellipse.
  3267. **
  3268. > Parameters
  3269. **
  3270. - x (number) x coordinate of the centre
  3271. - y (number) y coordinate of the centre
  3272. - rx (number) horizontal radius
  3273. - ry (number) vertical radius
  3274. = (object) Raphaël element object with type “ellipse”
  3275. **
  3276. > Usage
  3277. | var c = paper.ellipse(50, 50, 40, 20);
  3278. \*/
  3279. paperproto.ellipse = function (x, y, rx, ry) {
  3280. var out = R._engine.ellipse(this, x || 0, y || 0, rx || 0, ry || 0);
  3281. this.__set__ && this.__set__.push(out);
  3282. return out;
  3283. };
  3284. /*\
  3285. * Paper.path
  3286. [ method ]
  3287. **
  3288. * Creates a path element by given path data string.
  3289. > Parameters
  3290. - pathString (string) #optional path string in SVG format.
  3291. * Path string consists of one-letter commands, followed by comma seprarated arguments in numercal form. Example:
  3292. | "M10,20L30,40"
  3293. * Here we can see two commands: “M”, with arguments `(10, 20)` and “L” with arguments `(30, 40)`. Upper case letter mean command is absolute, lower case—relative.
  3294. *
  3295. # <p>Here is short list of commands available, for more details see <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 path string format</a>.</p>
  3296. # <table><thead><tr><th>Command</th><th>Name</th><th>Parameters</th></tr></thead><tbody>
  3297. # <tr><td>M</td><td>moveto</td><td>(x y)+</td></tr>
  3298. # <tr><td>Z</td><td>closepath</td><td>(none)</td></tr>
  3299. # <tr><td>L</td><td>lineto</td><td>(x y)+</td></tr>
  3300. # <tr><td>H</td><td>horizontal lineto</td><td>x+</td></tr>
  3301. # <tr><td>V</td><td>vertical lineto</td><td>y+</td></tr>
  3302. # <tr><td>C</td><td>curveto</td><td>(x1 y1 x2 y2 x y)+</td></tr>
  3303. # <tr><td>S</td><td>smooth curveto</td><td>(x2 y2 x y)+</td></tr>
  3304. # <tr><td>Q</td><td>quadratic Bézier curveto</td><td>(x1 y1 x y)+</td></tr>
  3305. # <tr><td>T</td><td>smooth quadratic Bézier curveto</td><td>(x y)+</td></tr>
  3306. # <tr><td>A</td><td>elliptical arc</td><td>(rx ry x-axis-rotation large-arc-flag sweep-flag x y)+</td></tr>
  3307. # <tr><td>R</td><td><a href="http://en.wikipedia.org/wiki/Catmull–Rom_spline#Catmull.E2.80.93Rom_spline">Catmull-Rom curveto</a>*</td><td>x1 y1 (x y)+</td></tr></tbody></table>
  3308. * * “Catmull-Rom curveto” is a not standard SVG command and added in 2.0 to make life easier.
  3309. * Note: there is a special case when path consist of just three commands: “M10,10R…z”. In this case path will smoothly connects to its beginning.
  3310. > Usage
  3311. | var c = paper.path("M10 10L90 90");
  3312. | // draw a diagonal line:
  3313. | // move to 10,10, line to 90,90
  3314. * For example of path strings, check out these icons: http://raphaeljs.com/icons/
  3315. \*/
  3316. paperproto.path = function (pathString) {
  3317. pathString && !R.is(pathString, string) && !R.is(pathString[0], array) && (pathString += E);
  3318. var out = R._engine.path(R.format[apply](R, arguments), this);
  3319. this.__set__ && this.__set__.push(out);
  3320. return out;
  3321. };
  3322. /*\
  3323. * Paper.image
  3324. [ method ]
  3325. **
  3326. * Embeds an image into the surface.
  3327. **
  3328. > Parameters
  3329. **
  3330. - src (string) URI of the source image
  3331. - x (number) x coordinate position
  3332. - y (number) y coordinate position
  3333. - width (number) width of the image
  3334. - height (number) height of the image
  3335. = (object) Raphaël element object with type “image”
  3336. **
  3337. > Usage
  3338. | var c = paper.image("apple.png", 10, 10, 80, 80);
  3339. \*/
  3340. paperproto.image = function (src, x, y, w, h) {
  3341. var out = R._engine.image(this, src || "about:blank", x || 0, y || 0, w || 0, h || 0);
  3342. this.__set__ && this.__set__.push(out);
  3343. return out;
  3344. };
  3345. /*\
  3346. * Paper.text
  3347. [ method ]
  3348. **
  3349. * Draws a text string. If you need line breaks, put “\n” in the string.
  3350. **
  3351. > Parameters
  3352. **
  3353. - x (number) x coordinate position
  3354. - y (number) y coordinate position
  3355. - text (string) The text string to draw
  3356. = (object) Raphaël element object with type “text”
  3357. **
  3358. > Usage
  3359. | var t = paper.text(50, 50, "Raphaël\nkicks\nbutt!");
  3360. \*/
  3361. paperproto.text = function (x, y, text) {
  3362. var out = R._engine.text(this, x || 0, y || 0, Str(text));
  3363. this.__set__ && this.__set__.push(out);
  3364. return out;
  3365. };
  3366. /*\
  3367. * Paper.set
  3368. [ method ]
  3369. **
  3370. * Creates array-like object to keep and operate several elements at once.
  3371. * Warning: it doesn’t create any elements for itself in the page, it just groups existing elements.
  3372. * Sets act as pseudo elements — all methods available to an element can be used on a set.
  3373. = (object) array-like object that represents set of elements
  3374. **
  3375. > Usage
  3376. | var st = paper.set();
  3377. | st.push(
  3378. | paper.circle(10, 10, 5),
  3379. | paper.circle(30, 10, 5)
  3380. | );
  3381. | st.attr({fill: "red"}); // changes the fill of both circles
  3382. \*/
  3383. paperproto.set = function (itemsArray) {
  3384. !R.is(itemsArray, "array") && (itemsArray = Array.prototype.splice.call(arguments, 0, arguments.length));
  3385. var out = new Set(itemsArray);
  3386. this.__set__ && this.__set__.push(out);
  3387. out["paper"] = this;
  3388. out["type"] = "set";
  3389. return out;
  3390. };
  3391. /*\
  3392. * Paper.setStart
  3393. [ method ]
  3394. **
  3395. * Creates @Paper.set. All elements that will be created after calling this method and before calling
  3396. * @Paper.setFinish will be added to the set.
  3397. **
  3398. > Usage
  3399. | paper.setStart();
  3400. | paper.circle(10, 10, 5),
  3401. | paper.circle(30, 10, 5)
  3402. | var st = paper.setFinish();
  3403. | st.attr({fill: "red"}); // changes the fill of both circles
  3404. \*/
  3405. paperproto.setStart = function (set) {
  3406. this.__set__ = set || this.set();
  3407. };
  3408. /*\
  3409. * Paper.setFinish
  3410. [ method ]
  3411. **
  3412. * See @Paper.setStart. This method finishes catching and returns resulting set.
  3413. **
  3414. = (object) set
  3415. \*/
  3416. paperproto.setFinish = function (set) {
  3417. var out = this.__set__;
  3418. delete this.__set__;
  3419. return out;
  3420. };
  3421. /*\
  3422. * Paper.getSize
  3423. [ method ]
  3424. **
  3425. * Obtains current paper actual size.
  3426. **
  3427. = (object)
  3428. \*/
  3429. paperproto.getSize = function () {
  3430. var container = this.canvas.parentNode;
  3431. return {
  3432. width: container.offsetWidth,
  3433. height: container.offsetHeight
  3434. };
  3435. };
  3436. /*\
  3437. * Paper.setSize
  3438. [ method ]
  3439. **
  3440. * If you need to change dimensions of the canvas call this method
  3441. **
  3442. > Parameters
  3443. **
  3444. - width (number) new width of the canvas
  3445. - height (number) new height of the canvas
  3446. \*/
  3447. paperproto.setSize = function (width, height) {
  3448. return R._engine.setSize.call(this, width, height);
  3449. };
  3450. /*\
  3451. * Paper.setViewBox
  3452. [ method ]
  3453. **
  3454. * Sets the view box of the paper. Practically it gives you ability to zoom and pan whole paper surface by
  3455. * specifying new boundaries.
  3456. **
  3457. > Parameters
  3458. **
  3459. - x (number) new x position, default is `0`
  3460. - y (number) new y position, default is `0`
  3461. - w (number) new width of the canvas
  3462. - h (number) new height of the canvas
  3463. - fit (boolean) `true` if you want graphics to fit into new boundary box
  3464. \*/
  3465. paperproto.setViewBox = function (x, y, w, h, fit) {
  3466. return R._engine.setViewBox.call(this, x, y, w, h, fit);
  3467. };
  3468. /*\
  3469. * Paper.top
  3470. [ property ]
  3471. **
  3472. * Points to the topmost element on the paper
  3473. \*/
  3474. /*\
  3475. * Paper.bottom
  3476. [ property ]
  3477. **
  3478. * Points to the bottom element on the paper
  3479. \*/
  3480. paperproto.top = paperproto.bottom = null;
  3481. /*\
  3482. * Paper.raphael
  3483. [ property ]
  3484. **
  3485. * Points to the @Raphael object/function
  3486. \*/
  3487. paperproto.raphael = R;
  3488. var getOffset = function (elem) {
  3489. var box = elem.getBoundingClientRect(),
  3490. doc = elem.ownerDocument,
  3491. body = doc.body,
  3492. docElem = doc.documentElement,
  3493. clientTop = docElem.clientTop || body.clientTop || 0, clientLeft = docElem.clientLeft || body.clientLeft || 0,
  3494. top = box.top + (g.win.pageYOffset || docElem.scrollTop || body.scrollTop ) - clientTop,
  3495. left = box.left + (g.win.pageXOffset || docElem.scrollLeft || body.scrollLeft) - clientLeft;
  3496. return {
  3497. y: top,
  3498. x: left
  3499. };
  3500. };
  3501. /*\
  3502. * Paper.getElementByPoint
  3503. [ method ]
  3504. **
  3505. * Returns you topmost element under given point.
  3506. **
  3507. = (object) Raphaël element object
  3508. > Parameters
  3509. **
  3510. - x (number) x coordinate from the top left corner of the window
  3511. - y (number) y coordinate from the top left corner of the window
  3512. > Usage
  3513. | paper.getElementByPoint(mouseX, mouseY).attr({stroke: "#f00"});
  3514. \*/
  3515. paperproto.getElementByPoint = function (x, y) {
  3516. var paper = this,
  3517. svg = paper.canvas,
  3518. target = g.doc.elementFromPoint(x, y);
  3519. if (g.win.opera && target.tagName == "svg") {
  3520. var so = getOffset(svg),
  3521. sr = svg.createSVGRect();
  3522. sr.x = x - so.x;
  3523. sr.y = y - so.y;
  3524. sr.width = sr.height = 1;
  3525. var hits = svg.getIntersectionList(sr, null);
  3526. if (hits.length) {
  3527. target = hits[hits.length - 1];
  3528. }
  3529. }
  3530. if (!target) {
  3531. return null;
  3532. }
  3533. while (target.parentNode && target != svg.parentNode && !target.raphael) {
  3534. target = target.parentNode;
  3535. }
  3536. target == paper.canvas.parentNode && (target = svg);
  3537. target = target && target.raphael ? paper.getById(target.raphaelid) : null;
  3538. return target;
  3539. };
  3540. /*\
  3541. * Paper.getElementsByBBox
  3542. [ method ]
  3543. **
  3544. * Returns set of elements that have an intersecting bounding box
  3545. **
  3546. > Parameters
  3547. **
  3548. - bbox (object) bbox to check with
  3549. = (object) @Set
  3550. \*/
  3551. paperproto.getElementsByBBox = function (bbox) {
  3552. var set = this.set();
  3553. this.forEach(function (el) {
  3554. if (R.isBBoxIntersect(el.getBBox(), bbox)) {
  3555. set.push(el);
  3556. }
  3557. });
  3558. return set;
  3559. };
  3560. /*\
  3561. * Paper.getById
  3562. [ method ]
  3563. **
  3564. * Returns you element by its internal ID.
  3565. **
  3566. > Parameters
  3567. **
  3568. - id (number) id
  3569. = (object) Raphaël element object
  3570. \*/
  3571. paperproto.getById = function (id) {
  3572. var bot = this.bottom;
  3573. while (bot) {
  3574. if (bot.id == id) {
  3575. return bot;
  3576. }
  3577. bot = bot.next;
  3578. }
  3579. return null;
  3580. };
  3581. /*\
  3582. * Paper.forEach
  3583. [ method ]
  3584. **
  3585. * Executes given function for each element on the paper
  3586. *
  3587. * If callback function returns `false` it will stop loop running.
  3588. **
  3589. > Parameters
  3590. **
  3591. - callback (function) function to run
  3592. - thisArg (object) context object for the callback
  3593. = (object) Paper object
  3594. > Usage
  3595. | paper.forEach(function (el) {
  3596. | el.attr({ stroke: "blue" });
  3597. | });
  3598. \*/
  3599. paperproto.forEach = function (callback, thisArg) {
  3600. var bot = this.bottom;
  3601. while (bot) {
  3602. if (callback.call(thisArg, bot) === false) {
  3603. return this;
  3604. }
  3605. bot = bot.next;
  3606. }
  3607. return this;
  3608. };
  3609. /*\
  3610. * Paper.getElementsByPoint
  3611. [ method ]
  3612. **
  3613. * Returns set of elements that have common point inside
  3614. **
  3615. > Parameters
  3616. **
  3617. - x (number) x coordinate of the point
  3618. - y (number) y coordinate of the point
  3619. = (object) @Set
  3620. \*/
  3621. paperproto.getElementsByPoint = function (x, y) {
  3622. var set = this.set();
  3623. this.forEach(function (el) {
  3624. if (el.isPointInside(x, y)) {
  3625. set.push(el);
  3626. }
  3627. });
  3628. return set;
  3629. };
  3630. function x_y() {
  3631. return this.x + S + this.y;
  3632. }
  3633. function x_y_w_h() {
  3634. return this.x + S + this.y + S + this.width + " \xd7 " + this.height;
  3635. }
  3636. /*\
  3637. * Element.isPointInside
  3638. [ method ]
  3639. **
  3640. * Determine if given point is inside this element’s shape
  3641. **
  3642. > Parameters
  3643. **
  3644. - x (number) x coordinate of the point
  3645. - y (number) y coordinate of the point
  3646. = (boolean) `true` if point inside the shape
  3647. \*/
  3648. elproto.isPointInside = function (x, y) {
  3649. var rp = this.realPath = getPath[this.type](this);
  3650. if (this.attr('transform') && this.attr('transform').length) {
  3651. rp = R.transformPath(rp, this.attr('transform'));
  3652. }
  3653. return R.isPointInsidePath(rp, x, y);
  3654. };
  3655. /*\
  3656. * Element.getBBox
  3657. [ method ]
  3658. **
  3659. * Return bounding box for a given element
  3660. **
  3661. > Parameters
  3662. **
  3663. - isWithoutTransform (boolean) flag, `true` if you want to have bounding box before transformations. Default is `false`.
  3664. = (object) Bounding box object:
  3665. o {
  3666. o x: (number) top left corner x
  3667. o y: (number) top left corner y
  3668. o x2: (number) bottom right corner x
  3669. o y2: (number) bottom right corner y
  3670. o width: (number) width
  3671. o height: (number) height
  3672. o }
  3673. \*/
  3674. elproto.getBBox = function (isWithoutTransform) {
  3675. if (this.removed) {
  3676. return {};
  3677. }
  3678. var _ = this._;
  3679. if (isWithoutTransform) {
  3680. if (_.dirty || !_.bboxwt) {
  3681. this.realPath = getPath[this.type](this);
  3682. _.bboxwt = pathDimensions(this.realPath);
  3683. _.bboxwt.toString = x_y_w_h;
  3684. _.dirty = 0;
  3685. }
  3686. return _.bboxwt;
  3687. }
  3688. if (_.dirty || _.dirtyT || !_.bbox) {
  3689. if (_.dirty || !this.realPath) {
  3690. _.bboxwt = 0;
  3691. this.realPath = getPath[this.type](this);
  3692. }
  3693. _.bbox = pathDimensions(mapPath(this.realPath, this.matrix));
  3694. _.bbox.toString = x_y_w_h;
  3695. _.dirty = _.dirtyT = 0;
  3696. }
  3697. return _.bbox;
  3698. };
  3699. /*\
  3700. * Element.clone
  3701. [ method ]
  3702. **
  3703. = (object) clone of a given element
  3704. **
  3705. \*/
  3706. elproto.clone = function () {
  3707. if (this.removed) {
  3708. return null;
  3709. }
  3710. var out = this.paper[this.type]().attr(this.attr());
  3711. this.__set__ && this.__set__.push(out);
  3712. return out;
  3713. };
  3714. /*\
  3715. * Element.glow
  3716. [ method ]
  3717. **
  3718. * Return set of elements that create glow-like effect around given element. See @Paper.set.
  3719. *
  3720. * Note: Glow is not connected to the element. If you change element attributes it won’t adjust itself.
  3721. **
  3722. > Parameters
  3723. **
  3724. - glow (object) #optional parameters object with all properties optional:
  3725. o {
  3726. o width (number) size of the glow, default is `10`
  3727. o fill (boolean) will it be filled, default is `false`
  3728. o opacity (number) opacity, default is `0.5`
  3729. o offsetx (number) horizontal offset, default is `0`
  3730. o offsety (number) vertical offset, default is `0`
  3731. o color (string) glow colour, default is `black`
  3732. o }
  3733. = (object) @Paper.set of elements that represents glow
  3734. \*/
  3735. elproto.glow = function (glow) {
  3736. if (this.type == "text") {
  3737. return null;
  3738. }
  3739. glow = glow || {};
  3740. var s = {
  3741. width: (glow.width || 10) + (+this.attr("stroke-width") || 1),
  3742. fill: glow.fill || false,
  3743. opacity: glow.opacity == null ? .5 : glow.opacity,
  3744. offsetx: glow.offsetx || 0,
  3745. offsety: glow.offsety || 0,
  3746. color: glow.color || "#000"
  3747. },
  3748. c = s.width / 2,
  3749. r = this.paper,
  3750. out = r.set(),
  3751. path = this.realPath || getPath[this.type](this);
  3752. path = this.matrix ? mapPath(path, this.matrix) : path;
  3753. for (var i = 1; i < c + 1; i++) {
  3754. out.push(r.path(path).attr({
  3755. stroke: s.color,
  3756. fill: s.fill ? s.color : "none",
  3757. "stroke-linejoin": "round",
  3758. "stroke-linecap": "round",
  3759. "stroke-width": +(s.width / c * i).toFixed(3),
  3760. opacity: +(s.opacity / c).toFixed(3)
  3761. }));
  3762. }
  3763. return out.insertBefore(this).translate(s.offsetx, s.offsety);
  3764. };
  3765. var curveslengths = {},
  3766. getPointAtSegmentLength = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, length) {
  3767. if (length == null) {
  3768. return bezlen(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y);
  3769. } else {
  3770. return R.findDotsAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, getTatLen(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, length));
  3771. }
  3772. },
  3773. getLengthFactory = function (istotal, subpath) {
  3774. return function (path, length, onlystart) {
  3775. path = path2curve(path);
  3776. var x, y, p, l, sp = "", subpaths = {}, point,
  3777. len = 0;
  3778. for (var i = 0, ii = path.length; i < ii; i++) {
  3779. p = path[i];
  3780. if (p[0] == "M") {
  3781. x = +p[1];
  3782. y = +p[2];
  3783. } else {
  3784. l = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
  3785. if (len + l > length) {
  3786. if (subpath && !subpaths.start) {
  3787. point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
  3788. sp += ["C" + point.start.x, point.start.y, point.m.x, point.m.y, point.x, point.y];
  3789. if (onlystart) {return sp;}
  3790. subpaths.start = sp;
  3791. sp = ["M" + point.x, point.y + "C" + point.n.x, point.n.y, point.end.x, point.end.y, p[5], p[6]].join();
  3792. len += l;
  3793. x = +p[5];
  3794. y = +p[6];
  3795. continue;
  3796. }
  3797. if (!istotal && !subpath) {
  3798. point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
  3799. return {x: point.x, y: point.y, alpha: point.alpha};
  3800. }
  3801. }
  3802. len += l;
  3803. x = +p[5];
  3804. y = +p[6];
  3805. }
  3806. sp += p.shift() + p;
  3807. }
  3808. subpaths.end = sp;
  3809. point = istotal ? len : subpath ? subpaths : R.findDotsAtSegment(x, y, p[0], p[1], p[2], p[3], p[4], p[5], 1);
  3810. point.alpha && (point = {x: point.x, y: point.y, alpha: point.alpha});
  3811. return point;
  3812. };
  3813. };
  3814. var getTotalLength = getLengthFactory(1),
  3815. getPointAtLength = getLengthFactory(),
  3816. getSubpathsAtLength = getLengthFactory(0, 1);
  3817. /*\
  3818. * Raphael.getTotalLength
  3819. [ method ]
  3820. **
  3821. * Returns length of the given path in pixels.
  3822. **
  3823. > Parameters
  3824. **
  3825. - path (string) SVG path string.
  3826. **
  3827. = (number) length.
  3828. \*/
  3829. R.getTotalLength = getTotalLength;
  3830. /*\
  3831. * Raphael.getPointAtLength
  3832. [ method ]
  3833. **
  3834. * Return coordinates of the point located at the given length on the given path.
  3835. **
  3836. > Parameters
  3837. **
  3838. - path (string) SVG path string
  3839. - length (number)
  3840. **
  3841. = (object) representation of the point:
  3842. o {
  3843. o x: (number) x coordinate
  3844. o y: (number) y coordinate
  3845. o alpha: (number) angle of derivative
  3846. o }
  3847. \*/
  3848. R.getPointAtLength = getPointAtLength;
  3849. /*\
  3850. * Raphael.getSubpath
  3851. [ method ]
  3852. **
  3853. * Return subpath of a given path from given length to given length.
  3854. **
  3855. > Parameters
  3856. **
  3857. - path (string) SVG path string
  3858. - from (number) position of the start of the segment
  3859. - to (number) position of the end of the segment
  3860. **
  3861. = (string) pathstring for the segment
  3862. \*/
  3863. R.getSubpath = function (path, from, to) {
  3864. if (this.getTotalLength(path) - to < 1e-6) {
  3865. return getSubpathsAtLength(path, from).end;
  3866. }
  3867. var a = getSubpathsAtLength(path, to, 1);
  3868. return from ? getSubpathsAtLength(a, from).end : a;
  3869. };
  3870. /*\
  3871. * Element.getTotalLength
  3872. [ method ]
  3873. **
  3874. * Returns length of the path in pixels. Only works for element of “path” type.
  3875. = (number) length.
  3876. \*/
  3877. elproto.getTotalLength = function () {
  3878. var path = this.getPath();
  3879. if (!path) {
  3880. return;
  3881. }
  3882. if (this.node.getTotalLength) {
  3883. return this.node.getTotalLength();
  3884. }
  3885. return getTotalLength(path);
  3886. };
  3887. /*\
  3888. * Element.getPointAtLength
  3889. [ method ]
  3890. **
  3891. * Return coordinates of the point located at the given length on the given path. Only works for element of “path” type.
  3892. **
  3893. > Parameters
  3894. **
  3895. - length (number)
  3896. **
  3897. = (object) representation of the point:
  3898. o {
  3899. o x: (number) x coordinate
  3900. o y: (number) y coordinate
  3901. o alpha: (number) angle of derivative
  3902. o }
  3903. \*/
  3904. elproto.getPointAtLength = function (length) {
  3905. var path = this.getPath();
  3906. if (!path) {
  3907. return;
  3908. }
  3909. return getPointAtLength(path, length);
  3910. };
  3911. /*\
  3912. * Element.getPath
  3913. [ method ]
  3914. **
  3915. * Returns path of the element. Only works for elements of “path” type and simple elements like circle.
  3916. = (object) path
  3917. **
  3918. \*/
  3919. elproto.getPath = function () {
  3920. var path,
  3921. getPath = R._getPath[this.type];
  3922. if (this.type == "text" || this.type == "set") {
  3923. return;
  3924. }
  3925. if (getPath) {
  3926. path = getPath(this);
  3927. }
  3928. return path;
  3929. };
  3930. /*\
  3931. * Element.getSubpath
  3932. [ method ]
  3933. **
  3934. * Return subpath of a given element from given length to given length. Only works for element of “path” type.
  3935. **
  3936. > Parameters
  3937. **
  3938. - from (number) position of the start of the segment
  3939. - to (number) position of the end of the segment
  3940. **
  3941. = (string) pathstring for the segment
  3942. \*/
  3943. elproto.getSubpath = function (from, to) {
  3944. var path = this.getPath();
  3945. if (!path) {
  3946. return;
  3947. }
  3948. return R.getSubpath(path, from, to);
  3949. };
  3950. /*\
  3951. * Raphael.easing_formulas
  3952. [ property ]
  3953. **
  3954. * Object that contains easing formulas for animation. You could extend it with your own. By default it has following list of easing:
  3955. # <ul>
  3956. # <li>“linear”</li>
  3957. # <li>“&lt;” or “easeIn” or “ease-in”</li>
  3958. # <li>“>” or “easeOut” or “ease-out”</li>
  3959. # <li>“&lt;>” or “easeInOut” or “ease-in-out”</li>
  3960. # <li>“backIn” or “back-in”</li>
  3961. # <li>“backOut” or “back-out”</li>
  3962. # <li>“elastic”</li>
  3963. # <li>“bounce”</li>
  3964. # </ul>
  3965. # <p>See also <a href="http://raphaeljs.com/easing.html">Easing demo</a>.</p>
  3966. \*/
  3967. var ef = R.easing_formulas = {
  3968. linear: function (n) {
  3969. return n;
  3970. },
  3971. "<": function (n) {
  3972. return pow(n, 1.7);
  3973. },
  3974. ">": function (n) {
  3975. return pow(n, .48);
  3976. },
  3977. "<>": function (n) {
  3978. var q = .48 - n / 1.04,
  3979. Q = math.sqrt(.1734 + q * q),
  3980. x = Q - q,
  3981. X = pow(abs(x), 1 / 3) * (x < 0 ? -1 : 1),
  3982. y = -Q - q,
  3983. Y = pow(abs(y), 1 / 3) * (y < 0 ? -1 : 1),
  3984. t = X + Y + .5;
  3985. return (1 - t) * 3 * t * t + t * t * t;
  3986. },
  3987. backIn: function (n) {
  3988. var s = 1.70158;
  3989. return n * n * ((s + 1) * n - s);
  3990. },
  3991. backOut: function (n) {
  3992. n = n - 1;
  3993. var s = 1.70158;
  3994. return n * n * ((s + 1) * n + s) + 1;
  3995. },
  3996. elastic: function (n) {
  3997. if (n == !!n) {
  3998. return n;
  3999. }
  4000. return pow(2, -10 * n) * math.sin((n - .075) * (2 * PI) / .3) + 1;
  4001. },
  4002. bounce: function (n) {
  4003. var s = 7.5625,
  4004. p = 2.75,
  4005. l;
  4006. if (n < (1 / p)) {
  4007. l = s * n * n;
  4008. } else {
  4009. if (n < (2 / p)) {
  4010. n -= (1.5 / p);
  4011. l = s * n * n + .75;
  4012. } else {
  4013. if (n < (2.5 / p)) {
  4014. n -= (2.25 / p);
  4015. l = s * n * n + .9375;
  4016. } else {
  4017. n -= (2.625 / p);
  4018. l = s * n * n + .984375;
  4019. }
  4020. }
  4021. }
  4022. return l;
  4023. }
  4024. };
  4025. ef.easeIn = ef["ease-in"] = ef["<"];
  4026. ef.easeOut = ef["ease-out"] = ef[">"];
  4027. ef.easeInOut = ef["ease-in-out"] = ef["<>"];
  4028. ef["back-in"] = ef.backIn;
  4029. ef["back-out"] = ef.backOut;
  4030. var animationElements = [],
  4031. requestAnimFrame = window.requestAnimationFrame ||
  4032. window.webkitRequestAnimationFrame ||
  4033. window.mozRequestAnimationFrame ||
  4034. window.oRequestAnimationFrame ||
  4035. window.msRequestAnimationFrame ||
  4036. function (callback) {
  4037. setTimeout(callback, 16);
  4038. },
  4039. animation = function () {
  4040. var Now = +new Date,
  4041. l = 0;
  4042. for (; l < animationElements.length; l++) {
  4043. var e = animationElements[l];
  4044. if (e.el.removed || e.paused) {
  4045. continue;
  4046. }
  4047. var time = Now - e.start,
  4048. ms = e.ms,
  4049. easing = e.easing,
  4050. from = e.from,
  4051. diff = e.diff,
  4052. to = e.to,
  4053. t = e.t,
  4054. that = e.el,
  4055. set = {},
  4056. now,
  4057. init = {},
  4058. key;
  4059. if (e.initstatus) {
  4060. time = (e.initstatus * e.anim.top - e.prev) / (e.percent - e.prev) * ms;
  4061. e.status = e.initstatus;
  4062. delete e.initstatus;
  4063. e.stop && animationElements.splice(l--, 1);
  4064. } else {
  4065. e.status = (e.prev + (e.percent - e.prev) * (time / ms)) / e.anim.top;
  4066. }
  4067. if (time < 0) {
  4068. continue;
  4069. }
  4070. if (time < ms) {
  4071. var pos = easing(time / ms);
  4072. for (var attr in from) if (from[has](attr)) {
  4073. switch (availableAnimAttrs[attr]) {
  4074. case nu:
  4075. now = +from[attr] + pos * ms * diff[attr];
  4076. break;
  4077. case "colour":
  4078. now = "rgb(" + [
  4079. upto255(round(from[attr].r + pos * ms * diff[attr].r)),
  4080. upto255(round(from[attr].g + pos * ms * diff[attr].g)),
  4081. upto255(round(from[attr].b + pos * ms * diff[attr].b))
  4082. ].join(",") + ")";
  4083. break;
  4084. case "path":
  4085. now = [];
  4086. for (var i = 0, ii = from[attr].length; i < ii; i++) {
  4087. now[i] = [from[attr][i][0]];
  4088. for (var j = 1, jj = from[attr][i].length; j < jj; j++) {
  4089. now[i][j] = +from[attr][i][j] + pos * ms * diff[attr][i][j];
  4090. }
  4091. now[i] = now[i].join(S);
  4092. }
  4093. now = now.join(S);
  4094. break;
  4095. case "transform":
  4096. if (diff[attr].real) {
  4097. now = [];
  4098. for (i = 0, ii = from[attr].length; i < ii; i++) {
  4099. now[i] = [from[attr][i][0]];
  4100. for (j = 1, jj = from[attr][i].length; j < jj; j++) {
  4101. now[i][j] = from[attr][i][j] + pos * ms * diff[attr][i][j];
  4102. }
  4103. }
  4104. } else {
  4105. var get = function (i) {
  4106. return +from[attr][i] + pos * ms * diff[attr][i];
  4107. };
  4108. // now = [["r", get(2), 0, 0], ["t", get(3), get(4)], ["s", get(0), get(1), 0, 0]];
  4109. now = [["m", get(0), get(1), get(2), get(3), get(4), get(5)]];
  4110. }
  4111. break;
  4112. case "csv":
  4113. if (attr == "clip-rect") {
  4114. now = [];
  4115. i = 4;
  4116. while (i--) {
  4117. now[i] = +from[attr][i] + pos * ms * diff[attr][i];
  4118. }
  4119. }
  4120. break;
  4121. default:
  4122. var from2 = [][concat](from[attr]);
  4123. now = [];
  4124. i = that.paper.customAttributes[attr].length;
  4125. while (i--) {
  4126. now[i] = +from2[i] + pos * ms * diff[attr][i];
  4127. }
  4128. break;
  4129. }
  4130. set[attr] = now;
  4131. }
  4132. that.attr(set);
  4133. (function (id, that, anim) {
  4134. setTimeout(function () {
  4135. eve("raphael.anim.frame." + id, that, anim);
  4136. });
  4137. })(that.id, that, e.anim);
  4138. } else {
  4139. (function(f, el, a) {
  4140. setTimeout(function() {
  4141. eve("raphael.anim.frame." + el.id, el, a);
  4142. eve("raphael.anim.finish." + el.id, el, a);
  4143. R.is(f, "function") && f.call(el);
  4144. });
  4145. })(e.callback, that, e.anim);
  4146. that.attr(to);
  4147. animationElements.splice(l--, 1);
  4148. if (e.repeat > 1 && !e.next) {
  4149. for (key in to) if (to[has](key)) {
  4150. init[key] = e.totalOrigin[key];
  4151. }
  4152. e.el.attr(init);
  4153. runAnimation(e.anim, e.el, e.anim.percents[0], null, e.totalOrigin, e.repeat - 1);
  4154. }
  4155. if (e.next && !e.stop) {
  4156. runAnimation(e.anim, e.el, e.next, null, e.totalOrigin, e.repeat);
  4157. }
  4158. }
  4159. }
  4160. animationElements.length && requestAnimFrame(animation);
  4161. },
  4162. upto255 = function (color) {
  4163. return color > 255 ? 255 : color < 0 ? 0 : color;
  4164. };
  4165. /*\
  4166. * Element.animateWith
  4167. [ method ]
  4168. **
  4169. * Acts similar to @Element.animate, but ensure that given animation runs in sync with another given element.
  4170. **
  4171. > Parameters
  4172. **
  4173. - el (object) element to sync with
  4174. - anim (object) animation to sync with
  4175. - params (object) #optional final attributes for the element, see also @Element.attr
  4176. - ms (number) #optional number of milliseconds for animation to run
  4177. - easing (string) #optional easing type. Accept on of @Raphael.easing_formulas or CSS format: `cubic&#x2010;bezier(XX,&#160;XX,&#160;XX,&#160;XX)`
  4178. - callback (function) #optional callback function. Will be called at the end of animation.
  4179. * or
  4180. - element (object) element to sync with
  4181. - anim (object) animation to sync with
  4182. - animation (object) #optional animation object, see @Raphael.animation
  4183. **
  4184. = (object) original element
  4185. \*/
  4186. elproto.animateWith = function (el, anim, params, ms, easing, callback) {
  4187. var element = this;
  4188. if (element.removed) {
  4189. callback && callback.call(element);
  4190. return element;
  4191. }
  4192. var a = params instanceof Animation ? params : R.animation(params, ms, easing, callback),
  4193. x, y;
  4194. runAnimation(a, element, a.percents[0], null, element.attr());
  4195. for (var i = 0, ii = animationElements.length; i < ii; i++) {
  4196. if (animationElements[i].anim == anim && animationElements[i].el == el) {
  4197. animationElements[ii - 1].start = animationElements[i].start;
  4198. break;
  4199. }
  4200. }
  4201. return element;
  4202. //
  4203. //
  4204. // var a = params ? R.animation(params, ms, easing, callback) : anim,
  4205. // status = element.status(anim);
  4206. // return this.animate(a).status(a, status * anim.ms / a.ms);
  4207. };
  4208. function CubicBezierAtTime(t, p1x, p1y, p2x, p2y, duration) {
  4209. var cx = 3 * p1x,
  4210. bx = 3 * (p2x - p1x) - cx,
  4211. ax = 1 - cx - bx,
  4212. cy = 3 * p1y,
  4213. by = 3 * (p2y - p1y) - cy,
  4214. ay = 1 - cy - by;
  4215. function sampleCurveX(t) {
  4216. return ((ax * t + bx) * t + cx) * t;
  4217. }
  4218. function solve(x, epsilon) {
  4219. var t = solveCurveX(x, epsilon);
  4220. return ((ay * t + by) * t + cy) * t;
  4221. }
  4222. function solveCurveX(x, epsilon) {
  4223. var t0, t1, t2, x2, d2, i;
  4224. for(t2 = x, i = 0; i < 8; i++) {
  4225. x2 = sampleCurveX(t2) - x;
  4226. if (abs(x2) < epsilon) {
  4227. return t2;
  4228. }
  4229. d2 = (3 * ax * t2 + 2 * bx) * t2 + cx;
  4230. if (abs(d2) < 1e-6) {
  4231. break;
  4232. }
  4233. t2 = t2 - x2 / d2;
  4234. }
  4235. t0 = 0;
  4236. t1 = 1;
  4237. t2 = x;
  4238. if (t2 < t0) {
  4239. return t0;
  4240. }
  4241. if (t2 > t1) {
  4242. return t1;
  4243. }
  4244. while (t0 < t1) {
  4245. x2 = sampleCurveX(t2);
  4246. if (abs(x2 - x) < epsilon) {
  4247. return t2;
  4248. }
  4249. if (x > x2) {
  4250. t0 = t2;
  4251. } else {
  4252. t1 = t2;
  4253. }
  4254. t2 = (t1 - t0) / 2 + t0;
  4255. }
  4256. return t2;
  4257. }
  4258. return solve(t, 1 / (200 * duration));
  4259. }
  4260. elproto.onAnimation = function (f) {
  4261. f ? eve.on("raphael.anim.frame." + this.id, f) : eve.unbind("raphael.anim.frame." + this.id);
  4262. return this;
  4263. };
  4264. function Animation(anim, ms) {
  4265. var percents = [],
  4266. newAnim = {};
  4267. this.ms = ms;
  4268. this.times = 1;
  4269. if (anim) {
  4270. for (var attr in anim) if (anim[has](attr)) {
  4271. newAnim[toFloat(attr)] = anim[attr];
  4272. percents.push(toFloat(attr));
  4273. }
  4274. percents.sort(sortByNumber);
  4275. }
  4276. this.anim = newAnim;
  4277. this.top = percents[percents.length - 1];
  4278. this.percents = percents;
  4279. }
  4280. /*\
  4281. * Animation.delay
  4282. [ method ]
  4283. **
  4284. * Creates a copy of existing animation object with given delay.
  4285. **
  4286. > Parameters
  4287. **
  4288. - delay (number) number of ms to pass between animation start and actual animation
  4289. **
  4290. = (object) new altered Animation object
  4291. | var anim = Raphael.animation({cx: 10, cy: 20}, 2e3);
  4292. | circle1.animate(anim); // run the given animation immediately
  4293. | circle2.animate(anim.delay(500)); // run the given animation after 500 ms
  4294. \*/
  4295. Animation.prototype.delay = function (delay) {
  4296. var a = new Animation(this.anim, this.ms);
  4297. a.times = this.times;
  4298. a.del = +delay || 0;
  4299. return a;
  4300. };
  4301. /*\
  4302. * Animation.repeat
  4303. [ method ]
  4304. **
  4305. * Creates a copy of existing animation object with given repetition.
  4306. **
  4307. > Parameters
  4308. **
  4309. - repeat (number) number iterations of animation. For infinite animation pass `Infinity`
  4310. **
  4311. = (object) new altered Animation object
  4312. \*/
  4313. Animation.prototype.repeat = function (times) {
  4314. var a = new Animation(this.anim, this.ms);
  4315. a.del = this.del;
  4316. a.times = math.floor(mmax(times, 0)) || 1;
  4317. return a;
  4318. };
  4319. function runAnimation(anim, element, percent, status, totalOrigin, times) {
  4320. percent = toFloat(percent);
  4321. var params,
  4322. isInAnim,
  4323. isInAnimSet,
  4324. percents = [],
  4325. next,
  4326. prev,
  4327. timestamp,
  4328. ms = anim.ms,
  4329. from = {},
  4330. to = {},
  4331. diff = {};
  4332. if (status) {
  4333. for (i = 0, ii = animationElements.length; i < ii; i++) {
  4334. var e = animationElements[i];
  4335. if (e.el.id == element.id && e.anim == anim) {
  4336. if (e.percent != percent) {
  4337. animationElements.splice(i, 1);
  4338. isInAnimSet = 1;
  4339. } else {
  4340. isInAnim = e;
  4341. }
  4342. element.attr(e.totalOrigin);
  4343. break;
  4344. }
  4345. }
  4346. } else {
  4347. status = +to; // NaN
  4348. }
  4349. for (var i = 0, ii = anim.percents.length; i < ii; i++) {
  4350. if (anim.percents[i] == percent || anim.percents[i] > status * anim.top) {
  4351. percent = anim.percents[i];
  4352. prev = anim.percents[i - 1] || 0;
  4353. ms = ms / anim.top * (percent - prev);
  4354. next = anim.percents[i + 1];
  4355. params = anim.anim[percent];
  4356. break;
  4357. } else if (status) {
  4358. element.attr(anim.anim[anim.percents[i]]);
  4359. }
  4360. }
  4361. if (!params) {
  4362. return;
  4363. }
  4364. if (!isInAnim) {
  4365. for (var attr in params) if (params[has](attr)) {
  4366. if (availableAnimAttrs[has](attr) || element.paper.customAttributes[has](attr)) {
  4367. from[attr] = element.attr(attr);
  4368. (from[attr] == null) && (from[attr] = availableAttrs[attr]);
  4369. to[attr] = params[attr];
  4370. switch (availableAnimAttrs[attr]) {
  4371. case nu:
  4372. diff[attr] = (to[attr] - from[attr]) / ms;
  4373. break;
  4374. case "colour":
  4375. from[attr] = R.getRGB(from[attr]);
  4376. var toColour = R.getRGB(to[attr]);
  4377. diff[attr] = {
  4378. r: (toColour.r - from[attr].r) / ms,
  4379. g: (toColour.g - from[attr].g) / ms,
  4380. b: (toColour.b - from[attr].b) / ms
  4381. };
  4382. break;
  4383. case "path":
  4384. var pathes = path2curve(from[attr], to[attr]),
  4385. toPath = pathes[1];
  4386. from[attr] = pathes[0];
  4387. diff[attr] = [];
  4388. for (i = 0, ii = from[attr].length; i < ii; i++) {
  4389. diff[attr][i] = [0];
  4390. for (var j = 1, jj = from[attr][i].length; j < jj; j++) {
  4391. diff[attr][i][j] = (toPath[i][j] - from[attr][i][j]) / ms;
  4392. }
  4393. }
  4394. break;
  4395. case "transform":
  4396. var _ = element._,
  4397. eq = equaliseTransform(_[attr], to[attr]);
  4398. if (eq) {
  4399. from[attr] = eq.from;
  4400. to[attr] = eq.to;
  4401. diff[attr] = [];
  4402. diff[attr].real = true;
  4403. for (i = 0, ii = from[attr].length; i < ii; i++) {
  4404. diff[attr][i] = [from[attr][i][0]];
  4405. for (j = 1, jj = from[attr][i].length; j < jj; j++) {
  4406. diff[attr][i][j] = (to[attr][i][j] - from[attr][i][j]) / ms;
  4407. }
  4408. }
  4409. } else {
  4410. var m = (element.matrix || new Matrix),
  4411. to2 = {
  4412. _: {transform: _.transform},
  4413. getBBox: function () {
  4414. return element.getBBox(1);
  4415. }
  4416. };
  4417. from[attr] = [
  4418. m.a,
  4419. m.b,
  4420. m.c,
  4421. m.d,
  4422. m.e,
  4423. m.f
  4424. ];
  4425. extractTransform(to2, to[attr]);
  4426. to[attr] = to2._.transform;
  4427. diff[attr] = [
  4428. (to2.matrix.a - m.a) / ms,
  4429. (to2.matrix.b - m.b) / ms,
  4430. (to2.matrix.c - m.c) / ms,
  4431. (to2.matrix.d - m.d) / ms,
  4432. (to2.matrix.e - m.e) / ms,
  4433. (to2.matrix.f - m.f) / ms
  4434. ];
  4435. // from[attr] = [_.sx, _.sy, _.deg, _.dx, _.dy];
  4436. // var to2 = {_:{}, getBBox: function () { return element.getBBox(); }};
  4437. // extractTransform(to2, to[attr]);
  4438. // diff[attr] = [
  4439. // (to2._.sx - _.sx) / ms,
  4440. // (to2._.sy - _.sy) / ms,
  4441. // (to2._.deg - _.deg) / ms,
  4442. // (to2._.dx - _.dx) / ms,
  4443. // (to2._.dy - _.dy) / ms
  4444. // ];
  4445. }
  4446. break;
  4447. case "csv":
  4448. var values = Str(params[attr])[split](separator),
  4449. from2 = Str(from[attr])[split](separator);
  4450. if (attr == "clip-rect") {
  4451. from[attr] = from2;
  4452. diff[attr] = [];
  4453. i = from2.length;
  4454. while (i--) {
  4455. diff[attr][i] = (values[i] - from[attr][i]) / ms;
  4456. }
  4457. }
  4458. to[attr] = values;
  4459. break;
  4460. default:
  4461. values = [][concat](params[attr]);
  4462. from2 = [][concat](from[attr]);
  4463. diff[attr] = [];
  4464. i = element.paper.customAttributes[attr].length;
  4465. while (i--) {
  4466. diff[attr][i] = ((values[i] || 0) - (from2[i] || 0)) / ms;
  4467. }
  4468. break;
  4469. }
  4470. }
  4471. }
  4472. var easing = params.easing,
  4473. easyeasy = R.easing_formulas[easing];
  4474. if (!easyeasy) {
  4475. easyeasy = Str(easing).match(bezierrg);
  4476. if (easyeasy && easyeasy.length == 5) {
  4477. var curve = easyeasy;
  4478. easyeasy = function (t) {
  4479. return CubicBezierAtTime(t, +curve[1], +curve[2], +curve[3], +curve[4], ms);
  4480. };
  4481. } else {
  4482. easyeasy = pipe;
  4483. }
  4484. }
  4485. timestamp = params.start || anim.start || +new Date;
  4486. e = {
  4487. anim: anim,
  4488. percent: percent,
  4489. timestamp: timestamp,
  4490. start: timestamp + (anim.del || 0),
  4491. status: 0,
  4492. initstatus: status || 0,
  4493. stop: false,
  4494. ms: ms,
  4495. easing: easyeasy,
  4496. from: from,
  4497. diff: diff,
  4498. to: to,
  4499. el: element,
  4500. callback: params.callback,
  4501. prev: prev,
  4502. next: next,
  4503. repeat: times || anim.times,
  4504. origin: element.attr(),
  4505. totalOrigin: totalOrigin
  4506. };
  4507. animationElements.push(e);
  4508. if (status && !isInAnim && !isInAnimSet) {
  4509. e.stop = true;
  4510. e.start = new Date - ms * status;
  4511. if (animationElements.length == 1) {
  4512. return animation();
  4513. }
  4514. }
  4515. if (isInAnimSet) {
  4516. e.start = new Date - e.ms * status;
  4517. }
  4518. animationElements.length == 1 && requestAnimFrame(animation);
  4519. } else {
  4520. isInAnim.initstatus = status;
  4521. isInAnim.start = new Date - isInAnim.ms * status;
  4522. }
  4523. eve("raphael.anim.start." + element.id, element, anim);
  4524. }
  4525. /*\
  4526. * Raphael.animation
  4527. [ method ]
  4528. **
  4529. * Creates an animation object that can be passed to the @Element.animate or @Element.animateWith methods.
  4530. * See also @Animation.delay and @Animation.repeat methods.
  4531. **
  4532. > Parameters
  4533. **
  4534. - params (object) final attributes for the element, see also @Element.attr
  4535. - ms (number) number of milliseconds for animation to run
  4536. - easing (string) #optional easing type. Accept one of @Raphael.easing_formulas or CSS format: `cubic&#x2010;bezier(XX,&#160;XX,&#160;XX,&#160;XX)`
  4537. - callback (function) #optional callback function. Will be called at the end of animation.
  4538. **
  4539. = (object) @Animation
  4540. \*/
  4541. R.animation = function (params, ms, easing, callback) {
  4542. if (params instanceof Animation) {
  4543. return params;
  4544. }
  4545. if (R.is(easing, "function") || !easing) {
  4546. callback = callback || easing || null;
  4547. easing = null;
  4548. }
  4549. params = Object(params);
  4550. ms = +ms || 0;
  4551. var p = {},
  4552. json,
  4553. attr;
  4554. for (attr in params) if (params[has](attr) && toFloat(attr) != attr && toFloat(attr) + "%" != attr) {
  4555. json = true;
  4556. p[attr] = params[attr];
  4557. }
  4558. if (!json) {
  4559. // if percent-like syntax is used and end-of-all animation callback used
  4560. if(callback){
  4561. // find the last one
  4562. var lastKey = 0;
  4563. for(var i in params){
  4564. var percent = toInt(i);
  4565. if(params[has](i) && percent > lastKey){
  4566. lastKey = percent;
  4567. }
  4568. }
  4569. lastKey += '%';
  4570. // if already defined callback in the last keyframe, skip
  4571. !params[lastKey].callback && (params[lastKey].callback = callback);
  4572. }
  4573. return new Animation(params, ms);
  4574. } else {
  4575. easing && (p.easing = easing);
  4576. callback && (p.callback = callback);
  4577. return new Animation({100: p}, ms);
  4578. }
  4579. };
  4580. /*\
  4581. * Element.animate
  4582. [ method ]
  4583. **
  4584. * Creates and starts animation for given element.
  4585. **
  4586. > Parameters
  4587. **
  4588. - params (object) final attributes for the element, see also @Element.attr
  4589. - ms (number) number of milliseconds for animation to run
  4590. - easing (string) #optional easing type. Accept one of @Raphael.easing_formulas or CSS format: `cubic&#x2010;bezier(XX,&#160;XX,&#160;XX,&#160;XX)`
  4591. - callback (function) #optional callback function. Will be called at the end of animation.
  4592. * or
  4593. - animation (object) animation object, see @Raphael.animation
  4594. **
  4595. = (object) original element
  4596. \*/
  4597. elproto.animate = function (params, ms, easing, callback) {
  4598. var element = this;
  4599. if (element.removed) {
  4600. callback && callback.call(element);
  4601. return element;
  4602. }
  4603. var anim = params instanceof Animation ? params : R.animation(params, ms, easing, callback);
  4604. runAnimation(anim, element, anim.percents[0], null, element.attr());
  4605. return element;
  4606. };
  4607. /*\
  4608. * Element.setTime
  4609. [ method ]
  4610. **
  4611. * Sets the status of animation of the element in milliseconds. Similar to @Element.status method.
  4612. **
  4613. > Parameters
  4614. **
  4615. - anim (object) animation object
  4616. - value (number) number of milliseconds from the beginning of the animation
  4617. **
  4618. = (object) original element if `value` is specified
  4619. * Note, that during animation following events are triggered:
  4620. *
  4621. * On each animation frame event `anim.frame.<id>`, on start `anim.start.<id>` and on end `anim.finish.<id>`.
  4622. \*/
  4623. elproto.setTime = function (anim, value) {
  4624. if (anim && value != null) {
  4625. this.status(anim, mmin(value, anim.ms) / anim.ms);
  4626. }
  4627. return this;
  4628. };
  4629. /*\
  4630. * Element.status
  4631. [ method ]
  4632. **
  4633. * Gets or sets the status of animation of the element.
  4634. **
  4635. > Parameters
  4636. **
  4637. - anim (object) #optional animation object
  4638. - 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.
  4639. **
  4640. = (number) status
  4641. * or
  4642. = (array) status if `anim` is not specified. Array of objects in format:
  4643. o {
  4644. o anim: (object) animation object
  4645. o status: (number) status
  4646. o }
  4647. * or
  4648. = (object) original element if `value` is specified
  4649. \*/
  4650. elproto.status = function (anim, value) {
  4651. var out = [],
  4652. i = 0,
  4653. len,
  4654. e;
  4655. if (value != null) {
  4656. runAnimation(anim, this, -1, mmin(value, 1));
  4657. return this;
  4658. } else {
  4659. len = animationElements.length;
  4660. for (; i < len; i++) {
  4661. e = animationElements[i];
  4662. if (e.el.id == this.id && (!anim || e.anim == anim)) {
  4663. if (anim) {
  4664. return e.status;
  4665. }
  4666. out.push({
  4667. anim: e.anim,
  4668. status: e.status
  4669. });
  4670. }
  4671. }
  4672. if (anim) {
  4673. return 0;
  4674. }
  4675. return out;
  4676. }
  4677. };
  4678. /*\
  4679. * Element.pause
  4680. [ method ]
  4681. **
  4682. * Stops animation of the element with ability to resume it later on.
  4683. **
  4684. > Parameters
  4685. **
  4686. - anim (object) #optional animation object
  4687. **
  4688. = (object) original element
  4689. \*/
  4690. elproto.pause = function (anim) {
  4691. for (var i = 0; i < animationElements.length; i++) if (animationElements[i].el.id == this.id && (!anim || animationElements[i].anim == anim)) {
  4692. if (eve("raphael.anim.pause." + this.id, this, animationElements[i].anim) !== false) {
  4693. animationElements[i].paused = true;
  4694. }
  4695. }
  4696. return this;
  4697. };
  4698. /*\
  4699. * Element.resume
  4700. [ method ]
  4701. **
  4702. * Resumes animation if it was paused with @Element.pause method.
  4703. **
  4704. > Parameters
  4705. **
  4706. - anim (object) #optional animation object
  4707. **
  4708. = (object) original element
  4709. \*/
  4710. elproto.resume = function (anim) {
  4711. for (var i = 0; i < animationElements.length; i++) if (animationElements[i].el.id == this.id && (!anim || animationElements[i].anim == anim)) {
  4712. var e = animationElements[i];
  4713. if (eve("raphael.anim.resume." + this.id, this, e.anim) !== false) {
  4714. delete e.paused;
  4715. this.status(e.anim, e.status);
  4716. }
  4717. }
  4718. return this;
  4719. };
  4720. /*\
  4721. * Element.stop
  4722. [ method ]
  4723. **
  4724. * Stops animation of the element.
  4725. **
  4726. > Parameters
  4727. **
  4728. - anim (object) #optional animation object
  4729. **
  4730. = (object) original element
  4731. \*/
  4732. elproto.stop = function (anim) {
  4733. for (var i = 0; i < animationElements.length; i++) if (animationElements[i].el.id == this.id && (!anim || animationElements[i].anim == anim)) {
  4734. if (eve("raphael.anim.stop." + this.id, this, animationElements[i].anim) !== false) {
  4735. animationElements.splice(i--, 1);
  4736. }
  4737. }
  4738. return this;
  4739. };
  4740. function stopAnimation(paper) {
  4741. for (var i = 0; i < animationElements.length; i++) if (animationElements[i].el.paper == paper) {
  4742. animationElements.splice(i--, 1);
  4743. }
  4744. }
  4745. eve.on("raphael.remove", stopAnimation);
  4746. eve.on("raphael.clear", stopAnimation);
  4747. elproto.toString = function () {
  4748. return "Rapha\xebl\u2019s object";
  4749. };
  4750. // Set
  4751. var Set = function (items) {
  4752. this.items = [];
  4753. this.length = 0;
  4754. this.type = "set";
  4755. if (items) {
  4756. for (var i = 0, ii = items.length; i < ii; i++) {
  4757. if (items[i] && (items[i].constructor == elproto.constructor || items[i].constructor == Set)) {
  4758. this[this.items.length] = this.items[this.items.length] = items[i];
  4759. this.length++;
  4760. }
  4761. }
  4762. }
  4763. },
  4764. setproto = Set.prototype;
  4765. /*\
  4766. * Set.push
  4767. [ method ]
  4768. **
  4769. * Adds each argument to the current set.
  4770. = (object) original element
  4771. \*/
  4772. setproto.push = function () {
  4773. var item,
  4774. len;
  4775. for (var i = 0, ii = arguments.length; i < ii; i++) {
  4776. item = arguments[i];
  4777. if (item && (item.constructor == elproto.constructor || item.constructor == Set)) {
  4778. len = this.items.length;
  4779. this[len] = this.items[len] = item;
  4780. this.length++;
  4781. }
  4782. }
  4783. return this;
  4784. };
  4785. /*\
  4786. * Set.pop
  4787. [ method ]
  4788. **
  4789. * Removes last element and returns it.
  4790. = (object) element
  4791. \*/
  4792. setproto.pop = function () {
  4793. this.length && delete this[this.length--];
  4794. return this.items.pop();
  4795. };
  4796. /*\
  4797. * Set.forEach
  4798. [ method ]
  4799. **
  4800. * Executes given function for each element in the set.
  4801. *
  4802. * If function returns `false` it will stop loop running.
  4803. **
  4804. > Parameters
  4805. **
  4806. - callback (function) function to run
  4807. - thisArg (object) context object for the callback
  4808. = (object) Set object
  4809. \*/
  4810. setproto.forEach = function (callback, thisArg) {
  4811. for (var i = 0, ii = this.items.length; i < ii; i++) {
  4812. if (callback.call(thisArg, this.items[i], i) === false) {
  4813. return this;
  4814. }
  4815. }
  4816. return this;
  4817. };
  4818. for (var method in elproto) if (elproto[has](method)) {
  4819. setproto[method] = (function (methodname) {
  4820. return function () {
  4821. var arg = arguments;
  4822. return this.forEach(function (el) {
  4823. el[methodname][apply](el, arg);
  4824. });
  4825. };
  4826. })(method);
  4827. }
  4828. setproto.attr = function (name, value) {
  4829. if (name && R.is(name, array) && R.is(name[0], "object")) {
  4830. for (var j = 0, jj = name.length; j < jj; j++) {
  4831. this.items[j].attr(name[j]);
  4832. }
  4833. } else {
  4834. for (var i = 0, ii = this.items.length; i < ii; i++) {
  4835. this.items[i].attr(name, value);
  4836. }
  4837. }
  4838. return this;
  4839. };
  4840. /*\
  4841. * Set.clear
  4842. [ method ]
  4843. **
  4844. * Removes all elements from the set
  4845. \*/
  4846. setproto.clear = function () {
  4847. while (this.length) {
  4848. this.pop();
  4849. }
  4850. };
  4851. /*\
  4852. * Set.splice
  4853. [ method ]
  4854. **
  4855. * Removes given element from the set
  4856. **
  4857. > Parameters
  4858. **
  4859. - index (number) position of the deletion
  4860. - count (number) number of element to remove
  4861. - insertion… (object) #optional elements to insert
  4862. = (object) set elements that were deleted
  4863. \*/
  4864. setproto.splice = function (index, count, insertion) {
  4865. index = index < 0 ? mmax(this.length + index, 0) : index;
  4866. count = mmax(0, mmin(this.length - index, count));
  4867. var tail = [],
  4868. todel = [],
  4869. args = [],
  4870. i;
  4871. for (i = 2; i < arguments.length; i++) {
  4872. args.push(arguments[i]);
  4873. }
  4874. for (i = 0; i < count; i++) {
  4875. todel.push(this[index + i]);
  4876. }
  4877. for (; i < this.length - index; i++) {
  4878. tail.push(this[index + i]);
  4879. }
  4880. var arglen = args.length;
  4881. for (i = 0; i < arglen + tail.length; i++) {
  4882. this.items[index + i] = this[index + i] = i < arglen ? args[i] : tail[i - arglen];
  4883. }
  4884. i = this.items.length = this.length -= count - arglen;
  4885. while (this[i]) {
  4886. delete this[i++];
  4887. }
  4888. return new Set(todel);
  4889. };
  4890. /*\
  4891. * Set.exclude
  4892. [ method ]
  4893. **
  4894. * Removes given element from the set
  4895. **
  4896. > Parameters
  4897. **
  4898. - element (object) element to remove
  4899. = (boolean) `true` if object was found & removed from the set
  4900. \*/
  4901. setproto.exclude = function (el) {
  4902. for (var i = 0, ii = this.length; i < ii; i++) if (this[i] == el) {
  4903. this.splice(i, 1);
  4904. return true;
  4905. }
  4906. };
  4907. setproto.animate = function (params, ms, easing, callback) {
  4908. (R.is(easing, "function") || !easing) && (callback = easing || null);
  4909. var len = this.items.length,
  4910. i = len,
  4911. item,
  4912. set = this,
  4913. collector;
  4914. if (!len) {
  4915. return this;
  4916. }
  4917. callback && (collector = function () {
  4918. !--len && callback.call(set);
  4919. });
  4920. easing = R.is(easing, string) ? easing : collector;
  4921. var anim = R.animation(params, ms, easing, collector);
  4922. item = this.items[--i].animate(anim);
  4923. while (i--) {
  4924. this.items[i] && !this.items[i].removed && this.items[i].animateWith(item, anim, anim);
  4925. (this.items[i] && !this.items[i].removed) || len--;
  4926. }
  4927. return this;
  4928. };
  4929. setproto.insertAfter = function (el) {
  4930. var i = this.items.length;
  4931. while (i--) {
  4932. this.items[i].insertAfter(el);
  4933. }
  4934. return this;
  4935. };
  4936. setproto.getBBox = function () {
  4937. var x = [],
  4938. y = [],
  4939. x2 = [],
  4940. y2 = [];
  4941. for (var i = this.items.length; i--;) if (!this.items[i].removed) {
  4942. var box = this.items[i].getBBox();
  4943. x.push(box.x);
  4944. y.push(box.y);
  4945. x2.push(box.x + box.width);
  4946. y2.push(box.y + box.height);
  4947. }
  4948. x = mmin[apply](0, x);
  4949. y = mmin[apply](0, y);
  4950. x2 = mmax[apply](0, x2);
  4951. y2 = mmax[apply](0, y2);
  4952. return {
  4953. x: x,
  4954. y: y,
  4955. x2: x2,
  4956. y2: y2,
  4957. width: x2 - x,
  4958. height: y2 - y
  4959. };
  4960. };
  4961. setproto.clone = function (s) {
  4962. s = this.paper.set();
  4963. for (var i = 0, ii = this.items.length; i < ii; i++) {
  4964. s.push(this.items[i].clone());
  4965. }
  4966. return s;
  4967. };
  4968. setproto.toString = function () {
  4969. return "Rapha\xebl\u2018s set";
  4970. };
  4971. setproto.glow = function(glowConfig) {
  4972. var ret = this.paper.set();
  4973. this.forEach(function(shape, index){
  4974. var g = shape.glow(glowConfig);
  4975. if(g != null){
  4976. g.forEach(function(shape2, index2){
  4977. ret.push(shape2);
  4978. });
  4979. }
  4980. });
  4981. return ret;
  4982. };
  4983. /*\
  4984. * Set.isPointInside
  4985. [ method ]
  4986. **
  4987. * Determine if given point is inside this set’s elements
  4988. **
  4989. > Parameters
  4990. **
  4991. - x (number) x coordinate of the point
  4992. - y (number) y coordinate of the point
  4993. = (boolean) `true` if point is inside any of the set's elements
  4994. \*/
  4995. setproto.isPointInside = function (x, y) {
  4996. var isPointInside = false;
  4997. this.forEach(function (el) {
  4998. if (el.isPointInside(x, y)) {
  4999. isPointInside = true;
  5000. return false; // stop loop
  5001. }
  5002. });
  5003. return isPointInside;
  5004. };
  5005. /*\
  5006. * Raphael.registerFont
  5007. [ method ]
  5008. **
  5009. * Adds given font to the registered set of fonts for Raphaël. Should be used as an internal call from within Cufón’s font file.
  5010. * Returns original parameter, so it could be used with chaining.
  5011. # <a href="http://wiki.github.com/sorccu/cufon/about">More about Cufón and how to convert your font form TTF, OTF, etc to JavaScript file.</a>
  5012. **
  5013. > Parameters
  5014. **
  5015. - font (object) the font to register
  5016. = (object) the font you passed in
  5017. > Usage
  5018. | Cufon.registerFont(Raphael.registerFont({…}));
  5019. \*/
  5020. R.registerFont = function (font) {
  5021. if (!font.face) {
  5022. return font;
  5023. }
  5024. this.fonts = this.fonts || {};
  5025. var fontcopy = {
  5026. w: font.w,
  5027. face: {},
  5028. glyphs: {}
  5029. },
  5030. family = font.face["font-family"];
  5031. for (var prop in font.face) if (font.face[has](prop)) {
  5032. fontcopy.face[prop] = font.face[prop];
  5033. }
  5034. if (this.fonts[family]) {
  5035. this.fonts[family].push(fontcopy);
  5036. } else {
  5037. this.fonts[family] = [fontcopy];
  5038. }
  5039. if (!font.svg) {
  5040. fontcopy.face["units-per-em"] = toInt(font.face["units-per-em"], 10);
  5041. for (var glyph in font.glyphs) if (font.glyphs[has](glyph)) {
  5042. var path = font.glyphs[glyph];
  5043. fontcopy.glyphs[glyph] = {
  5044. w: path.w,
  5045. k: {},
  5046. d: path.d && "M" + path.d.replace(/[mlcxtrv]/g, function (command) {
  5047. return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
  5048. }) + "z"
  5049. };
  5050. if (path.k) {
  5051. for (var k in path.k) if (path[has](k)) {
  5052. fontcopy.glyphs[glyph].k[k] = path.k[k];
  5053. }
  5054. }
  5055. }
  5056. }
  5057. return font;
  5058. };
  5059. /*\
  5060. * Paper.getFont
  5061. [ method ]
  5062. **
  5063. * Finds font object in the registered fonts by given parameters. You could specify only one word from the font name, like “Myriad” for “Myriad Pro”.
  5064. **
  5065. > Parameters
  5066. **
  5067. - family (string) font family name or any word from it
  5068. - weight (string) #optional font weight
  5069. - style (string) #optional font style
  5070. - stretch (string) #optional font stretch
  5071. = (object) the font object
  5072. > Usage
  5073. | paper.print(100, 100, "Test string", paper.getFont("Times", 800), 30);
  5074. \*/
  5075. paperproto.getFont = function (family, weight, style, stretch) {
  5076. stretch = stretch || "normal";
  5077. style = style || "normal";
  5078. weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
  5079. if (!R.fonts) {
  5080. return;
  5081. }
  5082. var font = R.fonts[family];
  5083. if (!font) {
  5084. var name = new RegExp("(^|\\s)" + family.replace(/[^\w\d\s+!~.:_-]/g, E) + "(\\s|$)", "i");
  5085. for (var fontName in R.fonts) if (R.fonts[has](fontName)) {
  5086. if (name.test(fontName)) {
  5087. font = R.fonts[fontName];
  5088. break;
  5089. }
  5090. }
  5091. }
  5092. var thefont;
  5093. if (font) {
  5094. for (var i = 0, ii = font.length; i < ii; i++) {
  5095. thefont = font[i];
  5096. if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
  5097. break;
  5098. }
  5099. }
  5100. }
  5101. return thefont;
  5102. };
  5103. /*\
  5104. * Paper.print
  5105. [ method ]
  5106. **
  5107. * Creates path that represent given text written using given font at given position with given size.
  5108. * Result of the method is path element that contains whole text as a separate path.
  5109. **
  5110. > Parameters
  5111. **
  5112. - x (number) x position of the text
  5113. - y (number) y position of the text
  5114. - string (string) text to print
  5115. - font (object) font object, see @Paper.getFont
  5116. - size (number) #optional size of the font, default is `16`
  5117. - origin (string) #optional could be `"baseline"` or `"middle"`, default is `"middle"`
  5118. - letter_spacing (number) #optional number in range `-1..1`, default is `0`
  5119. - line_spacing (number) #optional number in range `1..3`, default is `1`
  5120. = (object) resulting path element, which consist of all letters
  5121. > Usage
  5122. | var txt = r.print(10, 50, "print", r.getFont("Museo"), 30).attr({fill: "#fff"});
  5123. \*/
  5124. paperproto.print = function (x, y, string, font, size, origin, letter_spacing, line_spacing) {
  5125. origin = origin || "middle"; // baseline|middle
  5126. letter_spacing = mmax(mmin(letter_spacing || 0, 1), -1);
  5127. line_spacing = mmax(mmin(line_spacing || 1, 3), 1);
  5128. var letters = Str(string)[split](E),
  5129. shift = 0,
  5130. notfirst = 0,
  5131. path = E,
  5132. scale;
  5133. R.is(font, "string") && (font = this.getFont(font));
  5134. if (font) {
  5135. scale = (size || 16) / font.face["units-per-em"];
  5136. var bb = font.face.bbox[split](separator),
  5137. top = +bb[0],
  5138. lineHeight = bb[3] - bb[1],
  5139. shifty = 0,
  5140. height = +bb[1] + (origin == "baseline" ? lineHeight + (+font.face.descent) : lineHeight / 2);
  5141. for (var i = 0, ii = letters.length; i < ii; i++) {
  5142. if (letters[i] == "\n") {
  5143. shift = 0;
  5144. curr = 0;
  5145. notfirst = 0;
  5146. shifty += lineHeight * line_spacing;
  5147. } else {
  5148. var prev = notfirst && font.glyphs[letters[i - 1]] || {},
  5149. curr = font.glyphs[letters[i]];
  5150. shift += notfirst ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) + (font.w * letter_spacing) : 0;
  5151. notfirst = 1;
  5152. }
  5153. if (curr && curr.d) {
  5154. path += R.transformPath(curr.d, ["t", shift * scale, shifty * scale, "s", scale, scale, top, height, "t", (x - top) / scale, (y - height) / scale]);
  5155. }
  5156. }
  5157. }
  5158. return this.path(path).attr({
  5159. fill: "#000",
  5160. stroke: "none"
  5161. });
  5162. };
  5163. /*\
  5164. * Paper.add
  5165. [ method ]
  5166. **
  5167. * Imports elements in JSON array in format `{type: type, <attributes>}`
  5168. **
  5169. > Parameters
  5170. **
  5171. - json (array)
  5172. = (object) resulting set of imported elements
  5173. > Usage
  5174. | paper.add([
  5175. | {
  5176. | type: "circle",
  5177. | cx: 10,
  5178. | cy: 10,
  5179. | r: 5
  5180. | },
  5181. | {
  5182. | type: "rect",
  5183. | x: 10,
  5184. | y: 10,
  5185. | width: 10,
  5186. | height: 10,
  5187. | fill: "#fc0"
  5188. | }
  5189. | ]);
  5190. \*/
  5191. paperproto.add = function (json) {
  5192. if (R.is(json, "array")) {
  5193. var res = this.set(),
  5194. i = 0,
  5195. ii = json.length,
  5196. j;
  5197. for (; i < ii; i++) {
  5198. j = json[i] || {};
  5199. elements[has](j.type) && res.push(this[j.type]().attr(j));
  5200. }
  5201. }
  5202. return res;
  5203. };
  5204. /*\
  5205. * Raphael.format
  5206. [ method ]
  5207. **
  5208. * Simple format function. Replaces construction of type “`{<number>}`” to the corresponding argument.
  5209. **
  5210. > Parameters
  5211. **
  5212. - token (string) string to format
  5213. - … (string) rest of arguments will be treated as parameters for replacement
  5214. = (string) formated string
  5215. > Usage
  5216. | var x = 10,
  5217. | y = 20,
  5218. | width = 40,
  5219. | height = 50;
  5220. | // this will draw a rectangular shape equivalent to "M10,20h40v50h-40z"
  5221. | paper.path(Raphael.format("M{0},{1}h{2}v{3}h{4}z", x, y, width, height, -width));
  5222. \*/
  5223. R.format = function (token, params) {
  5224. var args = R.is(params, array) ? [0][concat](params) : arguments;
  5225. token && R.is(token, string) && args.length - 1 && (token = token.replace(formatrg, function (str, i) {
  5226. return args[++i] == null ? E : args[i];
  5227. }));
  5228. return token || E;
  5229. };
  5230. /*\
  5231. * Raphael.fullfill
  5232. [ method ]
  5233. **
  5234. * A little bit more advanced format function than @Raphael.format. Replaces construction of type “`{<name>}`” to the corresponding argument.
  5235. **
  5236. > Parameters
  5237. **
  5238. - token (string) string to format
  5239. - json (object) object which properties will be used as a replacement
  5240. = (string) formated string
  5241. > Usage
  5242. | // this will draw a rectangular shape equivalent to "M10,20h40v50h-40z"
  5243. | paper.path(Raphael.fullfill("M{x},{y}h{dim.width}v{dim.height}h{dim['negative width']}z", {
  5244. | x: 10,
  5245. | y: 20,
  5246. | dim: {
  5247. | width: 40,
  5248. | height: 50,
  5249. | "negative width": -40
  5250. | }
  5251. | }));
  5252. \*/
  5253. R.fullfill = (function () {
  5254. var tokenRegex = /\{([^\}]+)\}/g,
  5255. objNotationRegex = /(?:(?:^|\.)(.+?)(?=\[|\.|$|\()|\[('|")(.+?)\2\])(\(\))?/g, // matches .xxxxx or ["xxxxx"] to run over object properties
  5256. replacer = function (all, key, obj) {
  5257. var res = obj;
  5258. key.replace(objNotationRegex, function (all, name, quote, quotedName, isFunc) {
  5259. name = name || quotedName;
  5260. if (res) {
  5261. if (name in res) {
  5262. res = res[name];
  5263. }
  5264. typeof res == "function" && isFunc && (res = res());
  5265. }
  5266. });
  5267. res = (res == null || res == obj ? all : res) + "";
  5268. return res;
  5269. };
  5270. return function (str, obj) {
  5271. return String(str).replace(tokenRegex, function (all, key) {
  5272. return replacer(all, key, obj);
  5273. });
  5274. };
  5275. })();
  5276. /*\
  5277. * Raphael.ninja
  5278. [ method ]
  5279. **
  5280. * If you want to leave no trace of Raphaël (Well, Raphaël creates only one global variable `Raphael`, but anyway.) You can use `ninja` method.
  5281. * Beware, that in this case plugins could stop working, because they are depending on global variable existence.
  5282. **
  5283. = (object) Raphael object
  5284. > Usage
  5285. | (function (local_raphael) {
  5286. | var paper = local_raphael(10, 10, 320, 200);
  5287. | …
  5288. | })(Raphael.ninja());
  5289. \*/
  5290. R.ninja = function () {
  5291. if (oldRaphael.was) {
  5292. g.win.Raphael = oldRaphael.is;
  5293. } else {
  5294. // IE8 raises an error when deleting window property
  5295. window.Raphael = undefined;
  5296. try {
  5297. delete window.Raphael;
  5298. } catch(e) {}
  5299. }
  5300. return R;
  5301. };
  5302. /*\
  5303. * Raphael.st
  5304. [ property (object) ]
  5305. **
  5306. * You can add your own method to elements and sets. It is wise to add a set method for each element method
  5307. * you added, so you will be able to call the same method on sets too.
  5308. **
  5309. * See also @Raphael.el.
  5310. > Usage
  5311. | Raphael.el.red = function () {
  5312. | this.attr({fill: "#f00"});
  5313. | };
  5314. | Raphael.st.red = function () {
  5315. | this.forEach(function (el) {
  5316. | el.red();
  5317. | });
  5318. | };
  5319. | // then use it
  5320. | paper.set(paper.circle(100, 100, 20), paper.circle(110, 100, 20)).red();
  5321. \*/
  5322. R.st = setproto;
  5323. eve.on("raphael.DOMload", function () {
  5324. loaded = true;
  5325. });
  5326. // Firefox <3.6 fix: http://webreflection.blogspot.com/2009/11/195-chars-to-help-lazy-loading.html
  5327. (function (doc, loaded, f) {
  5328. if (doc.readyState == null && doc.addEventListener){
  5329. doc.addEventListener(loaded, f = function () {
  5330. doc.removeEventListener(loaded, f, false);
  5331. doc.readyState = "complete";
  5332. }, false);
  5333. doc.readyState = "loading";
  5334. }
  5335. function isLoaded() {
  5336. (/in/).test(doc.readyState) ? setTimeout(isLoaded, 9) : R.eve("raphael.DOMload");
  5337. }
  5338. isLoaded();
  5339. })(document, "DOMContentLoaded");
  5340. return R;
  5341. });