raphael.core.js 190 KB

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