zrender.js 562 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  3. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  4. (factory((global.zrender = {})));
  5. }(this, (function (exports) { 'use strict';
  6. /**
  7. * zrender: 生成唯一id
  8. *
  9. * @author errorrik (errorrik@gmail.com)
  10. */
  11. var idStart = 0x0907;
  12. var guid = function () {
  13. return idStart++;
  14. };
  15. /**
  16. * echarts设备环境识别
  17. *
  18. * @desc echarts基于Canvas,纯Javascript图表库,提供直观,生动,可交互,可个性化定制的数据统计图表。
  19. * @author firede[firede@firede.us]
  20. * @desc thanks zepto.
  21. */
  22. /* global wx */
  23. var env = {};
  24. if (typeof wx === 'object' && typeof wx.getSystemInfoSync === 'function') {
  25. // In Weixin Application
  26. env = {
  27. browser: {},
  28. os: {},
  29. node: false,
  30. wxa: true, // Weixin Application
  31. canvasSupported: true,
  32. svgSupported: false,
  33. touchEventsSupported: true,
  34. domSupported: false
  35. };
  36. }
  37. else if (typeof document === 'undefined' && typeof self !== 'undefined') {
  38. // In worker
  39. env = {
  40. browser: {},
  41. os: {},
  42. node: false,
  43. worker: true,
  44. canvasSupported: true,
  45. domSupported: false
  46. };
  47. }
  48. else if (typeof navigator === 'undefined') {
  49. // In node
  50. env = {
  51. browser: {},
  52. os: {},
  53. node: true,
  54. worker: false,
  55. // Assume canvas is supported
  56. canvasSupported: true,
  57. svgSupported: true,
  58. domSupported: false
  59. };
  60. }
  61. else {
  62. env = detect(navigator.userAgent);
  63. }
  64. var env$1 = env;
  65. // Zepto.js
  66. // (c) 2010-2013 Thomas Fuchs
  67. // Zepto.js may be freely distributed under the MIT license.
  68. function detect(ua) {
  69. var os = {};
  70. var browser = {};
  71. // var webkit = ua.match(/Web[kK]it[\/]{0,1}([\d.]+)/);
  72. // var android = ua.match(/(Android);?[\s\/]+([\d.]+)?/);
  73. // var ipad = ua.match(/(iPad).*OS\s([\d_]+)/);
  74. // var ipod = ua.match(/(iPod)(.*OS\s([\d_]+))?/);
  75. // var iphone = !ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/);
  76. // var webos = ua.match(/(webOS|hpwOS)[\s\/]([\d.]+)/);
  77. // var touchpad = webos && ua.match(/TouchPad/);
  78. // var kindle = ua.match(/Kindle\/([\d.]+)/);
  79. // var silk = ua.match(/Silk\/([\d._]+)/);
  80. // var blackberry = ua.match(/(BlackBerry).*Version\/([\d.]+)/);
  81. // var bb10 = ua.match(/(BB10).*Version\/([\d.]+)/);
  82. // var rimtabletos = ua.match(/(RIM\sTablet\sOS)\s([\d.]+)/);
  83. // var playbook = ua.match(/PlayBook/);
  84. // var chrome = ua.match(/Chrome\/([\d.]+)/) || ua.match(/CriOS\/([\d.]+)/);
  85. var firefox = ua.match(/Firefox\/([\d.]+)/);
  86. // var safari = webkit && ua.match(/Mobile\//) && !chrome;
  87. // var webview = ua.match(/(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari)/) && !chrome;
  88. var ie = ua.match(/MSIE\s([\d.]+)/)
  89. // IE 11 Trident/7.0; rv:11.0
  90. || ua.match(/Trident\/.+?rv:(([\d.]+))/);
  91. var edge = ua.match(/Edge\/([\d.]+)/); // IE 12 and 12+
  92. var weChat = (/micromessenger/i).test(ua);
  93. // Todo: clean this up with a better OS/browser seperation:
  94. // - discern (more) between multiple browsers on android
  95. // - decide if kindle fire in silk mode is android or not
  96. // - Firefox on Android doesn't specify the Android version
  97. // - possibly devide in os, device and browser hashes
  98. // if (browser.webkit = !!webkit) browser.version = webkit[1];
  99. // if (android) os.android = true, os.version = android[2];
  100. // if (iphone && !ipod) os.ios = os.iphone = true, os.version = iphone[2].replace(/_/g, '.');
  101. // if (ipad) os.ios = os.ipad = true, os.version = ipad[2].replace(/_/g, '.');
  102. // if (ipod) os.ios = os.ipod = true, os.version = ipod[3] ? ipod[3].replace(/_/g, '.') : null;
  103. // if (webos) os.webos = true, os.version = webos[2];
  104. // if (touchpad) os.touchpad = true;
  105. // if (blackberry) os.blackberry = true, os.version = blackberry[2];
  106. // if (bb10) os.bb10 = true, os.version = bb10[2];
  107. // if (rimtabletos) os.rimtabletos = true, os.version = rimtabletos[2];
  108. // if (playbook) browser.playbook = true;
  109. // if (kindle) os.kindle = true, os.version = kindle[1];
  110. // if (silk) browser.silk = true, browser.version = silk[1];
  111. // if (!silk && os.android && ua.match(/Kindle Fire/)) browser.silk = true;
  112. // if (chrome) browser.chrome = true, browser.version = chrome[1];
  113. if (firefox) {
  114. browser.firefox = true;
  115. browser.version = firefox[1];
  116. }
  117. // if (safari && (ua.match(/Safari/) || !!os.ios)) browser.safari = true;
  118. // if (webview) browser.webview = true;
  119. if (ie) {
  120. browser.ie = true;
  121. browser.version = ie[1];
  122. }
  123. if (edge) {
  124. browser.edge = true;
  125. browser.version = edge[1];
  126. }
  127. // It is difficult to detect WeChat in Win Phone precisely, because ua can
  128. // not be set on win phone. So we do not consider Win Phone.
  129. if (weChat) {
  130. browser.weChat = true;
  131. }
  132. // os.tablet = !!(ipad || playbook || (android && !ua.match(/Mobile/)) ||
  133. // (firefox && ua.match(/Tablet/)) || (ie && !ua.match(/Phone/) && ua.match(/Touch/)));
  134. // os.phone = !!(!os.tablet && !os.ipod && (android || iphone || webos ||
  135. // (chrome && ua.match(/Android/)) || (chrome && ua.match(/CriOS\/([\d.]+)/)) ||
  136. // (firefox && ua.match(/Mobile/)) || (ie && ua.match(/Touch/))));
  137. return {
  138. browser: browser,
  139. os: os,
  140. node: false,
  141. // 原生canvas支持,改极端点了
  142. // canvasSupported : !(browser.ie && parseFloat(browser.version) < 9)
  143. canvasSupported: !!document.createElement('canvas').getContext,
  144. svgSupported: typeof SVGRect !== 'undefined',
  145. // works on most browsers
  146. // IE10/11 does not support touch event, and MS Edge supports them but not by
  147. // default, so we dont check navigator.maxTouchPoints for them here.
  148. touchEventsSupported: 'ontouchstart' in window && !browser.ie && !browser.edge,
  149. // <http://caniuse.com/#search=pointer%20event>.
  150. pointerEventsSupported:
  151. // (1) Firefox supports pointer but not by default, only MS browsers are reliable on pointer
  152. // events currently. So we dont use that on other browsers unless tested sufficiently.
  153. // For example, in iOS 13 Mobile Chromium 78, if the touching behavior starts page
  154. // scroll, the `pointermove` event can not be fired any more. That will break some
  155. // features like "pan horizontally to move something and pan vertically to page scroll".
  156. // The horizontal pan probably be interrupted by the casually triggered page scroll.
  157. // (2) Although IE 10 supports pointer event, it use old style and is different from the
  158. // standard. So we exclude that. (IE 10 is hardly used on touch device)
  159. 'onpointerdown' in window
  160. && (browser.edge || (browser.ie && browser.version >= 11)),
  161. // passiveSupported: detectPassiveSupport()
  162. domSupported: typeof document !== 'undefined'
  163. };
  164. }
  165. // See https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md#feature-detection
  166. // function detectPassiveSupport() {
  167. // // Test via a getter in the options object to see if the passive property is accessed
  168. // var supportsPassive = false;
  169. // try {
  170. // var opts = Object.defineProperty({}, 'passive', {
  171. // get: function() {
  172. // supportsPassive = true;
  173. // }
  174. // });
  175. // window.addEventListener('testPassive', function() {}, opts);
  176. // } catch (e) {
  177. // }
  178. // return supportsPassive;
  179. // }
  180. /**
  181. * @module zrender/core/util
  182. */
  183. // 用于处理merge时无法遍历Date等对象的问题
  184. var BUILTIN_OBJECT = {
  185. '[object Function]': 1,
  186. '[object RegExp]': 1,
  187. '[object Date]': 1,
  188. '[object Error]': 1,
  189. '[object CanvasGradient]': 1,
  190. '[object CanvasPattern]': 1,
  191. // For node-canvas
  192. '[object Image]': 1,
  193. '[object Canvas]': 1
  194. };
  195. var TYPED_ARRAY = {
  196. '[object Int8Array]': 1,
  197. '[object Uint8Array]': 1,
  198. '[object Uint8ClampedArray]': 1,
  199. '[object Int16Array]': 1,
  200. '[object Uint16Array]': 1,
  201. '[object Int32Array]': 1,
  202. '[object Uint32Array]': 1,
  203. '[object Float32Array]': 1,
  204. '[object Float64Array]': 1
  205. };
  206. var objToString = Object.prototype.toString;
  207. var arrayProto = Array.prototype;
  208. var nativeForEach = arrayProto.forEach;
  209. var nativeFilter = arrayProto.filter;
  210. var nativeSlice = arrayProto.slice;
  211. var nativeMap = arrayProto.map;
  212. var nativeReduce = arrayProto.reduce;
  213. // Avoid assign to an exported variable, for transforming to cjs.
  214. var methods = {};
  215. function $override(name, fn) {
  216. // Clear ctx instance for different environment
  217. if (name === 'createCanvas') {
  218. _ctx = null;
  219. }
  220. methods[name] = fn;
  221. }
  222. /**
  223. * Those data types can be cloned:
  224. * Plain object, Array, TypedArray, number, string, null, undefined.
  225. * Those data types will be assgined using the orginal data:
  226. * BUILTIN_OBJECT
  227. * Instance of user defined class will be cloned to a plain object, without
  228. * properties in prototype.
  229. * Other data types is not supported (not sure what will happen).
  230. *
  231. * Caution: do not support clone Date, for performance consideration.
  232. * (There might be a large number of date in `series.data`).
  233. * So date should not be modified in and out of echarts.
  234. *
  235. * @param {*} source
  236. * @return {*} new
  237. */
  238. function clone(source) {
  239. if (source == null || typeof source !== 'object') {
  240. return source;
  241. }
  242. var result = source;
  243. var typeStr = objToString.call(source);
  244. if (typeStr === '[object Array]') {
  245. if (!isPrimitive(source)) {
  246. result = [];
  247. for (var i = 0, len = source.length; i < len; i++) {
  248. result[i] = clone(source[i]);
  249. }
  250. }
  251. }
  252. else if (TYPED_ARRAY[typeStr]) {
  253. if (!isPrimitive(source)) {
  254. var Ctor = source.constructor;
  255. if (source.constructor.from) {
  256. result = Ctor.from(source);
  257. }
  258. else {
  259. result = new Ctor(source.length);
  260. for (var i = 0, len = source.length; i < len; i++) {
  261. result[i] = clone(source[i]);
  262. }
  263. }
  264. }
  265. }
  266. else if (!BUILTIN_OBJECT[typeStr] && !isPrimitive(source) && !isDom(source)) {
  267. result = {};
  268. for (var key in source) {
  269. if (source.hasOwnProperty(key)) {
  270. result[key] = clone(source[key]);
  271. }
  272. }
  273. }
  274. return result;
  275. }
  276. /**
  277. * @memberOf module:zrender/core/util
  278. * @param {*} target
  279. * @param {*} source
  280. * @param {boolean} [overwrite=false]
  281. */
  282. function merge(target, source, overwrite) {
  283. // We should escapse that source is string
  284. // and enter for ... in ...
  285. if (!isObject(source) || !isObject(target)) {
  286. return overwrite ? clone(source) : target;
  287. }
  288. for (var key in source) {
  289. if (source.hasOwnProperty(key)) {
  290. var targetProp = target[key];
  291. var sourceProp = source[key];
  292. if (isObject(sourceProp)
  293. && isObject(targetProp)
  294. && !isArray(sourceProp)
  295. && !isArray(targetProp)
  296. && !isDom(sourceProp)
  297. && !isDom(targetProp)
  298. && !isBuiltInObject(sourceProp)
  299. && !isBuiltInObject(targetProp)
  300. && !isPrimitive(sourceProp)
  301. && !isPrimitive(targetProp)
  302. ) {
  303. // 如果需要递归覆盖,就递归调用merge
  304. merge(targetProp, sourceProp, overwrite);
  305. }
  306. else if (overwrite || !(key in target)) {
  307. // 否则只处理overwrite为true,或者在目标对象中没有此属性的情况
  308. // NOTE,在 target[key] 不存在的时候也是直接覆盖
  309. target[key] = clone(source[key], true);
  310. }
  311. }
  312. }
  313. return target;
  314. }
  315. /**
  316. * @param {Array} targetAndSources The first item is target, and the rests are source.
  317. * @param {boolean} [overwrite=false]
  318. * @return {*} target
  319. */
  320. function mergeAll(targetAndSources, overwrite) {
  321. var result = targetAndSources[0];
  322. for (var i = 1, len = targetAndSources.length; i < len; i++) {
  323. result = merge(result, targetAndSources[i], overwrite);
  324. }
  325. return result;
  326. }
  327. /**
  328. * @param {*} target
  329. * @param {*} source
  330. * @memberOf module:zrender/core/util
  331. */
  332. function extend(target, source) {
  333. for (var key in source) {
  334. if (source.hasOwnProperty(key)) {
  335. target[key] = source[key];
  336. }
  337. }
  338. return target;
  339. }
  340. /**
  341. * @param {*} target
  342. * @param {*} source
  343. * @param {boolean} [overlay=false]
  344. * @memberOf module:zrender/core/util
  345. */
  346. function defaults(target, source, overlay) {
  347. for (var key in source) {
  348. if (source.hasOwnProperty(key)
  349. && (overlay ? source[key] != null : target[key] == null)
  350. ) {
  351. target[key] = source[key];
  352. }
  353. }
  354. return target;
  355. }
  356. var createCanvas = function () {
  357. return methods.createCanvas();
  358. };
  359. methods.createCanvas = function () {
  360. return document.createElement('canvas');
  361. };
  362. // FIXME
  363. var _ctx;
  364. function getContext() {
  365. if (!_ctx) {
  366. // Use util.createCanvas instead of createCanvas
  367. // because createCanvas may be overwritten in different environment
  368. _ctx = createCanvas().getContext('2d');
  369. }
  370. return _ctx;
  371. }
  372. /**
  373. * 查询数组中元素的index
  374. * @memberOf module:zrender/core/util
  375. */
  376. function indexOf(array, value) {
  377. if (array) {
  378. if (array.indexOf) {
  379. return array.indexOf(value);
  380. }
  381. for (var i = 0, len = array.length; i < len; i++) {
  382. if (array[i] === value) {
  383. return i;
  384. }
  385. }
  386. }
  387. return -1;
  388. }
  389. /**
  390. * 构造类继承关系
  391. *
  392. * @memberOf module:zrender/core/util
  393. * @param {Function} clazz 源类
  394. * @param {Function} baseClazz 基类
  395. */
  396. function inherits(clazz, baseClazz) {
  397. var clazzPrototype = clazz.prototype;
  398. function F() {}
  399. F.prototype = baseClazz.prototype;
  400. clazz.prototype = new F();
  401. for (var prop in clazzPrototype) {
  402. if (clazzPrototype.hasOwnProperty(prop)) {
  403. clazz.prototype[prop] = clazzPrototype[prop];
  404. }
  405. }
  406. clazz.prototype.constructor = clazz;
  407. clazz.superClass = baseClazz;
  408. }
  409. /**
  410. * @memberOf module:zrender/core/util
  411. * @param {Object|Function} target
  412. * @param {Object|Function} sorce
  413. * @param {boolean} overlay
  414. */
  415. function mixin(target, source, overlay) {
  416. target = 'prototype' in target ? target.prototype : target;
  417. source = 'prototype' in source ? source.prototype : source;
  418. defaults(target, source, overlay);
  419. }
  420. /**
  421. * Consider typed array.
  422. * @param {Array|TypedArray} data
  423. */
  424. function isArrayLike(data) {
  425. if (!data) {
  426. return;
  427. }
  428. if (typeof data === 'string') {
  429. return false;
  430. }
  431. return typeof data.length === 'number';
  432. }
  433. /**
  434. * 数组或对象遍历
  435. * @memberOf module:zrender/core/util
  436. * @param {Object|Array} obj
  437. * @param {Function} cb
  438. * @param {*} [context]
  439. */
  440. function each(obj, cb, context) {
  441. if (!(obj && cb)) {
  442. return;
  443. }
  444. if (obj.forEach && obj.forEach === nativeForEach) {
  445. obj.forEach(cb, context);
  446. }
  447. else if (obj.length === +obj.length) {
  448. for (var i = 0, len = obj.length; i < len; i++) {
  449. cb.call(context, obj[i], i, obj);
  450. }
  451. }
  452. else {
  453. for (var key in obj) {
  454. if (obj.hasOwnProperty(key)) {
  455. cb.call(context, obj[key], key, obj);
  456. }
  457. }
  458. }
  459. }
  460. /**
  461. * 数组映射
  462. * @memberOf module:zrender/core/util
  463. * @param {Array} obj
  464. * @param {Function} cb
  465. * @param {*} [context]
  466. * @return {Array}
  467. */
  468. function map(obj, cb, context) {
  469. if (!(obj && cb)) {
  470. return;
  471. }
  472. if (obj.map && obj.map === nativeMap) {
  473. return obj.map(cb, context);
  474. }
  475. else {
  476. var result = [];
  477. for (var i = 0, len = obj.length; i < len; i++) {
  478. result.push(cb.call(context, obj[i], i, obj));
  479. }
  480. return result;
  481. }
  482. }
  483. /**
  484. * @memberOf module:zrender/core/util
  485. * @param {Array} obj
  486. * @param {Function} cb
  487. * @param {Object} [memo]
  488. * @param {*} [context]
  489. * @return {Array}
  490. */
  491. function reduce(obj, cb, memo, context) {
  492. if (!(obj && cb)) {
  493. return;
  494. }
  495. if (obj.reduce && obj.reduce === nativeReduce) {
  496. return obj.reduce(cb, memo, context);
  497. }
  498. else {
  499. for (var i = 0, len = obj.length; i < len; i++) {
  500. memo = cb.call(context, memo, obj[i], i, obj);
  501. }
  502. return memo;
  503. }
  504. }
  505. /**
  506. * 数组过滤
  507. * @memberOf module:zrender/core/util
  508. * @param {Array} obj
  509. * @param {Function} cb
  510. * @param {*} [context]
  511. * @return {Array}
  512. */
  513. function filter(obj, cb, context) {
  514. if (!(obj && cb)) {
  515. return;
  516. }
  517. if (obj.filter && obj.filter === nativeFilter) {
  518. return obj.filter(cb, context);
  519. }
  520. else {
  521. var result = [];
  522. for (var i = 0, len = obj.length; i < len; i++) {
  523. if (cb.call(context, obj[i], i, obj)) {
  524. result.push(obj[i]);
  525. }
  526. }
  527. return result;
  528. }
  529. }
  530. /**
  531. * 数组项查找
  532. * @memberOf module:zrender/core/util
  533. * @param {Array} obj
  534. * @param {Function} cb
  535. * @param {*} [context]
  536. * @return {*}
  537. */
  538. function find(obj, cb, context) {
  539. if (!(obj && cb)) {
  540. return;
  541. }
  542. for (var i = 0, len = obj.length; i < len; i++) {
  543. if (cb.call(context, obj[i], i, obj)) {
  544. return obj[i];
  545. }
  546. }
  547. }
  548. /**
  549. * @memberOf module:zrender/core/util
  550. * @param {Function} func
  551. * @param {*} context
  552. * @return {Function}
  553. */
  554. function bind(func, context) {
  555. var args = nativeSlice.call(arguments, 2);
  556. return function () {
  557. return func.apply(context, args.concat(nativeSlice.call(arguments)));
  558. };
  559. }
  560. /**
  561. * @memberOf module:zrender/core/util
  562. * @param {Function} func
  563. * @return {Function}
  564. */
  565. function curry(func) {
  566. var args = nativeSlice.call(arguments, 1);
  567. return function () {
  568. return func.apply(this, args.concat(nativeSlice.call(arguments)));
  569. };
  570. }
  571. /**
  572. * @memberOf module:zrender/core/util
  573. * @param {*} value
  574. * @return {boolean}
  575. */
  576. function isArray(value) {
  577. return objToString.call(value) === '[object Array]';
  578. }
  579. /**
  580. * @memberOf module:zrender/core/util
  581. * @param {*} value
  582. * @return {boolean}
  583. */
  584. function isFunction(value) {
  585. return typeof value === 'function';
  586. }
  587. /**
  588. * @memberOf module:zrender/core/util
  589. * @param {*} value
  590. * @return {boolean}
  591. */
  592. function isString(value) {
  593. return objToString.call(value) === '[object String]';
  594. }
  595. /**
  596. * @memberOf module:zrender/core/util
  597. * @param {*} value
  598. * @return {boolean}
  599. */
  600. function isObject(value) {
  601. // Avoid a V8 JIT bug in Chrome 19-20.
  602. // See https://code.google.com/p/v8/issues/detail?id=2291 for more details.
  603. var type = typeof value;
  604. return type === 'function' || (!!value && type === 'object');
  605. }
  606. /**
  607. * @memberOf module:zrender/core/util
  608. * @param {*} value
  609. * @return {boolean}
  610. */
  611. function isBuiltInObject(value) {
  612. return !!BUILTIN_OBJECT[objToString.call(value)];
  613. }
  614. /**
  615. * @memberOf module:zrender/core/util
  616. * @param {*} value
  617. * @return {boolean}
  618. */
  619. function isTypedArray(value) {
  620. return !!TYPED_ARRAY[objToString.call(value)];
  621. }
  622. /**
  623. * @memberOf module:zrender/core/util
  624. * @param {*} value
  625. * @return {boolean}
  626. */
  627. function isDom(value) {
  628. return typeof value === 'object'
  629. && typeof value.nodeType === 'number'
  630. && typeof value.ownerDocument === 'object';
  631. }
  632. /**
  633. * Whether is exactly NaN. Notice isNaN('a') returns true.
  634. * @param {*} value
  635. * @return {boolean}
  636. */
  637. function eqNaN(value) {
  638. /* eslint-disable-next-line no-self-compare */
  639. return value !== value;
  640. }
  641. /**
  642. * If value1 is not null, then return value1, otherwise judget rest of values.
  643. * Low performance.
  644. * @memberOf module:zrender/core/util
  645. * @return {*} Final value
  646. */
  647. function retrieve(values) {
  648. for (var i = 0, len = arguments.length; i < len; i++) {
  649. if (arguments[i] != null) {
  650. return arguments[i];
  651. }
  652. }
  653. }
  654. function retrieve2(value0, value1) {
  655. return value0 != null
  656. ? value0
  657. : value1;
  658. }
  659. function retrieve3(value0, value1, value2) {
  660. return value0 != null
  661. ? value0
  662. : value1 != null
  663. ? value1
  664. : value2;
  665. }
  666. /**
  667. * @memberOf module:zrender/core/util
  668. * @param {Array} arr
  669. * @param {number} startIndex
  670. * @param {number} endIndex
  671. * @return {Array}
  672. */
  673. function slice() {
  674. return Function.call.apply(nativeSlice, arguments);
  675. }
  676. /**
  677. * Normalize css liked array configuration
  678. * e.g.
  679. * 3 => [3, 3, 3, 3]
  680. * [4, 2] => [4, 2, 4, 2]
  681. * [4, 3, 2] => [4, 3, 2, 3]
  682. * @param {number|Array.<number>} val
  683. * @return {Array.<number>}
  684. */
  685. function normalizeCssArray(val) {
  686. if (typeof (val) === 'number') {
  687. return [val, val, val, val];
  688. }
  689. var len = val.length;
  690. if (len === 2) {
  691. // vertical | horizontal
  692. return [val[0], val[1], val[0], val[1]];
  693. }
  694. else if (len === 3) {
  695. // top | horizontal | bottom
  696. return [val[0], val[1], val[2], val[1]];
  697. }
  698. return val;
  699. }
  700. /**
  701. * @memberOf module:zrender/core/util
  702. * @param {boolean} condition
  703. * @param {string} message
  704. */
  705. function assert(condition, message) {
  706. if (!condition) {
  707. throw new Error(message);
  708. }
  709. }
  710. /**
  711. * @memberOf module:zrender/core/util
  712. * @param {string} str string to be trimed
  713. * @return {string} trimed string
  714. */
  715. function trim(str) {
  716. if (str == null) {
  717. return null;
  718. }
  719. else if (typeof str.trim === 'function') {
  720. return str.trim();
  721. }
  722. else {
  723. return str.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  724. }
  725. }
  726. var primitiveKey = '__ec_primitive__';
  727. /**
  728. * Set an object as primitive to be ignored traversing children in clone or merge
  729. */
  730. function setAsPrimitive(obj) {
  731. obj[primitiveKey] = true;
  732. }
  733. function isPrimitive(obj) {
  734. return obj[primitiveKey];
  735. }
  736. /**
  737. * @constructor
  738. * @param {Object} obj Only apply `ownProperty`.
  739. */
  740. function HashMap(obj) {
  741. var isArr = isArray(obj);
  742. // Key should not be set on this, otherwise
  743. // methods get/set/... may be overrided.
  744. this.data = {};
  745. var thisMap = this;
  746. (obj instanceof HashMap)
  747. ? obj.each(visit)
  748. : (obj && each(obj, visit));
  749. function visit(value, key) {
  750. isArr ? thisMap.set(value, key) : thisMap.set(key, value);
  751. }
  752. }
  753. HashMap.prototype = {
  754. constructor: HashMap,
  755. // Do not provide `has` method to avoid defining what is `has`.
  756. // (We usually treat `null` and `undefined` as the same, different
  757. // from ES6 Map).
  758. get: function (key) {
  759. return this.data.hasOwnProperty(key) ? this.data[key] : null;
  760. },
  761. set: function (key, value) {
  762. // Comparing with invocation chaining, `return value` is more commonly
  763. // used in this case: `var someVal = map.set('a', genVal());`
  764. return (this.data[key] = value);
  765. },
  766. // Although util.each can be performed on this hashMap directly, user
  767. // should not use the exposed keys, who are prefixed.
  768. each: function (cb, context) {
  769. context !== void 0 && (cb = bind(cb, context));
  770. /* eslint-disable guard-for-in */
  771. for (var key in this.data) {
  772. this.data.hasOwnProperty(key) && cb(this.data[key], key);
  773. }
  774. /* eslint-enable guard-for-in */
  775. },
  776. // Do not use this method if performance sensitive.
  777. removeKey: function (key) {
  778. delete this.data[key];
  779. }
  780. };
  781. function createHashMap(obj) {
  782. return new HashMap(obj);
  783. }
  784. function concatArray(a, b) {
  785. var newArray = new a.constructor(a.length + b.length);
  786. for (var i = 0; i < a.length; i++) {
  787. newArray[i] = a[i];
  788. }
  789. var offset = a.length;
  790. for (i = 0; i < b.length; i++) {
  791. newArray[i + offset] = b[i];
  792. }
  793. return newArray;
  794. }
  795. function noop() {}
  796. var util = (Object.freeze || Object)({
  797. $override: $override,
  798. clone: clone,
  799. merge: merge,
  800. mergeAll: mergeAll,
  801. extend: extend,
  802. defaults: defaults,
  803. createCanvas: createCanvas,
  804. getContext: getContext,
  805. indexOf: indexOf,
  806. inherits: inherits,
  807. mixin: mixin,
  808. isArrayLike: isArrayLike,
  809. each: each,
  810. map: map,
  811. reduce: reduce,
  812. filter: filter,
  813. find: find,
  814. bind: bind,
  815. curry: curry,
  816. isArray: isArray,
  817. isFunction: isFunction,
  818. isString: isString,
  819. isObject: isObject,
  820. isBuiltInObject: isBuiltInObject,
  821. isTypedArray: isTypedArray,
  822. isDom: isDom,
  823. eqNaN: eqNaN,
  824. retrieve: retrieve,
  825. retrieve2: retrieve2,
  826. retrieve3: retrieve3,
  827. slice: slice,
  828. normalizeCssArray: normalizeCssArray,
  829. assert: assert,
  830. trim: trim,
  831. setAsPrimitive: setAsPrimitive,
  832. isPrimitive: isPrimitive,
  833. createHashMap: createHashMap,
  834. concatArray: concatArray,
  835. noop: noop
  836. });
  837. /* global Float32Array */
  838. var ArrayCtor = typeof Float32Array === 'undefined'
  839. ? Array
  840. : Float32Array;
  841. /**
  842. * 创建一个向量
  843. * @param {number} [x=0]
  844. * @param {number} [y=0]
  845. * @return {Vector2}
  846. */
  847. function create(x, y) {
  848. var out = new ArrayCtor(2);
  849. if (x == null) {
  850. x = 0;
  851. }
  852. if (y == null) {
  853. y = 0;
  854. }
  855. out[0] = x;
  856. out[1] = y;
  857. return out;
  858. }
  859. /**
  860. * 复制向量数据
  861. * @param {Vector2} out
  862. * @param {Vector2} v
  863. * @return {Vector2}
  864. */
  865. function copy(out, v) {
  866. out[0] = v[0];
  867. out[1] = v[1];
  868. return out;
  869. }
  870. /**
  871. * 克隆一个向量
  872. * @param {Vector2} v
  873. * @return {Vector2}
  874. */
  875. function clone$1(v) {
  876. var out = new ArrayCtor(2);
  877. out[0] = v[0];
  878. out[1] = v[1];
  879. return out;
  880. }
  881. /**
  882. * 设置向量的两个项
  883. * @param {Vector2} out
  884. * @param {number} a
  885. * @param {number} b
  886. * @return {Vector2} 结果
  887. */
  888. function set(out, a, b) {
  889. out[0] = a;
  890. out[1] = b;
  891. return out;
  892. }
  893. /**
  894. * 向量相加
  895. * @param {Vector2} out
  896. * @param {Vector2} v1
  897. * @param {Vector2} v2
  898. */
  899. function add(out, v1, v2) {
  900. out[0] = v1[0] + v2[0];
  901. out[1] = v1[1] + v2[1];
  902. return out;
  903. }
  904. /**
  905. * 向量缩放后相加
  906. * @param {Vector2} out
  907. * @param {Vector2} v1
  908. * @param {Vector2} v2
  909. * @param {number} a
  910. */
  911. function scaleAndAdd(out, v1, v2, a) {
  912. out[0] = v1[0] + v2[0] * a;
  913. out[1] = v1[1] + v2[1] * a;
  914. return out;
  915. }
  916. /**
  917. * 向量相减
  918. * @param {Vector2} out
  919. * @param {Vector2} v1
  920. * @param {Vector2} v2
  921. */
  922. function sub(out, v1, v2) {
  923. out[0] = v1[0] - v2[0];
  924. out[1] = v1[1] - v2[1];
  925. return out;
  926. }
  927. /**
  928. * 向量长度
  929. * @param {Vector2} v
  930. * @return {number}
  931. */
  932. function len(v) {
  933. return Math.sqrt(lenSquare(v));
  934. }
  935. var length = len; // jshint ignore:line
  936. /**
  937. * 向量长度平方
  938. * @param {Vector2} v
  939. * @return {number}
  940. */
  941. function lenSquare(v) {
  942. return v[0] * v[0] + v[1] * v[1];
  943. }
  944. var lengthSquare = lenSquare;
  945. /**
  946. * 向量乘法
  947. * @param {Vector2} out
  948. * @param {Vector2} v1
  949. * @param {Vector2} v2
  950. */
  951. function mul(out, v1, v2) {
  952. out[0] = v1[0] * v2[0];
  953. out[1] = v1[1] * v2[1];
  954. return out;
  955. }
  956. /**
  957. * 向量除法
  958. * @param {Vector2} out
  959. * @param {Vector2} v1
  960. * @param {Vector2} v2
  961. */
  962. function div(out, v1, v2) {
  963. out[0] = v1[0] / v2[0];
  964. out[1] = v1[1] / v2[1];
  965. return out;
  966. }
  967. /**
  968. * 向量点乘
  969. * @param {Vector2} v1
  970. * @param {Vector2} v2
  971. * @return {number}
  972. */
  973. function dot(v1, v2) {
  974. return v1[0] * v2[0] + v1[1] * v2[1];
  975. }
  976. /**
  977. * 向量缩放
  978. * @param {Vector2} out
  979. * @param {Vector2} v
  980. * @param {number} s
  981. */
  982. function scale(out, v, s) {
  983. out[0] = v[0] * s;
  984. out[1] = v[1] * s;
  985. return out;
  986. }
  987. /**
  988. * 向量归一化
  989. * @param {Vector2} out
  990. * @param {Vector2} v
  991. */
  992. function normalize(out, v) {
  993. var d = len(v);
  994. if (d === 0) {
  995. out[0] = 0;
  996. out[1] = 0;
  997. }
  998. else {
  999. out[0] = v[0] / d;
  1000. out[1] = v[1] / d;
  1001. }
  1002. return out;
  1003. }
  1004. /**
  1005. * 计算向量间距离
  1006. * @param {Vector2} v1
  1007. * @param {Vector2} v2
  1008. * @return {number}
  1009. */
  1010. function distance(v1, v2) {
  1011. return Math.sqrt(
  1012. (v1[0] - v2[0]) * (v1[0] - v2[0])
  1013. + (v1[1] - v2[1]) * (v1[1] - v2[1])
  1014. );
  1015. }
  1016. var dist = distance;
  1017. /**
  1018. * 向量距离平方
  1019. * @param {Vector2} v1
  1020. * @param {Vector2} v2
  1021. * @return {number}
  1022. */
  1023. function distanceSquare(v1, v2) {
  1024. return (v1[0] - v2[0]) * (v1[0] - v2[0])
  1025. + (v1[1] - v2[1]) * (v1[1] - v2[1]);
  1026. }
  1027. var distSquare = distanceSquare;
  1028. /**
  1029. * 求负向量
  1030. * @param {Vector2} out
  1031. * @param {Vector2} v
  1032. */
  1033. function negate(out, v) {
  1034. out[0] = -v[0];
  1035. out[1] = -v[1];
  1036. return out;
  1037. }
  1038. /**
  1039. * 插值两个点
  1040. * @param {Vector2} out
  1041. * @param {Vector2} v1
  1042. * @param {Vector2} v2
  1043. * @param {number} t
  1044. */
  1045. function lerp(out, v1, v2, t) {
  1046. out[0] = v1[0] + t * (v2[0] - v1[0]);
  1047. out[1] = v1[1] + t * (v2[1] - v1[1]);
  1048. return out;
  1049. }
  1050. /**
  1051. * 矩阵左乘向量
  1052. * @param {Vector2} out
  1053. * @param {Vector2} v
  1054. * @param {Vector2} m
  1055. */
  1056. function applyTransform(out, v, m) {
  1057. var x = v[0];
  1058. var y = v[1];
  1059. out[0] = m[0] * x + m[2] * y + m[4];
  1060. out[1] = m[1] * x + m[3] * y + m[5];
  1061. return out;
  1062. }
  1063. /**
  1064. * 求两个向量最小值
  1065. * @param {Vector2} out
  1066. * @param {Vector2} v1
  1067. * @param {Vector2} v2
  1068. */
  1069. function min(out, v1, v2) {
  1070. out[0] = Math.min(v1[0], v2[0]);
  1071. out[1] = Math.min(v1[1], v2[1]);
  1072. return out;
  1073. }
  1074. /**
  1075. * 求两个向量最大值
  1076. * @param {Vector2} out
  1077. * @param {Vector2} v1
  1078. * @param {Vector2} v2
  1079. */
  1080. function max(out, v1, v2) {
  1081. out[0] = Math.max(v1[0], v2[0]);
  1082. out[1] = Math.max(v1[1], v2[1]);
  1083. return out;
  1084. }
  1085. var vector = (Object.freeze || Object)({
  1086. create: create,
  1087. copy: copy,
  1088. clone: clone$1,
  1089. set: set,
  1090. add: add,
  1091. scaleAndAdd: scaleAndAdd,
  1092. sub: sub,
  1093. len: len,
  1094. length: length,
  1095. lenSquare: lenSquare,
  1096. lengthSquare: lengthSquare,
  1097. mul: mul,
  1098. div: div,
  1099. dot: dot,
  1100. scale: scale,
  1101. normalize: normalize,
  1102. distance: distance,
  1103. dist: dist,
  1104. distanceSquare: distanceSquare,
  1105. distSquare: distSquare,
  1106. negate: negate,
  1107. lerp: lerp,
  1108. applyTransform: applyTransform,
  1109. min: min,
  1110. max: max
  1111. });
  1112. // TODO Draggable for group
  1113. // FIXME Draggable on element which has parent rotation or scale
  1114. function Draggable() {
  1115. this.on('mousedown', this._dragStart, this);
  1116. this.on('mousemove', this._drag, this);
  1117. this.on('mouseup', this._dragEnd, this);
  1118. // `mosuemove` and `mouseup` can be continue to fire when dragging.
  1119. // See [Drag outside] in `Handler.js`. So we do not need to trigger
  1120. // `_dragEnd` when globalout. That would brings better user experience.
  1121. // this.on('globalout', this._dragEnd, this);
  1122. // this._dropTarget = null;
  1123. // this._draggingTarget = null;
  1124. // this._x = 0;
  1125. // this._y = 0;
  1126. }
  1127. Draggable.prototype = {
  1128. constructor: Draggable,
  1129. _dragStart: function (e) {
  1130. var draggingTarget = e.target;
  1131. // Find if there is draggable in the ancestor
  1132. while (draggingTarget && !draggingTarget.draggable) {
  1133. draggingTarget = draggingTarget.parent;
  1134. }
  1135. if (draggingTarget) {
  1136. this._draggingTarget = draggingTarget;
  1137. draggingTarget.dragging = true;
  1138. this._x = e.offsetX;
  1139. this._y = e.offsetY;
  1140. this.dispatchToElement(param(draggingTarget, e), 'dragstart', e.event);
  1141. }
  1142. },
  1143. _drag: function (e) {
  1144. var draggingTarget = this._draggingTarget;
  1145. if (draggingTarget) {
  1146. var x = e.offsetX;
  1147. var y = e.offsetY;
  1148. var dx = x - this._x;
  1149. var dy = y - this._y;
  1150. this._x = x;
  1151. this._y = y;
  1152. draggingTarget.drift(dx, dy, e);
  1153. this.dispatchToElement(param(draggingTarget, e), 'drag', e.event);
  1154. var dropTarget = this.findHover(x, y, draggingTarget).target;
  1155. var lastDropTarget = this._dropTarget;
  1156. this._dropTarget = dropTarget;
  1157. if (draggingTarget !== dropTarget) {
  1158. if (lastDropTarget && dropTarget !== lastDropTarget) {
  1159. this.dispatchToElement(param(lastDropTarget, e), 'dragleave', e.event);
  1160. }
  1161. if (dropTarget && dropTarget !== lastDropTarget) {
  1162. this.dispatchToElement(param(dropTarget, e), 'dragenter', e.event);
  1163. }
  1164. }
  1165. }
  1166. },
  1167. _dragEnd: function (e) {
  1168. var draggingTarget = this._draggingTarget;
  1169. if (draggingTarget) {
  1170. draggingTarget.dragging = false;
  1171. }
  1172. this.dispatchToElement(param(draggingTarget, e), 'dragend', e.event);
  1173. if (this._dropTarget) {
  1174. this.dispatchToElement(param(this._dropTarget, e), 'drop', e.event);
  1175. }
  1176. this._draggingTarget = null;
  1177. this._dropTarget = null;
  1178. }
  1179. };
  1180. function param(target, e) {
  1181. return {target: target, topTarget: e && e.topTarget};
  1182. }
  1183. /**
  1184. * Event Mixin
  1185. * @module zrender/mixin/Eventful
  1186. * @author Kener (@Kener-林峰, kener.linfeng@gmail.com)
  1187. * pissang (https://www.github.com/pissang)
  1188. */
  1189. var arrySlice = Array.prototype.slice;
  1190. /**
  1191. * Event dispatcher.
  1192. *
  1193. * @alias module:zrender/mixin/Eventful
  1194. * @constructor
  1195. * @param {Object} [eventProcessor] The object eventProcessor is the scope when
  1196. * `eventProcessor.xxx` called.
  1197. * @param {Function} [eventProcessor.normalizeQuery]
  1198. * param: {string|Object} Raw query.
  1199. * return: {string|Object} Normalized query.
  1200. * @param {Function} [eventProcessor.filter] Event will be dispatched only
  1201. * if it returns `true`.
  1202. * param: {string} eventType
  1203. * param: {string|Object} query
  1204. * return: {boolean}
  1205. * @param {Function} [eventProcessor.afterTrigger] Called after all handlers called.
  1206. * param: {string} eventType
  1207. */
  1208. var Eventful = function (eventProcessor) {
  1209. this._$handlers = {};
  1210. this._$eventProcessor = eventProcessor;
  1211. };
  1212. Eventful.prototype = {
  1213. constructor: Eventful,
  1214. /**
  1215. * The handler can only be triggered once, then removed.
  1216. *
  1217. * @param {string} event The event name.
  1218. * @param {string|Object} [query] Condition used on event filter.
  1219. * @param {Function} handler The event handler.
  1220. * @param {Object} context
  1221. */
  1222. one: function (event, query, handler, context) {
  1223. return on(this, event, query, handler, context, true);
  1224. },
  1225. /**
  1226. * Bind a handler.
  1227. *
  1228. * @param {string} event The event name.
  1229. * @param {string|Object} [query] Condition used on event filter.
  1230. * @param {Function} handler The event handler.
  1231. * @param {Object} [context]
  1232. */
  1233. on: function (event, query, handler, context) {
  1234. return on(this, event, query, handler, context, false);
  1235. },
  1236. /**
  1237. * Whether any handler has bound.
  1238. *
  1239. * @param {string} event
  1240. * @return {boolean}
  1241. */
  1242. isSilent: function (event) {
  1243. var _h = this._$handlers;
  1244. return !_h[event] || !_h[event].length;
  1245. },
  1246. /**
  1247. * Unbind a event.
  1248. *
  1249. * @param {string} [event] The event name.
  1250. * If no `event` input, "off" all listeners.
  1251. * @param {Function} [handler] The event handler.
  1252. * If no `handler` input, "off" all listeners of the `event`.
  1253. */
  1254. off: function (event, handler) {
  1255. var _h = this._$handlers;
  1256. if (!event) {
  1257. this._$handlers = {};
  1258. return this;
  1259. }
  1260. if (handler) {
  1261. if (_h[event]) {
  1262. var newList = [];
  1263. for (var i = 0, l = _h[event].length; i < l; i++) {
  1264. if (_h[event][i].h !== handler) {
  1265. newList.push(_h[event][i]);
  1266. }
  1267. }
  1268. _h[event] = newList;
  1269. }
  1270. if (_h[event] && _h[event].length === 0) {
  1271. delete _h[event];
  1272. }
  1273. }
  1274. else {
  1275. delete _h[event];
  1276. }
  1277. return this;
  1278. },
  1279. /**
  1280. * Dispatch a event.
  1281. *
  1282. * @param {string} type The event name.
  1283. */
  1284. trigger: function (type) {
  1285. var _h = this._$handlers[type];
  1286. var eventProcessor = this._$eventProcessor;
  1287. if (_h) {
  1288. var args = arguments;
  1289. var argLen = args.length;
  1290. if (argLen > 3) {
  1291. args = arrySlice.call(args, 1);
  1292. }
  1293. var len = _h.length;
  1294. for (var i = 0; i < len;) {
  1295. var hItem = _h[i];
  1296. if (eventProcessor
  1297. && eventProcessor.filter
  1298. && hItem.query != null
  1299. && !eventProcessor.filter(type, hItem.query)
  1300. ) {
  1301. i++;
  1302. continue;
  1303. }
  1304. // Optimize advise from backbone
  1305. switch (argLen) {
  1306. case 1:
  1307. hItem.h.call(hItem.ctx);
  1308. break;
  1309. case 2:
  1310. hItem.h.call(hItem.ctx, args[1]);
  1311. break;
  1312. case 3:
  1313. hItem.h.call(hItem.ctx, args[1], args[2]);
  1314. break;
  1315. default:
  1316. // have more than 2 given arguments
  1317. hItem.h.apply(hItem.ctx, args);
  1318. break;
  1319. }
  1320. if (hItem.one) {
  1321. _h.splice(i, 1);
  1322. len--;
  1323. }
  1324. else {
  1325. i++;
  1326. }
  1327. }
  1328. }
  1329. eventProcessor && eventProcessor.afterTrigger
  1330. && eventProcessor.afterTrigger(type);
  1331. return this;
  1332. },
  1333. /**
  1334. * Dispatch a event with context, which is specified at the last parameter.
  1335. *
  1336. * @param {string} type The event name.
  1337. */
  1338. triggerWithContext: function (type) {
  1339. var _h = this._$handlers[type];
  1340. var eventProcessor = this._$eventProcessor;
  1341. if (_h) {
  1342. var args = arguments;
  1343. var argLen = args.length;
  1344. if (argLen > 4) {
  1345. args = arrySlice.call(args, 1, args.length - 1);
  1346. }
  1347. var ctx = args[args.length - 1];
  1348. var len = _h.length;
  1349. for (var i = 0; i < len;) {
  1350. var hItem = _h[i];
  1351. if (eventProcessor
  1352. && eventProcessor.filter
  1353. && hItem.query != null
  1354. && !eventProcessor.filter(type, hItem.query)
  1355. ) {
  1356. i++;
  1357. continue;
  1358. }
  1359. // Optimize advise from backbone
  1360. switch (argLen) {
  1361. case 1:
  1362. hItem.h.call(ctx);
  1363. break;
  1364. case 2:
  1365. hItem.h.call(ctx, args[1]);
  1366. break;
  1367. case 3:
  1368. hItem.h.call(ctx, args[1], args[2]);
  1369. break;
  1370. default:
  1371. // have more than 2 given arguments
  1372. hItem.h.apply(ctx, args);
  1373. break;
  1374. }
  1375. if (hItem.one) {
  1376. _h.splice(i, 1);
  1377. len--;
  1378. }
  1379. else {
  1380. i++;
  1381. }
  1382. }
  1383. }
  1384. eventProcessor && eventProcessor.afterTrigger
  1385. && eventProcessor.afterTrigger(type);
  1386. return this;
  1387. }
  1388. };
  1389. function normalizeQuery(host, query) {
  1390. var eventProcessor = host._$eventProcessor;
  1391. if (query != null && eventProcessor && eventProcessor.normalizeQuery) {
  1392. query = eventProcessor.normalizeQuery(query);
  1393. }
  1394. return query;
  1395. }
  1396. function on(eventful, event, query, handler, context, isOnce) {
  1397. var _h = eventful._$handlers;
  1398. if (typeof query === 'function') {
  1399. context = handler;
  1400. handler = query;
  1401. query = null;
  1402. }
  1403. if (!handler || !event) {
  1404. return eventful;
  1405. }
  1406. query = normalizeQuery(eventful, query);
  1407. if (!_h[event]) {
  1408. _h[event] = [];
  1409. }
  1410. for (var i = 0; i < _h[event].length; i++) {
  1411. if (_h[event][i].h === handler) {
  1412. return eventful;
  1413. }
  1414. }
  1415. var wrap = {
  1416. h: handler,
  1417. one: isOnce,
  1418. query: query,
  1419. ctx: context || eventful,
  1420. // FIXME
  1421. // Do not publish this feature util it is proved that it makes sense.
  1422. callAtLast: handler.zrEventfulCallAtLast
  1423. };
  1424. var lastIndex = _h[event].length - 1;
  1425. var lastWrap = _h[event][lastIndex];
  1426. (lastWrap && lastWrap.callAtLast)
  1427. ? _h[event].splice(lastIndex, 0, wrap)
  1428. : _h[event].push(wrap);
  1429. return eventful;
  1430. }
  1431. /**
  1432. * The algoritm is learnt from
  1433. * https://franklinta.com/2014/09/08/computing-css-matrix3d-transforms/
  1434. * And we made some optimization for matrix inversion.
  1435. * Other similar approaches:
  1436. * "cv::getPerspectiveTransform", "Direct Linear Transformation".
  1437. */
  1438. var LN2 = Math.log(2);
  1439. function determinant(rows, rank, rowStart, rowMask, colMask, detCache) {
  1440. var cacheKey = rowMask + '-' + colMask;
  1441. var fullRank = rows.length;
  1442. if (detCache.hasOwnProperty(cacheKey)) {
  1443. return detCache[cacheKey];
  1444. }
  1445. if (rank === 1) {
  1446. // In this case the colMask must be like: `11101111`. We can find the place of `0`.
  1447. var colStart = Math.round(Math.log(((1 << fullRank) - 1) & ~colMask) / LN2);
  1448. return rows[rowStart][colStart];
  1449. }
  1450. var subRowMask = rowMask | (1 << rowStart);
  1451. var subRowStart = rowStart + 1;
  1452. while (rowMask & (1 << subRowStart)) {
  1453. subRowStart++;
  1454. }
  1455. var sum = 0;
  1456. for (var j = 0, colLocalIdx = 0; j < fullRank; j++) {
  1457. var colTag = 1 << j;
  1458. if (!(colTag & colMask)) {
  1459. sum += (colLocalIdx % 2 ? -1 : 1) * rows[rowStart][j]
  1460. // det(subMatrix(0, j))
  1461. * determinant(rows, rank - 1, subRowStart, subRowMask, colMask | colTag, detCache);
  1462. colLocalIdx++;
  1463. }
  1464. }
  1465. detCache[cacheKey] = sum;
  1466. return sum;
  1467. }
  1468. /**
  1469. * Usage:
  1470. * ```js
  1471. * var transformer = buildTransformer(
  1472. * [10, 44, 100, 44, 100, 300, 10, 300],
  1473. * [50, 54, 130, 14, 140, 330, 14, 220]
  1474. * );
  1475. * var out = [];
  1476. * transformer && transformer([11, 33], out);
  1477. * ```
  1478. *
  1479. * Notice: `buildTransformer` may take more than 10ms in some Android device.
  1480. *
  1481. * @param {Array.<number>} src source four points, [x0, y0, x1, y1, x2, y2, x3, y3]
  1482. * @param {Array.<number>} dest destination four points, [x0, y0, x1, y1, x2, y2, x3, y3]
  1483. * @return {Function} transformer If fail, return null/undefined.
  1484. */
  1485. function buildTransformer(src, dest) {
  1486. var mA = [
  1487. [src[0], src[1], 1, 0, 0, 0, -dest[0] * src[0], -dest[0] * src[1]],
  1488. [0, 0, 0, src[0], src[1], 1, -dest[1] * src[0], -dest[1] * src[1]],
  1489. [src[2], src[3], 1, 0, 0, 0, -dest[2] * src[2], -dest[2] * src[3]],
  1490. [0, 0, 0, src[2], src[3], 1, -dest[3] * src[2], -dest[3] * src[3]],
  1491. [src[4], src[5], 1, 0, 0, 0, -dest[4] * src[4], -dest[4] * src[5]],
  1492. [0, 0, 0, src[4], src[5], 1, -dest[5] * src[4], -dest[5] * src[5]],
  1493. [src[6], src[7], 1, 0, 0, 0, -dest[6] * src[6], -dest[6] * src[7]],
  1494. [0, 0, 0, src[6], src[7], 1, -dest[7] * src[6], -dest[7] * src[7]]
  1495. ];
  1496. var detCache = {};
  1497. var det = determinant(mA, 8, 0, 0, 0, detCache);
  1498. if (det === 0) {
  1499. // can not make transformer when and only when
  1500. // any three of the markers are collinear.
  1501. return;
  1502. }
  1503. // `invert(mA) * dest`, that is, `adj(mA) / det * dest`.
  1504. var vh = [];
  1505. for (var i = 0; i < 8; i++) {
  1506. for (var j = 0; j < 8; j++) {
  1507. vh[j] == null && (vh[j] = 0);
  1508. vh[j] += ((i + j) % 2 ? -1 : 1)
  1509. // det(subMatrix(i, j))
  1510. * determinant(mA, 7, i === 0 ? 1 : 0, 1 << i, 1 << j, detCache)
  1511. / det * dest[i];
  1512. }
  1513. }
  1514. return function (out, srcPointX, srcPointY) {
  1515. var pk = srcPointX * vh[6] + srcPointY * vh[7] + 1;
  1516. out[0] = (srcPointX * vh[0] + srcPointY * vh[1] + vh[2]) / pk;
  1517. out[1] = (srcPointX * vh[3] + srcPointY * vh[4] + vh[5]) / pk;
  1518. };
  1519. }
  1520. var EVENT_SAVED_PROP = '___zrEVENTSAVED';
  1521. /**
  1522. * Transform "local coord" from `elFrom` to `elTarget`.
  1523. * "local coord": the coord based on the input `el`. The origin point is at
  1524. * the position of "left: 0; top: 0;" in the `el`.
  1525. *
  1526. * Support when CSS transform is used.
  1527. *
  1528. * Having the `out` (that is, `[outX, outY]`), we can create an DOM element
  1529. * and set the CSS style as "left: outX; top: outY;" and append it to `elTarge`
  1530. * to locate the element.
  1531. *
  1532. * For example, this code below positions a child of `document.body` on the event
  1533. * point, no matter whether `body` has `margin`/`paddin`/`transfrom`/... :
  1534. * ```js
  1535. * transformLocalCoord(out, container, document.body, event.offsetX, event.offsetY);
  1536. * if (!eqNaN(out[0])) {
  1537. * // Then locate the tip element on the event point.
  1538. * var tipEl = document.createElement('div');
  1539. * tipEl.style.cssText = 'position: absolute; left:' + out[0] + ';top:' + out[1] + ';';
  1540. * document.body.appendChild(tipEl);
  1541. * }
  1542. * ```
  1543. *
  1544. * Notice: In some env this method is not supported. If called, `out` will be `[NaN, NaN]`.
  1545. *
  1546. * @param {Array.<number>} out [inX: number, inY: number] The output..
  1547. * If can not transform, `out` will not be modified but return `false`.
  1548. * @param {HTMLElement} elFrom The `[inX, inY]` is based on elFrom.
  1549. * @param {HTMLElement} elTarget The `out` is based on elTarget.
  1550. * @param {number} inX
  1551. * @param {number} inY
  1552. * @return {boolean} Whether transform successfully.
  1553. */
  1554. /**
  1555. * Transform between a "viewport coord" and a "local coord".
  1556. * "viewport coord": the coord based on the left-top corner of the viewport
  1557. * of the browser.
  1558. * "local coord": the coord based on the input `el`. The origin point is at
  1559. * the position of "left: 0; top: 0;" in the `el`.
  1560. *
  1561. * Support the case when CSS transform is used on el.
  1562. *
  1563. * @param {Array.<number>} out [inX: number, inY: number] The output. If `inverse: false`,
  1564. * it represents "local coord", otherwise "vireport coord".
  1565. * If can not transform, `out` will not be modified but return `false`.
  1566. * @param {HTMLElement} el The "local coord" is based on the `el`, see comment above.
  1567. * @param {number} inX If `inverse: false`,
  1568. * it represents "vireport coord", otherwise "local coord".
  1569. * @param {number} inY If `inverse: false`,
  1570. * it represents "vireport coord", otherwise "local coord".
  1571. * @param {boolean} [inverse=false]
  1572. * `true`: from "viewport coord" to "local coord".
  1573. * `false`: from "local coord" to "viewport coord".
  1574. * @return {boolean} Whether transform successfully.
  1575. */
  1576. function transformCoordWithViewport(out, el, inX, inY, inverse) {
  1577. if (el.getBoundingClientRect && env$1.domSupported && !isCanvasEl(el)) {
  1578. var saved = el[EVENT_SAVED_PROP] || (el[EVENT_SAVED_PROP] = {});
  1579. var markers = prepareCoordMarkers(el, saved);
  1580. var transformer = preparePointerTransformer(markers, saved, inverse);
  1581. if (transformer) {
  1582. transformer(out, inX, inY);
  1583. return true;
  1584. }
  1585. }
  1586. return false;
  1587. }
  1588. function prepareCoordMarkers(el, saved) {
  1589. var markers = saved.markers;
  1590. if (markers) {
  1591. return markers;
  1592. }
  1593. markers = saved.markers = [];
  1594. var propLR = ['left', 'right'];
  1595. var propTB = ['top', 'bottom'];
  1596. for (var i = 0; i < 4; i++) {
  1597. var marker = document.createElement('div');
  1598. var stl = marker.style;
  1599. var idxLR = i % 2;
  1600. var idxTB = (i >> 1) % 2;
  1601. stl.cssText = [
  1602. 'position: absolute',
  1603. 'visibility: hidden',
  1604. 'padding: 0',
  1605. 'margin: 0',
  1606. 'border-width: 0',
  1607. 'user-select: none',
  1608. 'width:0',
  1609. 'height:0',
  1610. // 'width: 5px',
  1611. // 'height: 5px',
  1612. propLR[idxLR] + ':0',
  1613. propTB[idxTB] + ':0',
  1614. propLR[1 - idxLR] + ':auto',
  1615. propTB[1 - idxTB] + ':auto',
  1616. ''
  1617. ].join('!important;');
  1618. el.appendChild(marker);
  1619. markers.push(marker);
  1620. }
  1621. return markers;
  1622. }
  1623. function preparePointerTransformer(markers, saved, inverse) {
  1624. var transformerName = inverse ? 'invTrans' : 'trans';
  1625. var transformer = saved[transformerName];
  1626. var oldSrcCoords = saved.srcCoords;
  1627. var oldCoordTheSame = true;
  1628. var srcCoords = [];
  1629. var destCoords = [];
  1630. for (var i = 0; i < 4; i++) {
  1631. var rect = markers[i].getBoundingClientRect();
  1632. var ii = 2 * i;
  1633. var x = rect.left;
  1634. var y = rect.top;
  1635. srcCoords.push(x, y);
  1636. oldCoordTheSame = oldCoordTheSame && oldSrcCoords && x === oldSrcCoords[ii] && y === oldSrcCoords[ii + 1];
  1637. destCoords.push(markers[i].offsetLeft, markers[i].offsetTop);
  1638. }
  1639. // Cache to avoid time consuming of `buildTransformer`.
  1640. return (oldCoordTheSame && transformer)
  1641. ? transformer
  1642. : (
  1643. saved.srcCoords = srcCoords,
  1644. saved[transformerName] = inverse
  1645. ? buildTransformer(destCoords, srcCoords)
  1646. : buildTransformer(srcCoords, destCoords)
  1647. );
  1648. }
  1649. function isCanvasEl(el) {
  1650. return el.nodeName.toUpperCase() === 'CANVAS';
  1651. }
  1652. /**
  1653. * Utilities for mouse or touch events.
  1654. */
  1655. var isDomLevel2 = (typeof window !== 'undefined') && !!window.addEventListener;
  1656. var MOUSE_EVENT_REG = /^(?:mouse|pointer|contextmenu|drag|drop)|click/;
  1657. var _calcOut = [];
  1658. /**
  1659. * Get the `zrX` and `zrY`, which are relative to the top-left of
  1660. * the input `el`.
  1661. * CSS transform (2D & 3D) is supported.
  1662. *
  1663. * The strategy to fetch the coords:
  1664. * + If `calculate` is not set as `true`, users of this method should
  1665. * ensure that `el` is the same or the same size & location as `e.target`.
  1666. * Otherwise the result coords are probably not expected. Because we
  1667. * firstly try to get coords from e.offsetX/e.offsetY.
  1668. * + If `calculate` is set as `true`, the input `el` can be any element
  1669. * and we force to calculate the coords based on `el`.
  1670. * + The input `el` should be positionable (not position:static).
  1671. *
  1672. * The force `calculate` can be used in case like:
  1673. * When mousemove event triggered on ec tooltip, `e.target` is not `el`(zr painter.dom).
  1674. *
  1675. * @param {HTMLElement} el DOM element.
  1676. * @param {Event} e Mouse event or touch event.
  1677. * @param {Object} out Get `out.zrX` and `out.zrY` as the result.
  1678. * @param {boolean} [calculate=false] Whether to force calculate
  1679. * the coordinates but not use ones provided by browser.
  1680. */
  1681. function clientToLocal(el, e, out, calculate) {
  1682. out = out || {};
  1683. // According to the W3C Working Draft, offsetX and offsetY should be relative
  1684. // to the padding edge of the target element. The only browser using this convention
  1685. // is IE. Webkit uses the border edge, Opera uses the content edge, and FireFox does
  1686. // not support the properties.
  1687. // (see http://www.jacklmoore.com/notes/mouse-position/)
  1688. // In zr painter.dom, padding edge equals to border edge.
  1689. if (calculate || !env$1.canvasSupported) {
  1690. calculateZrXY(el, e, out);
  1691. }
  1692. // Caution: In FireFox, layerX/layerY Mouse position relative to the closest positioned
  1693. // ancestor element, so we should make sure el is positioned (e.g., not position:static).
  1694. // BTW1, Webkit don't return the same results as FF in non-simple cases (like add
  1695. // zoom-factor, overflow / opacity layers, transforms ...)
  1696. // BTW2, (ev.offsetY || ev.pageY - $(ev.target).offset().top) is not correct in preserve-3d.
  1697. // <https://bugs.jquery.com/ticket/8523#comment:14>
  1698. // BTW3, In ff, offsetX/offsetY is always 0.
  1699. else if (env$1.browser.firefox && e.layerX != null && e.layerX !== e.offsetX) {
  1700. out.zrX = e.layerX;
  1701. out.zrY = e.layerY;
  1702. }
  1703. // For IE6+, chrome, safari, opera. (When will ff support offsetX?)
  1704. else if (e.offsetX != null) {
  1705. out.zrX = e.offsetX;
  1706. out.zrY = e.offsetY;
  1707. }
  1708. // For some other device, e.g., IOS safari.
  1709. else {
  1710. calculateZrXY(el, e, out);
  1711. }
  1712. return out;
  1713. }
  1714. function calculateZrXY(el, e, out) {
  1715. // BlackBerry 5, iOS 3 (original iPhone) don't have getBoundingRect.
  1716. if (env$1.domSupported && el.getBoundingClientRect) {
  1717. var ex = e.clientX;
  1718. var ey = e.clientY;
  1719. if (isCanvasEl(el)) {
  1720. // Original approach, which do not support CSS transform.
  1721. // marker can not be locationed in a canvas container
  1722. // (getBoundingClientRect is always 0). We do not support
  1723. // that input a pre-created canvas to zr while using css
  1724. // transform in iOS.
  1725. var box = el.getBoundingClientRect();
  1726. out.zrX = ex - box.left;
  1727. out.zrY = ey - box.top;
  1728. return;
  1729. }
  1730. else {
  1731. if (transformCoordWithViewport(_calcOut, el, ex, ey)) {
  1732. out.zrX = _calcOut[0];
  1733. out.zrY = _calcOut[1];
  1734. return;
  1735. }
  1736. }
  1737. }
  1738. out.zrX = out.zrY = 0;
  1739. }
  1740. /**
  1741. * Find native event compat for legency IE.
  1742. * Should be called at the begining of a native event listener.
  1743. *
  1744. * @param {Event} [e] Mouse event or touch event or pointer event.
  1745. * For lagency IE, we use `window.event` is used.
  1746. * @return {Event} The native event.
  1747. */
  1748. function getNativeEvent(e) {
  1749. return e || window.event;
  1750. }
  1751. /**
  1752. * Normalize the coordinates of the input event.
  1753. *
  1754. * Get the `e.zrX` and `e.zrY`, which are relative to the top-left of
  1755. * the input `el`.
  1756. * Get `e.zrDelta` if using mouse wheel.
  1757. * Get `e.which`, see the comment inside this function.
  1758. *
  1759. * Do not calculate repeatly if `zrX` and `zrY` already exist.
  1760. *
  1761. * Notice: see comments in `clientToLocal`. check the relationship
  1762. * between the result coords and the parameters `el` and `calculate`.
  1763. *
  1764. * @param {HTMLElement} el DOM element.
  1765. * @param {Event} [e] See `getNativeEvent`.
  1766. * @param {boolean} [calculate=false] Whether to force calculate
  1767. * the coordinates but not use ones provided by browser.
  1768. * @return {UIEvent} The normalized native UIEvent.
  1769. */
  1770. function normalizeEvent(el, e, calculate) {
  1771. e = getNativeEvent(e);
  1772. if (e.zrX != null) {
  1773. return e;
  1774. }
  1775. var eventType = e.type;
  1776. var isTouch = eventType && eventType.indexOf('touch') >= 0;
  1777. if (!isTouch) {
  1778. clientToLocal(el, e, e, calculate);
  1779. e.zrDelta = (e.wheelDelta) ? e.wheelDelta / 120 : -(e.detail || 0) / 3;
  1780. }
  1781. else {
  1782. var touch = eventType !== 'touchend'
  1783. ? e.targetTouches[0]
  1784. : e.changedTouches[0];
  1785. touch && clientToLocal(el, touch, e, calculate);
  1786. }
  1787. // Add which for click: 1 === left; 2 === middle; 3 === right; otherwise: 0;
  1788. // See jQuery: https://github.com/jquery/jquery/blob/master/src/event.js
  1789. // If e.which has been defined, it may be readonly,
  1790. // see: https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/which
  1791. var button = e.button;
  1792. if (e.which == null && button !== undefined && MOUSE_EVENT_REG.test(e.type)) {
  1793. e.which = (button & 1 ? 1 : (button & 2 ? 3 : (button & 4 ? 2 : 0)));
  1794. }
  1795. // [Caution]: `e.which` from browser is not always reliable. For example,
  1796. // when press left button and `mousemove (pointermove)` in Edge, the `e.which`
  1797. // is 65536 and the `e.button` is -1. But the `mouseup (pointerup)` and
  1798. // `mousedown (pointerdown)` is the same as Chrome does.
  1799. return e;
  1800. }
  1801. /**
  1802. * @param {HTMLElement} el
  1803. * @param {string} name
  1804. * @param {Function} handler
  1805. * @param {Object|boolean} opt If boolean, means `opt.capture`
  1806. * @param {boolean} [opt.capture=false]
  1807. * @param {boolean} [opt.passive=false]
  1808. */
  1809. function addEventListener(el, name, handler, opt) {
  1810. if (isDomLevel2) {
  1811. // Reproduct the console warning:
  1812. // [Violation] Added non-passive event listener to a scroll-blocking <some> event.
  1813. // Consider marking event handler as 'passive' to make the page more responsive.
  1814. // Just set console log level: verbose in chrome dev tool.
  1815. // then the warning log will be printed when addEventListener called.
  1816. // See https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md
  1817. // We have not yet found a neat way to using passive. Because in zrender the dom event
  1818. // listener delegate all of the upper events of element. Some of those events need
  1819. // to prevent default. For example, the feature `preventDefaultMouseMove` of echarts.
  1820. // Before passive can be adopted, these issues should be considered:
  1821. // (1) Whether and how a zrender user specifies an event listener passive. And by default,
  1822. // passive or not.
  1823. // (2) How to tread that some zrender event listener is passive, and some is not. If
  1824. // we use other way but not preventDefault of mousewheel and touchmove, browser
  1825. // compatibility should be handled.
  1826. // var opts = (env.passiveSupported && name === 'mousewheel')
  1827. // ? {passive: true}
  1828. // // By default, the third param of el.addEventListener is `capture: false`.
  1829. // : void 0;
  1830. // el.addEventListener(name, handler /* , opts */);
  1831. el.addEventListener(name, handler, opt);
  1832. }
  1833. else {
  1834. // For simplicity, do not implement `setCapture` for IE9-.
  1835. el.attachEvent('on' + name, handler);
  1836. }
  1837. }
  1838. /**
  1839. * Parameter are the same as `addEventListener`.
  1840. *
  1841. * Notice that if a listener is registered twice, one with capture and one without,
  1842. * remove each one separately. Removal of a capturing listener does not affect a
  1843. * non-capturing version of the same listener, and vice versa.
  1844. */
  1845. function removeEventListener(el, name, handler, opt) {
  1846. if (isDomLevel2) {
  1847. el.removeEventListener(name, handler, opt);
  1848. }
  1849. else {
  1850. el.detachEvent('on' + name, handler);
  1851. }
  1852. }
  1853. /**
  1854. * preventDefault and stopPropagation.
  1855. * Notice: do not use this method in zrender. It can only be
  1856. * used by upper applications if necessary.
  1857. *
  1858. * @param {Event} e A mouse or touch event.
  1859. */
  1860. var stop = isDomLevel2
  1861. ? function (e) {
  1862. e.preventDefault();
  1863. e.stopPropagation();
  1864. e.cancelBubble = true;
  1865. }
  1866. : function (e) {
  1867. e.returnValue = false;
  1868. e.cancelBubble = true;
  1869. };
  1870. /**
  1871. * This method only works for mouseup and mousedown. The functionality is restricted
  1872. * for fault tolerance, See the `e.which` compatibility above.
  1873. *
  1874. * @param {MouseEvent} e
  1875. * @return {boolean}
  1876. */
  1877. /**
  1878. * To be removed.
  1879. * @deprecated
  1880. */
  1881. /**
  1882. * Only implements needed gestures for mobile.
  1883. */
  1884. var GestureMgr = function () {
  1885. /**
  1886. * @private
  1887. * @type {Array.<Object>}
  1888. */
  1889. this._track = [];
  1890. };
  1891. GestureMgr.prototype = {
  1892. constructor: GestureMgr,
  1893. recognize: function (event, target, root) {
  1894. this._doTrack(event, target, root);
  1895. return this._recognize(event);
  1896. },
  1897. clear: function () {
  1898. this._track.length = 0;
  1899. return this;
  1900. },
  1901. _doTrack: function (event, target, root) {
  1902. var touches = event.touches;
  1903. if (!touches) {
  1904. return;
  1905. }
  1906. var trackItem = {
  1907. points: [],
  1908. touches: [],
  1909. target: target,
  1910. event: event
  1911. };
  1912. for (var i = 0, len = touches.length; i < len; i++) {
  1913. var touch = touches[i];
  1914. var pos = clientToLocal(root, touch, {});
  1915. trackItem.points.push([pos.zrX, pos.zrY]);
  1916. trackItem.touches.push(touch);
  1917. }
  1918. this._track.push(trackItem);
  1919. },
  1920. _recognize: function (event) {
  1921. for (var eventName in recognizers) {
  1922. if (recognizers.hasOwnProperty(eventName)) {
  1923. var gestureInfo = recognizers[eventName](this._track, event);
  1924. if (gestureInfo) {
  1925. return gestureInfo;
  1926. }
  1927. }
  1928. }
  1929. }
  1930. };
  1931. function dist$1(pointPair) {
  1932. var dx = pointPair[1][0] - pointPair[0][0];
  1933. var dy = pointPair[1][1] - pointPair[0][1];
  1934. return Math.sqrt(dx * dx + dy * dy);
  1935. }
  1936. function center(pointPair) {
  1937. return [
  1938. (pointPair[0][0] + pointPair[1][0]) / 2,
  1939. (pointPair[0][1] + pointPair[1][1]) / 2
  1940. ];
  1941. }
  1942. var recognizers = {
  1943. pinch: function (track, event) {
  1944. var trackLen = track.length;
  1945. if (!trackLen) {
  1946. return;
  1947. }
  1948. var pinchEnd = (track[trackLen - 1] || {}).points;
  1949. var pinchPre = (track[trackLen - 2] || {}).points || pinchEnd;
  1950. if (pinchPre
  1951. && pinchPre.length > 1
  1952. && pinchEnd
  1953. && pinchEnd.length > 1
  1954. ) {
  1955. var pinchScale = dist$1(pinchEnd) / dist$1(pinchPre);
  1956. !isFinite(pinchScale) && (pinchScale = 1);
  1957. event.pinchScale = pinchScale;
  1958. var pinchCenter = center(pinchEnd);
  1959. event.pinchX = pinchCenter[0];
  1960. event.pinchY = pinchCenter[1];
  1961. return {
  1962. type: 'pinch',
  1963. target: track[0].target,
  1964. event: event
  1965. };
  1966. }
  1967. }
  1968. // Only pinch currently.
  1969. };
  1970. /**
  1971. * [The interface between `Handler` and `HandlerProxy`]:
  1972. *
  1973. * The default `HandlerProxy` only support the common standard web environment
  1974. * (e.g., standalone browser, headless browser, embed browser in mobild APP, ...).
  1975. * But `HandlerProxy` can be replaced to support more non-standard environment
  1976. * (e.g., mini app), or to support more feature that the default `HandlerProxy`
  1977. * not provided (like echarts-gl did).
  1978. * So the interface between `Handler` and `HandlerProxy` should be stable. Do not
  1979. * make break changes util inevitable. The interface include the public methods
  1980. * of `Handler` and the events listed in `handlerNames` below, by which `HandlerProxy`
  1981. * drives `Handler`.
  1982. */
  1983. /**
  1984. * [Drag outside]:
  1985. *
  1986. * That is, triggering `mousemove` and `mouseup` event when the pointer is out of the
  1987. * zrender area when dragging. That is important for the improvement of the user experience
  1988. * when dragging something near the boundary without being terminated unexpectedly.
  1989. *
  1990. * We originally consider to introduce new events like `pagemovemove` and `pagemouseup`
  1991. * to resolve this issue. But some drawbacks of it is described in
  1992. * https://github.com/ecomfe/zrender/pull/536#issuecomment-560286899
  1993. *
  1994. * Instead, we referenced the specifications:
  1995. * https://www.w3.org/TR/touch-events/#the-touchmove-event
  1996. * https://www.w3.org/TR/2014/WD-DOM-Level-3-Events-20140925/#event-type-mousemove
  1997. * where the the mousemove/touchmove can be continue to fire if the user began a drag
  1998. * operation and the pointer has left the boundary. (for the mouse event, browsers
  1999. * only do it on `document` and when the pointer has left the boundary of the browser.)
  2000. *
  2001. * So the default `HandlerProxy` supports this feature similarly: if it is in the dragging
  2002. * state (see `pointerCapture` in `HandlerProxy`), the `mousemove` and `mouseup` continue
  2003. * to fire until release the pointer. That is implemented by listen to those event on
  2004. * `document`.
  2005. * If we implement some other `HandlerProxy` only for touch device, that would be easier.
  2006. * The touch event support this feature by default.
  2007. *
  2008. * Note:
  2009. * There might be some cases that the mouse event can not be
  2010. * received on `document`. For example,
  2011. * (A) `useCapture` is not supported and some user defined event listeners on the ancestor
  2012. * of zr dom throw Error .
  2013. * (B) `useCapture` is not supported Some user defined event listeners on the ancestor of
  2014. * zr dom call `stopPropagation`.
  2015. * In these cases, the `mousemove` event might be keep triggered event
  2016. * if the mouse is released. We try to reduce the side-effect in those cases.
  2017. * That is, do nothing (especially, `findHover`) in those cases. See `isOutsideBoundary`.
  2018. *
  2019. * Note:
  2020. * If `HandlerProxy` listens to `document` with `useCapture`, `HandlerProxy` needs to
  2021. * make sure `stopPropagation` and `preventDefault` doing nothing if and only if the event
  2022. * target is not zrender dom. Becuase it is dangerous to enable users to call them in
  2023. * `document` capture phase to prevent the propagation to any listener of the webpage.
  2024. * But they are needed to work when the pointer inside the zrender dom.
  2025. */
  2026. var SILENT = 'silent';
  2027. function makeEventPacket(eveType, targetInfo, event) {
  2028. return {
  2029. type: eveType,
  2030. event: event,
  2031. // target can only be an element that is not silent.
  2032. target: targetInfo.target,
  2033. // topTarget can be a silent element.
  2034. topTarget: targetInfo.topTarget,
  2035. cancelBubble: false,
  2036. offsetX: event.zrX,
  2037. offsetY: event.zrY,
  2038. gestureEvent: event.gestureEvent,
  2039. pinchX: event.pinchX,
  2040. pinchY: event.pinchY,
  2041. pinchScale: event.pinchScale,
  2042. wheelDelta: event.zrDelta,
  2043. zrByTouch: event.zrByTouch,
  2044. which: event.which,
  2045. stop: stopEvent
  2046. };
  2047. }
  2048. function stopEvent() {
  2049. stop(this.event);
  2050. }
  2051. function EmptyProxy() {}
  2052. EmptyProxy.prototype.dispose = function () {};
  2053. var handlerNames = [
  2054. 'click', 'dblclick', 'mousewheel', 'mouseout',
  2055. 'mouseup', 'mousedown', 'mousemove', 'contextmenu'
  2056. ];
  2057. /**
  2058. * @alias module:zrender/Handler
  2059. * @constructor
  2060. * @extends module:zrender/mixin/Eventful
  2061. * @param {module:zrender/Storage} storage Storage instance.
  2062. * @param {module:zrender/Painter} painter Painter instance.
  2063. * @param {module:zrender/dom/HandlerProxy} proxy HandlerProxy instance.
  2064. * @param {HTMLElement} painterRoot painter.root (not painter.getViewportRoot()).
  2065. */
  2066. var Handler = function (storage, painter, proxy, painterRoot) {
  2067. Eventful.call(this);
  2068. this.storage = storage;
  2069. this.painter = painter;
  2070. this.painterRoot = painterRoot;
  2071. proxy = proxy || new EmptyProxy();
  2072. /**
  2073. * Proxy of event. can be Dom, WebGLSurface, etc.
  2074. */
  2075. this.proxy = null;
  2076. /**
  2077. * {target, topTarget, x, y}
  2078. * @private
  2079. * @type {Object}
  2080. */
  2081. this._hovered = {};
  2082. /**
  2083. * @private
  2084. * @type {Date}
  2085. */
  2086. this._lastTouchMoment;
  2087. /**
  2088. * @private
  2089. * @type {number}
  2090. */
  2091. this._lastX;
  2092. /**
  2093. * @private
  2094. * @type {number}
  2095. */
  2096. this._lastY;
  2097. /**
  2098. * @private
  2099. * @type {module:zrender/core/GestureMgr}
  2100. */
  2101. this._gestureMgr;
  2102. Draggable.call(this);
  2103. this.setHandlerProxy(proxy);
  2104. };
  2105. Handler.prototype = {
  2106. constructor: Handler,
  2107. setHandlerProxy: function (proxy) {
  2108. if (this.proxy) {
  2109. this.proxy.dispose();
  2110. }
  2111. if (proxy) {
  2112. each(handlerNames, function (name) {
  2113. proxy.on && proxy.on(name, this[name], this);
  2114. }, this);
  2115. // Attach handler
  2116. proxy.handler = this;
  2117. }
  2118. this.proxy = proxy;
  2119. },
  2120. mousemove: function (event) {
  2121. var x = event.zrX;
  2122. var y = event.zrY;
  2123. var isOutside = isOutsideBoundary(this, x, y);
  2124. var lastHovered = this._hovered;
  2125. var lastHoveredTarget = lastHovered.target;
  2126. // If lastHoveredTarget is removed from zr (detected by '__zr') by some API call
  2127. // (like 'setOption' or 'dispatchAction') in event handlers, we should find
  2128. // lastHovered again here. Otherwise 'mouseout' can not be triggered normally.
  2129. // See #6198.
  2130. if (lastHoveredTarget && !lastHoveredTarget.__zr) {
  2131. lastHovered = this.findHover(lastHovered.x, lastHovered.y);
  2132. lastHoveredTarget = lastHovered.target;
  2133. }
  2134. var hovered = this._hovered = isOutside ? {x: x, y: y} : this.findHover(x, y);
  2135. var hoveredTarget = hovered.target;
  2136. var proxy = this.proxy;
  2137. proxy.setCursor && proxy.setCursor(hoveredTarget ? hoveredTarget.cursor : 'default');
  2138. // Mouse out on previous hovered element
  2139. if (lastHoveredTarget && hoveredTarget !== lastHoveredTarget) {
  2140. this.dispatchToElement(lastHovered, 'mouseout', event);
  2141. }
  2142. // Mouse moving on one element
  2143. this.dispatchToElement(hovered, 'mousemove', event);
  2144. // Mouse over on a new element
  2145. if (hoveredTarget && hoveredTarget !== lastHoveredTarget) {
  2146. this.dispatchToElement(hovered, 'mouseover', event);
  2147. }
  2148. },
  2149. mouseout: function (event) {
  2150. var eventControl = event.zrEventControl;
  2151. var zrIsToLocalDOM = event.zrIsToLocalDOM;
  2152. if (eventControl !== 'only_globalout') {
  2153. this.dispatchToElement(this._hovered, 'mouseout', event);
  2154. }
  2155. if (eventControl !== 'no_globalout') {
  2156. // FIXME: if the pointer moving from the extra doms to realy "outside",
  2157. // the `globalout` should have been triggered. But currently not.
  2158. !zrIsToLocalDOM && this.trigger('globalout', {type: 'globalout', event: event});
  2159. }
  2160. },
  2161. /**
  2162. * Resize
  2163. */
  2164. resize: function (event) {
  2165. this._hovered = {};
  2166. },
  2167. /**
  2168. * Dispatch event
  2169. * @param {string} eventName
  2170. * @param {event=} eventArgs
  2171. */
  2172. dispatch: function (eventName, eventArgs) {
  2173. var handler = this[eventName];
  2174. handler && handler.call(this, eventArgs);
  2175. },
  2176. /**
  2177. * Dispose
  2178. */
  2179. dispose: function () {
  2180. this.proxy.dispose();
  2181. this.storage =
  2182. this.proxy =
  2183. this.painter = null;
  2184. },
  2185. /**
  2186. * 设置默认的cursor style
  2187. * @param {string} [cursorStyle='default'] 例如 crosshair
  2188. */
  2189. setCursorStyle: function (cursorStyle) {
  2190. var proxy = this.proxy;
  2191. proxy.setCursor && proxy.setCursor(cursorStyle);
  2192. },
  2193. /**
  2194. * 事件分发代理
  2195. *
  2196. * @private
  2197. * @param {Object} targetInfo {target, topTarget} 目标图形元素
  2198. * @param {string} eventName 事件名称
  2199. * @param {Object} event 事件对象
  2200. */
  2201. dispatchToElement: function (targetInfo, eventName, event) {
  2202. targetInfo = targetInfo || {};
  2203. var el = targetInfo.target;
  2204. if (el && el.silent) {
  2205. return;
  2206. }
  2207. var eventHandler = 'on' + eventName;
  2208. var eventPacket = makeEventPacket(eventName, targetInfo, event);
  2209. while (el) {
  2210. el[eventHandler]
  2211. && (eventPacket.cancelBubble = el[eventHandler].call(el, eventPacket));
  2212. el.trigger(eventName, eventPacket);
  2213. el = el.parent;
  2214. if (eventPacket.cancelBubble) {
  2215. break;
  2216. }
  2217. }
  2218. if (!eventPacket.cancelBubble) {
  2219. // 冒泡到顶级 zrender 对象
  2220. this.trigger(eventName, eventPacket);
  2221. // 分发事件到用户自定义层
  2222. // 用户有可能在全局 click 事件中 dispose,所以需要判断下 painter 是否存在
  2223. this.painter && this.painter.eachOtherLayer(function (layer) {
  2224. if (typeof (layer[eventHandler]) === 'function') {
  2225. layer[eventHandler].call(layer, eventPacket);
  2226. }
  2227. if (layer.trigger) {
  2228. layer.trigger(eventName, eventPacket);
  2229. }
  2230. });
  2231. }
  2232. },
  2233. /**
  2234. * @private
  2235. * @param {number} x
  2236. * @param {number} y
  2237. * @param {module:zrender/graphic/Displayable} exclude
  2238. * @return {model:zrender/Element}
  2239. * @method
  2240. */
  2241. findHover: function (x, y, exclude) {
  2242. var list = this.storage.getDisplayList();
  2243. var out = {x: x, y: y};
  2244. for (var i = list.length - 1; i >= 0; i--) {
  2245. var hoverCheckResult;
  2246. if (list[i] !== exclude
  2247. // getDisplayList may include ignored item in VML mode
  2248. && !list[i].ignore
  2249. && (hoverCheckResult = isHover(list[i], x, y))
  2250. ) {
  2251. !out.topTarget && (out.topTarget = list[i]);
  2252. if (hoverCheckResult !== SILENT) {
  2253. out.target = list[i];
  2254. break;
  2255. }
  2256. }
  2257. }
  2258. return out;
  2259. },
  2260. processGesture: function (event, stage) {
  2261. if (!this._gestureMgr) {
  2262. this._gestureMgr = new GestureMgr();
  2263. }
  2264. var gestureMgr = this._gestureMgr;
  2265. stage === 'start' && gestureMgr.clear();
  2266. var gestureInfo = gestureMgr.recognize(
  2267. event,
  2268. this.findHover(event.zrX, event.zrY, null).target,
  2269. this.proxy.dom
  2270. );
  2271. stage === 'end' && gestureMgr.clear();
  2272. // Do not do any preventDefault here. Upper application do that if necessary.
  2273. if (gestureInfo) {
  2274. var type = gestureInfo.type;
  2275. event.gestureEvent = type;
  2276. this.dispatchToElement({target: gestureInfo.target}, type, gestureInfo.event);
  2277. }
  2278. }
  2279. };
  2280. // Common handlers
  2281. each(['click', 'mousedown', 'mouseup', 'mousewheel', 'dblclick', 'contextmenu'], function (name) {
  2282. Handler.prototype[name] = function (event) {
  2283. var x = event.zrX;
  2284. var y = event.zrY;
  2285. var isOutside = isOutsideBoundary(this, x, y);
  2286. var hovered;
  2287. var hoveredTarget;
  2288. if (name !== 'mouseup' || !isOutside) {
  2289. // Find hover again to avoid click event is dispatched manually. Or click is triggered without mouseover
  2290. hovered = this.findHover(x, y);
  2291. hoveredTarget = hovered.target;
  2292. }
  2293. if (name === 'mousedown') {
  2294. this._downEl = hoveredTarget;
  2295. this._downPoint = [event.zrX, event.zrY];
  2296. // In case click triggered before mouseup
  2297. this._upEl = hoveredTarget;
  2298. }
  2299. else if (name === 'mouseup') {
  2300. this._upEl = hoveredTarget;
  2301. }
  2302. else if (name === 'click') {
  2303. if (this._downEl !== this._upEl
  2304. // Original click event is triggered on the whole canvas element,
  2305. // including the case that `mousedown` - `mousemove` - `mouseup`,
  2306. // which should be filtered, otherwise it will bring trouble to
  2307. // pan and zoom.
  2308. || !this._downPoint
  2309. // Arbitrary value
  2310. || dist(this._downPoint, [event.zrX, event.zrY]) > 4
  2311. ) {
  2312. return;
  2313. }
  2314. this._downPoint = null;
  2315. }
  2316. this.dispatchToElement(hovered, name, event);
  2317. };
  2318. });
  2319. function isHover(displayable, x, y) {
  2320. if (displayable[displayable.rectHover ? 'rectContain' : 'contain'](x, y)) {
  2321. var el = displayable;
  2322. var isSilent;
  2323. while (el) {
  2324. // If clipped by ancestor.
  2325. // FIXME: If clipPath has neither stroke nor fill,
  2326. // el.clipPath.contain(x, y) will always return false.
  2327. if (el.clipPath && !el.clipPath.contain(x, y)) {
  2328. return false;
  2329. }
  2330. if (el.silent) {
  2331. isSilent = true;
  2332. }
  2333. el = el.parent;
  2334. }
  2335. return isSilent ? SILENT : true;
  2336. }
  2337. return false;
  2338. }
  2339. /**
  2340. * See [Drag outside].
  2341. */
  2342. function isOutsideBoundary(handlerInstance, x, y) {
  2343. var painter = handlerInstance.painter;
  2344. return x < 0 || x > painter.getWidth() || y < 0 || y > painter.getHeight();
  2345. }
  2346. mixin(Handler, Eventful);
  2347. mixin(Handler, Draggable);
  2348. /**
  2349. * 3x2矩阵操作类
  2350. * @exports zrender/tool/matrix
  2351. */
  2352. /* global Float32Array */
  2353. var ArrayCtor$1 = typeof Float32Array === 'undefined'
  2354. ? Array
  2355. : Float32Array;
  2356. /**
  2357. * Create a identity matrix.
  2358. * @return {Float32Array|Array.<number>}
  2359. */
  2360. function create$1() {
  2361. var out = new ArrayCtor$1(6);
  2362. identity(out);
  2363. return out;
  2364. }
  2365. /**
  2366. * 设置矩阵为单位矩阵
  2367. * @param {Float32Array|Array.<number>} out
  2368. */
  2369. function identity(out) {
  2370. out[0] = 1;
  2371. out[1] = 0;
  2372. out[2] = 0;
  2373. out[3] = 1;
  2374. out[4] = 0;
  2375. out[5] = 0;
  2376. return out;
  2377. }
  2378. /**
  2379. * 复制矩阵
  2380. * @param {Float32Array|Array.<number>} out
  2381. * @param {Float32Array|Array.<number>} m
  2382. */
  2383. function copy$1(out, m) {
  2384. out[0] = m[0];
  2385. out[1] = m[1];
  2386. out[2] = m[2];
  2387. out[3] = m[3];
  2388. out[4] = m[4];
  2389. out[5] = m[5];
  2390. return out;
  2391. }
  2392. /**
  2393. * 矩阵相乘
  2394. * @param {Float32Array|Array.<number>} out
  2395. * @param {Float32Array|Array.<number>} m1
  2396. * @param {Float32Array|Array.<number>} m2
  2397. */
  2398. function mul$1(out, m1, m2) {
  2399. // Consider matrix.mul(m, m2, m);
  2400. // where out is the same as m2.
  2401. // So use temp variable to escape error.
  2402. var out0 = m1[0] * m2[0] + m1[2] * m2[1];
  2403. var out1 = m1[1] * m2[0] + m1[3] * m2[1];
  2404. var out2 = m1[0] * m2[2] + m1[2] * m2[3];
  2405. var out3 = m1[1] * m2[2] + m1[3] * m2[3];
  2406. var out4 = m1[0] * m2[4] + m1[2] * m2[5] + m1[4];
  2407. var out5 = m1[1] * m2[4] + m1[3] * m2[5] + m1[5];
  2408. out[0] = out0;
  2409. out[1] = out1;
  2410. out[2] = out2;
  2411. out[3] = out3;
  2412. out[4] = out4;
  2413. out[5] = out5;
  2414. return out;
  2415. }
  2416. /**
  2417. * 平移变换
  2418. * @param {Float32Array|Array.<number>} out
  2419. * @param {Float32Array|Array.<number>} a
  2420. * @param {Float32Array|Array.<number>} v
  2421. */
  2422. function translate(out, a, v) {
  2423. out[0] = a[0];
  2424. out[1] = a[1];
  2425. out[2] = a[2];
  2426. out[3] = a[3];
  2427. out[4] = a[4] + v[0];
  2428. out[5] = a[5] + v[1];
  2429. return out;
  2430. }
  2431. /**
  2432. * 旋转变换
  2433. * @param {Float32Array|Array.<number>} out
  2434. * @param {Float32Array|Array.<number>} a
  2435. * @param {number} rad
  2436. */
  2437. function rotate(out, a, rad) {
  2438. var aa = a[0];
  2439. var ac = a[2];
  2440. var atx = a[4];
  2441. var ab = a[1];
  2442. var ad = a[3];
  2443. var aty = a[5];
  2444. var st = Math.sin(rad);
  2445. var ct = Math.cos(rad);
  2446. out[0] = aa * ct + ab * st;
  2447. out[1] = -aa * st + ab * ct;
  2448. out[2] = ac * ct + ad * st;
  2449. out[3] = -ac * st + ct * ad;
  2450. out[4] = ct * atx + st * aty;
  2451. out[5] = ct * aty - st * atx;
  2452. return out;
  2453. }
  2454. /**
  2455. * 缩放变换
  2456. * @param {Float32Array|Array.<number>} out
  2457. * @param {Float32Array|Array.<number>} a
  2458. * @param {Float32Array|Array.<number>} v
  2459. */
  2460. function scale$1(out, a, v) {
  2461. var vx = v[0];
  2462. var vy = v[1];
  2463. out[0] = a[0] * vx;
  2464. out[1] = a[1] * vy;
  2465. out[2] = a[2] * vx;
  2466. out[3] = a[3] * vy;
  2467. out[4] = a[4] * vx;
  2468. out[5] = a[5] * vy;
  2469. return out;
  2470. }
  2471. /**
  2472. * 求逆矩阵
  2473. * @param {Float32Array|Array.<number>} out
  2474. * @param {Float32Array|Array.<number>} a
  2475. */
  2476. function invert(out, a) {
  2477. var aa = a[0];
  2478. var ac = a[2];
  2479. var atx = a[4];
  2480. var ab = a[1];
  2481. var ad = a[3];
  2482. var aty = a[5];
  2483. var det = aa * ad - ab * ac;
  2484. if (!det) {
  2485. return null;
  2486. }
  2487. det = 1.0 / det;
  2488. out[0] = ad * det;
  2489. out[1] = -ab * det;
  2490. out[2] = -ac * det;
  2491. out[3] = aa * det;
  2492. out[4] = (ac * aty - ad * atx) * det;
  2493. out[5] = (ab * atx - aa * aty) * det;
  2494. return out;
  2495. }
  2496. /**
  2497. * Clone a new matrix.
  2498. * @param {Float32Array|Array.<number>} a
  2499. */
  2500. function clone$2(a) {
  2501. var b = create$1();
  2502. copy$1(b, a);
  2503. return b;
  2504. }
  2505. var matrix = (Object.freeze || Object)({
  2506. create: create$1,
  2507. identity: identity,
  2508. copy: copy$1,
  2509. mul: mul$1,
  2510. translate: translate,
  2511. rotate: rotate,
  2512. scale: scale$1,
  2513. invert: invert,
  2514. clone: clone$2
  2515. });
  2516. /**
  2517. * 提供变换扩展
  2518. * @module zrender/mixin/Transformable
  2519. * @author pissang (https://www.github.com/pissang)
  2520. */
  2521. var mIdentity = identity;
  2522. var EPSILON = 5e-5;
  2523. function isNotAroundZero(val) {
  2524. return val > EPSILON || val < -EPSILON;
  2525. }
  2526. /**
  2527. * @alias module:zrender/mixin/Transformable
  2528. * @constructor
  2529. */
  2530. var Transformable = function (opts) {
  2531. opts = opts || {};
  2532. // If there are no given position, rotation, scale
  2533. if (!opts.position) {
  2534. /**
  2535. * 平移
  2536. * @type {Array.<number>}
  2537. * @default [0, 0]
  2538. */
  2539. this.position = [0, 0];
  2540. }
  2541. if (opts.rotation == null) {
  2542. /**
  2543. * 旋转
  2544. * @type {Array.<number>}
  2545. * @default 0
  2546. */
  2547. this.rotation = 0;
  2548. }
  2549. if (!opts.scale) {
  2550. /**
  2551. * 缩放
  2552. * @type {Array.<number>}
  2553. * @default [1, 1]
  2554. */
  2555. this.scale = [1, 1];
  2556. }
  2557. /**
  2558. * 旋转和缩放的原点
  2559. * @type {Array.<number>}
  2560. * @default null
  2561. */
  2562. this.origin = this.origin || null;
  2563. };
  2564. var transformableProto = Transformable.prototype;
  2565. transformableProto.transform = null;
  2566. /**
  2567. * 判断是否需要有坐标变换
  2568. * 如果有坐标变换, 则从position, rotation, scale以及父节点的transform计算出自身的transform矩阵
  2569. */
  2570. transformableProto.needLocalTransform = function () {
  2571. return isNotAroundZero(this.rotation)
  2572. || isNotAroundZero(this.position[0])
  2573. || isNotAroundZero(this.position[1])
  2574. || isNotAroundZero(this.scale[0] - 1)
  2575. || isNotAroundZero(this.scale[1] - 1);
  2576. };
  2577. var scaleTmp = [];
  2578. transformableProto.updateTransform = function () {
  2579. var parent = this.parent;
  2580. var parentHasTransform = parent && parent.transform;
  2581. var needLocalTransform = this.needLocalTransform();
  2582. var m = this.transform;
  2583. if (!(needLocalTransform || parentHasTransform)) {
  2584. m && mIdentity(m);
  2585. return;
  2586. }
  2587. m = m || create$1();
  2588. if (needLocalTransform) {
  2589. this.getLocalTransform(m);
  2590. }
  2591. else {
  2592. mIdentity(m);
  2593. }
  2594. // 应用父节点变换
  2595. if (parentHasTransform) {
  2596. if (needLocalTransform) {
  2597. mul$1(m, parent.transform, m);
  2598. }
  2599. else {
  2600. copy$1(m, parent.transform);
  2601. }
  2602. }
  2603. // 保存这个变换矩阵
  2604. this.transform = m;
  2605. var globalScaleRatio = this.globalScaleRatio;
  2606. if (globalScaleRatio != null && globalScaleRatio !== 1) {
  2607. this.getGlobalScale(scaleTmp);
  2608. var relX = scaleTmp[0] < 0 ? -1 : 1;
  2609. var relY = scaleTmp[1] < 0 ? -1 : 1;
  2610. var sx = ((scaleTmp[0] - relX) * globalScaleRatio + relX) / scaleTmp[0] || 0;
  2611. var sy = ((scaleTmp[1] - relY) * globalScaleRatio + relY) / scaleTmp[1] || 0;
  2612. m[0] *= sx;
  2613. m[1] *= sx;
  2614. m[2] *= sy;
  2615. m[3] *= sy;
  2616. }
  2617. this.invTransform = this.invTransform || create$1();
  2618. invert(this.invTransform, m);
  2619. };
  2620. transformableProto.getLocalTransform = function (m) {
  2621. return Transformable.getLocalTransform(this, m);
  2622. };
  2623. /**
  2624. * 将自己的transform应用到context上
  2625. * @param {CanvasRenderingContext2D} ctx
  2626. */
  2627. transformableProto.setTransform = function (ctx) {
  2628. var m = this.transform;
  2629. var dpr = ctx.dpr || 1;
  2630. if (m) {
  2631. ctx.setTransform(dpr * m[0], dpr * m[1], dpr * m[2], dpr * m[3], dpr * m[4], dpr * m[5]);
  2632. }
  2633. else {
  2634. ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
  2635. }
  2636. };
  2637. transformableProto.restoreTransform = function (ctx) {
  2638. var dpr = ctx.dpr || 1;
  2639. ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
  2640. };
  2641. var tmpTransform = [];
  2642. var originTransform = create$1();
  2643. transformableProto.setLocalTransform = function (m) {
  2644. if (!m) {
  2645. // TODO return or set identity?
  2646. return;
  2647. }
  2648. var sx = m[0] * m[0] + m[1] * m[1];
  2649. var sy = m[2] * m[2] + m[3] * m[3];
  2650. var position = this.position;
  2651. var scale$$1 = this.scale;
  2652. if (isNotAroundZero(sx - 1)) {
  2653. sx = Math.sqrt(sx);
  2654. }
  2655. if (isNotAroundZero(sy - 1)) {
  2656. sy = Math.sqrt(sy);
  2657. }
  2658. if (m[0] < 0) {
  2659. sx = -sx;
  2660. }
  2661. if (m[3] < 0) {
  2662. sy = -sy;
  2663. }
  2664. position[0] = m[4];
  2665. position[1] = m[5];
  2666. scale$$1[0] = sx;
  2667. scale$$1[1] = sy;
  2668. this.rotation = Math.atan2(-m[1] / sy, m[0] / sx);
  2669. };
  2670. /**
  2671. * 分解`transform`矩阵到`position`, `rotation`, `scale`
  2672. */
  2673. transformableProto.decomposeTransform = function () {
  2674. if (!this.transform) {
  2675. return;
  2676. }
  2677. var parent = this.parent;
  2678. var m = this.transform;
  2679. if (parent && parent.transform) {
  2680. // Get local transform and decompose them to position, scale, rotation
  2681. mul$1(tmpTransform, parent.invTransform, m);
  2682. m = tmpTransform;
  2683. }
  2684. var origin = this.origin;
  2685. if (origin && (origin[0] || origin[1])) {
  2686. originTransform[4] = origin[0];
  2687. originTransform[5] = origin[1];
  2688. mul$1(tmpTransform, m, originTransform);
  2689. tmpTransform[4] -= origin[0];
  2690. tmpTransform[5] -= origin[1];
  2691. m = tmpTransform;
  2692. }
  2693. this.setLocalTransform(m);
  2694. };
  2695. /**
  2696. * Get global scale
  2697. * @return {Array.<number>}
  2698. */
  2699. transformableProto.getGlobalScale = function (out) {
  2700. var m = this.transform;
  2701. out = out || [];
  2702. if (!m) {
  2703. out[0] = 1;
  2704. out[1] = 1;
  2705. return out;
  2706. }
  2707. out[0] = Math.sqrt(m[0] * m[0] + m[1] * m[1]);
  2708. out[1] = Math.sqrt(m[2] * m[2] + m[3] * m[3]);
  2709. if (m[0] < 0) {
  2710. out[0] = -out[0];
  2711. }
  2712. if (m[3] < 0) {
  2713. out[1] = -out[1];
  2714. }
  2715. return out;
  2716. };
  2717. /**
  2718. * 变换坐标位置到 shape 的局部坐标空间
  2719. * @method
  2720. * @param {number} x
  2721. * @param {number} y
  2722. * @return {Array.<number>}
  2723. */
  2724. transformableProto.transformCoordToLocal = function (x, y) {
  2725. var v2 = [x, y];
  2726. var invTransform = this.invTransform;
  2727. if (invTransform) {
  2728. applyTransform(v2, v2, invTransform);
  2729. }
  2730. return v2;
  2731. };
  2732. /**
  2733. * 变换局部坐标位置到全局坐标空间
  2734. * @method
  2735. * @param {number} x
  2736. * @param {number} y
  2737. * @return {Array.<number>}
  2738. */
  2739. transformableProto.transformCoordToGlobal = function (x, y) {
  2740. var v2 = [x, y];
  2741. var transform = this.transform;
  2742. if (transform) {
  2743. applyTransform(v2, v2, transform);
  2744. }
  2745. return v2;
  2746. };
  2747. /**
  2748. * @static
  2749. * @param {Object} target
  2750. * @param {Array.<number>} target.origin
  2751. * @param {number} target.rotation
  2752. * @param {Array.<number>} target.position
  2753. * @param {Array.<number>} [m]
  2754. */
  2755. Transformable.getLocalTransform = function (target, m) {
  2756. m = m || [];
  2757. mIdentity(m);
  2758. var origin = target.origin;
  2759. var scale$$1 = target.scale || [1, 1];
  2760. var rotation = target.rotation || 0;
  2761. var position = target.position || [0, 0];
  2762. if (origin) {
  2763. // Translate to origin
  2764. m[4] -= origin[0];
  2765. m[5] -= origin[1];
  2766. }
  2767. scale$1(m, m, scale$$1);
  2768. if (rotation) {
  2769. rotate(m, m, rotation);
  2770. }
  2771. if (origin) {
  2772. // Translate back from origin
  2773. m[4] += origin[0];
  2774. m[5] += origin[1];
  2775. }
  2776. m[4] += position[0];
  2777. m[5] += position[1];
  2778. return m;
  2779. };
  2780. /**
  2781. * 缓动代码来自 https://github.com/sole/tween.js/blob/master/src/Tween.js
  2782. * @see http://sole.github.io/tween.js/examples/03_graphs.html
  2783. * @exports zrender/animation/easing
  2784. */
  2785. var easing = {
  2786. /**
  2787. * @param {number} k
  2788. * @return {number}
  2789. */
  2790. linear: function (k) {
  2791. return k;
  2792. },
  2793. /**
  2794. * @param {number} k
  2795. * @return {number}
  2796. */
  2797. quadraticIn: function (k) {
  2798. return k * k;
  2799. },
  2800. /**
  2801. * @param {number} k
  2802. * @return {number}
  2803. */
  2804. quadraticOut: function (k) {
  2805. return k * (2 - k);
  2806. },
  2807. /**
  2808. * @param {number} k
  2809. * @return {number}
  2810. */
  2811. quadraticInOut: function (k) {
  2812. if ((k *= 2) < 1) {
  2813. return 0.5 * k * k;
  2814. }
  2815. return -0.5 * (--k * (k - 2) - 1);
  2816. },
  2817. // 三次方的缓动(t^3)
  2818. /**
  2819. * @param {number} k
  2820. * @return {number}
  2821. */
  2822. cubicIn: function (k) {
  2823. return k * k * k;
  2824. },
  2825. /**
  2826. * @param {number} k
  2827. * @return {number}
  2828. */
  2829. cubicOut: function (k) {
  2830. return --k * k * k + 1;
  2831. },
  2832. /**
  2833. * @param {number} k
  2834. * @return {number}
  2835. */
  2836. cubicInOut: function (k) {
  2837. if ((k *= 2) < 1) {
  2838. return 0.5 * k * k * k;
  2839. }
  2840. return 0.5 * ((k -= 2) * k * k + 2);
  2841. },
  2842. // 四次方的缓动(t^4)
  2843. /**
  2844. * @param {number} k
  2845. * @return {number}
  2846. */
  2847. quarticIn: function (k) {
  2848. return k * k * k * k;
  2849. },
  2850. /**
  2851. * @param {number} k
  2852. * @return {number}
  2853. */
  2854. quarticOut: function (k) {
  2855. return 1 - (--k * k * k * k);
  2856. },
  2857. /**
  2858. * @param {number} k
  2859. * @return {number}
  2860. */
  2861. quarticInOut: function (k) {
  2862. if ((k *= 2) < 1) {
  2863. return 0.5 * k * k * k * k;
  2864. }
  2865. return -0.5 * ((k -= 2) * k * k * k - 2);
  2866. },
  2867. // 五次方的缓动(t^5)
  2868. /**
  2869. * @param {number} k
  2870. * @return {number}
  2871. */
  2872. quinticIn: function (k) {
  2873. return k * k * k * k * k;
  2874. },
  2875. /**
  2876. * @param {number} k
  2877. * @return {number}
  2878. */
  2879. quinticOut: function (k) {
  2880. return --k * k * k * k * k + 1;
  2881. },
  2882. /**
  2883. * @param {number} k
  2884. * @return {number}
  2885. */
  2886. quinticInOut: function (k) {
  2887. if ((k *= 2) < 1) {
  2888. return 0.5 * k * k * k * k * k;
  2889. }
  2890. return 0.5 * ((k -= 2) * k * k * k * k + 2);
  2891. },
  2892. // 正弦曲线的缓动(sin(t))
  2893. /**
  2894. * @param {number} k
  2895. * @return {number}
  2896. */
  2897. sinusoidalIn: function (k) {
  2898. return 1 - Math.cos(k * Math.PI / 2);
  2899. },
  2900. /**
  2901. * @param {number} k
  2902. * @return {number}
  2903. */
  2904. sinusoidalOut: function (k) {
  2905. return Math.sin(k * Math.PI / 2);
  2906. },
  2907. /**
  2908. * @param {number} k
  2909. * @return {number}
  2910. */
  2911. sinusoidalInOut: function (k) {
  2912. return 0.5 * (1 - Math.cos(Math.PI * k));
  2913. },
  2914. // 指数曲线的缓动(2^t)
  2915. /**
  2916. * @param {number} k
  2917. * @return {number}
  2918. */
  2919. exponentialIn: function (k) {
  2920. return k === 0 ? 0 : Math.pow(1024, k - 1);
  2921. },
  2922. /**
  2923. * @param {number} k
  2924. * @return {number}
  2925. */
  2926. exponentialOut: function (k) {
  2927. return k === 1 ? 1 : 1 - Math.pow(2, -10 * k);
  2928. },
  2929. /**
  2930. * @param {number} k
  2931. * @return {number}
  2932. */
  2933. exponentialInOut: function (k) {
  2934. if (k === 0) {
  2935. return 0;
  2936. }
  2937. if (k === 1) {
  2938. return 1;
  2939. }
  2940. if ((k *= 2) < 1) {
  2941. return 0.5 * Math.pow(1024, k - 1);
  2942. }
  2943. return 0.5 * (-Math.pow(2, -10 * (k - 1)) + 2);
  2944. },
  2945. // 圆形曲线的缓动(sqrt(1-t^2))
  2946. /**
  2947. * @param {number} k
  2948. * @return {number}
  2949. */
  2950. circularIn: function (k) {
  2951. return 1 - Math.sqrt(1 - k * k);
  2952. },
  2953. /**
  2954. * @param {number} k
  2955. * @return {number}
  2956. */
  2957. circularOut: function (k) {
  2958. return Math.sqrt(1 - (--k * k));
  2959. },
  2960. /**
  2961. * @param {number} k
  2962. * @return {number}
  2963. */
  2964. circularInOut: function (k) {
  2965. if ((k *= 2) < 1) {
  2966. return -0.5 * (Math.sqrt(1 - k * k) - 1);
  2967. }
  2968. return 0.5 * (Math.sqrt(1 - (k -= 2) * k) + 1);
  2969. },
  2970. // 创建类似于弹簧在停止前来回振荡的动画
  2971. /**
  2972. * @param {number} k
  2973. * @return {number}
  2974. */
  2975. elasticIn: function (k) {
  2976. var s;
  2977. var a = 0.1;
  2978. var p = 0.4;
  2979. if (k === 0) {
  2980. return 0;
  2981. }
  2982. if (k === 1) {
  2983. return 1;
  2984. }
  2985. if (!a || a < 1) {
  2986. a = 1;
  2987. s = p / 4;
  2988. }
  2989. else {
  2990. s = p * Math.asin(1 / a) / (2 * Math.PI);
  2991. }
  2992. return -(a * Math.pow(2, 10 * (k -= 1))
  2993. * Math.sin((k - s) * (2 * Math.PI) / p));
  2994. },
  2995. /**
  2996. * @param {number} k
  2997. * @return {number}
  2998. */
  2999. elasticOut: function (k) {
  3000. var s;
  3001. var a = 0.1;
  3002. var p = 0.4;
  3003. if (k === 0) {
  3004. return 0;
  3005. }
  3006. if (k === 1) {
  3007. return 1;
  3008. }
  3009. if (!a || a < 1) {
  3010. a = 1;
  3011. s = p / 4;
  3012. }
  3013. else {
  3014. s = p * Math.asin(1 / a) / (2 * Math.PI);
  3015. }
  3016. return (a * Math.pow(2, -10 * k)
  3017. * Math.sin((k - s) * (2 * Math.PI) / p) + 1);
  3018. },
  3019. /**
  3020. * @param {number} k
  3021. * @return {number}
  3022. */
  3023. elasticInOut: function (k) {
  3024. var s;
  3025. var a = 0.1;
  3026. var p = 0.4;
  3027. if (k === 0) {
  3028. return 0;
  3029. }
  3030. if (k === 1) {
  3031. return 1;
  3032. }
  3033. if (!a || a < 1) {
  3034. a = 1;
  3035. s = p / 4;
  3036. }
  3037. else {
  3038. s = p * Math.asin(1 / a) / (2 * Math.PI);
  3039. }
  3040. if ((k *= 2) < 1) {
  3041. return -0.5 * (a * Math.pow(2, 10 * (k -= 1))
  3042. * Math.sin((k - s) * (2 * Math.PI) / p));
  3043. }
  3044. return a * Math.pow(2, -10 * (k -= 1))
  3045. * Math.sin((k - s) * (2 * Math.PI) / p) * 0.5 + 1;
  3046. },
  3047. // 在某一动画开始沿指示的路径进行动画处理前稍稍收回该动画的移动
  3048. /**
  3049. * @param {number} k
  3050. * @return {number}
  3051. */
  3052. backIn: function (k) {
  3053. var s = 1.70158;
  3054. return k * k * ((s + 1) * k - s);
  3055. },
  3056. /**
  3057. * @param {number} k
  3058. * @return {number}
  3059. */
  3060. backOut: function (k) {
  3061. var s = 1.70158;
  3062. return --k * k * ((s + 1) * k + s) + 1;
  3063. },
  3064. /**
  3065. * @param {number} k
  3066. * @return {number}
  3067. */
  3068. backInOut: function (k) {
  3069. var s = 1.70158 * 1.525;
  3070. if ((k *= 2) < 1) {
  3071. return 0.5 * (k * k * ((s + 1) * k - s));
  3072. }
  3073. return 0.5 * ((k -= 2) * k * ((s + 1) * k + s) + 2);
  3074. },
  3075. // 创建弹跳效果
  3076. /**
  3077. * @param {number} k
  3078. * @return {number}
  3079. */
  3080. bounceIn: function (k) {
  3081. return 1 - easing.bounceOut(1 - k);
  3082. },
  3083. /**
  3084. * @param {number} k
  3085. * @return {number}
  3086. */
  3087. bounceOut: function (k) {
  3088. if (k < (1 / 2.75)) {
  3089. return 7.5625 * k * k;
  3090. }
  3091. else if (k < (2 / 2.75)) {
  3092. return 7.5625 * (k -= (1.5 / 2.75)) * k + 0.75;
  3093. }
  3094. else if (k < (2.5 / 2.75)) {
  3095. return 7.5625 * (k -= (2.25 / 2.75)) * k + 0.9375;
  3096. }
  3097. else {
  3098. return 7.5625 * (k -= (2.625 / 2.75)) * k + 0.984375;
  3099. }
  3100. },
  3101. /**
  3102. * @param {number} k
  3103. * @return {number}
  3104. */
  3105. bounceInOut: function (k) {
  3106. if (k < 0.5) {
  3107. return easing.bounceIn(k * 2) * 0.5;
  3108. }
  3109. return easing.bounceOut(k * 2 - 1) * 0.5 + 0.5;
  3110. }
  3111. };
  3112. /**
  3113. * 动画主控制器
  3114. * @config target 动画对象,可以是数组,如果是数组的话会批量分发onframe等事件
  3115. * @config life(1000) 动画时长
  3116. * @config delay(0) 动画延迟时间
  3117. * @config loop(true)
  3118. * @config gap(0) 循环的间隔时间
  3119. * @config onframe
  3120. * @config easing(optional)
  3121. * @config ondestroy(optional)
  3122. * @config onrestart(optional)
  3123. *
  3124. * TODO pause
  3125. */
  3126. function Clip(options) {
  3127. this._target = options.target;
  3128. // 生命周期
  3129. this._life = options.life || 1000;
  3130. // 延时
  3131. this._delay = options.delay || 0;
  3132. // 开始时间
  3133. // this._startTime = new Date().getTime() + this._delay;// 单位毫秒
  3134. this._initialized = false;
  3135. // 是否循环
  3136. this.loop = options.loop == null ? false : options.loop;
  3137. this.gap = options.gap || 0;
  3138. this.easing = options.easing || 'Linear';
  3139. this.onframe = options.onframe;
  3140. this.ondestroy = options.ondestroy;
  3141. this.onrestart = options.onrestart;
  3142. this._pausedTime = 0;
  3143. this._paused = false;
  3144. }
  3145. Clip.prototype = {
  3146. constructor: Clip,
  3147. step: function (globalTime, deltaTime) {
  3148. // Set startTime on first step, or _startTime may has milleseconds different between clips
  3149. // PENDING
  3150. if (!this._initialized) {
  3151. this._startTime = globalTime + this._delay;
  3152. this._initialized = true;
  3153. }
  3154. if (this._paused) {
  3155. this._pausedTime += deltaTime;
  3156. return;
  3157. }
  3158. var percent = (globalTime - this._startTime - this._pausedTime) / this._life;
  3159. // 还没开始
  3160. if (percent < 0) {
  3161. return;
  3162. }
  3163. percent = Math.min(percent, 1);
  3164. var easing$$1 = this.easing;
  3165. var easingFunc = typeof easing$$1 === 'string' ? easing[easing$$1] : easing$$1;
  3166. var schedule = typeof easingFunc === 'function'
  3167. ? easingFunc(percent)
  3168. : percent;
  3169. this.fire('frame', schedule);
  3170. // 结束
  3171. if (percent === 1) {
  3172. if (this.loop) {
  3173. this.restart(globalTime);
  3174. // 重新开始周期
  3175. // 抛出而不是直接调用事件直到 stage.update 后再统一调用这些事件
  3176. return 'restart';
  3177. }
  3178. // 动画完成将这个控制器标识为待删除
  3179. // 在Animation.update中进行批量删除
  3180. this._needsRemove = true;
  3181. return 'destroy';
  3182. }
  3183. return null;
  3184. },
  3185. restart: function (globalTime) {
  3186. var remainder = (globalTime - this._startTime - this._pausedTime) % this._life;
  3187. this._startTime = globalTime - remainder + this.gap;
  3188. this._pausedTime = 0;
  3189. this._needsRemove = false;
  3190. },
  3191. fire: function (eventType, arg) {
  3192. eventType = 'on' + eventType;
  3193. if (this[eventType]) {
  3194. this[eventType](this._target, arg);
  3195. }
  3196. },
  3197. pause: function () {
  3198. this._paused = true;
  3199. },
  3200. resume: function () {
  3201. this._paused = false;
  3202. }
  3203. };
  3204. // Simple LRU cache use doubly linked list
  3205. // @module zrender/core/LRU
  3206. /**
  3207. * Simple double linked list. Compared with array, it has O(1) remove operation.
  3208. * @constructor
  3209. */
  3210. var LinkedList = function () {
  3211. /**
  3212. * @type {module:zrender/core/LRU~Entry}
  3213. */
  3214. this.head = null;
  3215. /**
  3216. * @type {module:zrender/core/LRU~Entry}
  3217. */
  3218. this.tail = null;
  3219. this._len = 0;
  3220. };
  3221. var linkedListProto = LinkedList.prototype;
  3222. /**
  3223. * Insert a new value at the tail
  3224. * @param {} val
  3225. * @return {module:zrender/core/LRU~Entry}
  3226. */
  3227. linkedListProto.insert = function (val) {
  3228. var entry = new Entry(val);
  3229. this.insertEntry(entry);
  3230. return entry;
  3231. };
  3232. /**
  3233. * Insert an entry at the tail
  3234. * @param {module:zrender/core/LRU~Entry} entry
  3235. */
  3236. linkedListProto.insertEntry = function (entry) {
  3237. if (!this.head) {
  3238. this.head = this.tail = entry;
  3239. }
  3240. else {
  3241. this.tail.next = entry;
  3242. entry.prev = this.tail;
  3243. entry.next = null;
  3244. this.tail = entry;
  3245. }
  3246. this._len++;
  3247. };
  3248. /**
  3249. * Remove entry.
  3250. * @param {module:zrender/core/LRU~Entry} entry
  3251. */
  3252. linkedListProto.remove = function (entry) {
  3253. var prev = entry.prev;
  3254. var next = entry.next;
  3255. if (prev) {
  3256. prev.next = next;
  3257. }
  3258. else {
  3259. // Is head
  3260. this.head = next;
  3261. }
  3262. if (next) {
  3263. next.prev = prev;
  3264. }
  3265. else {
  3266. // Is tail
  3267. this.tail = prev;
  3268. }
  3269. entry.next = entry.prev = null;
  3270. this._len--;
  3271. };
  3272. /**
  3273. * @return {number}
  3274. */
  3275. linkedListProto.len = function () {
  3276. return this._len;
  3277. };
  3278. /**
  3279. * Clear list
  3280. */
  3281. linkedListProto.clear = function () {
  3282. this.head = this.tail = null;
  3283. this._len = 0;
  3284. };
  3285. /**
  3286. * @constructor
  3287. * @param {} val
  3288. */
  3289. var Entry = function (val) {
  3290. /**
  3291. * @type {}
  3292. */
  3293. this.value = val;
  3294. /**
  3295. * @type {module:zrender/core/LRU~Entry}
  3296. */
  3297. this.next;
  3298. /**
  3299. * @type {module:zrender/core/LRU~Entry}
  3300. */
  3301. this.prev;
  3302. };
  3303. /**
  3304. * LRU Cache
  3305. * @constructor
  3306. * @alias module:zrender/core/LRU
  3307. */
  3308. var LRU = function (maxSize) {
  3309. this._list = new LinkedList();
  3310. this._map = {};
  3311. this._maxSize = maxSize || 10;
  3312. this._lastRemovedEntry = null;
  3313. };
  3314. var LRUProto = LRU.prototype;
  3315. /**
  3316. * @param {string} key
  3317. * @param {} value
  3318. * @return {} Removed value
  3319. */
  3320. LRUProto.put = function (key, value) {
  3321. var list = this._list;
  3322. var map = this._map;
  3323. var removed = null;
  3324. if (map[key] == null) {
  3325. var len = list.len();
  3326. // Reuse last removed entry
  3327. var entry = this._lastRemovedEntry;
  3328. if (len >= this._maxSize && len > 0) {
  3329. // Remove the least recently used
  3330. var leastUsedEntry = list.head;
  3331. list.remove(leastUsedEntry);
  3332. delete map[leastUsedEntry.key];
  3333. removed = leastUsedEntry.value;
  3334. this._lastRemovedEntry = leastUsedEntry;
  3335. }
  3336. if (entry) {
  3337. entry.value = value;
  3338. }
  3339. else {
  3340. entry = new Entry(value);
  3341. }
  3342. entry.key = key;
  3343. list.insertEntry(entry);
  3344. map[key] = entry;
  3345. }
  3346. return removed;
  3347. };
  3348. /**
  3349. * @param {string} key
  3350. * @return {}
  3351. */
  3352. LRUProto.get = function (key) {
  3353. var entry = this._map[key];
  3354. var list = this._list;
  3355. if (entry != null) {
  3356. // Put the latest used entry in the tail
  3357. if (entry !== list.tail) {
  3358. list.remove(entry);
  3359. list.insertEntry(entry);
  3360. }
  3361. return entry.value;
  3362. }
  3363. };
  3364. /**
  3365. * Clear the cache
  3366. */
  3367. LRUProto.clear = function () {
  3368. this._list.clear();
  3369. this._map = {};
  3370. };
  3371. var kCSSColorTable = {
  3372. 'transparent': [0, 0, 0, 0], 'aliceblue': [240, 248, 255, 1],
  3373. 'antiquewhite': [250, 235, 215, 1], 'aqua': [0, 255, 255, 1],
  3374. 'aquamarine': [127, 255, 212, 1], 'azure': [240, 255, 255, 1],
  3375. 'beige': [245, 245, 220, 1], 'bisque': [255, 228, 196, 1],
  3376. 'black': [0, 0, 0, 1], 'blanchedalmond': [255, 235, 205, 1],
  3377. 'blue': [0, 0, 255, 1], 'blueviolet': [138, 43, 226, 1],
  3378. 'brown': [165, 42, 42, 1], 'burlywood': [222, 184, 135, 1],
  3379. 'cadetblue': [95, 158, 160, 1], 'chartreuse': [127, 255, 0, 1],
  3380. 'chocolate': [210, 105, 30, 1], 'coral': [255, 127, 80, 1],
  3381. 'cornflowerblue': [100, 149, 237, 1], 'cornsilk': [255, 248, 220, 1],
  3382. 'crimson': [220, 20, 60, 1], 'cyan': [0, 255, 255, 1],
  3383. 'darkblue': [0, 0, 139, 1], 'darkcyan': [0, 139, 139, 1],
  3384. 'darkgoldenrod': [184, 134, 11, 1], 'darkgray': [169, 169, 169, 1],
  3385. 'darkgreen': [0, 100, 0, 1], 'darkgrey': [169, 169, 169, 1],
  3386. 'darkkhaki': [189, 183, 107, 1], 'darkmagenta': [139, 0, 139, 1],
  3387. 'darkolivegreen': [85, 107, 47, 1], 'darkorange': [255, 140, 0, 1],
  3388. 'darkorchid': [153, 50, 204, 1], 'darkred': [139, 0, 0, 1],
  3389. 'darksalmon': [233, 150, 122, 1], 'darkseagreen': [143, 188, 143, 1],
  3390. 'darkslateblue': [72, 61, 139, 1], 'darkslategray': [47, 79, 79, 1],
  3391. 'darkslategrey': [47, 79, 79, 1], 'darkturquoise': [0, 206, 209, 1],
  3392. 'darkviolet': [148, 0, 211, 1], 'deeppink': [255, 20, 147, 1],
  3393. 'deepskyblue': [0, 191, 255, 1], 'dimgray': [105, 105, 105, 1],
  3394. 'dimgrey': [105, 105, 105, 1], 'dodgerblue': [30, 144, 255, 1],
  3395. 'firebrick': [178, 34, 34, 1], 'floralwhite': [255, 250, 240, 1],
  3396. 'forestgreen': [34, 139, 34, 1], 'fuchsia': [255, 0, 255, 1],
  3397. 'gainsboro': [220, 220, 220, 1], 'ghostwhite': [248, 248, 255, 1],
  3398. 'gold': [255, 215, 0, 1], 'goldenrod': [218, 165, 32, 1],
  3399. 'gray': [128, 128, 128, 1], 'green': [0, 128, 0, 1],
  3400. 'greenyellow': [173, 255, 47, 1], 'grey': [128, 128, 128, 1],
  3401. 'honeydew': [240, 255, 240, 1], 'hotpink': [255, 105, 180, 1],
  3402. 'indianred': [205, 92, 92, 1], 'indigo': [75, 0, 130, 1],
  3403. 'ivory': [255, 255, 240, 1], 'khaki': [240, 230, 140, 1],
  3404. 'lavender': [230, 230, 250, 1], 'lavenderblush': [255, 240, 245, 1],
  3405. 'lawngreen': [124, 252, 0, 1], 'lemonchiffon': [255, 250, 205, 1],
  3406. 'lightblue': [173, 216, 230, 1], 'lightcoral': [240, 128, 128, 1],
  3407. 'lightcyan': [224, 255, 255, 1], 'lightgoldenrodyellow': [250, 250, 210, 1],
  3408. 'lightgray': [211, 211, 211, 1], 'lightgreen': [144, 238, 144, 1],
  3409. 'lightgrey': [211, 211, 211, 1], 'lightpink': [255, 182, 193, 1],
  3410. 'lightsalmon': [255, 160, 122, 1], 'lightseagreen': [32, 178, 170, 1],
  3411. 'lightskyblue': [135, 206, 250, 1], 'lightslategray': [119, 136, 153, 1],
  3412. 'lightslategrey': [119, 136, 153, 1], 'lightsteelblue': [176, 196, 222, 1],
  3413. 'lightyellow': [255, 255, 224, 1], 'lime': [0, 255, 0, 1],
  3414. 'limegreen': [50, 205, 50, 1], 'linen': [250, 240, 230, 1],
  3415. 'magenta': [255, 0, 255, 1], 'maroon': [128, 0, 0, 1],
  3416. 'mediumaquamarine': [102, 205, 170, 1], 'mediumblue': [0, 0, 205, 1],
  3417. 'mediumorchid': [186, 85, 211, 1], 'mediumpurple': [147, 112, 219, 1],
  3418. 'mediumseagreen': [60, 179, 113, 1], 'mediumslateblue': [123, 104, 238, 1],
  3419. 'mediumspringgreen': [0, 250, 154, 1], 'mediumturquoise': [72, 209, 204, 1],
  3420. 'mediumvioletred': [199, 21, 133, 1], 'midnightblue': [25, 25, 112, 1],
  3421. 'mintcream': [245, 255, 250, 1], 'mistyrose': [255, 228, 225, 1],
  3422. 'moccasin': [255, 228, 181, 1], 'navajowhite': [255, 222, 173, 1],
  3423. 'navy': [0, 0, 128, 1], 'oldlace': [253, 245, 230, 1],
  3424. 'olive': [128, 128, 0, 1], 'olivedrab': [107, 142, 35, 1],
  3425. 'orange': [255, 165, 0, 1], 'orangered': [255, 69, 0, 1],
  3426. 'orchid': [218, 112, 214, 1], 'palegoldenrod': [238, 232, 170, 1],
  3427. 'palegreen': [152, 251, 152, 1], 'paleturquoise': [175, 238, 238, 1],
  3428. 'palevioletred': [219, 112, 147, 1], 'papayawhip': [255, 239, 213, 1],
  3429. 'peachpuff': [255, 218, 185, 1], 'peru': [205, 133, 63, 1],
  3430. 'pink': [255, 192, 203, 1], 'plum': [221, 160, 221, 1],
  3431. 'powderblue': [176, 224, 230, 1], 'purple': [128, 0, 128, 1],
  3432. 'red': [255, 0, 0, 1], 'rosybrown': [188, 143, 143, 1],
  3433. 'royalblue': [65, 105, 225, 1], 'saddlebrown': [139, 69, 19, 1],
  3434. 'salmon': [250, 128, 114, 1], 'sandybrown': [244, 164, 96, 1],
  3435. 'seagreen': [46, 139, 87, 1], 'seashell': [255, 245, 238, 1],
  3436. 'sienna': [160, 82, 45, 1], 'silver': [192, 192, 192, 1],
  3437. 'skyblue': [135, 206, 235, 1], 'slateblue': [106, 90, 205, 1],
  3438. 'slategray': [112, 128, 144, 1], 'slategrey': [112, 128, 144, 1],
  3439. 'snow': [255, 250, 250, 1], 'springgreen': [0, 255, 127, 1],
  3440. 'steelblue': [70, 130, 180, 1], 'tan': [210, 180, 140, 1],
  3441. 'teal': [0, 128, 128, 1], 'thistle': [216, 191, 216, 1],
  3442. 'tomato': [255, 99, 71, 1], 'turquoise': [64, 224, 208, 1],
  3443. 'violet': [238, 130, 238, 1], 'wheat': [245, 222, 179, 1],
  3444. 'white': [255, 255, 255, 1], 'whitesmoke': [245, 245, 245, 1],
  3445. 'yellow': [255, 255, 0, 1], 'yellowgreen': [154, 205, 50, 1]
  3446. };
  3447. function clampCssByte(i) { // Clamp to integer 0 .. 255.
  3448. i = Math.round(i); // Seems to be what Chrome does (vs truncation).
  3449. return i < 0 ? 0 : i > 255 ? 255 : i;
  3450. }
  3451. function clampCssAngle(i) { // Clamp to integer 0 .. 360.
  3452. i = Math.round(i); // Seems to be what Chrome does (vs truncation).
  3453. return i < 0 ? 0 : i > 360 ? 360 : i;
  3454. }
  3455. function clampCssFloat(f) { // Clamp to float 0.0 .. 1.0.
  3456. return f < 0 ? 0 : f > 1 ? 1 : f;
  3457. }
  3458. function parseCssInt(str) { // int or percentage.
  3459. if (str.length && str.charAt(str.length - 1) === '%') {
  3460. return clampCssByte(parseFloat(str) / 100 * 255);
  3461. }
  3462. return clampCssByte(parseInt(str, 10));
  3463. }
  3464. function parseCssFloat(str) { // float or percentage.
  3465. if (str.length && str.charAt(str.length - 1) === '%') {
  3466. return clampCssFloat(parseFloat(str) / 100);
  3467. }
  3468. return clampCssFloat(parseFloat(str));
  3469. }
  3470. function cssHueToRgb(m1, m2, h) {
  3471. if (h < 0) {
  3472. h += 1;
  3473. }
  3474. else if (h > 1) {
  3475. h -= 1;
  3476. }
  3477. if (h * 6 < 1) {
  3478. return m1 + (m2 - m1) * h * 6;
  3479. }
  3480. if (h * 2 < 1) {
  3481. return m2;
  3482. }
  3483. if (h * 3 < 2) {
  3484. return m1 + (m2 - m1) * (2 / 3 - h) * 6;
  3485. }
  3486. return m1;
  3487. }
  3488. function lerpNumber(a, b, p) {
  3489. return a + (b - a) * p;
  3490. }
  3491. function setRgba(out, r, g, b, a) {
  3492. out[0] = r;
  3493. out[1] = g;
  3494. out[2] = b;
  3495. out[3] = a;
  3496. return out;
  3497. }
  3498. function copyRgba(out, a) {
  3499. out[0] = a[0];
  3500. out[1] = a[1];
  3501. out[2] = a[2];
  3502. out[3] = a[3];
  3503. return out;
  3504. }
  3505. var colorCache = new LRU(20);
  3506. var lastRemovedArr = null;
  3507. function putToCache(colorStr, rgbaArr) {
  3508. // Reuse removed array
  3509. if (lastRemovedArr) {
  3510. copyRgba(lastRemovedArr, rgbaArr);
  3511. }
  3512. lastRemovedArr = colorCache.put(colorStr, lastRemovedArr || (rgbaArr.slice()));
  3513. }
  3514. /**
  3515. * @param {string} colorStr
  3516. * @param {Array.<number>} out
  3517. * @return {Array.<number>}
  3518. * @memberOf module:zrender/util/color
  3519. */
  3520. function parse(colorStr, rgbaArr) {
  3521. if (!colorStr) {
  3522. return;
  3523. }
  3524. rgbaArr = rgbaArr || [];
  3525. var cached = colorCache.get(colorStr);
  3526. if (cached) {
  3527. return copyRgba(rgbaArr, cached);
  3528. }
  3529. // colorStr may be not string
  3530. colorStr = colorStr + '';
  3531. // Remove all whitespace, not compliant, but should just be more accepting.
  3532. var str = colorStr.replace(/ /g, '').toLowerCase();
  3533. // Color keywords (and transparent) lookup.
  3534. if (str in kCSSColorTable) {
  3535. copyRgba(rgbaArr, kCSSColorTable[str]);
  3536. putToCache(colorStr, rgbaArr);
  3537. return rgbaArr;
  3538. }
  3539. // #abc and #abc123 syntax.
  3540. if (str.charAt(0) === '#') {
  3541. if (str.length === 4) {
  3542. var iv = parseInt(str.substr(1), 16); // TODO(deanm): Stricter parsing.
  3543. if (!(iv >= 0 && iv <= 0xfff)) {
  3544. setRgba(rgbaArr, 0, 0, 0, 1);
  3545. return; // Covers NaN.
  3546. }
  3547. setRgba(rgbaArr,
  3548. ((iv & 0xf00) >> 4) | ((iv & 0xf00) >> 8),
  3549. (iv & 0xf0) | ((iv & 0xf0) >> 4),
  3550. (iv & 0xf) | ((iv & 0xf) << 4),
  3551. 1
  3552. );
  3553. putToCache(colorStr, rgbaArr);
  3554. return rgbaArr;
  3555. }
  3556. else if (str.length === 7) {
  3557. var iv = parseInt(str.substr(1), 16); // TODO(deanm): Stricter parsing.
  3558. if (!(iv >= 0 && iv <= 0xffffff)) {
  3559. setRgba(rgbaArr, 0, 0, 0, 1);
  3560. return; // Covers NaN.
  3561. }
  3562. setRgba(rgbaArr,
  3563. (iv & 0xff0000) >> 16,
  3564. (iv & 0xff00) >> 8,
  3565. iv & 0xff,
  3566. 1
  3567. );
  3568. putToCache(colorStr, rgbaArr);
  3569. return rgbaArr;
  3570. }
  3571. return;
  3572. }
  3573. var op = str.indexOf('(');
  3574. var ep = str.indexOf(')');
  3575. if (op !== -1 && ep + 1 === str.length) {
  3576. var fname = str.substr(0, op);
  3577. var params = str.substr(op + 1, ep - (op + 1)).split(',');
  3578. var alpha = 1; // To allow case fallthrough.
  3579. switch (fname) {
  3580. case 'rgba':
  3581. if (params.length !== 4) {
  3582. setRgba(rgbaArr, 0, 0, 0, 1);
  3583. return;
  3584. }
  3585. alpha = parseCssFloat(params.pop()); // jshint ignore:line
  3586. // Fall through.
  3587. case 'rgb':
  3588. if (params.length !== 3) {
  3589. setRgba(rgbaArr, 0, 0, 0, 1);
  3590. return;
  3591. }
  3592. setRgba(rgbaArr,
  3593. parseCssInt(params[0]),
  3594. parseCssInt(params[1]),
  3595. parseCssInt(params[2]),
  3596. alpha
  3597. );
  3598. putToCache(colorStr, rgbaArr);
  3599. return rgbaArr;
  3600. case 'hsla':
  3601. if (params.length !== 4) {
  3602. setRgba(rgbaArr, 0, 0, 0, 1);
  3603. return;
  3604. }
  3605. params[3] = parseCssFloat(params[3]);
  3606. hsla2rgba(params, rgbaArr);
  3607. putToCache(colorStr, rgbaArr);
  3608. return rgbaArr;
  3609. case 'hsl':
  3610. if (params.length !== 3) {
  3611. setRgba(rgbaArr, 0, 0, 0, 1);
  3612. return;
  3613. }
  3614. hsla2rgba(params, rgbaArr);
  3615. putToCache(colorStr, rgbaArr);
  3616. return rgbaArr;
  3617. default:
  3618. return;
  3619. }
  3620. }
  3621. setRgba(rgbaArr, 0, 0, 0, 1);
  3622. return;
  3623. }
  3624. /**
  3625. * @param {Array.<number>} hsla
  3626. * @param {Array.<number>} rgba
  3627. * @return {Array.<number>} rgba
  3628. */
  3629. function hsla2rgba(hsla, rgba) {
  3630. var h = (((parseFloat(hsla[0]) % 360) + 360) % 360) / 360; // 0 .. 1
  3631. // NOTE(deanm): According to the CSS spec s/l should only be
  3632. // percentages, but we don't bother and let float or percentage.
  3633. var s = parseCssFloat(hsla[1]);
  3634. var l = parseCssFloat(hsla[2]);
  3635. var m2 = l <= 0.5 ? l * (s + 1) : l + s - l * s;
  3636. var m1 = l * 2 - m2;
  3637. rgba = rgba || [];
  3638. setRgba(rgba,
  3639. clampCssByte(cssHueToRgb(m1, m2, h + 1 / 3) * 255),
  3640. clampCssByte(cssHueToRgb(m1, m2, h) * 255),
  3641. clampCssByte(cssHueToRgb(m1, m2, h - 1 / 3) * 255),
  3642. 1
  3643. );
  3644. if (hsla.length === 4) {
  3645. rgba[3] = hsla[3];
  3646. }
  3647. return rgba;
  3648. }
  3649. /**
  3650. * @param {Array.<number>} rgba
  3651. * @return {Array.<number>} hsla
  3652. */
  3653. function rgba2hsla(rgba) {
  3654. if (!rgba) {
  3655. return;
  3656. }
  3657. // RGB from 0 to 255
  3658. var R = rgba[0] / 255;
  3659. var G = rgba[1] / 255;
  3660. var B = rgba[2] / 255;
  3661. var vMin = Math.min(R, G, B); // Min. value of RGB
  3662. var vMax = Math.max(R, G, B); // Max. value of RGB
  3663. var delta = vMax - vMin; // Delta RGB value
  3664. var L = (vMax + vMin) / 2;
  3665. var H;
  3666. var S;
  3667. // HSL results from 0 to 1
  3668. if (delta === 0) {
  3669. H = 0;
  3670. S = 0;
  3671. }
  3672. else {
  3673. if (L < 0.5) {
  3674. S = delta / (vMax + vMin);
  3675. }
  3676. else {
  3677. S = delta / (2 - vMax - vMin);
  3678. }
  3679. var deltaR = (((vMax - R) / 6) + (delta / 2)) / delta;
  3680. var deltaG = (((vMax - G) / 6) + (delta / 2)) / delta;
  3681. var deltaB = (((vMax - B) / 6) + (delta / 2)) / delta;
  3682. if (R === vMax) {
  3683. H = deltaB - deltaG;
  3684. }
  3685. else if (G === vMax) {
  3686. H = (1 / 3) + deltaR - deltaB;
  3687. }
  3688. else if (B === vMax) {
  3689. H = (2 / 3) + deltaG - deltaR;
  3690. }
  3691. if (H < 0) {
  3692. H += 1;
  3693. }
  3694. if (H > 1) {
  3695. H -= 1;
  3696. }
  3697. }
  3698. var hsla = [H * 360, S, L];
  3699. if (rgba[3] != null) {
  3700. hsla.push(rgba[3]);
  3701. }
  3702. return hsla;
  3703. }
  3704. /**
  3705. * @param {string} color
  3706. * @param {number} level
  3707. * @return {string}
  3708. * @memberOf module:zrender/util/color
  3709. */
  3710. function lift(color, level) {
  3711. var colorArr = parse(color);
  3712. if (colorArr) {
  3713. for (var i = 0; i < 3; i++) {
  3714. if (level < 0) {
  3715. colorArr[i] = colorArr[i] * (1 - level) | 0;
  3716. }
  3717. else {
  3718. colorArr[i] = ((255 - colorArr[i]) * level + colorArr[i]) | 0;
  3719. }
  3720. if (colorArr[i] > 255) {
  3721. colorArr[i] = 255;
  3722. }
  3723. else if (color[i] < 0) {
  3724. colorArr[i] = 0;
  3725. }
  3726. }
  3727. return stringify(colorArr, colorArr.length === 4 ? 'rgba' : 'rgb');
  3728. }
  3729. }
  3730. /**
  3731. * @param {string} color
  3732. * @return {string}
  3733. * @memberOf module:zrender/util/color
  3734. */
  3735. function toHex(color) {
  3736. var colorArr = parse(color);
  3737. if (colorArr) {
  3738. return ((1 << 24) + (colorArr[0] << 16) + (colorArr[1] << 8) + (+colorArr[2])).toString(16).slice(1);
  3739. }
  3740. }
  3741. /**
  3742. * Map value to color. Faster than lerp methods because color is represented by rgba array.
  3743. * @param {number} normalizedValue A float between 0 and 1.
  3744. * @param {Array.<Array.<number>>} colors List of rgba color array
  3745. * @param {Array.<number>} [out] Mapped gba color array
  3746. * @return {Array.<number>} will be null/undefined if input illegal.
  3747. */
  3748. function fastLerp(normalizedValue, colors, out) {
  3749. if (!(colors && colors.length)
  3750. || !(normalizedValue >= 0 && normalizedValue <= 1)
  3751. ) {
  3752. return;
  3753. }
  3754. out = out || [];
  3755. var value = normalizedValue * (colors.length - 1);
  3756. var leftIndex = Math.floor(value);
  3757. var rightIndex = Math.ceil(value);
  3758. var leftColor = colors[leftIndex];
  3759. var rightColor = colors[rightIndex];
  3760. var dv = value - leftIndex;
  3761. out[0] = clampCssByte(lerpNumber(leftColor[0], rightColor[0], dv));
  3762. out[1] = clampCssByte(lerpNumber(leftColor[1], rightColor[1], dv));
  3763. out[2] = clampCssByte(lerpNumber(leftColor[2], rightColor[2], dv));
  3764. out[3] = clampCssFloat(lerpNumber(leftColor[3], rightColor[3], dv));
  3765. return out;
  3766. }
  3767. /**
  3768. * @deprecated
  3769. */
  3770. var fastMapToColor = fastLerp;
  3771. /**
  3772. * @param {number} normalizedValue A float between 0 and 1.
  3773. * @param {Array.<string>} colors Color list.
  3774. * @param {boolean=} fullOutput Default false.
  3775. * @return {(string|Object)} Result color. If fullOutput,
  3776. * return {color: ..., leftIndex: ..., rightIndex: ..., value: ...},
  3777. * @memberOf module:zrender/util/color
  3778. */
  3779. function lerp$1(normalizedValue, colors, fullOutput) {
  3780. if (!(colors && colors.length)
  3781. || !(normalizedValue >= 0 && normalizedValue <= 1)
  3782. ) {
  3783. return;
  3784. }
  3785. var value = normalizedValue * (colors.length - 1);
  3786. var leftIndex = Math.floor(value);
  3787. var rightIndex = Math.ceil(value);
  3788. var leftColor = parse(colors[leftIndex]);
  3789. var rightColor = parse(colors[rightIndex]);
  3790. var dv = value - leftIndex;
  3791. var color = stringify(
  3792. [
  3793. clampCssByte(lerpNumber(leftColor[0], rightColor[0], dv)),
  3794. clampCssByte(lerpNumber(leftColor[1], rightColor[1], dv)),
  3795. clampCssByte(lerpNumber(leftColor[2], rightColor[2], dv)),
  3796. clampCssFloat(lerpNumber(leftColor[3], rightColor[3], dv))
  3797. ],
  3798. 'rgba'
  3799. );
  3800. return fullOutput
  3801. ? {
  3802. color: color,
  3803. leftIndex: leftIndex,
  3804. rightIndex: rightIndex,
  3805. value: value
  3806. }
  3807. : color;
  3808. }
  3809. /**
  3810. * @deprecated
  3811. */
  3812. var mapToColor = lerp$1;
  3813. /**
  3814. * @param {string} color
  3815. * @param {number=} h 0 ~ 360, ignore when null.
  3816. * @param {number=} s 0 ~ 1, ignore when null.
  3817. * @param {number=} l 0 ~ 1, ignore when null.
  3818. * @return {string} Color string in rgba format.
  3819. * @memberOf module:zrender/util/color
  3820. */
  3821. function modifyHSL(color, h, s, l) {
  3822. color = parse(color);
  3823. if (color) {
  3824. color = rgba2hsla(color);
  3825. h != null && (color[0] = clampCssAngle(h));
  3826. s != null && (color[1] = parseCssFloat(s));
  3827. l != null && (color[2] = parseCssFloat(l));
  3828. return stringify(hsla2rgba(color), 'rgba');
  3829. }
  3830. }
  3831. /**
  3832. * @param {string} color
  3833. * @param {number=} alpha 0 ~ 1
  3834. * @return {string} Color string in rgba format.
  3835. * @memberOf module:zrender/util/color
  3836. */
  3837. function modifyAlpha(color, alpha) {
  3838. color = parse(color);
  3839. if (color && alpha != null) {
  3840. color[3] = clampCssFloat(alpha);
  3841. return stringify(color, 'rgba');
  3842. }
  3843. }
  3844. /**
  3845. * @param {Array.<number>} arrColor like [12,33,44,0.4]
  3846. * @param {string} type 'rgba', 'hsva', ...
  3847. * @return {string} Result color. (If input illegal, return undefined).
  3848. */
  3849. function stringify(arrColor, type) {
  3850. if (!arrColor || !arrColor.length) {
  3851. return;
  3852. }
  3853. var colorStr = arrColor[0] + ',' + arrColor[1] + ',' + arrColor[2];
  3854. if (type === 'rgba' || type === 'hsva' || type === 'hsla') {
  3855. colorStr += ',' + arrColor[3];
  3856. }
  3857. return type + '(' + colorStr + ')';
  3858. }
  3859. var color = (Object.freeze || Object)({
  3860. parse: parse,
  3861. lift: lift,
  3862. toHex: toHex,
  3863. fastLerp: fastLerp,
  3864. fastMapToColor: fastMapToColor,
  3865. lerp: lerp$1,
  3866. mapToColor: mapToColor,
  3867. modifyHSL: modifyHSL,
  3868. modifyAlpha: modifyAlpha,
  3869. stringify: stringify
  3870. });
  3871. /**
  3872. * @module echarts/animation/Animator
  3873. */
  3874. var arraySlice = Array.prototype.slice;
  3875. function defaultGetter(target, key) {
  3876. return target[key];
  3877. }
  3878. function defaultSetter(target, key, value) {
  3879. target[key] = value;
  3880. }
  3881. /**
  3882. * @param {number} p0
  3883. * @param {number} p1
  3884. * @param {number} percent
  3885. * @return {number}
  3886. */
  3887. function interpolateNumber(p0, p1, percent) {
  3888. return (p1 - p0) * percent + p0;
  3889. }
  3890. /**
  3891. * @param {string} p0
  3892. * @param {string} p1
  3893. * @param {number} percent
  3894. * @return {string}
  3895. */
  3896. function interpolateString(p0, p1, percent) {
  3897. return percent > 0.5 ? p1 : p0;
  3898. }
  3899. /**
  3900. * @param {Array} p0
  3901. * @param {Array} p1
  3902. * @param {number} percent
  3903. * @param {Array} out
  3904. * @param {number} arrDim
  3905. */
  3906. function interpolateArray(p0, p1, percent, out, arrDim) {
  3907. var len = p0.length;
  3908. if (arrDim === 1) {
  3909. for (var i = 0; i < len; i++) {
  3910. out[i] = interpolateNumber(p0[i], p1[i], percent);
  3911. }
  3912. }
  3913. else {
  3914. var len2 = len && p0[0].length;
  3915. for (var i = 0; i < len; i++) {
  3916. for (var j = 0; j < len2; j++) {
  3917. out[i][j] = interpolateNumber(
  3918. p0[i][j], p1[i][j], percent
  3919. );
  3920. }
  3921. }
  3922. }
  3923. }
  3924. // arr0 is source array, arr1 is target array.
  3925. // Do some preprocess to avoid error happened when interpolating from arr0 to arr1
  3926. function fillArr(arr0, arr1, arrDim) {
  3927. var arr0Len = arr0.length;
  3928. var arr1Len = arr1.length;
  3929. if (arr0Len !== arr1Len) {
  3930. // FIXME Not work for TypedArray
  3931. var isPreviousLarger = arr0Len > arr1Len;
  3932. if (isPreviousLarger) {
  3933. // Cut the previous
  3934. arr0.length = arr1Len;
  3935. }
  3936. else {
  3937. // Fill the previous
  3938. for (var i = arr0Len; i < arr1Len; i++) {
  3939. arr0.push(
  3940. arrDim === 1 ? arr1[i] : arraySlice.call(arr1[i])
  3941. );
  3942. }
  3943. }
  3944. }
  3945. // Handling NaN value
  3946. var len2 = arr0[0] && arr0[0].length;
  3947. for (var i = 0; i < arr0.length; i++) {
  3948. if (arrDim === 1) {
  3949. if (isNaN(arr0[i])) {
  3950. arr0[i] = arr1[i];
  3951. }
  3952. }
  3953. else {
  3954. for (var j = 0; j < len2; j++) {
  3955. if (isNaN(arr0[i][j])) {
  3956. arr0[i][j] = arr1[i][j];
  3957. }
  3958. }
  3959. }
  3960. }
  3961. }
  3962. /**
  3963. * @param {Array} arr0
  3964. * @param {Array} arr1
  3965. * @param {number} arrDim
  3966. * @return {boolean}
  3967. */
  3968. function isArraySame(arr0, arr1, arrDim) {
  3969. if (arr0 === arr1) {
  3970. return true;
  3971. }
  3972. var len = arr0.length;
  3973. if (len !== arr1.length) {
  3974. return false;
  3975. }
  3976. if (arrDim === 1) {
  3977. for (var i = 0; i < len; i++) {
  3978. if (arr0[i] !== arr1[i]) {
  3979. return false;
  3980. }
  3981. }
  3982. }
  3983. else {
  3984. var len2 = arr0[0].length;
  3985. for (var i = 0; i < len; i++) {
  3986. for (var j = 0; j < len2; j++) {
  3987. if (arr0[i][j] !== arr1[i][j]) {
  3988. return false;
  3989. }
  3990. }
  3991. }
  3992. }
  3993. return true;
  3994. }
  3995. /**
  3996. * Catmull Rom interpolate array
  3997. * @param {Array} p0
  3998. * @param {Array} p1
  3999. * @param {Array} p2
  4000. * @param {Array} p3
  4001. * @param {number} t
  4002. * @param {number} t2
  4003. * @param {number} t3
  4004. * @param {Array} out
  4005. * @param {number} arrDim
  4006. */
  4007. function catmullRomInterpolateArray(
  4008. p0, p1, p2, p3, t, t2, t3, out, arrDim
  4009. ) {
  4010. var len = p0.length;
  4011. if (arrDim === 1) {
  4012. for (var i = 0; i < len; i++) {
  4013. out[i] = catmullRomInterpolate(
  4014. p0[i], p1[i], p2[i], p3[i], t, t2, t3
  4015. );
  4016. }
  4017. }
  4018. else {
  4019. var len2 = p0[0].length;
  4020. for (var i = 0; i < len; i++) {
  4021. for (var j = 0; j < len2; j++) {
  4022. out[i][j] = catmullRomInterpolate(
  4023. p0[i][j], p1[i][j], p2[i][j], p3[i][j],
  4024. t, t2, t3
  4025. );
  4026. }
  4027. }
  4028. }
  4029. }
  4030. /**
  4031. * Catmull Rom interpolate number
  4032. * @param {number} p0
  4033. * @param {number} p1
  4034. * @param {number} p2
  4035. * @param {number} p3
  4036. * @param {number} t
  4037. * @param {number} t2
  4038. * @param {number} t3
  4039. * @return {number}
  4040. */
  4041. function catmullRomInterpolate(p0, p1, p2, p3, t, t2, t3) {
  4042. var v0 = (p2 - p0) * 0.5;
  4043. var v1 = (p3 - p1) * 0.5;
  4044. return (2 * (p1 - p2) + v0 + v1) * t3
  4045. + (-3 * (p1 - p2) - 2 * v0 - v1) * t2
  4046. + v0 * t + p1;
  4047. }
  4048. function cloneValue(value) {
  4049. if (isArrayLike(value)) {
  4050. var len = value.length;
  4051. if (isArrayLike(value[0])) {
  4052. var ret = [];
  4053. for (var i = 0; i < len; i++) {
  4054. ret.push(arraySlice.call(value[i]));
  4055. }
  4056. return ret;
  4057. }
  4058. return arraySlice.call(value);
  4059. }
  4060. return value;
  4061. }
  4062. function rgba2String(rgba) {
  4063. rgba[0] = Math.floor(rgba[0]);
  4064. rgba[1] = Math.floor(rgba[1]);
  4065. rgba[2] = Math.floor(rgba[2]);
  4066. return 'rgba(' + rgba.join(',') + ')';
  4067. }
  4068. function getArrayDim(keyframes) {
  4069. var lastValue = keyframes[keyframes.length - 1].value;
  4070. return isArrayLike(lastValue && lastValue[0]) ? 2 : 1;
  4071. }
  4072. function createTrackClip(animator, easing, oneTrackDone, keyframes, propName, forceAnimate) {
  4073. var getter = animator._getter;
  4074. var setter = animator._setter;
  4075. var useSpline = easing === 'spline';
  4076. var trackLen = keyframes.length;
  4077. if (!trackLen) {
  4078. return;
  4079. }
  4080. // Guess data type
  4081. var firstVal = keyframes[0].value;
  4082. var isValueArray = isArrayLike(firstVal);
  4083. var isValueColor = false;
  4084. var isValueString = false;
  4085. // For vertices morphing
  4086. var arrDim = isValueArray ? getArrayDim(keyframes) : 0;
  4087. var trackMaxTime;
  4088. // Sort keyframe as ascending
  4089. keyframes.sort(function (a, b) {
  4090. return a.time - b.time;
  4091. });
  4092. trackMaxTime = keyframes[trackLen - 1].time;
  4093. // Percents of each keyframe
  4094. var kfPercents = [];
  4095. // Value of each keyframe
  4096. var kfValues = [];
  4097. var prevValue = keyframes[0].value;
  4098. var isAllValueEqual = true;
  4099. for (var i = 0; i < trackLen; i++) {
  4100. kfPercents.push(keyframes[i].time / trackMaxTime);
  4101. // Assume value is a color when it is a string
  4102. var value = keyframes[i].value;
  4103. // Check if value is equal, deep check if value is array
  4104. if (!((isValueArray && isArraySame(value, prevValue, arrDim))
  4105. || (!isValueArray && value === prevValue))) {
  4106. isAllValueEqual = false;
  4107. }
  4108. prevValue = value;
  4109. // Try converting a string to a color array
  4110. if (typeof value === 'string') {
  4111. var colorArray = parse(value);
  4112. if (colorArray) {
  4113. value = colorArray;
  4114. isValueColor = true;
  4115. }
  4116. else {
  4117. isValueString = true;
  4118. }
  4119. }
  4120. kfValues.push(value);
  4121. }
  4122. if (!forceAnimate && isAllValueEqual) {
  4123. return;
  4124. }
  4125. var lastValue = kfValues[trackLen - 1];
  4126. // Polyfill array and NaN value
  4127. for (var i = 0; i < trackLen - 1; i++) {
  4128. if (isValueArray) {
  4129. fillArr(kfValues[i], lastValue, arrDim);
  4130. }
  4131. else {
  4132. if (isNaN(kfValues[i]) && !isNaN(lastValue) && !isValueString && !isValueColor) {
  4133. kfValues[i] = lastValue;
  4134. }
  4135. }
  4136. }
  4137. isValueArray && fillArr(getter(animator._target, propName), lastValue, arrDim);
  4138. // Cache the key of last frame to speed up when
  4139. // animation playback is sequency
  4140. var lastFrame = 0;
  4141. var lastFramePercent = 0;
  4142. var start;
  4143. var w;
  4144. var p0;
  4145. var p1;
  4146. var p2;
  4147. var p3;
  4148. if (isValueColor) {
  4149. var rgba = [0, 0, 0, 0];
  4150. }
  4151. var onframe = function (target, percent) {
  4152. // Find the range keyframes
  4153. // kf1-----kf2---------current--------kf3
  4154. // find kf2 and kf3 and do interpolation
  4155. var frame;
  4156. // In the easing function like elasticOut, percent may less than 0
  4157. if (percent < 0) {
  4158. frame = 0;
  4159. }
  4160. else if (percent < lastFramePercent) {
  4161. // Start from next key
  4162. // PENDING start from lastFrame ?
  4163. start = Math.min(lastFrame + 1, trackLen - 1);
  4164. for (frame = start; frame >= 0; frame--) {
  4165. if (kfPercents[frame] <= percent) {
  4166. break;
  4167. }
  4168. }
  4169. // PENDING really need to do this ?
  4170. frame = Math.min(frame, trackLen - 2);
  4171. }
  4172. else {
  4173. for (frame = lastFrame; frame < trackLen; frame++) {
  4174. if (kfPercents[frame] > percent) {
  4175. break;
  4176. }
  4177. }
  4178. frame = Math.min(frame - 1, trackLen - 2);
  4179. }
  4180. lastFrame = frame;
  4181. lastFramePercent = percent;
  4182. var range = (kfPercents[frame + 1] - kfPercents[frame]);
  4183. if (range === 0) {
  4184. return;
  4185. }
  4186. else {
  4187. w = (percent - kfPercents[frame]) / range;
  4188. }
  4189. if (useSpline) {
  4190. p1 = kfValues[frame];
  4191. p0 = kfValues[frame === 0 ? frame : frame - 1];
  4192. p2 = kfValues[frame > trackLen - 2 ? trackLen - 1 : frame + 1];
  4193. p3 = kfValues[frame > trackLen - 3 ? trackLen - 1 : frame + 2];
  4194. if (isValueArray) {
  4195. catmullRomInterpolateArray(
  4196. p0, p1, p2, p3, w, w * w, w * w * w,
  4197. getter(target, propName),
  4198. arrDim
  4199. );
  4200. }
  4201. else {
  4202. var value;
  4203. if (isValueColor) {
  4204. value = catmullRomInterpolateArray(
  4205. p0, p1, p2, p3, w, w * w, w * w * w,
  4206. rgba, 1
  4207. );
  4208. value = rgba2String(rgba);
  4209. }
  4210. else if (isValueString) {
  4211. // String is step(0.5)
  4212. return interpolateString(p1, p2, w);
  4213. }
  4214. else {
  4215. value = catmullRomInterpolate(
  4216. p0, p1, p2, p3, w, w * w, w * w * w
  4217. );
  4218. }
  4219. setter(
  4220. target,
  4221. propName,
  4222. value
  4223. );
  4224. }
  4225. }
  4226. else {
  4227. if (isValueArray) {
  4228. interpolateArray(
  4229. kfValues[frame], kfValues[frame + 1], w,
  4230. getter(target, propName),
  4231. arrDim
  4232. );
  4233. }
  4234. else {
  4235. var value;
  4236. if (isValueColor) {
  4237. interpolateArray(
  4238. kfValues[frame], kfValues[frame + 1], w,
  4239. rgba, 1
  4240. );
  4241. value = rgba2String(rgba);
  4242. }
  4243. else if (isValueString) {
  4244. // String is step(0.5)
  4245. return interpolateString(kfValues[frame], kfValues[frame + 1], w);
  4246. }
  4247. else {
  4248. value = interpolateNumber(kfValues[frame], kfValues[frame + 1], w);
  4249. }
  4250. setter(
  4251. target,
  4252. propName,
  4253. value
  4254. );
  4255. }
  4256. }
  4257. };
  4258. var clip = new Clip({
  4259. target: animator._target,
  4260. life: trackMaxTime,
  4261. loop: animator._loop,
  4262. delay: animator._delay,
  4263. onframe: onframe,
  4264. ondestroy: oneTrackDone
  4265. });
  4266. if (easing && easing !== 'spline') {
  4267. clip.easing = easing;
  4268. }
  4269. return clip;
  4270. }
  4271. /**
  4272. * @alias module:zrender/animation/Animator
  4273. * @constructor
  4274. * @param {Object} target
  4275. * @param {boolean} loop
  4276. * @param {Function} getter
  4277. * @param {Function} setter
  4278. */
  4279. var Animator = function (target, loop, getter, setter) {
  4280. this._tracks = {};
  4281. this._target = target;
  4282. this._loop = loop || false;
  4283. this._getter = getter || defaultGetter;
  4284. this._setter = setter || defaultSetter;
  4285. this._clipCount = 0;
  4286. this._delay = 0;
  4287. this._doneList = [];
  4288. this._onframeList = [];
  4289. this._clipList = [];
  4290. };
  4291. Animator.prototype = {
  4292. /**
  4293. * Set Animation keyframe
  4294. * @param {number} time 关键帧时间,单位是ms
  4295. * @param {Object} props 关键帧的属性值,key-value表示
  4296. * @return {module:zrender/animation/Animator}
  4297. */
  4298. when: function (time /* ms */, props) {
  4299. var tracks = this._tracks;
  4300. for (var propName in props) {
  4301. if (!props.hasOwnProperty(propName)) {
  4302. continue;
  4303. }
  4304. if (!tracks[propName]) {
  4305. tracks[propName] = [];
  4306. // Invalid value
  4307. var value = this._getter(this._target, propName);
  4308. if (value == null) {
  4309. // zrLog('Invalid property ' + propName);
  4310. continue;
  4311. }
  4312. // If time is 0
  4313. // Then props is given initialize value
  4314. // Else
  4315. // Initialize value from current prop value
  4316. if (time !== 0) {
  4317. tracks[propName].push({
  4318. time: 0,
  4319. value: cloneValue(value)
  4320. });
  4321. }
  4322. }
  4323. tracks[propName].push({
  4324. time: time,
  4325. value: props[propName]
  4326. });
  4327. }
  4328. return this;
  4329. },
  4330. /**
  4331. * 添加动画每一帧的回调函数
  4332. * @param {Function} callback
  4333. * @return {module:zrender/animation/Animator}
  4334. */
  4335. during: function (callback) {
  4336. this._onframeList.push(callback);
  4337. return this;
  4338. },
  4339. pause: function () {
  4340. for (var i = 0; i < this._clipList.length; i++) {
  4341. this._clipList[i].pause();
  4342. }
  4343. this._paused = true;
  4344. },
  4345. resume: function () {
  4346. for (var i = 0; i < this._clipList.length; i++) {
  4347. this._clipList[i].resume();
  4348. }
  4349. this._paused = false;
  4350. },
  4351. isPaused: function () {
  4352. return !!this._paused;
  4353. },
  4354. _doneCallback: function () {
  4355. // Clear all tracks
  4356. this._tracks = {};
  4357. // Clear all clips
  4358. this._clipList.length = 0;
  4359. var doneList = this._doneList;
  4360. var len = doneList.length;
  4361. for (var i = 0; i < len; i++) {
  4362. doneList[i].call(this);
  4363. }
  4364. },
  4365. /**
  4366. * Start the animation
  4367. * @param {string|Function} [easing]
  4368. * 动画缓动函数,详见{@link module:zrender/animation/easing}
  4369. * @param {boolean} forceAnimate
  4370. * @return {module:zrender/animation/Animator}
  4371. */
  4372. start: function (easing, forceAnimate) {
  4373. var self = this;
  4374. var clipCount = 0;
  4375. var oneTrackDone = function () {
  4376. clipCount--;
  4377. if (!clipCount) {
  4378. self._doneCallback();
  4379. }
  4380. };
  4381. var lastClip;
  4382. for (var propName in this._tracks) {
  4383. if (!this._tracks.hasOwnProperty(propName)) {
  4384. continue;
  4385. }
  4386. var clip = createTrackClip(
  4387. this, easing, oneTrackDone,
  4388. this._tracks[propName], propName, forceAnimate
  4389. );
  4390. if (clip) {
  4391. this._clipList.push(clip);
  4392. clipCount++;
  4393. // If start after added to animation
  4394. if (this.animation) {
  4395. this.animation.addClip(clip);
  4396. }
  4397. lastClip = clip;
  4398. }
  4399. }
  4400. // Add during callback on the last clip
  4401. if (lastClip) {
  4402. var oldOnFrame = lastClip.onframe;
  4403. lastClip.onframe = function (target, percent) {
  4404. oldOnFrame(target, percent);
  4405. for (var i = 0; i < self._onframeList.length; i++) {
  4406. self._onframeList[i](target, percent);
  4407. }
  4408. };
  4409. }
  4410. // This optimization will help the case that in the upper application
  4411. // the view may be refreshed frequently, where animation will be
  4412. // called repeatly but nothing changed.
  4413. if (!clipCount) {
  4414. this._doneCallback();
  4415. }
  4416. return this;
  4417. },
  4418. /**
  4419. * Stop animation
  4420. * @param {boolean} forwardToLast If move to last frame before stop
  4421. */
  4422. stop: function (forwardToLast) {
  4423. var clipList = this._clipList;
  4424. var animation = this.animation;
  4425. for (var i = 0; i < clipList.length; i++) {
  4426. var clip = clipList[i];
  4427. if (forwardToLast) {
  4428. // Move to last frame before stop
  4429. clip.onframe(this._target, 1);
  4430. }
  4431. animation && animation.removeClip(clip);
  4432. }
  4433. clipList.length = 0;
  4434. },
  4435. /**
  4436. * Set when animation delay starts
  4437. * @param {number} time 单位ms
  4438. * @return {module:zrender/animation/Animator}
  4439. */
  4440. delay: function (time) {
  4441. this._delay = time;
  4442. return this;
  4443. },
  4444. /**
  4445. * Add callback for animation end
  4446. * @param {Function} cb
  4447. * @return {module:zrender/animation/Animator}
  4448. */
  4449. done: function (cb) {
  4450. if (cb) {
  4451. this._doneList.push(cb);
  4452. }
  4453. return this;
  4454. },
  4455. /**
  4456. * @return {Array.<module:zrender/animation/Clip>}
  4457. */
  4458. getClips: function () {
  4459. return this._clipList;
  4460. }
  4461. };
  4462. var dpr = 1;
  4463. // If in browser environment
  4464. if (typeof window !== 'undefined') {
  4465. dpr = Math.max(window.devicePixelRatio || 1, 1);
  4466. }
  4467. /**
  4468. * config默认配置项
  4469. * @exports zrender/config
  4470. * @author Kener (@Kener-林峰, kener.linfeng@gmail.com)
  4471. */
  4472. /**
  4473. * Debug log mode:
  4474. * 0: Do nothing, for release.
  4475. * 1: console.error, for debug.
  4476. */
  4477. var debugMode = 0;
  4478. // retina 屏幕优化
  4479. var devicePixelRatio = dpr;
  4480. var logError = function () {
  4481. };
  4482. if (debugMode === 1) {
  4483. logError = console.error;
  4484. }
  4485. var logError$1 = logError;
  4486. /**
  4487. * @alias module:zrender/mixin/Animatable
  4488. * @constructor
  4489. */
  4490. var Animatable = function () {
  4491. /**
  4492. * @type {Array.<module:zrender/animation/Animator>}
  4493. * @readOnly
  4494. */
  4495. this.animators = [];
  4496. };
  4497. Animatable.prototype = {
  4498. constructor: Animatable,
  4499. /**
  4500. * 动画
  4501. *
  4502. * @param {string} path The path to fetch value from object, like 'a.b.c'.
  4503. * @param {boolean} [loop] Whether to loop animation.
  4504. * @return {module:zrender/animation/Animator}
  4505. * @example:
  4506. * el.animate('style', false)
  4507. * .when(1000, {x: 10} )
  4508. * .done(function(){ // Animation done })
  4509. * .start()
  4510. */
  4511. animate: function (path, loop) {
  4512. var target;
  4513. var animatingShape = false;
  4514. var el = this;
  4515. var zr = this.__zr;
  4516. if (path) {
  4517. var pathSplitted = path.split('.');
  4518. var prop = el;
  4519. // If animating shape
  4520. animatingShape = pathSplitted[0] === 'shape';
  4521. for (var i = 0, l = pathSplitted.length; i < l; i++) {
  4522. if (!prop) {
  4523. continue;
  4524. }
  4525. prop = prop[pathSplitted[i]];
  4526. }
  4527. if (prop) {
  4528. target = prop;
  4529. }
  4530. }
  4531. else {
  4532. target = el;
  4533. }
  4534. if (!target) {
  4535. logError$1(
  4536. 'Property "'
  4537. + path
  4538. + '" is not existed in element '
  4539. + el.id
  4540. );
  4541. return;
  4542. }
  4543. var animators = el.animators;
  4544. var animator = new Animator(target, loop);
  4545. animator.during(function (target) {
  4546. el.dirty(animatingShape);
  4547. })
  4548. .done(function () {
  4549. // FIXME Animator will not be removed if use `Animator#stop` to stop animation
  4550. animators.splice(indexOf(animators, animator), 1);
  4551. });
  4552. animators.push(animator);
  4553. // If animate after added to the zrender
  4554. if (zr) {
  4555. zr.animation.addAnimator(animator);
  4556. }
  4557. return animator;
  4558. },
  4559. /**
  4560. * 停止动画
  4561. * @param {boolean} forwardToLast If move to last frame before stop
  4562. */
  4563. stopAnimation: function (forwardToLast) {
  4564. var animators = this.animators;
  4565. var len = animators.length;
  4566. for (var i = 0; i < len; i++) {
  4567. animators[i].stop(forwardToLast);
  4568. }
  4569. animators.length = 0;
  4570. return this;
  4571. },
  4572. /**
  4573. * Caution: this method will stop previous animation.
  4574. * So do not use this method to one element twice before
  4575. * animation starts, unless you know what you are doing.
  4576. * @param {Object} target
  4577. * @param {number} [time=500] Time in ms
  4578. * @param {string} [easing='linear']
  4579. * @param {number} [delay=0]
  4580. * @param {Function} [callback]
  4581. * @param {Function} [forceAnimate] Prevent stop animation and callback
  4582. * immediently when target values are the same as current values.
  4583. *
  4584. * @example
  4585. * // Animate position
  4586. * el.animateTo({
  4587. * position: [10, 10]
  4588. * }, function () { // done })
  4589. *
  4590. * // Animate shape, style and position in 100ms, delayed 100ms, with cubicOut easing
  4591. * el.animateTo({
  4592. * shape: {
  4593. * width: 500
  4594. * },
  4595. * style: {
  4596. * fill: 'red'
  4597. * }
  4598. * position: [10, 10]
  4599. * }, 100, 100, 'cubicOut', function () { // done })
  4600. */
  4601. // TODO Return animation key
  4602. animateTo: function (target, time, delay, easing, callback, forceAnimate) {
  4603. animateTo(this, target, time, delay, easing, callback, forceAnimate);
  4604. },
  4605. /**
  4606. * Animate from the target state to current state.
  4607. * The params and the return value are the same as `this.animateTo`.
  4608. */
  4609. animateFrom: function (target, time, delay, easing, callback, forceAnimate) {
  4610. animateTo(this, target, time, delay, easing, callback, forceAnimate, true);
  4611. }
  4612. };
  4613. function animateTo(animatable, target, time, delay, easing, callback, forceAnimate, reverse) {
  4614. // animateTo(target, time, easing, callback);
  4615. if (isString(delay)) {
  4616. callback = easing;
  4617. easing = delay;
  4618. delay = 0;
  4619. }
  4620. // animateTo(target, time, delay, callback);
  4621. else if (isFunction(easing)) {
  4622. callback = easing;
  4623. easing = 'linear';
  4624. delay = 0;
  4625. }
  4626. // animateTo(target, time, callback);
  4627. else if (isFunction(delay)) {
  4628. callback = delay;
  4629. delay = 0;
  4630. }
  4631. // animateTo(target, callback)
  4632. else if (isFunction(time)) {
  4633. callback = time;
  4634. time = 500;
  4635. }
  4636. // animateTo(target)
  4637. else if (!time) {
  4638. time = 500;
  4639. }
  4640. // Stop all previous animations
  4641. animatable.stopAnimation();
  4642. animateToShallow(animatable, '', animatable, target, time, delay, reverse);
  4643. // Animators may be removed immediately after start
  4644. // if there is nothing to animate
  4645. var animators = animatable.animators.slice();
  4646. var count = animators.length;
  4647. function done() {
  4648. count--;
  4649. if (!count) {
  4650. callback && callback();
  4651. }
  4652. }
  4653. // No animators. This should be checked before animators[i].start(),
  4654. // because 'done' may be executed immediately if no need to animate.
  4655. if (!count) {
  4656. callback && callback();
  4657. }
  4658. // Start after all animators created
  4659. // Incase any animator is done immediately when all animation properties are not changed
  4660. for (var i = 0; i < animators.length; i++) {
  4661. animators[i]
  4662. .done(done)
  4663. .start(easing, forceAnimate);
  4664. }
  4665. }
  4666. /**
  4667. * @param {string} path=''
  4668. * @param {Object} source=animatable
  4669. * @param {Object} target
  4670. * @param {number} [time=500]
  4671. * @param {number} [delay=0]
  4672. * @param {boolean} [reverse] If `true`, animate
  4673. * from the `target` to current state.
  4674. *
  4675. * @example
  4676. * // Animate position
  4677. * el._animateToShallow({
  4678. * position: [10, 10]
  4679. * })
  4680. *
  4681. * // Animate shape, style and position in 100ms, delayed 100ms
  4682. * el._animateToShallow({
  4683. * shape: {
  4684. * width: 500
  4685. * },
  4686. * style: {
  4687. * fill: 'red'
  4688. * }
  4689. * position: [10, 10]
  4690. * }, 100, 100)
  4691. */
  4692. function animateToShallow(animatable, path, source, target, time, delay, reverse) {
  4693. var objShallow = {};
  4694. var propertyCount = 0;
  4695. for (var name in target) {
  4696. if (!target.hasOwnProperty(name)) {
  4697. continue;
  4698. }
  4699. if (source[name] != null) {
  4700. if (isObject(target[name]) && !isArrayLike(target[name])) {
  4701. animateToShallow(
  4702. animatable,
  4703. path ? path + '.' + name : name,
  4704. source[name],
  4705. target[name],
  4706. time,
  4707. delay,
  4708. reverse
  4709. );
  4710. }
  4711. else {
  4712. if (reverse) {
  4713. objShallow[name] = source[name];
  4714. setAttrByPath(animatable, path, name, target[name]);
  4715. }
  4716. else {
  4717. objShallow[name] = target[name];
  4718. }
  4719. propertyCount++;
  4720. }
  4721. }
  4722. else if (target[name] != null && !reverse) {
  4723. setAttrByPath(animatable, path, name, target[name]);
  4724. }
  4725. }
  4726. if (propertyCount > 0) {
  4727. animatable.animate(path, false)
  4728. .when(time == null ? 500 : time, objShallow)
  4729. .delay(delay || 0);
  4730. }
  4731. }
  4732. function setAttrByPath(el, path, name, value) {
  4733. // Attr directly if not has property
  4734. // FIXME, if some property not needed for element ?
  4735. if (!path) {
  4736. el.attr(name, value);
  4737. }
  4738. else {
  4739. // Only support set shape or style
  4740. var props = {};
  4741. props[path] = {};
  4742. props[path][name] = value;
  4743. el.attr(props);
  4744. }
  4745. }
  4746. /**
  4747. * @alias module:zrender/Element
  4748. * @constructor
  4749. * @extends {module:zrender/mixin/Animatable}
  4750. * @extends {module:zrender/mixin/Transformable}
  4751. * @extends {module:zrender/mixin/Eventful}
  4752. */
  4753. var Element = function (opts) { // jshint ignore:line
  4754. Transformable.call(this, opts);
  4755. Eventful.call(this, opts);
  4756. Animatable.call(this, opts);
  4757. /**
  4758. * 画布元素ID
  4759. * @type {string}
  4760. */
  4761. this.id = opts.id || guid();
  4762. };
  4763. Element.prototype = {
  4764. /**
  4765. * 元素类型
  4766. * Element type
  4767. * @type {string}
  4768. */
  4769. type: 'element',
  4770. /**
  4771. * 元素名字
  4772. * Element name
  4773. * @type {string}
  4774. */
  4775. name: '',
  4776. /**
  4777. * ZRender 实例对象,会在 element 添加到 zrender 实例中后自动赋值
  4778. * ZRender instance will be assigned when element is associated with zrender
  4779. * @name module:/zrender/Element#__zr
  4780. * @type {module:zrender/ZRender}
  4781. */
  4782. __zr: null,
  4783. /**
  4784. * 图形是否忽略,为true时忽略图形的绘制以及事件触发
  4785. * If ignore drawing and events of the element object
  4786. * @name module:/zrender/Element#ignore
  4787. * @type {boolean}
  4788. * @default false
  4789. */
  4790. ignore: false,
  4791. /**
  4792. * 用于裁剪的路径(shape),所有 Group 内的路径在绘制时都会被这个路径裁剪
  4793. * 该路径会继承被裁减对象的变换
  4794. * @type {module:zrender/graphic/Path}
  4795. * @see http://www.w3.org/TR/2dcontext/#clipping-region
  4796. * @readOnly
  4797. */
  4798. clipPath: null,
  4799. /**
  4800. * 是否是 Group
  4801. * @type {boolean}
  4802. */
  4803. isGroup: false,
  4804. /**
  4805. * Drift element
  4806. * @param {number} dx dx on the global space
  4807. * @param {number} dy dy on the global space
  4808. */
  4809. drift: function (dx, dy) {
  4810. switch (this.draggable) {
  4811. case 'horizontal':
  4812. dy = 0;
  4813. break;
  4814. case 'vertical':
  4815. dx = 0;
  4816. break;
  4817. }
  4818. var m = this.transform;
  4819. if (!m) {
  4820. m = this.transform = [1, 0, 0, 1, 0, 0];
  4821. }
  4822. m[4] += dx;
  4823. m[5] += dy;
  4824. this.decomposeTransform();
  4825. this.dirty(false);
  4826. },
  4827. /**
  4828. * Hook before update
  4829. */
  4830. beforeUpdate: function () {},
  4831. /**
  4832. * Hook after update
  4833. */
  4834. afterUpdate: function () {},
  4835. /**
  4836. * Update each frame
  4837. */
  4838. update: function () {
  4839. this.updateTransform();
  4840. },
  4841. /**
  4842. * @param {Function} cb
  4843. * @param {} context
  4844. */
  4845. traverse: function (cb, context) {},
  4846. /**
  4847. * @protected
  4848. */
  4849. attrKV: function (key, value) {
  4850. if (key === 'position' || key === 'scale' || key === 'origin') {
  4851. // Copy the array
  4852. if (value) {
  4853. var target = this[key];
  4854. if (!target) {
  4855. target = this[key] = [];
  4856. }
  4857. target[0] = value[0];
  4858. target[1] = value[1];
  4859. }
  4860. }
  4861. else {
  4862. this[key] = value;
  4863. }
  4864. },
  4865. /**
  4866. * Hide the element
  4867. */
  4868. hide: function () {
  4869. this.ignore = true;
  4870. this.__zr && this.__zr.refresh();
  4871. },
  4872. /**
  4873. * Show the element
  4874. */
  4875. show: function () {
  4876. this.ignore = false;
  4877. this.__zr && this.__zr.refresh();
  4878. },
  4879. /**
  4880. * @param {string|Object} key
  4881. * @param {*} value
  4882. */
  4883. attr: function (key, value) {
  4884. if (typeof key === 'string') {
  4885. this.attrKV(key, value);
  4886. }
  4887. else if (isObject(key)) {
  4888. for (var name in key) {
  4889. if (key.hasOwnProperty(name)) {
  4890. this.attrKV(name, key[name]);
  4891. }
  4892. }
  4893. }
  4894. this.dirty(false);
  4895. return this;
  4896. },
  4897. /**
  4898. * @param {module:zrender/graphic/Path} clipPath
  4899. */
  4900. setClipPath: function (clipPath) {
  4901. var zr = this.__zr;
  4902. if (zr) {
  4903. clipPath.addSelfToZr(zr);
  4904. }
  4905. // Remove previous clip path
  4906. if (this.clipPath && this.clipPath !== clipPath) {
  4907. this.removeClipPath();
  4908. }
  4909. this.clipPath = clipPath;
  4910. clipPath.__zr = zr;
  4911. clipPath.__clipTarget = this;
  4912. this.dirty(false);
  4913. },
  4914. /**
  4915. */
  4916. removeClipPath: function () {
  4917. var clipPath = this.clipPath;
  4918. if (clipPath) {
  4919. if (clipPath.__zr) {
  4920. clipPath.removeSelfFromZr(clipPath.__zr);
  4921. }
  4922. clipPath.__zr = null;
  4923. clipPath.__clipTarget = null;
  4924. this.clipPath = null;
  4925. this.dirty(false);
  4926. }
  4927. },
  4928. /**
  4929. * Add self from zrender instance.
  4930. * Not recursively because it will be invoked when element added to storage.
  4931. * @param {module:zrender/ZRender} zr
  4932. */
  4933. addSelfToZr: function (zr) {
  4934. this.__zr = zr;
  4935. // 添加动画
  4936. var animators = this.animators;
  4937. if (animators) {
  4938. for (var i = 0; i < animators.length; i++) {
  4939. zr.animation.addAnimator(animators[i]);
  4940. }
  4941. }
  4942. if (this.clipPath) {
  4943. this.clipPath.addSelfToZr(zr);
  4944. }
  4945. },
  4946. /**
  4947. * Remove self from zrender instance.
  4948. * Not recursively because it will be invoked when element added to storage.
  4949. * @param {module:zrender/ZRender} zr
  4950. */
  4951. removeSelfFromZr: function (zr) {
  4952. this.__zr = null;
  4953. // 移除动画
  4954. var animators = this.animators;
  4955. if (animators) {
  4956. for (var i = 0; i < animators.length; i++) {
  4957. zr.animation.removeAnimator(animators[i]);
  4958. }
  4959. }
  4960. if (this.clipPath) {
  4961. this.clipPath.removeSelfFromZr(zr);
  4962. }
  4963. }
  4964. };
  4965. mixin(Element, Animatable);
  4966. mixin(Element, Transformable);
  4967. mixin(Element, Eventful);
  4968. /**
  4969. * @module echarts/core/BoundingRect
  4970. */
  4971. var v2ApplyTransform = applyTransform;
  4972. var mathMin = Math.min;
  4973. var mathMax = Math.max;
  4974. /**
  4975. * @alias module:echarts/core/BoundingRect
  4976. */
  4977. function BoundingRect(x, y, width, height) {
  4978. if (width < 0) {
  4979. x = x + width;
  4980. width = -width;
  4981. }
  4982. if (height < 0) {
  4983. y = y + height;
  4984. height = -height;
  4985. }
  4986. /**
  4987. * @type {number}
  4988. */
  4989. this.x = x;
  4990. /**
  4991. * @type {number}
  4992. */
  4993. this.y = y;
  4994. /**
  4995. * @type {number}
  4996. */
  4997. this.width = width;
  4998. /**
  4999. * @type {number}
  5000. */
  5001. this.height = height;
  5002. }
  5003. BoundingRect.prototype = {
  5004. constructor: BoundingRect,
  5005. /**
  5006. * @param {module:echarts/core/BoundingRect} other
  5007. */
  5008. union: function (other) {
  5009. var x = mathMin(other.x, this.x);
  5010. var y = mathMin(other.y, this.y);
  5011. this.width = mathMax(
  5012. other.x + other.width,
  5013. this.x + this.width
  5014. ) - x;
  5015. this.height = mathMax(
  5016. other.y + other.height,
  5017. this.y + this.height
  5018. ) - y;
  5019. this.x = x;
  5020. this.y = y;
  5021. },
  5022. /**
  5023. * @param {Array.<number>} m
  5024. * @methods
  5025. */
  5026. applyTransform: (function () {
  5027. var lt = [];
  5028. var rb = [];
  5029. var lb = [];
  5030. var rt = [];
  5031. return function (m) {
  5032. // In case usage like this
  5033. // el.getBoundingRect().applyTransform(el.transform)
  5034. // And element has no transform
  5035. if (!m) {
  5036. return;
  5037. }
  5038. lt[0] = lb[0] = this.x;
  5039. lt[1] = rt[1] = this.y;
  5040. rb[0] = rt[0] = this.x + this.width;
  5041. rb[1] = lb[1] = this.y + this.height;
  5042. v2ApplyTransform(lt, lt, m);
  5043. v2ApplyTransform(rb, rb, m);
  5044. v2ApplyTransform(lb, lb, m);
  5045. v2ApplyTransform(rt, rt, m);
  5046. this.x = mathMin(lt[0], rb[0], lb[0], rt[0]);
  5047. this.y = mathMin(lt[1], rb[1], lb[1], rt[1]);
  5048. var maxX = mathMax(lt[0], rb[0], lb[0], rt[0]);
  5049. var maxY = mathMax(lt[1], rb[1], lb[1], rt[1]);
  5050. this.width = maxX - this.x;
  5051. this.height = maxY - this.y;
  5052. };
  5053. })(),
  5054. /**
  5055. * Calculate matrix of transforming from self to target rect
  5056. * @param {module:zrender/core/BoundingRect} b
  5057. * @return {Array.<number>}
  5058. */
  5059. calculateTransform: function (b) {
  5060. var a = this;
  5061. var sx = b.width / a.width;
  5062. var sy = b.height / a.height;
  5063. var m = create$1();
  5064. // 矩阵右乘
  5065. translate(m, m, [-a.x, -a.y]);
  5066. scale$1(m, m, [sx, sy]);
  5067. translate(m, m, [b.x, b.y]);
  5068. return m;
  5069. },
  5070. /**
  5071. * @param {(module:echarts/core/BoundingRect|Object)} b
  5072. * @return {boolean}
  5073. */
  5074. intersect: function (b) {
  5075. if (!b) {
  5076. return false;
  5077. }
  5078. if (!(b instanceof BoundingRect)) {
  5079. // Normalize negative width/height.
  5080. b = BoundingRect.create(b);
  5081. }
  5082. var a = this;
  5083. var ax0 = a.x;
  5084. var ax1 = a.x + a.width;
  5085. var ay0 = a.y;
  5086. var ay1 = a.y + a.height;
  5087. var bx0 = b.x;
  5088. var bx1 = b.x + b.width;
  5089. var by0 = b.y;
  5090. var by1 = b.y + b.height;
  5091. return !(ax1 < bx0 || bx1 < ax0 || ay1 < by0 || by1 < ay0);
  5092. },
  5093. contain: function (x, y) {
  5094. var rect = this;
  5095. return x >= rect.x
  5096. && x <= (rect.x + rect.width)
  5097. && y >= rect.y
  5098. && y <= (rect.y + rect.height);
  5099. },
  5100. /**
  5101. * @return {module:echarts/core/BoundingRect}
  5102. */
  5103. clone: function () {
  5104. return new BoundingRect(this.x, this.y, this.width, this.height);
  5105. },
  5106. /**
  5107. * Copy from another rect
  5108. */
  5109. copy: function (other) {
  5110. this.x = other.x;
  5111. this.y = other.y;
  5112. this.width = other.width;
  5113. this.height = other.height;
  5114. },
  5115. plain: function () {
  5116. return {
  5117. x: this.x,
  5118. y: this.y,
  5119. width: this.width,
  5120. height: this.height
  5121. };
  5122. }
  5123. };
  5124. /**
  5125. * @param {Object|module:zrender/core/BoundingRect} rect
  5126. * @param {number} rect.x
  5127. * @param {number} rect.y
  5128. * @param {number} rect.width
  5129. * @param {number} rect.height
  5130. * @return {module:zrender/core/BoundingRect}
  5131. */
  5132. BoundingRect.create = function (rect) {
  5133. return new BoundingRect(rect.x, rect.y, rect.width, rect.height);
  5134. };
  5135. /**
  5136. * Group是一个容器,可以插入子节点,Group的变换也会被应用到子节点上
  5137. * @module zrender/graphic/Group
  5138. * @example
  5139. * var Group = require('zrender/container/Group');
  5140. * var Circle = require('zrender/graphic/shape/Circle');
  5141. * var g = new Group();
  5142. * g.position[0] = 100;
  5143. * g.position[1] = 100;
  5144. * g.add(new Circle({
  5145. * style: {
  5146. * x: 100,
  5147. * y: 100,
  5148. * r: 20,
  5149. * }
  5150. * }));
  5151. * zr.add(g);
  5152. */
  5153. /**
  5154. * @alias module:zrender/graphic/Group
  5155. * @constructor
  5156. * @extends module:zrender/mixin/Transformable
  5157. * @extends module:zrender/mixin/Eventful
  5158. */
  5159. var Group = function (opts) {
  5160. opts = opts || {};
  5161. Element.call(this, opts);
  5162. for (var key in opts) {
  5163. if (opts.hasOwnProperty(key)) {
  5164. this[key] = opts[key];
  5165. }
  5166. }
  5167. this._children = [];
  5168. this.__storage = null;
  5169. this.__dirty = true;
  5170. };
  5171. Group.prototype = {
  5172. constructor: Group,
  5173. isGroup: true,
  5174. /**
  5175. * @type {string}
  5176. */
  5177. type: 'group',
  5178. /**
  5179. * 所有子孙元素是否响应鼠标事件
  5180. * @name module:/zrender/container/Group#silent
  5181. * @type {boolean}
  5182. * @default false
  5183. */
  5184. silent: false,
  5185. /**
  5186. * @return {Array.<module:zrender/Element>}
  5187. */
  5188. children: function () {
  5189. return this._children.slice();
  5190. },
  5191. /**
  5192. * 获取指定 index 的儿子节点
  5193. * @param {number} idx
  5194. * @return {module:zrender/Element}
  5195. */
  5196. childAt: function (idx) {
  5197. return this._children[idx];
  5198. },
  5199. /**
  5200. * 获取指定名字的儿子节点
  5201. * @param {string} name
  5202. * @return {module:zrender/Element}
  5203. */
  5204. childOfName: function (name) {
  5205. var children = this._children;
  5206. for (var i = 0; i < children.length; i++) {
  5207. if (children[i].name === name) {
  5208. return children[i];
  5209. }
  5210. }
  5211. },
  5212. /**
  5213. * @return {number}
  5214. */
  5215. childCount: function () {
  5216. return this._children.length;
  5217. },
  5218. /**
  5219. * 添加子节点到最后
  5220. * @param {module:zrender/Element} child
  5221. */
  5222. add: function (child) {
  5223. if (child && child !== this && child.parent !== this) {
  5224. this._children.push(child);
  5225. this._doAdd(child);
  5226. }
  5227. return this;
  5228. },
  5229. /**
  5230. * 添加子节点在 nextSibling 之前
  5231. * @param {module:zrender/Element} child
  5232. * @param {module:zrender/Element} nextSibling
  5233. */
  5234. addBefore: function (child, nextSibling) {
  5235. if (child && child !== this && child.parent !== this
  5236. && nextSibling && nextSibling.parent === this) {
  5237. var children = this._children;
  5238. var idx = children.indexOf(nextSibling);
  5239. if (idx >= 0) {
  5240. children.splice(idx, 0, child);
  5241. this._doAdd(child);
  5242. }
  5243. }
  5244. return this;
  5245. },
  5246. _doAdd: function (child) {
  5247. if (child.parent) {
  5248. child.parent.remove(child);
  5249. }
  5250. child.parent = this;
  5251. var storage = this.__storage;
  5252. var zr = this.__zr;
  5253. if (storage && storage !== child.__storage) {
  5254. storage.addToStorage(child);
  5255. if (child instanceof Group) {
  5256. child.addChildrenToStorage(storage);
  5257. }
  5258. }
  5259. zr && zr.refresh();
  5260. },
  5261. /**
  5262. * 移除子节点
  5263. * @param {module:zrender/Element} child
  5264. */
  5265. remove: function (child) {
  5266. var zr = this.__zr;
  5267. var storage = this.__storage;
  5268. var children = this._children;
  5269. var idx = indexOf(children, child);
  5270. if (idx < 0) {
  5271. return this;
  5272. }
  5273. children.splice(idx, 1);
  5274. child.parent = null;
  5275. if (storage) {
  5276. storage.delFromStorage(child);
  5277. if (child instanceof Group) {
  5278. child.delChildrenFromStorage(storage);
  5279. }
  5280. }
  5281. zr && zr.refresh();
  5282. return this;
  5283. },
  5284. /**
  5285. * 移除所有子节点
  5286. */
  5287. removeAll: function () {
  5288. var children = this._children;
  5289. var storage = this.__storage;
  5290. var child;
  5291. var i;
  5292. for (i = 0; i < children.length; i++) {
  5293. child = children[i];
  5294. if (storage) {
  5295. storage.delFromStorage(child);
  5296. if (child instanceof Group) {
  5297. child.delChildrenFromStorage(storage);
  5298. }
  5299. }
  5300. child.parent = null;
  5301. }
  5302. children.length = 0;
  5303. return this;
  5304. },
  5305. /**
  5306. * 遍历所有子节点
  5307. * @param {Function} cb
  5308. * @param {} context
  5309. */
  5310. eachChild: function (cb, context) {
  5311. var children = this._children;
  5312. for (var i = 0; i < children.length; i++) {
  5313. var child = children[i];
  5314. cb.call(context, child, i);
  5315. }
  5316. return this;
  5317. },
  5318. /**
  5319. * 深度优先遍历所有子孙节点
  5320. * @param {Function} cb
  5321. * @param {} context
  5322. */
  5323. traverse: function (cb, context) {
  5324. for (var i = 0; i < this._children.length; i++) {
  5325. var child = this._children[i];
  5326. cb.call(context, child);
  5327. if (child.type === 'group') {
  5328. child.traverse(cb, context);
  5329. }
  5330. }
  5331. return this;
  5332. },
  5333. addChildrenToStorage: function (storage) {
  5334. for (var i = 0; i < this._children.length; i++) {
  5335. var child = this._children[i];
  5336. storage.addToStorage(child);
  5337. if (child instanceof Group) {
  5338. child.addChildrenToStorage(storage);
  5339. }
  5340. }
  5341. },
  5342. delChildrenFromStorage: function (storage) {
  5343. for (var i = 0; i < this._children.length; i++) {
  5344. var child = this._children[i];
  5345. storage.delFromStorage(child);
  5346. if (child instanceof Group) {
  5347. child.delChildrenFromStorage(storage);
  5348. }
  5349. }
  5350. },
  5351. dirty: function () {
  5352. this.__dirty = true;
  5353. this.__zr && this.__zr.refresh();
  5354. return this;
  5355. },
  5356. /**
  5357. * @return {module:zrender/core/BoundingRect}
  5358. */
  5359. getBoundingRect: function (includeChildren) {
  5360. // TODO Caching
  5361. var rect = null;
  5362. var tmpRect = new BoundingRect(0, 0, 0, 0);
  5363. var children = includeChildren || this._children;
  5364. var tmpMat = [];
  5365. for (var i = 0; i < children.length; i++) {
  5366. var child = children[i];
  5367. if (child.ignore || child.invisible) {
  5368. continue;
  5369. }
  5370. var childRect = child.getBoundingRect();
  5371. var transform = child.getLocalTransform(tmpMat);
  5372. // TODO
  5373. // The boundingRect cacluated by transforming original
  5374. // rect may be bigger than the actual bundingRect when rotation
  5375. // is used. (Consider a circle rotated aginst its center, where
  5376. // the actual boundingRect should be the same as that not be
  5377. // rotated.) But we can not find better approach to calculate
  5378. // actual boundingRect yet, considering performance.
  5379. if (transform) {
  5380. tmpRect.copy(childRect);
  5381. tmpRect.applyTransform(transform);
  5382. rect = rect || tmpRect.clone();
  5383. rect.union(tmpRect);
  5384. }
  5385. else {
  5386. rect = rect || childRect.clone();
  5387. rect.union(childRect);
  5388. }
  5389. }
  5390. return rect || tmpRect;
  5391. }
  5392. };
  5393. inherits(Group, Element);
  5394. // https://github.com/mziccard/node-timsort
  5395. var DEFAULT_MIN_MERGE = 32;
  5396. var DEFAULT_MIN_GALLOPING = 7;
  5397. function minRunLength(n) {
  5398. var r = 0;
  5399. while (n >= DEFAULT_MIN_MERGE) {
  5400. r |= n & 1;
  5401. n >>= 1;
  5402. }
  5403. return n + r;
  5404. }
  5405. function makeAscendingRun(array, lo, hi, compare) {
  5406. var runHi = lo + 1;
  5407. if (runHi === hi) {
  5408. return 1;
  5409. }
  5410. if (compare(array[runHi++], array[lo]) < 0) {
  5411. while (runHi < hi && compare(array[runHi], array[runHi - 1]) < 0) {
  5412. runHi++;
  5413. }
  5414. reverseRun(array, lo, runHi);
  5415. }
  5416. else {
  5417. while (runHi < hi && compare(array[runHi], array[runHi - 1]) >= 0) {
  5418. runHi++;
  5419. }
  5420. }
  5421. return runHi - lo;
  5422. }
  5423. function reverseRun(array, lo, hi) {
  5424. hi--;
  5425. while (lo < hi) {
  5426. var t = array[lo];
  5427. array[lo++] = array[hi];
  5428. array[hi--] = t;
  5429. }
  5430. }
  5431. function binaryInsertionSort(array, lo, hi, start, compare) {
  5432. if (start === lo) {
  5433. start++;
  5434. }
  5435. for (; start < hi; start++) {
  5436. var pivot = array[start];
  5437. var left = lo;
  5438. var right = start;
  5439. var mid;
  5440. while (left < right) {
  5441. mid = left + right >>> 1;
  5442. if (compare(pivot, array[mid]) < 0) {
  5443. right = mid;
  5444. }
  5445. else {
  5446. left = mid + 1;
  5447. }
  5448. }
  5449. var n = start - left;
  5450. switch (n) {
  5451. case 3:
  5452. array[left + 3] = array[left + 2];
  5453. case 2:
  5454. array[left + 2] = array[left + 1];
  5455. case 1:
  5456. array[left + 1] = array[left];
  5457. break;
  5458. default:
  5459. while (n > 0) {
  5460. array[left + n] = array[left + n - 1];
  5461. n--;
  5462. }
  5463. }
  5464. array[left] = pivot;
  5465. }
  5466. }
  5467. function gallopLeft(value, array, start, length, hint, compare) {
  5468. var lastOffset = 0;
  5469. var maxOffset = 0;
  5470. var offset = 1;
  5471. if (compare(value, array[start + hint]) > 0) {
  5472. maxOffset = length - hint;
  5473. while (offset < maxOffset && compare(value, array[start + hint + offset]) > 0) {
  5474. lastOffset = offset;
  5475. offset = (offset << 1) + 1;
  5476. if (offset <= 0) {
  5477. offset = maxOffset;
  5478. }
  5479. }
  5480. if (offset > maxOffset) {
  5481. offset = maxOffset;
  5482. }
  5483. lastOffset += hint;
  5484. offset += hint;
  5485. }
  5486. else {
  5487. maxOffset = hint + 1;
  5488. while (offset < maxOffset && compare(value, array[start + hint - offset]) <= 0) {
  5489. lastOffset = offset;
  5490. offset = (offset << 1) + 1;
  5491. if (offset <= 0) {
  5492. offset = maxOffset;
  5493. }
  5494. }
  5495. if (offset > maxOffset) {
  5496. offset = maxOffset;
  5497. }
  5498. var tmp = lastOffset;
  5499. lastOffset = hint - offset;
  5500. offset = hint - tmp;
  5501. }
  5502. lastOffset++;
  5503. while (lastOffset < offset) {
  5504. var m = lastOffset + (offset - lastOffset >>> 1);
  5505. if (compare(value, array[start + m]) > 0) {
  5506. lastOffset = m + 1;
  5507. }
  5508. else {
  5509. offset = m;
  5510. }
  5511. }
  5512. return offset;
  5513. }
  5514. function gallopRight(value, array, start, length, hint, compare) {
  5515. var lastOffset = 0;
  5516. var maxOffset = 0;
  5517. var offset = 1;
  5518. if (compare(value, array[start + hint]) < 0) {
  5519. maxOffset = hint + 1;
  5520. while (offset < maxOffset && compare(value, array[start + hint - offset]) < 0) {
  5521. lastOffset = offset;
  5522. offset = (offset << 1) + 1;
  5523. if (offset <= 0) {
  5524. offset = maxOffset;
  5525. }
  5526. }
  5527. if (offset > maxOffset) {
  5528. offset = maxOffset;
  5529. }
  5530. var tmp = lastOffset;
  5531. lastOffset = hint - offset;
  5532. offset = hint - tmp;
  5533. }
  5534. else {
  5535. maxOffset = length - hint;
  5536. while (offset < maxOffset && compare(value, array[start + hint + offset]) >= 0) {
  5537. lastOffset = offset;
  5538. offset = (offset << 1) + 1;
  5539. if (offset <= 0) {
  5540. offset = maxOffset;
  5541. }
  5542. }
  5543. if (offset > maxOffset) {
  5544. offset = maxOffset;
  5545. }
  5546. lastOffset += hint;
  5547. offset += hint;
  5548. }
  5549. lastOffset++;
  5550. while (lastOffset < offset) {
  5551. var m = lastOffset + (offset - lastOffset >>> 1);
  5552. if (compare(value, array[start + m]) < 0) {
  5553. offset = m;
  5554. }
  5555. else {
  5556. lastOffset = m + 1;
  5557. }
  5558. }
  5559. return offset;
  5560. }
  5561. function TimSort(array, compare) {
  5562. var minGallop = DEFAULT_MIN_GALLOPING;
  5563. var runStart;
  5564. var runLength;
  5565. var stackSize = 0;
  5566. var tmp = [];
  5567. runStart = [];
  5568. runLength = [];
  5569. function pushRun(_runStart, _runLength) {
  5570. runStart[stackSize] = _runStart;
  5571. runLength[stackSize] = _runLength;
  5572. stackSize += 1;
  5573. }
  5574. function mergeRuns() {
  5575. while (stackSize > 1) {
  5576. var n = stackSize - 2;
  5577. if (
  5578. (n >= 1 && runLength[n - 1] <= runLength[n] + runLength[n + 1])
  5579. || (n >= 2 && runLength[n - 2] <= runLength[n] + runLength[n - 1])
  5580. ) {
  5581. if (runLength[n - 1] < runLength[n + 1]) {
  5582. n--;
  5583. }
  5584. }
  5585. else if (runLength[n] > runLength[n + 1]) {
  5586. break;
  5587. }
  5588. mergeAt(n);
  5589. }
  5590. }
  5591. function forceMergeRuns() {
  5592. while (stackSize > 1) {
  5593. var n = stackSize - 2;
  5594. if (n > 0 && runLength[n - 1] < runLength[n + 1]) {
  5595. n--;
  5596. }
  5597. mergeAt(n);
  5598. }
  5599. }
  5600. function mergeAt(i) {
  5601. var start1 = runStart[i];
  5602. var length1 = runLength[i];
  5603. var start2 = runStart[i + 1];
  5604. var length2 = runLength[i + 1];
  5605. runLength[i] = length1 + length2;
  5606. if (i === stackSize - 3) {
  5607. runStart[i + 1] = runStart[i + 2];
  5608. runLength[i + 1] = runLength[i + 2];
  5609. }
  5610. stackSize--;
  5611. var k = gallopRight(array[start2], array, start1, length1, 0, compare);
  5612. start1 += k;
  5613. length1 -= k;
  5614. if (length1 === 0) {
  5615. return;
  5616. }
  5617. length2 = gallopLeft(array[start1 + length1 - 1], array, start2, length2, length2 - 1, compare);
  5618. if (length2 === 0) {
  5619. return;
  5620. }
  5621. if (length1 <= length2) {
  5622. mergeLow(start1, length1, start2, length2);
  5623. }
  5624. else {
  5625. mergeHigh(start1, length1, start2, length2);
  5626. }
  5627. }
  5628. function mergeLow(start1, length1, start2, length2) {
  5629. var i = 0;
  5630. for (i = 0; i < length1; i++) {
  5631. tmp[i] = array[start1 + i];
  5632. }
  5633. var cursor1 = 0;
  5634. var cursor2 = start2;
  5635. var dest = start1;
  5636. array[dest++] = array[cursor2++];
  5637. if (--length2 === 0) {
  5638. for (i = 0; i < length1; i++) {
  5639. array[dest + i] = tmp[cursor1 + i];
  5640. }
  5641. return;
  5642. }
  5643. if (length1 === 1) {
  5644. for (i = 0; i < length2; i++) {
  5645. array[dest + i] = array[cursor2 + i];
  5646. }
  5647. array[dest + length2] = tmp[cursor1];
  5648. return;
  5649. }
  5650. var _minGallop = minGallop;
  5651. var count1;
  5652. var count2;
  5653. var exit;
  5654. while (1) {
  5655. count1 = 0;
  5656. count2 = 0;
  5657. exit = false;
  5658. do {
  5659. if (compare(array[cursor2], tmp[cursor1]) < 0) {
  5660. array[dest++] = array[cursor2++];
  5661. count2++;
  5662. count1 = 0;
  5663. if (--length2 === 0) {
  5664. exit = true;
  5665. break;
  5666. }
  5667. }
  5668. else {
  5669. array[dest++] = tmp[cursor1++];
  5670. count1++;
  5671. count2 = 0;
  5672. if (--length1 === 1) {
  5673. exit = true;
  5674. break;
  5675. }
  5676. }
  5677. } while ((count1 | count2) < _minGallop);
  5678. if (exit) {
  5679. break;
  5680. }
  5681. do {
  5682. count1 = gallopRight(array[cursor2], tmp, cursor1, length1, 0, compare);
  5683. if (count1 !== 0) {
  5684. for (i = 0; i < count1; i++) {
  5685. array[dest + i] = tmp[cursor1 + i];
  5686. }
  5687. dest += count1;
  5688. cursor1 += count1;
  5689. length1 -= count1;
  5690. if (length1 <= 1) {
  5691. exit = true;
  5692. break;
  5693. }
  5694. }
  5695. array[dest++] = array[cursor2++];
  5696. if (--length2 === 0) {
  5697. exit = true;
  5698. break;
  5699. }
  5700. count2 = gallopLeft(tmp[cursor1], array, cursor2, length2, 0, compare);
  5701. if (count2 !== 0) {
  5702. for (i = 0; i < count2; i++) {
  5703. array[dest + i] = array[cursor2 + i];
  5704. }
  5705. dest += count2;
  5706. cursor2 += count2;
  5707. length2 -= count2;
  5708. if (length2 === 0) {
  5709. exit = true;
  5710. break;
  5711. }
  5712. }
  5713. array[dest++] = tmp[cursor1++];
  5714. if (--length1 === 1) {
  5715. exit = true;
  5716. break;
  5717. }
  5718. _minGallop--;
  5719. } while (count1 >= DEFAULT_MIN_GALLOPING || count2 >= DEFAULT_MIN_GALLOPING);
  5720. if (exit) {
  5721. break;
  5722. }
  5723. if (_minGallop < 0) {
  5724. _minGallop = 0;
  5725. }
  5726. _minGallop += 2;
  5727. }
  5728. minGallop = _minGallop;
  5729. minGallop < 1 && (minGallop = 1);
  5730. if (length1 === 1) {
  5731. for (i = 0; i < length2; i++) {
  5732. array[dest + i] = array[cursor2 + i];
  5733. }
  5734. array[dest + length2] = tmp[cursor1];
  5735. }
  5736. else if (length1 === 0) {
  5737. throw new Error();
  5738. // throw new Error('mergeLow preconditions were not respected');
  5739. }
  5740. else {
  5741. for (i = 0; i < length1; i++) {
  5742. array[dest + i] = tmp[cursor1 + i];
  5743. }
  5744. }
  5745. }
  5746. function mergeHigh(start1, length1, start2, length2) {
  5747. var i = 0;
  5748. for (i = 0; i < length2; i++) {
  5749. tmp[i] = array[start2 + i];
  5750. }
  5751. var cursor1 = start1 + length1 - 1;
  5752. var cursor2 = length2 - 1;
  5753. var dest = start2 + length2 - 1;
  5754. var customCursor = 0;
  5755. var customDest = 0;
  5756. array[dest--] = array[cursor1--];
  5757. if (--length1 === 0) {
  5758. customCursor = dest - (length2 - 1);
  5759. for (i = 0; i < length2; i++) {
  5760. array[customCursor + i] = tmp[i];
  5761. }
  5762. return;
  5763. }
  5764. if (length2 === 1) {
  5765. dest -= length1;
  5766. cursor1 -= length1;
  5767. customDest = dest + 1;
  5768. customCursor = cursor1 + 1;
  5769. for (i = length1 - 1; i >= 0; i--) {
  5770. array[customDest + i] = array[customCursor + i];
  5771. }
  5772. array[dest] = tmp[cursor2];
  5773. return;
  5774. }
  5775. var _minGallop = minGallop;
  5776. while (true) {
  5777. var count1 = 0;
  5778. var count2 = 0;
  5779. var exit = false;
  5780. do {
  5781. if (compare(tmp[cursor2], array[cursor1]) < 0) {
  5782. array[dest--] = array[cursor1--];
  5783. count1++;
  5784. count2 = 0;
  5785. if (--length1 === 0) {
  5786. exit = true;
  5787. break;
  5788. }
  5789. }
  5790. else {
  5791. array[dest--] = tmp[cursor2--];
  5792. count2++;
  5793. count1 = 0;
  5794. if (--length2 === 1) {
  5795. exit = true;
  5796. break;
  5797. }
  5798. }
  5799. } while ((count1 | count2) < _minGallop);
  5800. if (exit) {
  5801. break;
  5802. }
  5803. do {
  5804. count1 = length1 - gallopRight(tmp[cursor2], array, start1, length1, length1 - 1, compare);
  5805. if (count1 !== 0) {
  5806. dest -= count1;
  5807. cursor1 -= count1;
  5808. length1 -= count1;
  5809. customDest = dest + 1;
  5810. customCursor = cursor1 + 1;
  5811. for (i = count1 - 1; i >= 0; i--) {
  5812. array[customDest + i] = array[customCursor + i];
  5813. }
  5814. if (length1 === 0) {
  5815. exit = true;
  5816. break;
  5817. }
  5818. }
  5819. array[dest--] = tmp[cursor2--];
  5820. if (--length2 === 1) {
  5821. exit = true;
  5822. break;
  5823. }
  5824. count2 = length2 - gallopLeft(array[cursor1], tmp, 0, length2, length2 - 1, compare);
  5825. if (count2 !== 0) {
  5826. dest -= count2;
  5827. cursor2 -= count2;
  5828. length2 -= count2;
  5829. customDest = dest + 1;
  5830. customCursor = cursor2 + 1;
  5831. for (i = 0; i < count2; i++) {
  5832. array[customDest + i] = tmp[customCursor + i];
  5833. }
  5834. if (length2 <= 1) {
  5835. exit = true;
  5836. break;
  5837. }
  5838. }
  5839. array[dest--] = array[cursor1--];
  5840. if (--length1 === 0) {
  5841. exit = true;
  5842. break;
  5843. }
  5844. _minGallop--;
  5845. } while (count1 >= DEFAULT_MIN_GALLOPING || count2 >= DEFAULT_MIN_GALLOPING);
  5846. if (exit) {
  5847. break;
  5848. }
  5849. if (_minGallop < 0) {
  5850. _minGallop = 0;
  5851. }
  5852. _minGallop += 2;
  5853. }
  5854. minGallop = _minGallop;
  5855. if (minGallop < 1) {
  5856. minGallop = 1;
  5857. }
  5858. if (length2 === 1) {
  5859. dest -= length1;
  5860. cursor1 -= length1;
  5861. customDest = dest + 1;
  5862. customCursor = cursor1 + 1;
  5863. for (i = length1 - 1; i >= 0; i--) {
  5864. array[customDest + i] = array[customCursor + i];
  5865. }
  5866. array[dest] = tmp[cursor2];
  5867. }
  5868. else if (length2 === 0) {
  5869. throw new Error();
  5870. // throw new Error('mergeHigh preconditions were not respected');
  5871. }
  5872. else {
  5873. customCursor = dest - (length2 - 1);
  5874. for (i = 0; i < length2; i++) {
  5875. array[customCursor + i] = tmp[i];
  5876. }
  5877. }
  5878. }
  5879. this.mergeRuns = mergeRuns;
  5880. this.forceMergeRuns = forceMergeRuns;
  5881. this.pushRun = pushRun;
  5882. }
  5883. function sort(array, compare, lo, hi) {
  5884. if (!lo) {
  5885. lo = 0;
  5886. }
  5887. if (!hi) {
  5888. hi = array.length;
  5889. }
  5890. var remaining = hi - lo;
  5891. if (remaining < 2) {
  5892. return;
  5893. }
  5894. var runLength = 0;
  5895. if (remaining < DEFAULT_MIN_MERGE) {
  5896. runLength = makeAscendingRun(array, lo, hi, compare);
  5897. binaryInsertionSort(array, lo, hi, lo + runLength, compare);
  5898. return;
  5899. }
  5900. var ts = new TimSort(array, compare);
  5901. var minRun = minRunLength(remaining);
  5902. do {
  5903. runLength = makeAscendingRun(array, lo, hi, compare);
  5904. if (runLength < minRun) {
  5905. var force = remaining;
  5906. if (force > minRun) {
  5907. force = minRun;
  5908. }
  5909. binaryInsertionSort(array, lo, lo + force, lo + runLength, compare);
  5910. runLength = force;
  5911. }
  5912. ts.pushRun(lo, runLength);
  5913. ts.mergeRuns();
  5914. remaining -= runLength;
  5915. lo += runLength;
  5916. } while (remaining !== 0);
  5917. ts.forceMergeRuns();
  5918. }
  5919. // Use timsort because in most case elements are partially sorted
  5920. // https://jsfiddle.net/pissang/jr4x7mdm/8/
  5921. function shapeCompareFunc(a, b) {
  5922. if (a.zlevel === b.zlevel) {
  5923. if (a.z === b.z) {
  5924. // if (a.z2 === b.z2) {
  5925. // // FIXME Slow has renderidx compare
  5926. // // http://stackoverflow.com/questions/20883421/sorting-in-javascript-should-every-compare-function-have-a-return-0-statement
  5927. // // https://github.com/v8/v8/blob/47cce544a31ed5577ffe2963f67acb4144ee0232/src/js/array.js#L1012
  5928. // return a.__renderidx - b.__renderidx;
  5929. // }
  5930. return a.z2 - b.z2;
  5931. }
  5932. return a.z - b.z;
  5933. }
  5934. return a.zlevel - b.zlevel;
  5935. }
  5936. /**
  5937. * 内容仓库 (M)
  5938. * @alias module:zrender/Storage
  5939. * @constructor
  5940. */
  5941. var Storage = function () { // jshint ignore:line
  5942. this._roots = [];
  5943. this._displayList = [];
  5944. this._displayListLen = 0;
  5945. };
  5946. Storage.prototype = {
  5947. constructor: Storage,
  5948. /**
  5949. * @param {Function} cb
  5950. *
  5951. */
  5952. traverse: function (cb, context) {
  5953. for (var i = 0; i < this._roots.length; i++) {
  5954. this._roots[i].traverse(cb, context);
  5955. }
  5956. },
  5957. /**
  5958. * 返回所有图形的绘制队列
  5959. * @param {boolean} [update=false] 是否在返回前更新该数组
  5960. * @param {boolean} [includeIgnore=false] 是否包含 ignore 的数组, 在 update 为 true 的时候有效
  5961. *
  5962. * 详见{@link module:zrender/graphic/Displayable.prototype.updateDisplayList}
  5963. * @return {Array.<module:zrender/graphic/Displayable>}
  5964. */
  5965. getDisplayList: function (update, includeIgnore) {
  5966. includeIgnore = includeIgnore || false;
  5967. if (update) {
  5968. this.updateDisplayList(includeIgnore);
  5969. }
  5970. return this._displayList;
  5971. },
  5972. /**
  5973. * 更新图形的绘制队列。
  5974. * 每次绘制前都会调用,该方法会先深度优先遍历整个树,更新所有Group和Shape的变换并且把所有可见的Shape保存到数组中,
  5975. * 最后根据绘制的优先级(zlevel > z > 插入顺序)排序得到绘制队列
  5976. * @param {boolean} [includeIgnore=false] 是否包含 ignore 的数组
  5977. */
  5978. updateDisplayList: function (includeIgnore) {
  5979. this._displayListLen = 0;
  5980. var roots = this._roots;
  5981. var displayList = this._displayList;
  5982. for (var i = 0, len = roots.length; i < len; i++) {
  5983. this._updateAndAddDisplayable(roots[i], null, includeIgnore);
  5984. }
  5985. displayList.length = this._displayListLen;
  5986. env$1.canvasSupported && sort(displayList, shapeCompareFunc);
  5987. },
  5988. _updateAndAddDisplayable: function (el, clipPaths, includeIgnore) {
  5989. if (el.ignore && !includeIgnore) {
  5990. return;
  5991. }
  5992. el.beforeUpdate();
  5993. if (el.__dirty) {
  5994. el.update();
  5995. }
  5996. el.afterUpdate();
  5997. var userSetClipPath = el.clipPath;
  5998. if (userSetClipPath) {
  5999. // FIXME 效率影响
  6000. if (clipPaths) {
  6001. clipPaths = clipPaths.slice();
  6002. }
  6003. else {
  6004. clipPaths = [];
  6005. }
  6006. var currentClipPath = userSetClipPath;
  6007. var parentClipPath = el;
  6008. // Recursively add clip path
  6009. while (currentClipPath) {
  6010. // clipPath 的变换是基于使用这个 clipPath 的元素
  6011. currentClipPath.parent = parentClipPath;
  6012. currentClipPath.updateTransform();
  6013. clipPaths.push(currentClipPath);
  6014. parentClipPath = currentClipPath;
  6015. currentClipPath = currentClipPath.clipPath;
  6016. }
  6017. }
  6018. if (el.isGroup) {
  6019. var children = el._children;
  6020. for (var i = 0; i < children.length; i++) {
  6021. var child = children[i];
  6022. // Force to mark as dirty if group is dirty
  6023. // FIXME __dirtyPath ?
  6024. if (el.__dirty) {
  6025. child.__dirty = true;
  6026. }
  6027. this._updateAndAddDisplayable(child, clipPaths, includeIgnore);
  6028. }
  6029. // Mark group clean here
  6030. el.__dirty = false;
  6031. }
  6032. else {
  6033. el.__clipPaths = clipPaths;
  6034. this._displayList[this._displayListLen++] = el;
  6035. }
  6036. },
  6037. /**
  6038. * 添加图形(Shape)或者组(Group)到根节点
  6039. * @param {module:zrender/Element} el
  6040. */
  6041. addRoot: function (el) {
  6042. if (el.__storage === this) {
  6043. return;
  6044. }
  6045. if (el instanceof Group) {
  6046. el.addChildrenToStorage(this);
  6047. }
  6048. this.addToStorage(el);
  6049. this._roots.push(el);
  6050. },
  6051. /**
  6052. * 删除指定的图形(Shape)或者组(Group)
  6053. * @param {string|Array.<string>} [el] 如果为空清空整个Storage
  6054. */
  6055. delRoot: function (el) {
  6056. if (el == null) {
  6057. // 不指定el清空
  6058. for (var i = 0; i < this._roots.length; i++) {
  6059. var root = this._roots[i];
  6060. if (root instanceof Group) {
  6061. root.delChildrenFromStorage(this);
  6062. }
  6063. }
  6064. this._roots = [];
  6065. this._displayList = [];
  6066. this._displayListLen = 0;
  6067. return;
  6068. }
  6069. if (el instanceof Array) {
  6070. for (var i = 0, l = el.length; i < l; i++) {
  6071. this.delRoot(el[i]);
  6072. }
  6073. return;
  6074. }
  6075. var idx = indexOf(this._roots, el);
  6076. if (idx >= 0) {
  6077. this.delFromStorage(el);
  6078. this._roots.splice(idx, 1);
  6079. if (el instanceof Group) {
  6080. el.delChildrenFromStorage(this);
  6081. }
  6082. }
  6083. },
  6084. addToStorage: function (el) {
  6085. if (el) {
  6086. el.__storage = this;
  6087. el.dirty(false);
  6088. }
  6089. return this;
  6090. },
  6091. delFromStorage: function (el) {
  6092. if (el) {
  6093. el.__storage = null;
  6094. }
  6095. return this;
  6096. },
  6097. /**
  6098. * 清空并且释放Storage
  6099. */
  6100. dispose: function () {
  6101. this._renderList =
  6102. this._roots = null;
  6103. },
  6104. displayableSortFunc: shapeCompareFunc
  6105. };
  6106. var SHADOW_PROPS = {
  6107. 'shadowBlur': 1,
  6108. 'shadowOffsetX': 1,
  6109. 'shadowOffsetY': 1,
  6110. 'textShadowBlur': 1,
  6111. 'textShadowOffsetX': 1,
  6112. 'textShadowOffsetY': 1,
  6113. 'textBoxShadowBlur': 1,
  6114. 'textBoxShadowOffsetX': 1,
  6115. 'textBoxShadowOffsetY': 1
  6116. };
  6117. var fixShadow = function (ctx, propName, value) {
  6118. if (SHADOW_PROPS.hasOwnProperty(propName)) {
  6119. return value *= ctx.dpr;
  6120. }
  6121. return value;
  6122. };
  6123. var ContextCachedBy = {
  6124. NONE: 0,
  6125. STYLE_BIND: 1,
  6126. PLAIN_TEXT: 2
  6127. };
  6128. // Avoid confused with 0/false.
  6129. var WILL_BE_RESTORED = 9;
  6130. var STYLE_COMMON_PROPS = [
  6131. ['shadowBlur', 0], ['shadowOffsetX', 0], ['shadowOffsetY', 0], ['shadowColor', '#000'],
  6132. ['lineCap', 'butt'], ['lineJoin', 'miter'], ['miterLimit', 10]
  6133. ];
  6134. // var SHADOW_PROPS = STYLE_COMMON_PROPS.slice(0, 4);
  6135. // var LINE_PROPS = STYLE_COMMON_PROPS.slice(4);
  6136. var Style = function (opts) {
  6137. this.extendFrom(opts, false);
  6138. };
  6139. function createLinearGradient(ctx, obj, rect) {
  6140. var x = obj.x == null ? 0 : obj.x;
  6141. var x2 = obj.x2 == null ? 1 : obj.x2;
  6142. var y = obj.y == null ? 0 : obj.y;
  6143. var y2 = obj.y2 == null ? 0 : obj.y2;
  6144. if (!obj.global) {
  6145. x = x * rect.width + rect.x;
  6146. x2 = x2 * rect.width + rect.x;
  6147. y = y * rect.height + rect.y;
  6148. y2 = y2 * rect.height + rect.y;
  6149. }
  6150. // Fix NaN when rect is Infinity
  6151. x = isNaN(x) ? 0 : x;
  6152. x2 = isNaN(x2) ? 1 : x2;
  6153. y = isNaN(y) ? 0 : y;
  6154. y2 = isNaN(y2) ? 0 : y2;
  6155. var canvasGradient = ctx.createLinearGradient(x, y, x2, y2);
  6156. return canvasGradient;
  6157. }
  6158. function createRadialGradient(ctx, obj, rect) {
  6159. var width = rect.width;
  6160. var height = rect.height;
  6161. var min = Math.min(width, height);
  6162. var x = obj.x == null ? 0.5 : obj.x;
  6163. var y = obj.y == null ? 0.5 : obj.y;
  6164. var r = obj.r == null ? 0.5 : obj.r;
  6165. if (!obj.global) {
  6166. x = x * width + rect.x;
  6167. y = y * height + rect.y;
  6168. r = r * min;
  6169. }
  6170. var canvasGradient = ctx.createRadialGradient(x, y, 0, x, y, r);
  6171. return canvasGradient;
  6172. }
  6173. Style.prototype = {
  6174. constructor: Style,
  6175. /**
  6176. * @type {string}
  6177. */
  6178. fill: '#000',
  6179. /**
  6180. * @type {string}
  6181. */
  6182. stroke: null,
  6183. /**
  6184. * @type {number}
  6185. */
  6186. opacity: 1,
  6187. /**
  6188. * @type {number}
  6189. */
  6190. fillOpacity: null,
  6191. /**
  6192. * @type {number}
  6193. */
  6194. strokeOpacity: null,
  6195. /**
  6196. * `true` is not supported.
  6197. * `false`/`null`/`undefined` are the same.
  6198. * `false` is used to remove lineDash in some
  6199. * case that `null`/`undefined` can not be set.
  6200. * (e.g., emphasis.lineStyle in echarts)
  6201. * @type {Array.<number>|boolean}
  6202. */
  6203. lineDash: null,
  6204. /**
  6205. * @type {number}
  6206. */
  6207. lineDashOffset: 0,
  6208. /**
  6209. * @type {number}
  6210. */
  6211. shadowBlur: 0,
  6212. /**
  6213. * @type {number}
  6214. */
  6215. shadowOffsetX: 0,
  6216. /**
  6217. * @type {number}
  6218. */
  6219. shadowOffsetY: 0,
  6220. /**
  6221. * @type {number}
  6222. */
  6223. lineWidth: 1,
  6224. /**
  6225. * If stroke ignore scale
  6226. * @type {Boolean}
  6227. */
  6228. strokeNoScale: false,
  6229. // Bounding rect text configuration
  6230. // Not affected by element transform
  6231. /**
  6232. * @type {string}
  6233. */
  6234. text: null,
  6235. /**
  6236. * If `fontSize` or `fontFamily` exists, `font` will be reset by
  6237. * `fontSize`, `fontStyle`, `fontWeight`, `fontFamily`.
  6238. * So do not visit it directly in upper application (like echarts),
  6239. * but use `contain/text#makeFont` instead.
  6240. * @type {string}
  6241. */
  6242. font: null,
  6243. /**
  6244. * The same as font. Use font please.
  6245. * @deprecated
  6246. * @type {string}
  6247. */
  6248. textFont: null,
  6249. /**
  6250. * It helps merging respectively, rather than parsing an entire font string.
  6251. * @type {string}
  6252. */
  6253. fontStyle: null,
  6254. /**
  6255. * It helps merging respectively, rather than parsing an entire font string.
  6256. * @type {string}
  6257. */
  6258. fontWeight: null,
  6259. /**
  6260. * It helps merging respectively, rather than parsing an entire font string.
  6261. * Should be 12 but not '12px'.
  6262. * @type {number}
  6263. */
  6264. fontSize: null,
  6265. /**
  6266. * It helps merging respectively, rather than parsing an entire font string.
  6267. * @type {string}
  6268. */
  6269. fontFamily: null,
  6270. /**
  6271. * Reserved for special functinality, like 'hr'.
  6272. * @type {string}
  6273. */
  6274. textTag: null,
  6275. /**
  6276. * @type {string}
  6277. */
  6278. textFill: '#000',
  6279. /**
  6280. * @type {string}
  6281. */
  6282. textStroke: null,
  6283. /**
  6284. * @type {number}
  6285. */
  6286. textWidth: null,
  6287. /**
  6288. * Only for textBackground.
  6289. * @type {number}
  6290. */
  6291. textHeight: null,
  6292. /**
  6293. * textStroke may be set as some color as a default
  6294. * value in upper applicaion, where the default value
  6295. * of textStrokeWidth should be 0 to make sure that
  6296. * user can choose to do not use text stroke.
  6297. * @type {number}
  6298. */
  6299. textStrokeWidth: 0,
  6300. /**
  6301. * @type {number}
  6302. */
  6303. textLineHeight: null,
  6304. /**
  6305. * 'inside', 'left', 'right', 'top', 'bottom'
  6306. * [x, y]
  6307. * Based on x, y of rect.
  6308. * @type {string|Array.<number>}
  6309. * @default 'inside'
  6310. */
  6311. textPosition: 'inside',
  6312. /**
  6313. * If not specified, use the boundingRect of a `displayable`.
  6314. * @type {Object}
  6315. */
  6316. textRect: null,
  6317. /**
  6318. * [x, y]
  6319. * @type {Array.<number>}
  6320. */
  6321. textOffset: null,
  6322. /**
  6323. * @type {string}
  6324. */
  6325. textAlign: null,
  6326. /**
  6327. * @type {string}
  6328. */
  6329. textVerticalAlign: null,
  6330. /**
  6331. * @type {number}
  6332. */
  6333. textDistance: 5,
  6334. /**
  6335. * @type {string}
  6336. */
  6337. textShadowColor: 'transparent',
  6338. /**
  6339. * @type {number}
  6340. */
  6341. textShadowBlur: 0,
  6342. /**
  6343. * @type {number}
  6344. */
  6345. textShadowOffsetX: 0,
  6346. /**
  6347. * @type {number}
  6348. */
  6349. textShadowOffsetY: 0,
  6350. /**
  6351. * @type {string}
  6352. */
  6353. textBoxShadowColor: 'transparent',
  6354. /**
  6355. * @type {number}
  6356. */
  6357. textBoxShadowBlur: 0,
  6358. /**
  6359. * @type {number}
  6360. */
  6361. textBoxShadowOffsetX: 0,
  6362. /**
  6363. * @type {number}
  6364. */
  6365. textBoxShadowOffsetY: 0,
  6366. /**
  6367. * Whether transform text.
  6368. * Only available in Path and Image element,
  6369. * where the text is called as `RectText`.
  6370. * @type {boolean}
  6371. */
  6372. transformText: false,
  6373. /**
  6374. * Text rotate around position of Path or Image.
  6375. * The origin of the rotation can be specified by `textOrigin`.
  6376. * Only available in Path and Image element,
  6377. * where the text is called as `RectText`.
  6378. */
  6379. textRotation: 0,
  6380. /**
  6381. * Text origin of text rotation.
  6382. * Useful in the case like label rotation of circular symbol.
  6383. * Only available in Path and Image element, where the text is called
  6384. * as `RectText` and the element is called as "host element".
  6385. * The value can be:
  6386. * + If specified as a coordinate like `[10, 40]`, it is the `[x, y]`
  6387. * base on the left-top corner of the rect of its host element.
  6388. * + If specified as a string `center`, it is the center of the rect of
  6389. * its host element.
  6390. * + By default, this origin is the `textPosition`.
  6391. * @type {string|Array.<number>}
  6392. */
  6393. textOrigin: null,
  6394. /**
  6395. * @type {string}
  6396. */
  6397. textBackgroundColor: null,
  6398. /**
  6399. * @type {string}
  6400. */
  6401. textBorderColor: null,
  6402. /**
  6403. * @type {number}
  6404. */
  6405. textBorderWidth: 0,
  6406. /**
  6407. * @type {number}
  6408. */
  6409. textBorderRadius: 0,
  6410. /**
  6411. * Can be `2` or `[2, 4]` or `[2, 3, 4, 5]`
  6412. * @type {number|Array.<number>}
  6413. */
  6414. textPadding: null,
  6415. /**
  6416. * Text styles for rich text.
  6417. * @type {Object}
  6418. */
  6419. rich: null,
  6420. /**
  6421. * {outerWidth, outerHeight, ellipsis, placeholder}
  6422. * @type {Object}
  6423. */
  6424. truncate: null,
  6425. /**
  6426. * https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation
  6427. * @type {string}
  6428. */
  6429. blend: null,
  6430. /**
  6431. * @param {CanvasRenderingContext2D} ctx
  6432. */
  6433. bind: function (ctx, el, prevEl) {
  6434. var style = this;
  6435. var prevStyle = prevEl && prevEl.style;
  6436. // If no prevStyle, it means first draw.
  6437. // Only apply cache if the last time cachced by this function.
  6438. var notCheckCache = !prevStyle || ctx.__attrCachedBy !== ContextCachedBy.STYLE_BIND;
  6439. ctx.__attrCachedBy = ContextCachedBy.STYLE_BIND;
  6440. for (var i = 0; i < STYLE_COMMON_PROPS.length; i++) {
  6441. var prop = STYLE_COMMON_PROPS[i];
  6442. var styleName = prop[0];
  6443. if (notCheckCache || style[styleName] !== prevStyle[styleName]) {
  6444. // FIXME Invalid property value will cause style leak from previous element.
  6445. ctx[styleName] =
  6446. fixShadow(ctx, styleName, style[styleName] || prop[1]);
  6447. }
  6448. }
  6449. if ((notCheckCache || style.fill !== prevStyle.fill)) {
  6450. ctx.fillStyle = style.fill;
  6451. }
  6452. if ((notCheckCache || style.stroke !== prevStyle.stroke)) {
  6453. ctx.strokeStyle = style.stroke;
  6454. }
  6455. if ((notCheckCache || style.opacity !== prevStyle.opacity)) {
  6456. ctx.globalAlpha = style.opacity == null ? 1 : style.opacity;
  6457. }
  6458. if ((notCheckCache || style.blend !== prevStyle.blend)) {
  6459. ctx.globalCompositeOperation = style.blend || 'source-over';
  6460. }
  6461. if (this.hasStroke()) {
  6462. var lineWidth = style.lineWidth;
  6463. ctx.lineWidth = lineWidth / (
  6464. (this.strokeNoScale && el && el.getLineScale) ? el.getLineScale() : 1
  6465. );
  6466. }
  6467. },
  6468. hasFill: function () {
  6469. var fill = this.fill;
  6470. return fill != null && fill !== 'none';
  6471. },
  6472. hasStroke: function () {
  6473. var stroke = this.stroke;
  6474. return stroke != null && stroke !== 'none' && this.lineWidth > 0;
  6475. },
  6476. /**
  6477. * Extend from other style
  6478. * @param {zrender/graphic/Style} otherStyle
  6479. * @param {boolean} overwrite true: overwrirte any way.
  6480. * false: overwrite only when !target.hasOwnProperty
  6481. * others: overwrite when property is not null/undefined.
  6482. */
  6483. extendFrom: function (otherStyle, overwrite) {
  6484. if (otherStyle) {
  6485. for (var name in otherStyle) {
  6486. if (otherStyle.hasOwnProperty(name)
  6487. && (overwrite === true
  6488. || (
  6489. overwrite === false
  6490. ? !this.hasOwnProperty(name)
  6491. : otherStyle[name] != null
  6492. )
  6493. )
  6494. ) {
  6495. this[name] = otherStyle[name];
  6496. }
  6497. }
  6498. }
  6499. },
  6500. /**
  6501. * Batch setting style with a given object
  6502. * @param {Object|string} obj
  6503. * @param {*} [obj]
  6504. */
  6505. set: function (obj, value) {
  6506. if (typeof obj === 'string') {
  6507. this[obj] = value;
  6508. }
  6509. else {
  6510. this.extendFrom(obj, true);
  6511. }
  6512. },
  6513. /**
  6514. * Clone
  6515. * @return {zrender/graphic/Style} [description]
  6516. */
  6517. clone: function () {
  6518. var newStyle = new this.constructor();
  6519. newStyle.extendFrom(this, true);
  6520. return newStyle;
  6521. },
  6522. getGradient: function (ctx, obj, rect) {
  6523. var method = obj.type === 'radial' ? createRadialGradient : createLinearGradient;
  6524. var canvasGradient = method(ctx, obj, rect);
  6525. var colorStops = obj.colorStops;
  6526. for (var i = 0; i < colorStops.length; i++) {
  6527. canvasGradient.addColorStop(
  6528. colorStops[i].offset, colorStops[i].color
  6529. );
  6530. }
  6531. return canvasGradient;
  6532. }
  6533. };
  6534. var styleProto = Style.prototype;
  6535. for (var i = 0; i < STYLE_COMMON_PROPS.length; i++) {
  6536. var prop = STYLE_COMMON_PROPS[i];
  6537. if (!(prop[0] in styleProto)) {
  6538. styleProto[prop[0]] = prop[1];
  6539. }
  6540. }
  6541. // Provide for others
  6542. Style.getGradient = styleProto.getGradient;
  6543. var Pattern = function (image, repeat) {
  6544. // Should do nothing more in this constructor. Because gradient can be
  6545. // declard by `color: {image: ...}`, where this constructor will not be called.
  6546. this.image = image;
  6547. this.repeat = repeat;
  6548. // Can be cloned
  6549. this.type = 'pattern';
  6550. };
  6551. Pattern.prototype.getCanvasPattern = function (ctx) {
  6552. return ctx.createPattern(this.image, this.repeat || 'repeat');
  6553. };
  6554. /**
  6555. * @module zrender/Layer
  6556. * @author pissang(https://www.github.com/pissang)
  6557. */
  6558. function returnFalse() {
  6559. return false;
  6560. }
  6561. /**
  6562. * 创建dom
  6563. *
  6564. * @inner
  6565. * @param {string} id dom id 待用
  6566. * @param {Painter} painter painter instance
  6567. * @param {number} number
  6568. */
  6569. function createDom(id, painter, dpr) {
  6570. var newDom = createCanvas();
  6571. var width = painter.getWidth();
  6572. var height = painter.getHeight();
  6573. var newDomStyle = newDom.style;
  6574. if (newDomStyle) { // In node or some other non-browser environment
  6575. newDomStyle.position = 'absolute';
  6576. newDomStyle.left = 0;
  6577. newDomStyle.top = 0;
  6578. newDomStyle.width = width + 'px';
  6579. newDomStyle.height = height + 'px';
  6580. newDom.setAttribute('data-zr-dom-id', id);
  6581. }
  6582. newDom.width = width * dpr;
  6583. newDom.height = height * dpr;
  6584. return newDom;
  6585. }
  6586. /**
  6587. * @alias module:zrender/Layer
  6588. * @constructor
  6589. * @extends module:zrender/mixin/Transformable
  6590. * @param {string} id
  6591. * @param {module:zrender/Painter} painter
  6592. * @param {number} [dpr]
  6593. */
  6594. var Layer = function (id, painter, dpr) {
  6595. var dom;
  6596. dpr = dpr || devicePixelRatio;
  6597. if (typeof id === 'string') {
  6598. dom = createDom(id, painter, dpr);
  6599. }
  6600. // Not using isDom because in node it will return false
  6601. else if (isObject(id)) {
  6602. dom = id;
  6603. id = dom.id;
  6604. }
  6605. this.id = id;
  6606. this.dom = dom;
  6607. var domStyle = dom.style;
  6608. if (domStyle) { // Not in node
  6609. dom.onselectstart = returnFalse; // 避免页面选中的尴尬
  6610. domStyle['-webkit-user-select'] = 'none';
  6611. domStyle['user-select'] = 'none';
  6612. domStyle['-webkit-touch-callout'] = 'none';
  6613. domStyle['-webkit-tap-highlight-color'] = 'rgba(0,0,0,0)';
  6614. domStyle['padding'] = 0; // eslint-disable-line dot-notation
  6615. domStyle['margin'] = 0; // eslint-disable-line dot-notation
  6616. domStyle['border-width'] = 0;
  6617. }
  6618. this.domBack = null;
  6619. this.ctxBack = null;
  6620. this.painter = painter;
  6621. this.config = null;
  6622. // Configs
  6623. /**
  6624. * 每次清空画布的颜色
  6625. * @type {string}
  6626. * @default 0
  6627. */
  6628. this.clearColor = 0;
  6629. /**
  6630. * 是否开启动态模糊
  6631. * @type {boolean}
  6632. * @default false
  6633. */
  6634. this.motionBlur = false;
  6635. /**
  6636. * 在开启动态模糊的时候使用,与上一帧混合的alpha值,值越大尾迹越明显
  6637. * @type {number}
  6638. * @default 0.7
  6639. */
  6640. this.lastFrameAlpha = 0.7;
  6641. /**
  6642. * Layer dpr
  6643. * @type {number}
  6644. */
  6645. this.dpr = dpr;
  6646. };
  6647. Layer.prototype = {
  6648. constructor: Layer,
  6649. __dirty: true,
  6650. __used: false,
  6651. __drawIndex: 0,
  6652. __startIndex: 0,
  6653. __endIndex: 0,
  6654. incremental: false,
  6655. getElementCount: function () {
  6656. return this.__endIndex - this.__startIndex;
  6657. },
  6658. initContext: function () {
  6659. this.ctx = this.dom.getContext('2d');
  6660. this.ctx.dpr = this.dpr;
  6661. },
  6662. createBackBuffer: function () {
  6663. var dpr = this.dpr;
  6664. this.domBack = createDom('back-' + this.id, this.painter, dpr);
  6665. this.ctxBack = this.domBack.getContext('2d');
  6666. if (dpr !== 1) {
  6667. this.ctxBack.scale(dpr, dpr);
  6668. }
  6669. },
  6670. /**
  6671. * @param {number} width
  6672. * @param {number} height
  6673. */
  6674. resize: function (width, height) {
  6675. var dpr = this.dpr;
  6676. var dom = this.dom;
  6677. var domStyle = dom.style;
  6678. var domBack = this.domBack;
  6679. if (domStyle) {
  6680. domStyle.width = width + 'px';
  6681. domStyle.height = height + 'px';
  6682. }
  6683. dom.width = width * dpr;
  6684. dom.height = height * dpr;
  6685. if (domBack) {
  6686. domBack.width = width * dpr;
  6687. domBack.height = height * dpr;
  6688. if (dpr !== 1) {
  6689. this.ctxBack.scale(dpr, dpr);
  6690. }
  6691. }
  6692. },
  6693. /**
  6694. * 清空该层画布
  6695. * @param {boolean} [clearAll]=false Clear all with out motion blur
  6696. * @param {Color} [clearColor]
  6697. */
  6698. clear: function (clearAll, clearColor) {
  6699. var dom = this.dom;
  6700. var ctx = this.ctx;
  6701. var width = dom.width;
  6702. var height = dom.height;
  6703. var clearColor = clearColor || this.clearColor;
  6704. var haveMotionBLur = this.motionBlur && !clearAll;
  6705. var lastFrameAlpha = this.lastFrameAlpha;
  6706. var dpr = this.dpr;
  6707. if (haveMotionBLur) {
  6708. if (!this.domBack) {
  6709. this.createBackBuffer();
  6710. }
  6711. this.ctxBack.globalCompositeOperation = 'copy';
  6712. this.ctxBack.drawImage(
  6713. dom, 0, 0,
  6714. width / dpr,
  6715. height / dpr
  6716. );
  6717. }
  6718. ctx.clearRect(0, 0, width, height);
  6719. if (clearColor && clearColor !== 'transparent') {
  6720. var clearColorGradientOrPattern;
  6721. // Gradient
  6722. if (clearColor.colorStops) {
  6723. // Cache canvas gradient
  6724. clearColorGradientOrPattern = clearColor.__canvasGradient || Style.getGradient(ctx, clearColor, {
  6725. x: 0,
  6726. y: 0,
  6727. width: width,
  6728. height: height
  6729. });
  6730. clearColor.__canvasGradient = clearColorGradientOrPattern;
  6731. }
  6732. // Pattern
  6733. else if (clearColor.image) {
  6734. clearColorGradientOrPattern = Pattern.prototype.getCanvasPattern.call(clearColor, ctx);
  6735. }
  6736. ctx.save();
  6737. ctx.fillStyle = clearColorGradientOrPattern || clearColor;
  6738. ctx.fillRect(0, 0, width, height);
  6739. ctx.restore();
  6740. }
  6741. if (haveMotionBLur) {
  6742. var domBack = this.domBack;
  6743. ctx.save();
  6744. ctx.globalAlpha = lastFrameAlpha;
  6745. ctx.drawImage(domBack, 0, 0, width, height);
  6746. ctx.restore();
  6747. }
  6748. }
  6749. };
  6750. var requestAnimationFrame = (
  6751. typeof window !== 'undefined'
  6752. && (
  6753. (window.requestAnimationFrame && window.requestAnimationFrame.bind(window))
  6754. // https://github.com/ecomfe/zrender/issues/189#issuecomment-224919809
  6755. || (window.msRequestAnimationFrame && window.msRequestAnimationFrame.bind(window))
  6756. || window.mozRequestAnimationFrame
  6757. || window.webkitRequestAnimationFrame
  6758. )
  6759. ) || function (func) {
  6760. setTimeout(func, 16);
  6761. };
  6762. var globalImageCache = new LRU(50);
  6763. /**
  6764. * @param {string|HTMLImageElement|HTMLCanvasElement|Canvas} newImageOrSrc
  6765. * @return {HTMLImageElement|HTMLCanvasElement|Canvas} image
  6766. */
  6767. function findExistImage(newImageOrSrc) {
  6768. if (typeof newImageOrSrc === 'string') {
  6769. var cachedImgObj = globalImageCache.get(newImageOrSrc);
  6770. return cachedImgObj && cachedImgObj.image;
  6771. }
  6772. else {
  6773. return newImageOrSrc;
  6774. }
  6775. }
  6776. /**
  6777. * Caution: User should cache loaded images, but not just count on LRU.
  6778. * Consider if required images more than LRU size, will dead loop occur?
  6779. *
  6780. * @param {string|HTMLImageElement|HTMLCanvasElement|Canvas} newImageOrSrc
  6781. * @param {HTMLImageElement|HTMLCanvasElement|Canvas} image Existent image.
  6782. * @param {module:zrender/Element} [hostEl] For calling `dirty`.
  6783. * @param {Function} [cb] params: (image, cbPayload)
  6784. * @param {Object} [cbPayload] Payload on cb calling.
  6785. * @return {HTMLImageElement|HTMLCanvasElement|Canvas} image
  6786. */
  6787. function createOrUpdateImage(newImageOrSrc, image, hostEl, cb, cbPayload) {
  6788. if (!newImageOrSrc) {
  6789. return image;
  6790. }
  6791. else if (typeof newImageOrSrc === 'string') {
  6792. // Image should not be loaded repeatly.
  6793. if ((image && image.__zrImageSrc === newImageOrSrc) || !hostEl) {
  6794. return image;
  6795. }
  6796. // Only when there is no existent image or existent image src
  6797. // is different, this method is responsible for load.
  6798. var cachedImgObj = globalImageCache.get(newImageOrSrc);
  6799. var pendingWrap = {hostEl: hostEl, cb: cb, cbPayload: cbPayload};
  6800. if (cachedImgObj) {
  6801. image = cachedImgObj.image;
  6802. !isImageReady(image) && cachedImgObj.pending.push(pendingWrap);
  6803. }
  6804. else {
  6805. image = new Image();
  6806. image.onload = image.onerror = imageOnLoad;
  6807. globalImageCache.put(
  6808. newImageOrSrc,
  6809. image.__cachedImgObj = {
  6810. image: image,
  6811. pending: [pendingWrap]
  6812. }
  6813. );
  6814. image.src = image.__zrImageSrc = newImageOrSrc;
  6815. }
  6816. return image;
  6817. }
  6818. // newImageOrSrc is an HTMLImageElement or HTMLCanvasElement or Canvas
  6819. else {
  6820. return newImageOrSrc;
  6821. }
  6822. }
  6823. function imageOnLoad() {
  6824. var cachedImgObj = this.__cachedImgObj;
  6825. this.onload = this.onerror = this.__cachedImgObj = null;
  6826. for (var i = 0; i < cachedImgObj.pending.length; i++) {
  6827. var pendingWrap = cachedImgObj.pending[i];
  6828. var cb = pendingWrap.cb;
  6829. cb && cb(this, pendingWrap.cbPayload);
  6830. pendingWrap.hostEl.dirty();
  6831. }
  6832. cachedImgObj.pending.length = 0;
  6833. }
  6834. function isImageReady(image) {
  6835. return image && image.width && image.height;
  6836. }
  6837. var textWidthCache = {};
  6838. var textWidthCacheCounter = 0;
  6839. var TEXT_CACHE_MAX = 5000;
  6840. var STYLE_REG = /\{([a-zA-Z0-9_]+)\|([^}]*)\}/g;
  6841. var DEFAULT_FONT$1 = '12px sans-serif';
  6842. // Avoid assign to an exported variable, for transforming to cjs.
  6843. var methods$1 = {};
  6844. function $override$1(name, fn) {
  6845. methods$1[name] = fn;
  6846. }
  6847. /**
  6848. * @public
  6849. * @param {string} text
  6850. * @param {string} font
  6851. * @return {number} width
  6852. */
  6853. function getWidth(text, font) {
  6854. font = font || DEFAULT_FONT$1;
  6855. var key = text + ':' + font;
  6856. if (textWidthCache[key]) {
  6857. return textWidthCache[key];
  6858. }
  6859. var textLines = (text + '').split('\n');
  6860. var width = 0;
  6861. for (var i = 0, l = textLines.length; i < l; i++) {
  6862. // textContain.measureText may be overrided in SVG or VML
  6863. width = Math.max(measureText(textLines[i], font).width, width);
  6864. }
  6865. if (textWidthCacheCounter > TEXT_CACHE_MAX) {
  6866. textWidthCacheCounter = 0;
  6867. textWidthCache = {};
  6868. }
  6869. textWidthCacheCounter++;
  6870. textWidthCache[key] = width;
  6871. return width;
  6872. }
  6873. /**
  6874. * @public
  6875. * @param {string} text
  6876. * @param {string} font
  6877. * @param {string} [textAlign='left']
  6878. * @param {string} [textVerticalAlign='top']
  6879. * @param {Array.<number>} [textPadding]
  6880. * @param {Object} [rich]
  6881. * @param {Object} [truncate]
  6882. * @return {Object} {x, y, width, height, lineHeight}
  6883. */
  6884. function getBoundingRect(text, font, textAlign, textVerticalAlign, textPadding, textLineHeight, rich, truncate) {
  6885. return rich
  6886. ? getRichTextRect(text, font, textAlign, textVerticalAlign, textPadding, textLineHeight, rich, truncate)
  6887. : getPlainTextRect(text, font, textAlign, textVerticalAlign, textPadding, textLineHeight, truncate);
  6888. }
  6889. function getPlainTextRect(text, font, textAlign, textVerticalAlign, textPadding, textLineHeight, truncate) {
  6890. var contentBlock = parsePlainText(text, font, textPadding, textLineHeight, truncate);
  6891. var outerWidth = getWidth(text, font);
  6892. if (textPadding) {
  6893. outerWidth += textPadding[1] + textPadding[3];
  6894. }
  6895. var outerHeight = contentBlock.outerHeight;
  6896. var x = adjustTextX(0, outerWidth, textAlign);
  6897. var y = adjustTextY(0, outerHeight, textVerticalAlign);
  6898. var rect = new BoundingRect(x, y, outerWidth, outerHeight);
  6899. rect.lineHeight = contentBlock.lineHeight;
  6900. return rect;
  6901. }
  6902. function getRichTextRect(text, font, textAlign, textVerticalAlign, textPadding, textLineHeight, rich, truncate) {
  6903. var contentBlock = parseRichText(text, {
  6904. rich: rich,
  6905. truncate: truncate,
  6906. font: font,
  6907. textAlign: textAlign,
  6908. textPadding: textPadding,
  6909. textLineHeight: textLineHeight
  6910. });
  6911. var outerWidth = contentBlock.outerWidth;
  6912. var outerHeight = contentBlock.outerHeight;
  6913. var x = adjustTextX(0, outerWidth, textAlign);
  6914. var y = adjustTextY(0, outerHeight, textVerticalAlign);
  6915. return new BoundingRect(x, y, outerWidth, outerHeight);
  6916. }
  6917. /**
  6918. * @public
  6919. * @param {number} x
  6920. * @param {number} width
  6921. * @param {string} [textAlign='left']
  6922. * @return {number} Adjusted x.
  6923. */
  6924. function adjustTextX(x, width, textAlign) {
  6925. // FIXME Right to left language
  6926. if (textAlign === 'right') {
  6927. x -= width;
  6928. }
  6929. else if (textAlign === 'center') {
  6930. x -= width / 2;
  6931. }
  6932. return x;
  6933. }
  6934. /**
  6935. * @public
  6936. * @param {number} y
  6937. * @param {number} height
  6938. * @param {string} [textVerticalAlign='top']
  6939. * @return {number} Adjusted y.
  6940. */
  6941. function adjustTextY(y, height, textVerticalAlign) {
  6942. if (textVerticalAlign === 'middle') {
  6943. y -= height / 2;
  6944. }
  6945. else if (textVerticalAlign === 'bottom') {
  6946. y -= height;
  6947. }
  6948. return y;
  6949. }
  6950. /**
  6951. * Follow same interface to `Displayable.prototype.calculateTextPosition`.
  6952. * @public
  6953. * @param {Obejct} [out] Prepared out object. If not input, auto created in the method.
  6954. * @param {module:zrender/graphic/Style} style where `textPosition` and `textDistance` are visited.
  6955. * @param {Object} rect {x, y, width, height} Rect of the host elment, according to which the text positioned.
  6956. * @return {Object} The input `out`. Set: {x, y, textAlign, textVerticalAlign}
  6957. */
  6958. function calculateTextPosition(out, style, rect) {
  6959. var textPosition = style.textPosition;
  6960. var distance = style.textDistance;
  6961. var x = rect.x;
  6962. var y = rect.y;
  6963. distance = distance || 0;
  6964. var height = rect.height;
  6965. var width = rect.width;
  6966. var halfHeight = height / 2;
  6967. var textAlign = 'left';
  6968. var textVerticalAlign = 'top';
  6969. switch (textPosition) {
  6970. case 'left':
  6971. x -= distance;
  6972. y += halfHeight;
  6973. textAlign = 'right';
  6974. textVerticalAlign = 'middle';
  6975. break;
  6976. case 'right':
  6977. x += distance + width;
  6978. y += halfHeight;
  6979. textVerticalAlign = 'middle';
  6980. break;
  6981. case 'top':
  6982. x += width / 2;
  6983. y -= distance;
  6984. textAlign = 'center';
  6985. textVerticalAlign = 'bottom';
  6986. break;
  6987. case 'bottom':
  6988. x += width / 2;
  6989. y += height + distance;
  6990. textAlign = 'center';
  6991. break;
  6992. case 'inside':
  6993. x += width / 2;
  6994. y += halfHeight;
  6995. textAlign = 'center';
  6996. textVerticalAlign = 'middle';
  6997. break;
  6998. case 'insideLeft':
  6999. x += distance;
  7000. y += halfHeight;
  7001. textVerticalAlign = 'middle';
  7002. break;
  7003. case 'insideRight':
  7004. x += width - distance;
  7005. y += halfHeight;
  7006. textAlign = 'right';
  7007. textVerticalAlign = 'middle';
  7008. break;
  7009. case 'insideTop':
  7010. x += width / 2;
  7011. y += distance;
  7012. textAlign = 'center';
  7013. break;
  7014. case 'insideBottom':
  7015. x += width / 2;
  7016. y += height - distance;
  7017. textAlign = 'center';
  7018. textVerticalAlign = 'bottom';
  7019. break;
  7020. case 'insideTopLeft':
  7021. x += distance;
  7022. y += distance;
  7023. break;
  7024. case 'insideTopRight':
  7025. x += width - distance;
  7026. y += distance;
  7027. textAlign = 'right';
  7028. break;
  7029. case 'insideBottomLeft':
  7030. x += distance;
  7031. y += height - distance;
  7032. textVerticalAlign = 'bottom';
  7033. break;
  7034. case 'insideBottomRight':
  7035. x += width - distance;
  7036. y += height - distance;
  7037. textAlign = 'right';
  7038. textVerticalAlign = 'bottom';
  7039. break;
  7040. }
  7041. out = out || {};
  7042. out.x = x;
  7043. out.y = y;
  7044. out.textAlign = textAlign;
  7045. out.textVerticalAlign = textVerticalAlign;
  7046. return out;
  7047. }
  7048. /**
  7049. * To be removed. But still do not remove in case that some one has imported it.
  7050. * @deprecated
  7051. * @public
  7052. * @param {stirng} textPosition
  7053. * @param {Object} rect {x, y, width, height}
  7054. * @param {number} distance
  7055. * @return {Object} {x, y, textAlign, textVerticalAlign}
  7056. */
  7057. /**
  7058. * Show ellipsis if overflow.
  7059. *
  7060. * @public
  7061. * @param {string} text
  7062. * @param {string} containerWidth
  7063. * @param {string} font
  7064. * @param {number} [ellipsis='...']
  7065. * @param {Object} [options]
  7066. * @param {number} [options.maxIterations=3]
  7067. * @param {number} [options.minChar=0] If truncate result are less
  7068. * then minChar, ellipsis will not show, which is
  7069. * better for user hint in some cases.
  7070. * @param {number} [options.placeholder=''] When all truncated, use the placeholder.
  7071. * @return {string}
  7072. */
  7073. function truncateText(text, containerWidth, font, ellipsis, options) {
  7074. if (!containerWidth) {
  7075. return '';
  7076. }
  7077. var textLines = (text + '').split('\n');
  7078. options = prepareTruncateOptions(containerWidth, font, ellipsis, options);
  7079. // FIXME
  7080. // It is not appropriate that every line has '...' when truncate multiple lines.
  7081. for (var i = 0, len = textLines.length; i < len; i++) {
  7082. textLines[i] = truncateSingleLine(textLines[i], options);
  7083. }
  7084. return textLines.join('\n');
  7085. }
  7086. function prepareTruncateOptions(containerWidth, font, ellipsis, options) {
  7087. options = extend({}, options);
  7088. options.font = font;
  7089. var ellipsis = retrieve2(ellipsis, '...');
  7090. options.maxIterations = retrieve2(options.maxIterations, 2);
  7091. var minChar = options.minChar = retrieve2(options.minChar, 0);
  7092. // FIXME
  7093. // Other languages?
  7094. options.cnCharWidth = getWidth('国', font);
  7095. // FIXME
  7096. // Consider proportional font?
  7097. var ascCharWidth = options.ascCharWidth = getWidth('a', font);
  7098. options.placeholder = retrieve2(options.placeholder, '');
  7099. // Example 1: minChar: 3, text: 'asdfzxcv', truncate result: 'asdf', but not: 'a...'.
  7100. // Example 2: minChar: 3, text: '维度', truncate result: '维', but not: '...'.
  7101. var contentWidth = containerWidth = Math.max(0, containerWidth - 1); // Reserve some gap.
  7102. for (var i = 0; i < minChar && contentWidth >= ascCharWidth; i++) {
  7103. contentWidth -= ascCharWidth;
  7104. }
  7105. var ellipsisWidth = getWidth(ellipsis, font);
  7106. if (ellipsisWidth > contentWidth) {
  7107. ellipsis = '';
  7108. ellipsisWidth = 0;
  7109. }
  7110. contentWidth = containerWidth - ellipsisWidth;
  7111. options.ellipsis = ellipsis;
  7112. options.ellipsisWidth = ellipsisWidth;
  7113. options.contentWidth = contentWidth;
  7114. options.containerWidth = containerWidth;
  7115. return options;
  7116. }
  7117. function truncateSingleLine(textLine, options) {
  7118. var containerWidth = options.containerWidth;
  7119. var font = options.font;
  7120. var contentWidth = options.contentWidth;
  7121. if (!containerWidth) {
  7122. return '';
  7123. }
  7124. var lineWidth = getWidth(textLine, font);
  7125. if (lineWidth <= containerWidth) {
  7126. return textLine;
  7127. }
  7128. for (var j = 0; ; j++) {
  7129. if (lineWidth <= contentWidth || j >= options.maxIterations) {
  7130. textLine += options.ellipsis;
  7131. break;
  7132. }
  7133. var subLength = j === 0
  7134. ? estimateLength(textLine, contentWidth, options.ascCharWidth, options.cnCharWidth)
  7135. : lineWidth > 0
  7136. ? Math.floor(textLine.length * contentWidth / lineWidth)
  7137. : 0;
  7138. textLine = textLine.substr(0, subLength);
  7139. lineWidth = getWidth(textLine, font);
  7140. }
  7141. if (textLine === '') {
  7142. textLine = options.placeholder;
  7143. }
  7144. return textLine;
  7145. }
  7146. function estimateLength(text, contentWidth, ascCharWidth, cnCharWidth) {
  7147. var width = 0;
  7148. var i = 0;
  7149. for (var len = text.length; i < len && width < contentWidth; i++) {
  7150. var charCode = text.charCodeAt(i);
  7151. width += (0 <= charCode && charCode <= 127) ? ascCharWidth : cnCharWidth;
  7152. }
  7153. return i;
  7154. }
  7155. /**
  7156. * @public
  7157. * @param {string} font
  7158. * @return {number} line height
  7159. */
  7160. function getLineHeight(font) {
  7161. // FIXME A rough approach.
  7162. return getWidth('国', font);
  7163. }
  7164. /**
  7165. * @public
  7166. * @param {string} text
  7167. * @param {string} font
  7168. * @return {Object} width
  7169. */
  7170. function measureText(text, font) {
  7171. return methods$1.measureText(text, font);
  7172. }
  7173. // Avoid assign to an exported variable, for transforming to cjs.
  7174. methods$1.measureText = function (text, font) {
  7175. var ctx = getContext();
  7176. ctx.font = font || DEFAULT_FONT$1;
  7177. return ctx.measureText(text);
  7178. };
  7179. /**
  7180. * @public
  7181. * @param {string} text
  7182. * @param {string} font
  7183. * @param {Object} [truncate]
  7184. * @return {Object} block: {lineHeight, lines, height, outerHeight, canCacheByTextString}
  7185. * Notice: for performance, do not calculate outerWidth util needed.
  7186. * `canCacheByTextString` means the result `lines` is only determined by the input `text`.
  7187. * Thus we can simply comparing the `input` text to determin whether the result changed,
  7188. * without travel the result `lines`.
  7189. */
  7190. function parsePlainText(text, font, padding, textLineHeight, truncate) {
  7191. text != null && (text += '');
  7192. var lineHeight = retrieve2(textLineHeight, getLineHeight(font));
  7193. var lines = text ? text.split('\n') : [];
  7194. var height = lines.length * lineHeight;
  7195. var outerHeight = height;
  7196. var canCacheByTextString = true;
  7197. if (padding) {
  7198. outerHeight += padding[0] + padding[2];
  7199. }
  7200. if (text && truncate) {
  7201. canCacheByTextString = false;
  7202. var truncOuterHeight = truncate.outerHeight;
  7203. var truncOuterWidth = truncate.outerWidth;
  7204. if (truncOuterHeight != null && outerHeight > truncOuterHeight) {
  7205. text = '';
  7206. lines = [];
  7207. }
  7208. else if (truncOuterWidth != null) {
  7209. var options = prepareTruncateOptions(
  7210. truncOuterWidth - (padding ? padding[1] + padding[3] : 0),
  7211. font,
  7212. truncate.ellipsis,
  7213. {minChar: truncate.minChar, placeholder: truncate.placeholder}
  7214. );
  7215. // FIXME
  7216. // It is not appropriate that every line has '...' when truncate multiple lines.
  7217. for (var i = 0, len = lines.length; i < len; i++) {
  7218. lines[i] = truncateSingleLine(lines[i], options);
  7219. }
  7220. }
  7221. }
  7222. return {
  7223. lines: lines,
  7224. height: height,
  7225. outerHeight: outerHeight,
  7226. lineHeight: lineHeight,
  7227. canCacheByTextString: canCacheByTextString
  7228. };
  7229. }
  7230. /**
  7231. * For example: 'some text {a|some text}other text{b|some text}xxx{c|}xxx'
  7232. * Also consider 'bbbb{a|xxx\nzzz}xxxx\naaaa'.
  7233. *
  7234. * @public
  7235. * @param {string} text
  7236. * @param {Object} style
  7237. * @return {Object} block
  7238. * {
  7239. * width,
  7240. * height,
  7241. * lines: [{
  7242. * lineHeight,
  7243. * width,
  7244. * tokens: [[{
  7245. * styleName,
  7246. * text,
  7247. * width, // include textPadding
  7248. * height, // include textPadding
  7249. * textWidth, // pure text width
  7250. * textHeight, // pure text height
  7251. * lineHeihgt,
  7252. * font,
  7253. * textAlign,
  7254. * textVerticalAlign
  7255. * }], [...], ...]
  7256. * }, ...]
  7257. * }
  7258. * If styleName is undefined, it is plain text.
  7259. */
  7260. function parseRichText(text, style) {
  7261. var contentBlock = {lines: [], width: 0, height: 0};
  7262. text != null && (text += '');
  7263. if (!text) {
  7264. return contentBlock;
  7265. }
  7266. var lastIndex = STYLE_REG.lastIndex = 0;
  7267. var result;
  7268. while ((result = STYLE_REG.exec(text)) != null) {
  7269. var matchedIndex = result.index;
  7270. if (matchedIndex > lastIndex) {
  7271. pushTokens(contentBlock, text.substring(lastIndex, matchedIndex));
  7272. }
  7273. pushTokens(contentBlock, result[2], result[1]);
  7274. lastIndex = STYLE_REG.lastIndex;
  7275. }
  7276. if (lastIndex < text.length) {
  7277. pushTokens(contentBlock, text.substring(lastIndex, text.length));
  7278. }
  7279. var lines = contentBlock.lines;
  7280. var contentHeight = 0;
  7281. var contentWidth = 0;
  7282. // For `textWidth: 100%`
  7283. var pendingList = [];
  7284. var stlPadding = style.textPadding;
  7285. var truncate = style.truncate;
  7286. var truncateWidth = truncate && truncate.outerWidth;
  7287. var truncateHeight = truncate && truncate.outerHeight;
  7288. if (stlPadding) {
  7289. truncateWidth != null && (truncateWidth -= stlPadding[1] + stlPadding[3]);
  7290. truncateHeight != null && (truncateHeight -= stlPadding[0] + stlPadding[2]);
  7291. }
  7292. // Calculate layout info of tokens.
  7293. for (var i = 0; i < lines.length; i++) {
  7294. var line = lines[i];
  7295. var lineHeight = 0;
  7296. var lineWidth = 0;
  7297. for (var j = 0; j < line.tokens.length; j++) {
  7298. var token = line.tokens[j];
  7299. var tokenStyle = token.styleName && style.rich[token.styleName] || {};
  7300. // textPadding should not inherit from style.
  7301. var textPadding = token.textPadding = tokenStyle.textPadding;
  7302. // textFont has been asigned to font by `normalizeStyle`.
  7303. var font = token.font = tokenStyle.font || style.font;
  7304. // textHeight can be used when textVerticalAlign is specified in token.
  7305. var tokenHeight = token.textHeight = retrieve2(
  7306. // textHeight should not be inherited, consider it can be specified
  7307. // as box height of the block.
  7308. tokenStyle.textHeight, getLineHeight(font)
  7309. );
  7310. textPadding && (tokenHeight += textPadding[0] + textPadding[2]);
  7311. token.height = tokenHeight;
  7312. token.lineHeight = retrieve3(
  7313. tokenStyle.textLineHeight, style.textLineHeight, tokenHeight
  7314. );
  7315. token.textAlign = tokenStyle && tokenStyle.textAlign || style.textAlign;
  7316. token.textVerticalAlign = tokenStyle && tokenStyle.textVerticalAlign || 'middle';
  7317. if (truncateHeight != null && contentHeight + token.lineHeight > truncateHeight) {
  7318. return {lines: [], width: 0, height: 0};
  7319. }
  7320. token.textWidth = getWidth(token.text, font);
  7321. var tokenWidth = tokenStyle.textWidth;
  7322. var tokenWidthNotSpecified = tokenWidth == null || tokenWidth === 'auto';
  7323. // Percent width, can be `100%`, can be used in drawing separate
  7324. // line when box width is needed to be auto.
  7325. if (typeof tokenWidth === 'string' && tokenWidth.charAt(tokenWidth.length - 1) === '%') {
  7326. token.percentWidth = tokenWidth;
  7327. pendingList.push(token);
  7328. tokenWidth = 0;
  7329. // Do not truncate in this case, because there is no user case
  7330. // and it is too complicated.
  7331. }
  7332. else {
  7333. if (tokenWidthNotSpecified) {
  7334. tokenWidth = token.textWidth;
  7335. // FIXME: If image is not loaded and textWidth is not specified, calling
  7336. // `getBoundingRect()` will not get correct result.
  7337. var textBackgroundColor = tokenStyle.textBackgroundColor;
  7338. var bgImg = textBackgroundColor && textBackgroundColor.image;
  7339. // Use cases:
  7340. // (1) If image is not loaded, it will be loaded at render phase and call
  7341. // `dirty()` and `textBackgroundColor.image` will be replaced with the loaded
  7342. // image, and then the right size will be calculated here at the next tick.
  7343. // See `graphic/helper/text.js`.
  7344. // (2) If image loaded, and `textBackgroundColor.image` is image src string,
  7345. // use `imageHelper.findExistImage` to find cached image.
  7346. // `imageHelper.findExistImage` will always be called here before
  7347. // `imageHelper.createOrUpdateImage` in `graphic/helper/text.js#renderRichText`
  7348. // which ensures that image will not be rendered before correct size calcualted.
  7349. if (bgImg) {
  7350. bgImg = findExistImage(bgImg);
  7351. if (isImageReady(bgImg)) {
  7352. tokenWidth = Math.max(tokenWidth, bgImg.width * tokenHeight / bgImg.height);
  7353. }
  7354. }
  7355. }
  7356. var paddingW = textPadding ? textPadding[1] + textPadding[3] : 0;
  7357. tokenWidth += paddingW;
  7358. var remianTruncWidth = truncateWidth != null ? truncateWidth - lineWidth : null;
  7359. if (remianTruncWidth != null && remianTruncWidth < tokenWidth) {
  7360. if (!tokenWidthNotSpecified || remianTruncWidth < paddingW) {
  7361. token.text = '';
  7362. token.textWidth = tokenWidth = 0;
  7363. }
  7364. else {
  7365. token.text = truncateText(
  7366. token.text, remianTruncWidth - paddingW, font, truncate.ellipsis,
  7367. {minChar: truncate.minChar}
  7368. );
  7369. token.textWidth = getWidth(token.text, font);
  7370. tokenWidth = token.textWidth + paddingW;
  7371. }
  7372. }
  7373. }
  7374. lineWidth += (token.width = tokenWidth);
  7375. tokenStyle && (lineHeight = Math.max(lineHeight, token.lineHeight));
  7376. }
  7377. line.width = lineWidth;
  7378. line.lineHeight = lineHeight;
  7379. contentHeight += lineHeight;
  7380. contentWidth = Math.max(contentWidth, lineWidth);
  7381. }
  7382. contentBlock.outerWidth = contentBlock.width = retrieve2(style.textWidth, contentWidth);
  7383. contentBlock.outerHeight = contentBlock.height = retrieve2(style.textHeight, contentHeight);
  7384. if (stlPadding) {
  7385. contentBlock.outerWidth += stlPadding[1] + stlPadding[3];
  7386. contentBlock.outerHeight += stlPadding[0] + stlPadding[2];
  7387. }
  7388. for (var i = 0; i < pendingList.length; i++) {
  7389. var token = pendingList[i];
  7390. var percentWidth = token.percentWidth;
  7391. // Should not base on outerWidth, because token can not be placed out of padding.
  7392. token.width = parseInt(percentWidth, 10) / 100 * contentWidth;
  7393. }
  7394. return contentBlock;
  7395. }
  7396. function pushTokens(block, str, styleName) {
  7397. var isEmptyStr = str === '';
  7398. var strs = str.split('\n');
  7399. var lines = block.lines;
  7400. for (var i = 0; i < strs.length; i++) {
  7401. var text = strs[i];
  7402. var token = {
  7403. styleName: styleName,
  7404. text: text,
  7405. isLineHolder: !text && !isEmptyStr
  7406. };
  7407. // The first token should be appended to the last line.
  7408. if (!i) {
  7409. var tokens = (lines[lines.length - 1] || (lines[0] = {tokens: []})).tokens;
  7410. // Consider cases:
  7411. // (1) ''.split('\n') => ['', '\n', ''], the '' at the first item
  7412. // (which is a placeholder) should be replaced by new token.
  7413. // (2) A image backage, where token likes {a|}.
  7414. // (3) A redundant '' will affect textAlign in line.
  7415. // (4) tokens with the same tplName should not be merged, because
  7416. // they should be displayed in different box (with border and padding).
  7417. var tokensLen = tokens.length;
  7418. (tokensLen === 1 && tokens[0].isLineHolder)
  7419. ? (tokens[0] = token)
  7420. // Consider text is '', only insert when it is the "lineHolder" or
  7421. // "emptyStr". Otherwise a redundant '' will affect textAlign in line.
  7422. : ((text || !tokensLen || isEmptyStr) && tokens.push(token));
  7423. }
  7424. // Other tokens always start a new line.
  7425. else {
  7426. // If there is '', insert it as a placeholder.
  7427. lines.push({tokens: [token]});
  7428. }
  7429. }
  7430. }
  7431. function makeFont(style) {
  7432. // FIXME in node-canvas fontWeight is before fontStyle
  7433. // Use `fontSize` `fontFamily` to check whether font properties are defined.
  7434. var font = (style.fontSize || style.fontFamily) && [
  7435. style.fontStyle,
  7436. style.fontWeight,
  7437. (style.fontSize || 12) + 'px',
  7438. // If font properties are defined, `fontFamily` should not be ignored.
  7439. style.fontFamily || 'sans-serif'
  7440. ].join(' ');
  7441. return font && trim(font) || style.textFont || style.font;
  7442. }
  7443. /**
  7444. * @param {Object} ctx
  7445. * @param {Object} shape
  7446. * @param {number} shape.x
  7447. * @param {number} shape.y
  7448. * @param {number} shape.width
  7449. * @param {number} shape.height
  7450. * @param {number} shape.r
  7451. */
  7452. function buildPath(ctx, shape) {
  7453. var x = shape.x;
  7454. var y = shape.y;
  7455. var width = shape.width;
  7456. var height = shape.height;
  7457. var r = shape.r;
  7458. var r1;
  7459. var r2;
  7460. var r3;
  7461. var r4;
  7462. // Convert width and height to positive for better borderRadius
  7463. if (width < 0) {
  7464. x = x + width;
  7465. width = -width;
  7466. }
  7467. if (height < 0) {
  7468. y = y + height;
  7469. height = -height;
  7470. }
  7471. if (typeof r === 'number') {
  7472. r1 = r2 = r3 = r4 = r;
  7473. }
  7474. else if (r instanceof Array) {
  7475. if (r.length === 1) {
  7476. r1 = r2 = r3 = r4 = r[0];
  7477. }
  7478. else if (r.length === 2) {
  7479. r1 = r3 = r[0];
  7480. r2 = r4 = r[1];
  7481. }
  7482. else if (r.length === 3) {
  7483. r1 = r[0];
  7484. r2 = r4 = r[1];
  7485. r3 = r[2];
  7486. }
  7487. else {
  7488. r1 = r[0];
  7489. r2 = r[1];
  7490. r3 = r[2];
  7491. r4 = r[3];
  7492. }
  7493. }
  7494. else {
  7495. r1 = r2 = r3 = r4 = 0;
  7496. }
  7497. var total;
  7498. if (r1 + r2 > width) {
  7499. total = r1 + r2;
  7500. r1 *= width / total;
  7501. r2 *= width / total;
  7502. }
  7503. if (r3 + r4 > width) {
  7504. total = r3 + r4;
  7505. r3 *= width / total;
  7506. r4 *= width / total;
  7507. }
  7508. if (r2 + r3 > height) {
  7509. total = r2 + r3;
  7510. r2 *= height / total;
  7511. r3 *= height / total;
  7512. }
  7513. if (r1 + r4 > height) {
  7514. total = r1 + r4;
  7515. r1 *= height / total;
  7516. r4 *= height / total;
  7517. }
  7518. ctx.moveTo(x + r1, y);
  7519. ctx.lineTo(x + width - r2, y);
  7520. r2 !== 0 && ctx.arc(x + width - r2, y + r2, r2, -Math.PI / 2, 0);
  7521. ctx.lineTo(x + width, y + height - r3);
  7522. r3 !== 0 && ctx.arc(x + width - r3, y + height - r3, r3, 0, Math.PI / 2);
  7523. ctx.lineTo(x + r4, y + height);
  7524. r4 !== 0 && ctx.arc(x + r4, y + height - r4, r4, Math.PI / 2, Math.PI);
  7525. ctx.lineTo(x, y + r1);
  7526. r1 !== 0 && ctx.arc(x + r1, y + r1, r1, Math.PI, Math.PI * 1.5);
  7527. }
  7528. var DEFAULT_FONT = DEFAULT_FONT$1;
  7529. // TODO: Have not support 'start', 'end' yet.
  7530. var VALID_TEXT_ALIGN = {left: 1, right: 1, center: 1};
  7531. var VALID_TEXT_VERTICAL_ALIGN = {top: 1, bottom: 1, middle: 1};
  7532. // Different from `STYLE_COMMON_PROPS` of `graphic/Style`,
  7533. // the default value of shadowColor is `'transparent'`.
  7534. var SHADOW_STYLE_COMMON_PROPS = [
  7535. ['textShadowBlur', 'shadowBlur', 0],
  7536. ['textShadowOffsetX', 'shadowOffsetX', 0],
  7537. ['textShadowOffsetY', 'shadowOffsetY', 0],
  7538. ['textShadowColor', 'shadowColor', 'transparent']
  7539. ];
  7540. var _tmpTextPositionResult = {};
  7541. var _tmpBoxPositionResult = {};
  7542. /**
  7543. * @param {module:zrender/graphic/Style} style
  7544. * @return {module:zrender/graphic/Style} The input style.
  7545. */
  7546. function normalizeTextStyle(style) {
  7547. normalizeStyle(style);
  7548. each(style.rich, normalizeStyle);
  7549. return style;
  7550. }
  7551. function normalizeStyle(style) {
  7552. if (style) {
  7553. style.font = makeFont(style);
  7554. var textAlign = style.textAlign;
  7555. textAlign === 'middle' && (textAlign = 'center');
  7556. style.textAlign = (
  7557. textAlign == null || VALID_TEXT_ALIGN[textAlign]
  7558. ) ? textAlign : 'left';
  7559. // Compatible with textBaseline.
  7560. var textVerticalAlign = style.textVerticalAlign || style.textBaseline;
  7561. textVerticalAlign === 'center' && (textVerticalAlign = 'middle');
  7562. style.textVerticalAlign = (
  7563. textVerticalAlign == null || VALID_TEXT_VERTICAL_ALIGN[textVerticalAlign]
  7564. ) ? textVerticalAlign : 'top';
  7565. var textPadding = style.textPadding;
  7566. if (textPadding) {
  7567. style.textPadding = normalizeCssArray(style.textPadding);
  7568. }
  7569. }
  7570. }
  7571. /**
  7572. * @param {CanvasRenderingContext2D} ctx
  7573. * @param {string} text
  7574. * @param {module:zrender/graphic/Style} style
  7575. * @param {Object|boolean} [rect] {x, y, width, height}
  7576. * If set false, rect text is not used.
  7577. * @param {Element|module:zrender/graphic/helper/constant.WILL_BE_RESTORED} [prevEl] For ctx prop cache.
  7578. */
  7579. function renderText(hostEl, ctx, text, style, rect, prevEl) {
  7580. style.rich
  7581. ? renderRichText(hostEl, ctx, text, style, rect, prevEl)
  7582. : renderPlainText(hostEl, ctx, text, style, rect, prevEl);
  7583. }
  7584. // Avoid setting to ctx according to prevEl if possible for
  7585. // performance in scenarios of large amount text.
  7586. function renderPlainText(hostEl, ctx, text, style, rect, prevEl) {
  7587. 'use strict';
  7588. var needDrawBg = needDrawBackground(style);
  7589. var prevStyle;
  7590. var checkCache = false;
  7591. var cachedByMe = ctx.__attrCachedBy === ContextCachedBy.PLAIN_TEXT;
  7592. // Only take and check cache for `Text` el, but not RectText.
  7593. if (prevEl !== WILL_BE_RESTORED) {
  7594. if (prevEl) {
  7595. prevStyle = prevEl.style;
  7596. checkCache = !needDrawBg && cachedByMe && prevStyle;
  7597. }
  7598. // Prevent from using cache in `Style::bind`, because of the case:
  7599. // ctx property is modified by other properties than `Style::bind`
  7600. // used, and Style::bind is called next.
  7601. ctx.__attrCachedBy = needDrawBg ? ContextCachedBy.NONE : ContextCachedBy.PLAIN_TEXT;
  7602. }
  7603. // Since this will be restored, prevent from using these props to check cache in the next
  7604. // entering of this method. But do not need to clear other cache like `Style::bind`.
  7605. else if (cachedByMe) {
  7606. ctx.__attrCachedBy = ContextCachedBy.NONE;
  7607. }
  7608. var styleFont = style.font || DEFAULT_FONT;
  7609. // PENDING
  7610. // Only `Text` el set `font` and keep it (`RectText` will restore). So theoretically
  7611. // we can make font cache on ctx, which can cache for text el that are discontinuous.
  7612. // But layer save/restore needed to be considered.
  7613. // if (styleFont !== ctx.__fontCache) {
  7614. // ctx.font = styleFont;
  7615. // if (prevEl !== WILL_BE_RESTORED) {
  7616. // ctx.__fontCache = styleFont;
  7617. // }
  7618. // }
  7619. if (!checkCache || styleFont !== (prevStyle.font || DEFAULT_FONT)) {
  7620. ctx.font = styleFont;
  7621. }
  7622. // Use the final font from context-2d, because the final
  7623. // font might not be the style.font when it is illegal.
  7624. // But get `ctx.font` might be time consuming.
  7625. var computedFont = hostEl.__computedFont;
  7626. if (hostEl.__styleFont !== styleFont) {
  7627. hostEl.__styleFont = styleFont;
  7628. computedFont = hostEl.__computedFont = ctx.font;
  7629. }
  7630. var textPadding = style.textPadding;
  7631. var textLineHeight = style.textLineHeight;
  7632. var contentBlock = hostEl.__textCotentBlock;
  7633. if (!contentBlock || hostEl.__dirtyText) {
  7634. contentBlock = hostEl.__textCotentBlock = parsePlainText(
  7635. text, computedFont, textPadding, textLineHeight, style.truncate
  7636. );
  7637. }
  7638. var outerHeight = contentBlock.outerHeight;
  7639. var textLines = contentBlock.lines;
  7640. var lineHeight = contentBlock.lineHeight;
  7641. var boxPos = getBoxPosition(_tmpBoxPositionResult, hostEl, style, rect);
  7642. var baseX = boxPos.baseX;
  7643. var baseY = boxPos.baseY;
  7644. var textAlign = boxPos.textAlign || 'left';
  7645. var textVerticalAlign = boxPos.textVerticalAlign;
  7646. // Origin of textRotation should be the base point of text drawing.
  7647. applyTextRotation(ctx, style, rect, baseX, baseY);
  7648. var boxY = adjustTextY(baseY, outerHeight, textVerticalAlign);
  7649. var textX = baseX;
  7650. var textY = boxY;
  7651. if (needDrawBg || textPadding) {
  7652. // Consider performance, do not call getTextWidth util necessary.
  7653. var textWidth = getWidth(text, computedFont);
  7654. var outerWidth = textWidth;
  7655. textPadding && (outerWidth += textPadding[1] + textPadding[3]);
  7656. var boxX = adjustTextX(baseX, outerWidth, textAlign);
  7657. needDrawBg && drawBackground(hostEl, ctx, style, boxX, boxY, outerWidth, outerHeight);
  7658. if (textPadding) {
  7659. textX = getTextXForPadding(baseX, textAlign, textPadding);
  7660. textY += textPadding[0];
  7661. }
  7662. }
  7663. // Always set textAlign and textBase line, because it is difficute to calculate
  7664. // textAlign from prevEl, and we dont sure whether textAlign will be reset if
  7665. // font set happened.
  7666. ctx.textAlign = textAlign;
  7667. // Force baseline to be "middle". Otherwise, if using "top", the
  7668. // text will offset downward a little bit in font "Microsoft YaHei".
  7669. ctx.textBaseline = 'middle';
  7670. // Set text opacity
  7671. ctx.globalAlpha = style.opacity || 1;
  7672. // Always set shadowBlur and shadowOffset to avoid leak from displayable.
  7673. for (var i = 0; i < SHADOW_STYLE_COMMON_PROPS.length; i++) {
  7674. var propItem = SHADOW_STYLE_COMMON_PROPS[i];
  7675. var styleProp = propItem[0];
  7676. var ctxProp = propItem[1];
  7677. var val = style[styleProp];
  7678. if (!checkCache || val !== prevStyle[styleProp]) {
  7679. ctx[ctxProp] = fixShadow(ctx, ctxProp, val || propItem[2]);
  7680. }
  7681. }
  7682. // `textBaseline` is set as 'middle'.
  7683. textY += lineHeight / 2;
  7684. var textStrokeWidth = style.textStrokeWidth;
  7685. var textStrokeWidthPrev = checkCache ? prevStyle.textStrokeWidth : null;
  7686. var strokeWidthChanged = !checkCache || textStrokeWidth !== textStrokeWidthPrev;
  7687. var strokeChanged = !checkCache || strokeWidthChanged || style.textStroke !== prevStyle.textStroke;
  7688. var textStroke = getStroke(style.textStroke, textStrokeWidth);
  7689. var textFill = getFill(style.textFill);
  7690. if (textStroke) {
  7691. if (strokeWidthChanged) {
  7692. ctx.lineWidth = textStrokeWidth;
  7693. }
  7694. if (strokeChanged) {
  7695. ctx.strokeStyle = textStroke;
  7696. }
  7697. }
  7698. if (textFill) {
  7699. if (!checkCache || style.textFill !== prevStyle.textFill) {
  7700. ctx.fillStyle = textFill;
  7701. }
  7702. }
  7703. // Optimize simply, in most cases only one line exists.
  7704. if (textLines.length === 1) {
  7705. // Fill after stroke so the outline will not cover the main part.
  7706. textStroke && ctx.strokeText(textLines[0], textX, textY);
  7707. textFill && ctx.fillText(textLines[0], textX, textY);
  7708. }
  7709. else {
  7710. for (var i = 0; i < textLines.length; i++) {
  7711. // Fill after stroke so the outline will not cover the main part.
  7712. textStroke && ctx.strokeText(textLines[i], textX, textY);
  7713. textFill && ctx.fillText(textLines[i], textX, textY);
  7714. textY += lineHeight;
  7715. }
  7716. }
  7717. }
  7718. function renderRichText(hostEl, ctx, text, style, rect, prevEl) {
  7719. // Do not do cache for rich text because of the complexity.
  7720. // But `RectText` this will be restored, do not need to clear other cache like `Style::bind`.
  7721. if (prevEl !== WILL_BE_RESTORED) {
  7722. ctx.__attrCachedBy = ContextCachedBy.NONE;
  7723. }
  7724. var contentBlock = hostEl.__textCotentBlock;
  7725. if (!contentBlock || hostEl.__dirtyText) {
  7726. contentBlock = hostEl.__textCotentBlock = parseRichText(text, style);
  7727. }
  7728. drawRichText(hostEl, ctx, contentBlock, style, rect);
  7729. }
  7730. function drawRichText(hostEl, ctx, contentBlock, style, rect) {
  7731. var contentWidth = contentBlock.width;
  7732. var outerWidth = contentBlock.outerWidth;
  7733. var outerHeight = contentBlock.outerHeight;
  7734. var textPadding = style.textPadding;
  7735. var boxPos = getBoxPosition(_tmpBoxPositionResult, hostEl, style, rect);
  7736. var baseX = boxPos.baseX;
  7737. var baseY = boxPos.baseY;
  7738. var textAlign = boxPos.textAlign;
  7739. var textVerticalAlign = boxPos.textVerticalAlign;
  7740. // Origin of textRotation should be the base point of text drawing.
  7741. applyTextRotation(ctx, style, rect, baseX, baseY);
  7742. var boxX = adjustTextX(baseX, outerWidth, textAlign);
  7743. var boxY = adjustTextY(baseY, outerHeight, textVerticalAlign);
  7744. var xLeft = boxX;
  7745. var lineTop = boxY;
  7746. if (textPadding) {
  7747. xLeft += textPadding[3];
  7748. lineTop += textPadding[0];
  7749. }
  7750. var xRight = xLeft + contentWidth;
  7751. needDrawBackground(style) && drawBackground(
  7752. hostEl, ctx, style, boxX, boxY, outerWidth, outerHeight
  7753. );
  7754. for (var i = 0; i < contentBlock.lines.length; i++) {
  7755. var line = contentBlock.lines[i];
  7756. var tokens = line.tokens;
  7757. var tokenCount = tokens.length;
  7758. var lineHeight = line.lineHeight;
  7759. var usedWidth = line.width;
  7760. var leftIndex = 0;
  7761. var lineXLeft = xLeft;
  7762. var lineXRight = xRight;
  7763. var rightIndex = tokenCount - 1;
  7764. var token;
  7765. while (
  7766. leftIndex < tokenCount
  7767. && (token = tokens[leftIndex], !token.textAlign || token.textAlign === 'left')
  7768. ) {
  7769. placeToken(hostEl, ctx, token, style, lineHeight, lineTop, lineXLeft, 'left');
  7770. usedWidth -= token.width;
  7771. lineXLeft += token.width;
  7772. leftIndex++;
  7773. }
  7774. while (
  7775. rightIndex >= 0
  7776. && (token = tokens[rightIndex], token.textAlign === 'right')
  7777. ) {
  7778. placeToken(hostEl, ctx, token, style, lineHeight, lineTop, lineXRight, 'right');
  7779. usedWidth -= token.width;
  7780. lineXRight -= token.width;
  7781. rightIndex--;
  7782. }
  7783. // The other tokens are placed as textAlign 'center' if there is enough space.
  7784. lineXLeft += (contentWidth - (lineXLeft - xLeft) - (xRight - lineXRight) - usedWidth) / 2;
  7785. while (leftIndex <= rightIndex) {
  7786. token = tokens[leftIndex];
  7787. // Consider width specified by user, use 'center' rather than 'left'.
  7788. placeToken(hostEl, ctx, token, style, lineHeight, lineTop, lineXLeft + token.width / 2, 'center');
  7789. lineXLeft += token.width;
  7790. leftIndex++;
  7791. }
  7792. lineTop += lineHeight;
  7793. }
  7794. }
  7795. function applyTextRotation(ctx, style, rect, x, y) {
  7796. // textRotation only apply in RectText.
  7797. if (rect && style.textRotation) {
  7798. var origin = style.textOrigin;
  7799. if (origin === 'center') {
  7800. x = rect.width / 2 + rect.x;
  7801. y = rect.height / 2 + rect.y;
  7802. }
  7803. else if (origin) {
  7804. x = origin[0] + rect.x;
  7805. y = origin[1] + rect.y;
  7806. }
  7807. ctx.translate(x, y);
  7808. // Positive: anticlockwise
  7809. ctx.rotate(-style.textRotation);
  7810. ctx.translate(-x, -y);
  7811. }
  7812. }
  7813. function placeToken(hostEl, ctx, token, style, lineHeight, lineTop, x, textAlign) {
  7814. var tokenStyle = style.rich[token.styleName] || {};
  7815. tokenStyle.text = token.text;
  7816. // 'ctx.textBaseline' is always set as 'middle', for sake of
  7817. // the bias of "Microsoft YaHei".
  7818. var textVerticalAlign = token.textVerticalAlign;
  7819. var y = lineTop + lineHeight / 2;
  7820. if (textVerticalAlign === 'top') {
  7821. y = lineTop + token.height / 2;
  7822. }
  7823. else if (textVerticalAlign === 'bottom') {
  7824. y = lineTop + lineHeight - token.height / 2;
  7825. }
  7826. !token.isLineHolder && needDrawBackground(tokenStyle) && drawBackground(
  7827. hostEl,
  7828. ctx,
  7829. tokenStyle,
  7830. textAlign === 'right'
  7831. ? x - token.width
  7832. : textAlign === 'center'
  7833. ? x - token.width / 2
  7834. : x,
  7835. y - token.height / 2,
  7836. token.width,
  7837. token.height
  7838. );
  7839. var textPadding = token.textPadding;
  7840. if (textPadding) {
  7841. x = getTextXForPadding(x, textAlign, textPadding);
  7842. y -= token.height / 2 - textPadding[2] - token.textHeight / 2;
  7843. }
  7844. setCtx(ctx, 'shadowBlur', retrieve3(tokenStyle.textShadowBlur, style.textShadowBlur, 0));
  7845. setCtx(ctx, 'shadowColor', tokenStyle.textShadowColor || style.textShadowColor || 'transparent');
  7846. setCtx(ctx, 'shadowOffsetX', retrieve3(tokenStyle.textShadowOffsetX, style.textShadowOffsetX, 0));
  7847. setCtx(ctx, 'shadowOffsetY', retrieve3(tokenStyle.textShadowOffsetY, style.textShadowOffsetY, 0));
  7848. setCtx(ctx, 'textAlign', textAlign);
  7849. // Force baseline to be "middle". Otherwise, if using "top", the
  7850. // text will offset downward a little bit in font "Microsoft YaHei".
  7851. setCtx(ctx, 'textBaseline', 'middle');
  7852. setCtx(ctx, 'font', token.font || DEFAULT_FONT);
  7853. var textStroke = getStroke(tokenStyle.textStroke || style.textStroke, textStrokeWidth);
  7854. var textFill = getFill(tokenStyle.textFill || style.textFill);
  7855. var textStrokeWidth = retrieve2(tokenStyle.textStrokeWidth, style.textStrokeWidth);
  7856. // Fill after stroke so the outline will not cover the main part.
  7857. if (textStroke) {
  7858. setCtx(ctx, 'lineWidth', textStrokeWidth);
  7859. setCtx(ctx, 'strokeStyle', textStroke);
  7860. ctx.strokeText(token.text, x, y);
  7861. }
  7862. if (textFill) {
  7863. setCtx(ctx, 'fillStyle', textFill);
  7864. ctx.fillText(token.text, x, y);
  7865. }
  7866. }
  7867. function needDrawBackground(style) {
  7868. return !!(
  7869. style.textBackgroundColor
  7870. || (style.textBorderWidth && style.textBorderColor)
  7871. );
  7872. }
  7873. // style: {textBackgroundColor, textBorderWidth, textBorderColor, textBorderRadius, text}
  7874. // shape: {x, y, width, height}
  7875. function drawBackground(hostEl, ctx, style, x, y, width, height) {
  7876. var textBackgroundColor = style.textBackgroundColor;
  7877. var textBorderWidth = style.textBorderWidth;
  7878. var textBorderColor = style.textBorderColor;
  7879. var isPlainBg = isString(textBackgroundColor);
  7880. setCtx(ctx, 'shadowBlur', style.textBoxShadowBlur || 0);
  7881. setCtx(ctx, 'shadowColor', style.textBoxShadowColor || 'transparent');
  7882. setCtx(ctx, 'shadowOffsetX', style.textBoxShadowOffsetX || 0);
  7883. setCtx(ctx, 'shadowOffsetY', style.textBoxShadowOffsetY || 0);
  7884. if (isPlainBg || (textBorderWidth && textBorderColor)) {
  7885. ctx.beginPath();
  7886. var textBorderRadius = style.textBorderRadius;
  7887. if (!textBorderRadius) {
  7888. ctx.rect(x, y, width, height);
  7889. }
  7890. else {
  7891. buildPath(ctx, {
  7892. x: x, y: y, width: width, height: height, r: textBorderRadius
  7893. });
  7894. }
  7895. ctx.closePath();
  7896. }
  7897. if (isPlainBg) {
  7898. setCtx(ctx, 'fillStyle', textBackgroundColor);
  7899. if (style.fillOpacity != null) {
  7900. var originalGlobalAlpha = ctx.globalAlpha;
  7901. ctx.globalAlpha = style.fillOpacity * style.opacity;
  7902. ctx.fill();
  7903. ctx.globalAlpha = originalGlobalAlpha;
  7904. }
  7905. else {
  7906. ctx.fill();
  7907. }
  7908. }
  7909. else if (isObject(textBackgroundColor)) {
  7910. var image = textBackgroundColor.image;
  7911. image = createOrUpdateImage(
  7912. image, null, hostEl, onBgImageLoaded, textBackgroundColor
  7913. );
  7914. if (image && isImageReady(image)) {
  7915. ctx.drawImage(image, x, y, width, height);
  7916. }
  7917. }
  7918. if (textBorderWidth && textBorderColor) {
  7919. setCtx(ctx, 'lineWidth', textBorderWidth);
  7920. setCtx(ctx, 'strokeStyle', textBorderColor);
  7921. if (style.strokeOpacity != null) {
  7922. var originalGlobalAlpha = ctx.globalAlpha;
  7923. ctx.globalAlpha = style.strokeOpacity * style.opacity;
  7924. ctx.stroke();
  7925. ctx.globalAlpha = originalGlobalAlpha;
  7926. }
  7927. else {
  7928. ctx.stroke();
  7929. }
  7930. }
  7931. }
  7932. function onBgImageLoaded(image, textBackgroundColor) {
  7933. // Replace image, so that `contain/text.js#parseRichText`
  7934. // will get correct result in next tick.
  7935. textBackgroundColor.image = image;
  7936. }
  7937. function getBoxPosition(out, hostEl, style, rect) {
  7938. var baseX = style.x || 0;
  7939. var baseY = style.y || 0;
  7940. var textAlign = style.textAlign;
  7941. var textVerticalAlign = style.textVerticalAlign;
  7942. // Text position represented by coord
  7943. if (rect) {
  7944. var textPosition = style.textPosition;
  7945. if (textPosition instanceof Array) {
  7946. // Percent
  7947. baseX = rect.x + parsePercent(textPosition[0], rect.width);
  7948. baseY = rect.y + parsePercent(textPosition[1], rect.height);
  7949. }
  7950. else {
  7951. var res = (hostEl && hostEl.calculateTextPosition)
  7952. ? hostEl.calculateTextPosition(_tmpTextPositionResult, style, rect)
  7953. : calculateTextPosition(_tmpTextPositionResult, style, rect);
  7954. baseX = res.x;
  7955. baseY = res.y;
  7956. // Default align and baseline when has textPosition
  7957. textAlign = textAlign || res.textAlign;
  7958. textVerticalAlign = textVerticalAlign || res.textVerticalAlign;
  7959. }
  7960. // textOffset is only support in RectText, otherwise
  7961. // we have to adjust boundingRect for textOffset.
  7962. var textOffset = style.textOffset;
  7963. if (textOffset) {
  7964. baseX += textOffset[0];
  7965. baseY += textOffset[1];
  7966. }
  7967. }
  7968. out = out || {};
  7969. out.baseX = baseX;
  7970. out.baseY = baseY;
  7971. out.textAlign = textAlign;
  7972. out.textVerticalAlign = textVerticalAlign;
  7973. return out;
  7974. }
  7975. function setCtx(ctx, prop, value) {
  7976. ctx[prop] = fixShadow(ctx, prop, value);
  7977. return ctx[prop];
  7978. }
  7979. /**
  7980. * @param {string} [stroke] If specified, do not check style.textStroke.
  7981. * @param {string} [lineWidth] If specified, do not check style.textStroke.
  7982. * @param {number} style
  7983. */
  7984. function getStroke(stroke, lineWidth) {
  7985. return (stroke == null || lineWidth <= 0 || stroke === 'transparent' || stroke === 'none')
  7986. ? null
  7987. // TODO pattern and gradient?
  7988. : (stroke.image || stroke.colorStops)
  7989. ? '#000'
  7990. : stroke;
  7991. }
  7992. function getFill(fill) {
  7993. return (fill == null || fill === 'none')
  7994. ? null
  7995. // TODO pattern and gradient?
  7996. : (fill.image || fill.colorStops)
  7997. ? '#000'
  7998. : fill;
  7999. }
  8000. function parsePercent(value, maxValue) {
  8001. if (typeof value === 'string') {
  8002. if (value.lastIndexOf('%') >= 0) {
  8003. return parseFloat(value) / 100 * maxValue;
  8004. }
  8005. return parseFloat(value);
  8006. }
  8007. return value;
  8008. }
  8009. function getTextXForPadding(x, textAlign, textPadding) {
  8010. return textAlign === 'right'
  8011. ? (x - textPadding[1])
  8012. : textAlign === 'center'
  8013. ? (x + textPadding[3] / 2 - textPadding[1] / 2)
  8014. : (x + textPadding[3]);
  8015. }
  8016. /**
  8017. * @param {string} text
  8018. * @param {module:zrender/Style} style
  8019. * @return {boolean}
  8020. */
  8021. function needDrawText(text, style) {
  8022. return text != null
  8023. && (text
  8024. || style.textBackgroundColor
  8025. || (style.textBorderWidth && style.textBorderColor)
  8026. || style.textPadding
  8027. );
  8028. }
  8029. /**
  8030. * Mixin for drawing text in a element bounding rect
  8031. * @module zrender/mixin/RectText
  8032. */
  8033. var tmpRect$1 = new BoundingRect();
  8034. var RectText = function () {};
  8035. RectText.prototype = {
  8036. constructor: RectText,
  8037. /**
  8038. * Draw text in a rect with specified position.
  8039. * @param {CanvasRenderingContext2D} ctx
  8040. * @param {Object} rect Displayable rect
  8041. */
  8042. drawRectText: function (ctx, rect) {
  8043. var style = this.style;
  8044. rect = style.textRect || rect;
  8045. // Optimize, avoid normalize every time.
  8046. this.__dirty && normalizeTextStyle(style, true);
  8047. var text = style.text;
  8048. // Convert to string
  8049. text != null && (text += '');
  8050. if (!needDrawText(text, style)) {
  8051. return;
  8052. }
  8053. // FIXME
  8054. // Do not provide prevEl to `textHelper.renderText` for ctx prop cache,
  8055. // but use `ctx.save()` and `ctx.restore()`. Because the cache for rect
  8056. // text propably break the cache for its host elements.
  8057. ctx.save();
  8058. // Transform rect to view space
  8059. var transform = this.transform;
  8060. if (!style.transformText) {
  8061. if (transform) {
  8062. tmpRect$1.copy(rect);
  8063. tmpRect$1.applyTransform(transform);
  8064. rect = tmpRect$1;
  8065. }
  8066. }
  8067. else {
  8068. this.setTransform(ctx);
  8069. }
  8070. // transformText and textRotation can not be used at the same time.
  8071. renderText(this, ctx, text, style, rect, WILL_BE_RESTORED);
  8072. ctx.restore();
  8073. }
  8074. };
  8075. /**
  8076. * Base class of all displayable graphic objects
  8077. * @module zrender/graphic/Displayable
  8078. */
  8079. /**
  8080. * @alias module:zrender/graphic/Displayable
  8081. * @extends module:zrender/Element
  8082. * @extends module:zrender/graphic/mixin/RectText
  8083. */
  8084. function Displayable(opts) {
  8085. opts = opts || {};
  8086. Element.call(this, opts);
  8087. // Extend properties
  8088. for (var name in opts) {
  8089. if (
  8090. opts.hasOwnProperty(name)
  8091. && name !== 'style'
  8092. ) {
  8093. this[name] = opts[name];
  8094. }
  8095. }
  8096. /**
  8097. * @type {module:zrender/graphic/Style}
  8098. */
  8099. this.style = new Style(opts.style, this);
  8100. this._rect = null;
  8101. // Shapes for cascade clipping.
  8102. // Can only be `null`/`undefined` or an non-empty array, MUST NOT be an empty array.
  8103. // because it is easy to only using null to check whether clipPaths changed.
  8104. this.__clipPaths = null;
  8105. // FIXME Stateful must be mixined after style is setted
  8106. // Stateful.call(this, opts);
  8107. }
  8108. Displayable.prototype = {
  8109. constructor: Displayable,
  8110. type: 'displayable',
  8111. /**
  8112. * Dirty flag. From which painter will determine if this displayable object needs brush.
  8113. * @name module:zrender/graphic/Displayable#__dirty
  8114. * @type {boolean}
  8115. */
  8116. __dirty: true,
  8117. /**
  8118. * Whether the displayable object is visible. when it is true, the displayable object
  8119. * is not drawn, but the mouse event can still trigger the object.
  8120. * @name module:/zrender/graphic/Displayable#invisible
  8121. * @type {boolean}
  8122. * @default false
  8123. */
  8124. invisible: false,
  8125. /**
  8126. * @name module:/zrender/graphic/Displayable#z
  8127. * @type {number}
  8128. * @default 0
  8129. */
  8130. z: 0,
  8131. /**
  8132. * @name module:/zrender/graphic/Displayable#z
  8133. * @type {number}
  8134. * @default 0
  8135. */
  8136. z2: 0,
  8137. /**
  8138. * The z level determines the displayable object can be drawn in which layer canvas.
  8139. * @name module:/zrender/graphic/Displayable#zlevel
  8140. * @type {number}
  8141. * @default 0
  8142. */
  8143. zlevel: 0,
  8144. /**
  8145. * Whether it can be dragged.
  8146. * @name module:/zrender/graphic/Displayable#draggable
  8147. * @type {boolean}
  8148. * @default false
  8149. */
  8150. draggable: false,
  8151. /**
  8152. * Whether is it dragging.
  8153. * @name module:/zrender/graphic/Displayable#draggable
  8154. * @type {boolean}
  8155. * @default false
  8156. */
  8157. dragging: false,
  8158. /**
  8159. * Whether to respond to mouse events.
  8160. * @name module:/zrender/graphic/Displayable#silent
  8161. * @type {boolean}
  8162. * @default false
  8163. */
  8164. silent: false,
  8165. /**
  8166. * If enable culling
  8167. * @type {boolean}
  8168. * @default false
  8169. */
  8170. culling: false,
  8171. /**
  8172. * Mouse cursor when hovered
  8173. * @name module:/zrender/graphic/Displayable#cursor
  8174. * @type {string}
  8175. */
  8176. cursor: 'pointer',
  8177. /**
  8178. * If hover area is bounding rect
  8179. * @name module:/zrender/graphic/Displayable#rectHover
  8180. * @type {string}
  8181. */
  8182. rectHover: false,
  8183. /**
  8184. * Render the element progressively when the value >= 0,
  8185. * usefull for large data.
  8186. * @type {boolean}
  8187. */
  8188. progressive: false,
  8189. /**
  8190. * @type {boolean}
  8191. */
  8192. incremental: false,
  8193. /**
  8194. * Scale ratio for global scale.
  8195. * @type {boolean}
  8196. */
  8197. globalScaleRatio: 1,
  8198. beforeBrush: function (ctx) {},
  8199. afterBrush: function (ctx) {},
  8200. /**
  8201. * Graphic drawing method.
  8202. * @param {CanvasRenderingContext2D} ctx
  8203. */
  8204. // Interface
  8205. brush: function (ctx, prevEl) {},
  8206. /**
  8207. * Get the minimum bounding box.
  8208. * @return {module:zrender/core/BoundingRect}
  8209. */
  8210. // Interface
  8211. getBoundingRect: function () {},
  8212. /**
  8213. * If displayable element contain coord x, y
  8214. * @param {number} x
  8215. * @param {number} y
  8216. * @return {boolean}
  8217. */
  8218. contain: function (x, y) {
  8219. return this.rectContain(x, y);
  8220. },
  8221. /**
  8222. * @param {Function} cb
  8223. * @param {} context
  8224. */
  8225. traverse: function (cb, context) {
  8226. cb.call(context, this);
  8227. },
  8228. /**
  8229. * If bounding rect of element contain coord x, y
  8230. * @param {number} x
  8231. * @param {number} y
  8232. * @return {boolean}
  8233. */
  8234. rectContain: function (x, y) {
  8235. var coord = this.transformCoordToLocal(x, y);
  8236. var rect = this.getBoundingRect();
  8237. return rect.contain(coord[0], coord[1]);
  8238. },
  8239. /**
  8240. * Mark displayable element dirty and refresh next frame
  8241. */
  8242. dirty: function () {
  8243. this.__dirty = this.__dirtyText = true;
  8244. this._rect = null;
  8245. this.__zr && this.__zr.refresh();
  8246. },
  8247. /**
  8248. * If displayable object binded any event
  8249. * @return {boolean}
  8250. */
  8251. // TODO, events bound by bind
  8252. // isSilent: function () {
  8253. // return !(
  8254. // this.hoverable || this.draggable
  8255. // || this.onmousemove || this.onmouseover || this.onmouseout
  8256. // || this.onmousedown || this.onmouseup || this.onclick
  8257. // || this.ondragenter || this.ondragover || this.ondragleave
  8258. // || this.ondrop
  8259. // );
  8260. // },
  8261. /**
  8262. * Alias for animate('style')
  8263. * @param {boolean} loop
  8264. */
  8265. animateStyle: function (loop) {
  8266. return this.animate('style', loop);
  8267. },
  8268. attrKV: function (key, value) {
  8269. if (key !== 'style') {
  8270. Element.prototype.attrKV.call(this, key, value);
  8271. }
  8272. else {
  8273. this.style.set(value);
  8274. }
  8275. },
  8276. /**
  8277. * @param {Object|string} key
  8278. * @param {*} value
  8279. */
  8280. setStyle: function (key, value) {
  8281. this.style.set(key, value);
  8282. this.dirty(false);
  8283. return this;
  8284. },
  8285. /**
  8286. * Use given style object
  8287. * @param {Object} obj
  8288. */
  8289. useStyle: function (obj) {
  8290. this.style = new Style(obj, this);
  8291. this.dirty(false);
  8292. return this;
  8293. },
  8294. /**
  8295. * The string value of `textPosition` needs to be calculated to a real postion.
  8296. * For example, `'inside'` is calculated to `[rect.width/2, rect.height/2]`
  8297. * by default. See `contain/text.js#calculateTextPosition` for more details.
  8298. * But some coutom shapes like "pin", "flag" have center that is not exactly
  8299. * `[width/2, height/2]`. So we provide this hook to customize the calculation
  8300. * for those shapes. It will be called if the `style.textPosition` is a string.
  8301. * @param {Obejct} [out] Prepared out object. If not provided, this method should
  8302. * be responsible for creating one.
  8303. * @param {module:zrender/graphic/Style} style
  8304. * @param {Object} rect {x, y, width, height}
  8305. * @return {Obejct} out The same as the input out.
  8306. * {
  8307. * x: number. mandatory.
  8308. * y: number. mandatory.
  8309. * textAlign: string. optional. use style.textAlign by default.
  8310. * textVerticalAlign: string. optional. use style.textVerticalAlign by default.
  8311. * }
  8312. */
  8313. calculateTextPosition: null
  8314. };
  8315. inherits(Displayable, Element);
  8316. mixin(Displayable, RectText);
  8317. /**
  8318. * @alias zrender/graphic/Image
  8319. * @extends module:zrender/graphic/Displayable
  8320. * @constructor
  8321. * @param {Object} opts
  8322. */
  8323. function ZImage(opts) {
  8324. Displayable.call(this, opts);
  8325. }
  8326. ZImage.prototype = {
  8327. constructor: ZImage,
  8328. type: 'image',
  8329. brush: function (ctx, prevEl) {
  8330. var style = this.style;
  8331. var src = style.image;
  8332. // Must bind each time
  8333. style.bind(ctx, this, prevEl);
  8334. var image = this._image = createOrUpdateImage(
  8335. src,
  8336. this._image,
  8337. this,
  8338. this.onload
  8339. );
  8340. if (!image || !isImageReady(image)) {
  8341. return;
  8342. }
  8343. // 图片已经加载完成
  8344. // if (image.nodeName.toUpperCase() == 'IMG') {
  8345. // if (!image.complete) {
  8346. // return;
  8347. // }
  8348. // }
  8349. // Else is canvas
  8350. var x = style.x || 0;
  8351. var y = style.y || 0;
  8352. var width = style.width;
  8353. var height = style.height;
  8354. var aspect = image.width / image.height;
  8355. if (width == null && height != null) {
  8356. // Keep image/height ratio
  8357. width = height * aspect;
  8358. }
  8359. else if (height == null && width != null) {
  8360. height = width / aspect;
  8361. }
  8362. else if (width == null && height == null) {
  8363. width = image.width;
  8364. height = image.height;
  8365. }
  8366. // 设置transform
  8367. this.setTransform(ctx);
  8368. if (style.sWidth && style.sHeight) {
  8369. var sx = style.sx || 0;
  8370. var sy = style.sy || 0;
  8371. ctx.drawImage(
  8372. image,
  8373. sx, sy, style.sWidth, style.sHeight,
  8374. x, y, width, height
  8375. );
  8376. }
  8377. else if (style.sx && style.sy) {
  8378. var sx = style.sx;
  8379. var sy = style.sy;
  8380. var sWidth = width - sx;
  8381. var sHeight = height - sy;
  8382. ctx.drawImage(
  8383. image,
  8384. sx, sy, sWidth, sHeight,
  8385. x, y, width, height
  8386. );
  8387. }
  8388. else {
  8389. ctx.drawImage(image, x, y, width, height);
  8390. }
  8391. // Draw rect text
  8392. if (style.text != null) {
  8393. // Only restore transform when needs draw text.
  8394. this.restoreTransform(ctx);
  8395. this.drawRectText(ctx, this.getBoundingRect());
  8396. }
  8397. },
  8398. getBoundingRect: function () {
  8399. var style = this.style;
  8400. if (!this._rect) {
  8401. this._rect = new BoundingRect(
  8402. style.x || 0, style.y || 0, style.width || 0, style.height || 0
  8403. );
  8404. }
  8405. return this._rect;
  8406. }
  8407. };
  8408. inherits(ZImage, Displayable);
  8409. var HOVER_LAYER_ZLEVEL = 1e5;
  8410. var CANVAS_ZLEVEL = 314159;
  8411. var EL_AFTER_INCREMENTAL_INC = 0.01;
  8412. var INCREMENTAL_INC = 0.001;
  8413. function parseInt10(val) {
  8414. return parseInt(val, 10);
  8415. }
  8416. function isLayerValid(layer) {
  8417. if (!layer) {
  8418. return false;
  8419. }
  8420. if (layer.__builtin__) {
  8421. return true;
  8422. }
  8423. if (typeof (layer.resize) !== 'function'
  8424. || typeof (layer.refresh) !== 'function'
  8425. ) {
  8426. return false;
  8427. }
  8428. return true;
  8429. }
  8430. var tmpRect = new BoundingRect(0, 0, 0, 0);
  8431. var viewRect = new BoundingRect(0, 0, 0, 0);
  8432. function isDisplayableCulled(el, width, height) {
  8433. tmpRect.copy(el.getBoundingRect());
  8434. if (el.transform) {
  8435. tmpRect.applyTransform(el.transform);
  8436. }
  8437. viewRect.width = width;
  8438. viewRect.height = height;
  8439. return !tmpRect.intersect(viewRect);
  8440. }
  8441. function isClipPathChanged(clipPaths, prevClipPaths) {
  8442. // displayable.__clipPaths can only be `null`/`undefined` or an non-empty array.
  8443. if (clipPaths === prevClipPaths) {
  8444. return false;
  8445. }
  8446. if (!clipPaths || !prevClipPaths || (clipPaths.length !== prevClipPaths.length)) {
  8447. return true;
  8448. }
  8449. for (var i = 0; i < clipPaths.length; i++) {
  8450. if (clipPaths[i] !== prevClipPaths[i]) {
  8451. return true;
  8452. }
  8453. }
  8454. return false;
  8455. }
  8456. function doClip(clipPaths, ctx) {
  8457. for (var i = 0; i < clipPaths.length; i++) {
  8458. var clipPath = clipPaths[i];
  8459. clipPath.setTransform(ctx);
  8460. ctx.beginPath();
  8461. clipPath.buildPath(ctx, clipPath.shape);
  8462. ctx.clip();
  8463. // Transform back
  8464. clipPath.restoreTransform(ctx);
  8465. }
  8466. }
  8467. function createRoot(width, height) {
  8468. var domRoot = document.createElement('div');
  8469. // domRoot.onselectstart = returnFalse; // Avoid page selected
  8470. domRoot.style.cssText = [
  8471. 'position:relative',
  8472. // IOS13 safari probably has a compositing bug (z order of the canvas and the consequent
  8473. // dom does not act as expected) when some of the parent dom has
  8474. // `-webkit-overflow-scrolling: touch;` and the webpage is longer than one screen and
  8475. // the canvas is not at the top part of the page.
  8476. // Check `https://bugs.webkit.org/show_bug.cgi?id=203681` for more details. We remove
  8477. // this `overflow:hidden` to avoid the bug.
  8478. // 'overflow:hidden',
  8479. 'width:' + width + 'px',
  8480. 'height:' + height + 'px',
  8481. 'padding:0',
  8482. 'margin:0',
  8483. 'border-width:0'
  8484. ].join(';') + ';';
  8485. return domRoot;
  8486. }
  8487. /**
  8488. * @alias module:zrender/Painter
  8489. * @constructor
  8490. * @param {HTMLElement} root 绘图容器
  8491. * @param {module:zrender/Storage} storage
  8492. * @param {Object} opts
  8493. */
  8494. var Painter = function (root, storage, opts) {
  8495. this.type = 'canvas';
  8496. // In node environment using node-canvas
  8497. var singleCanvas = !root.nodeName // In node ?
  8498. || root.nodeName.toUpperCase() === 'CANVAS';
  8499. this._opts = opts = extend({}, opts || {});
  8500. /**
  8501. * @type {number}
  8502. */
  8503. this.dpr = opts.devicePixelRatio || devicePixelRatio;
  8504. /**
  8505. * @type {boolean}
  8506. * @private
  8507. */
  8508. this._singleCanvas = singleCanvas;
  8509. /**
  8510. * 绘图容器
  8511. * @type {HTMLElement}
  8512. */
  8513. this.root = root;
  8514. var rootStyle = root.style;
  8515. if (rootStyle) {
  8516. rootStyle['-webkit-tap-highlight-color'] = 'transparent';
  8517. rootStyle['-webkit-user-select'] =
  8518. rootStyle['user-select'] =
  8519. rootStyle['-webkit-touch-callout'] = 'none';
  8520. root.innerHTML = '';
  8521. }
  8522. /**
  8523. * @type {module:zrender/Storage}
  8524. */
  8525. this.storage = storage;
  8526. /**
  8527. * @type {Array.<number>}
  8528. * @private
  8529. */
  8530. var zlevelList = this._zlevelList = [];
  8531. /**
  8532. * @type {Object.<string, module:zrender/Layer>}
  8533. * @private
  8534. */
  8535. var layers = this._layers = {};
  8536. /**
  8537. * @type {Object.<string, Object>}
  8538. * @private
  8539. */
  8540. this._layerConfig = {};
  8541. /**
  8542. * zrender will do compositing when root is a canvas and have multiple zlevels.
  8543. */
  8544. this._needsManuallyCompositing = false;
  8545. if (!singleCanvas) {
  8546. this._width = this._getSize(0);
  8547. this._height = this._getSize(1);
  8548. var domRoot = this._domRoot = createRoot(
  8549. this._width, this._height
  8550. );
  8551. root.appendChild(domRoot);
  8552. }
  8553. else {
  8554. var width = root.width;
  8555. var height = root.height;
  8556. if (opts.width != null) {
  8557. width = opts.width;
  8558. }
  8559. if (opts.height != null) {
  8560. height = opts.height;
  8561. }
  8562. this.dpr = opts.devicePixelRatio || 1;
  8563. // Use canvas width and height directly
  8564. root.width = width * this.dpr;
  8565. root.height = height * this.dpr;
  8566. this._width = width;
  8567. this._height = height;
  8568. // Create layer if only one given canvas
  8569. // Device can be specified to create a high dpi image.
  8570. var mainLayer = new Layer(root, this, this.dpr);
  8571. mainLayer.__builtin__ = true;
  8572. mainLayer.initContext();
  8573. // FIXME Use canvas width and height
  8574. // mainLayer.resize(width, height);
  8575. layers[CANVAS_ZLEVEL] = mainLayer;
  8576. mainLayer.zlevel = CANVAS_ZLEVEL;
  8577. // Not use common zlevel.
  8578. zlevelList.push(CANVAS_ZLEVEL);
  8579. this._domRoot = root;
  8580. }
  8581. /**
  8582. * @type {module:zrender/Layer}
  8583. * @private
  8584. */
  8585. this._hoverlayer = null;
  8586. this._hoverElements = [];
  8587. };
  8588. Painter.prototype = {
  8589. constructor: Painter,
  8590. getType: function () {
  8591. return 'canvas';
  8592. },
  8593. /**
  8594. * If painter use a single canvas
  8595. * @return {boolean}
  8596. */
  8597. isSingleCanvas: function () {
  8598. return this._singleCanvas;
  8599. },
  8600. /**
  8601. * @return {HTMLDivElement}
  8602. */
  8603. getViewportRoot: function () {
  8604. return this._domRoot;
  8605. },
  8606. getViewportRootOffset: function () {
  8607. var viewportRoot = this.getViewportRoot();
  8608. if (viewportRoot) {
  8609. return {
  8610. offsetLeft: viewportRoot.offsetLeft || 0,
  8611. offsetTop: viewportRoot.offsetTop || 0
  8612. };
  8613. }
  8614. },
  8615. /**
  8616. * 刷新
  8617. * @param {boolean} [paintAll=false] 强制绘制所有displayable
  8618. */
  8619. refresh: function (paintAll) {
  8620. var list = this.storage.getDisplayList(true);
  8621. var zlevelList = this._zlevelList;
  8622. this._redrawId = Math.random();
  8623. this._paintList(list, paintAll, this._redrawId);
  8624. // Paint custum layers
  8625. for (var i = 0; i < zlevelList.length; i++) {
  8626. var z = zlevelList[i];
  8627. var layer = this._layers[z];
  8628. if (!layer.__builtin__ && layer.refresh) {
  8629. var clearColor = i === 0 ? this._backgroundColor : null;
  8630. layer.refresh(clearColor);
  8631. }
  8632. }
  8633. this.refreshHover();
  8634. return this;
  8635. },
  8636. addHover: function (el, hoverStyle) {
  8637. if (el.__hoverMir) {
  8638. return;
  8639. }
  8640. var elMirror = new el.constructor({
  8641. style: el.style,
  8642. shape: el.shape,
  8643. z: el.z,
  8644. z2: el.z2,
  8645. silent: el.silent
  8646. });
  8647. elMirror.__from = el;
  8648. el.__hoverMir = elMirror;
  8649. hoverStyle && elMirror.setStyle(hoverStyle);
  8650. this._hoverElements.push(elMirror);
  8651. return elMirror;
  8652. },
  8653. removeHover: function (el) {
  8654. var elMirror = el.__hoverMir;
  8655. var hoverElements = this._hoverElements;
  8656. var idx = indexOf(hoverElements, elMirror);
  8657. if (idx >= 0) {
  8658. hoverElements.splice(idx, 1);
  8659. }
  8660. el.__hoverMir = null;
  8661. },
  8662. clearHover: function (el) {
  8663. var hoverElements = this._hoverElements;
  8664. for (var i = 0; i < hoverElements.length; i++) {
  8665. var from = hoverElements[i].__from;
  8666. if (from) {
  8667. from.__hoverMir = null;
  8668. }
  8669. }
  8670. hoverElements.length = 0;
  8671. },
  8672. refreshHover: function () {
  8673. var hoverElements = this._hoverElements;
  8674. var len = hoverElements.length;
  8675. var hoverLayer = this._hoverlayer;
  8676. hoverLayer && hoverLayer.clear();
  8677. if (!len) {
  8678. return;
  8679. }
  8680. sort(hoverElements, this.storage.displayableSortFunc);
  8681. // Use a extream large zlevel
  8682. // FIXME?
  8683. if (!hoverLayer) {
  8684. hoverLayer = this._hoverlayer = this.getLayer(HOVER_LAYER_ZLEVEL);
  8685. }
  8686. var scope = {};
  8687. hoverLayer.ctx.save();
  8688. for (var i = 0; i < len;) {
  8689. var el = hoverElements[i];
  8690. var originalEl = el.__from;
  8691. // Original el is removed
  8692. // PENDING
  8693. if (!(originalEl && originalEl.__zr)) {
  8694. hoverElements.splice(i, 1);
  8695. originalEl.__hoverMir = null;
  8696. len--;
  8697. continue;
  8698. }
  8699. i++;
  8700. // Use transform
  8701. // FIXME style and shape ?
  8702. if (!originalEl.invisible) {
  8703. el.transform = originalEl.transform;
  8704. el.invTransform = originalEl.invTransform;
  8705. el.__clipPaths = originalEl.__clipPaths;
  8706. // el.
  8707. this._doPaintEl(el, hoverLayer, true, scope);
  8708. }
  8709. }
  8710. hoverLayer.ctx.restore();
  8711. },
  8712. getHoverLayer: function () {
  8713. return this.getLayer(HOVER_LAYER_ZLEVEL);
  8714. },
  8715. _paintList: function (list, paintAll, redrawId) {
  8716. if (this._redrawId !== redrawId) {
  8717. return;
  8718. }
  8719. paintAll = paintAll || false;
  8720. this._updateLayerStatus(list);
  8721. var finished = this._doPaintList(list, paintAll);
  8722. if (this._needsManuallyCompositing) {
  8723. this._compositeManually();
  8724. }
  8725. if (!finished) {
  8726. var self = this;
  8727. requestAnimationFrame(function () {
  8728. self._paintList(list, paintAll, redrawId);
  8729. });
  8730. }
  8731. },
  8732. _compositeManually: function () {
  8733. var ctx = this.getLayer(CANVAS_ZLEVEL).ctx;
  8734. var width = this._domRoot.width;
  8735. var height = this._domRoot.height;
  8736. ctx.clearRect(0, 0, width, height);
  8737. // PENDING, If only builtin layer?
  8738. this.eachBuiltinLayer(function (layer) {
  8739. if (layer.virtual) {
  8740. ctx.drawImage(layer.dom, 0, 0, width, height);
  8741. }
  8742. });
  8743. },
  8744. _doPaintList: function (list, paintAll) {
  8745. var layerList = [];
  8746. for (var zi = 0; zi < this._zlevelList.length; zi++) {
  8747. var zlevel = this._zlevelList[zi];
  8748. var layer = this._layers[zlevel];
  8749. if (layer.__builtin__
  8750. && layer !== this._hoverlayer
  8751. && (layer.__dirty || paintAll)
  8752. ) {
  8753. layerList.push(layer);
  8754. }
  8755. }
  8756. var finished = true;
  8757. for (var k = 0; k < layerList.length; k++) {
  8758. var layer = layerList[k];
  8759. var ctx = layer.ctx;
  8760. var scope = {};
  8761. ctx.save();
  8762. var start = paintAll ? layer.__startIndex : layer.__drawIndex;
  8763. var useTimer = !paintAll && layer.incremental && Date.now;
  8764. var startTime = useTimer && Date.now();
  8765. var clearColor = layer.zlevel === this._zlevelList[0]
  8766. ? this._backgroundColor : null;
  8767. // All elements in this layer are cleared.
  8768. if (layer.__startIndex === layer.__endIndex) {
  8769. layer.clear(false, clearColor);
  8770. }
  8771. else if (start === layer.__startIndex) {
  8772. var firstEl = list[start];
  8773. if (!firstEl.incremental || !firstEl.notClear || paintAll) {
  8774. layer.clear(false, clearColor);
  8775. }
  8776. }
  8777. if (start === -1) {
  8778. console.error('For some unknown reason. drawIndex is -1');
  8779. start = layer.__startIndex;
  8780. }
  8781. for (var i = start; i < layer.__endIndex; i++) {
  8782. var el = list[i];
  8783. this._doPaintEl(el, layer, paintAll, scope);
  8784. el.__dirty = el.__dirtyText = false;
  8785. if (useTimer) {
  8786. // Date.now can be executed in 13,025,305 ops/second.
  8787. var dTime = Date.now() - startTime;
  8788. // Give 15 millisecond to draw.
  8789. // The rest elements will be drawn in the next frame.
  8790. if (dTime > 15) {
  8791. break;
  8792. }
  8793. }
  8794. }
  8795. layer.__drawIndex = i;
  8796. if (layer.__drawIndex < layer.__endIndex) {
  8797. finished = false;
  8798. }
  8799. if (scope.prevElClipPaths) {
  8800. // Needs restore the state. If last drawn element is in the clipping area.
  8801. ctx.restore();
  8802. }
  8803. ctx.restore();
  8804. }
  8805. if (env$1.wxa) {
  8806. // Flush for weixin application
  8807. each(this._layers, function (layer) {
  8808. if (layer && layer.ctx && layer.ctx.draw) {
  8809. layer.ctx.draw();
  8810. }
  8811. });
  8812. }
  8813. return finished;
  8814. },
  8815. _doPaintEl: function (el, currentLayer, forcePaint, scope) {
  8816. var ctx = currentLayer.ctx;
  8817. var m = el.transform;
  8818. if (
  8819. (currentLayer.__dirty || forcePaint)
  8820. // Ignore invisible element
  8821. && !el.invisible
  8822. // Ignore transparent element
  8823. && el.style.opacity !== 0
  8824. // Ignore scale 0 element, in some environment like node-canvas
  8825. // Draw a scale 0 element can cause all following draw wrong
  8826. // And setTransform with scale 0 will cause set back transform failed.
  8827. && !(m && !m[0] && !m[3])
  8828. // Ignore culled element
  8829. && !(el.culling && isDisplayableCulled(el, this._width, this._height))
  8830. ) {
  8831. var clipPaths = el.__clipPaths;
  8832. var prevElClipPaths = scope.prevElClipPaths;
  8833. // Optimize when clipping on group with several elements
  8834. if (!prevElClipPaths || isClipPathChanged(clipPaths, prevElClipPaths)) {
  8835. // If has previous clipping state, restore from it
  8836. if (prevElClipPaths) {
  8837. ctx.restore();
  8838. scope.prevElClipPaths = null;
  8839. // Reset prevEl since context has been restored
  8840. scope.prevEl = null;
  8841. }
  8842. // New clipping state
  8843. if (clipPaths) {
  8844. ctx.save();
  8845. doClip(clipPaths, ctx);
  8846. scope.prevElClipPaths = clipPaths;
  8847. }
  8848. }
  8849. el.beforeBrush && el.beforeBrush(ctx);
  8850. el.brush(ctx, scope.prevEl || null);
  8851. scope.prevEl = el;
  8852. el.afterBrush && el.afterBrush(ctx);
  8853. }
  8854. },
  8855. /**
  8856. * 获取 zlevel 所在层,如果不存在则会创建一个新的层
  8857. * @param {number} zlevel
  8858. * @param {boolean} virtual Virtual layer will not be inserted into dom.
  8859. * @return {module:zrender/Layer}
  8860. */
  8861. getLayer: function (zlevel, virtual) {
  8862. if (this._singleCanvas && !this._needsManuallyCompositing) {
  8863. zlevel = CANVAS_ZLEVEL;
  8864. }
  8865. var layer = this._layers[zlevel];
  8866. if (!layer) {
  8867. // Create a new layer
  8868. layer = new Layer('zr_' + zlevel, this, this.dpr);
  8869. layer.zlevel = zlevel;
  8870. layer.__builtin__ = true;
  8871. if (this._layerConfig[zlevel]) {
  8872. merge(layer, this._layerConfig[zlevel], true);
  8873. }
  8874. // TODO Remove EL_AFTER_INCREMENTAL_INC magic number
  8875. else if (this._layerConfig[zlevel - EL_AFTER_INCREMENTAL_INC]) {
  8876. merge(layer, this._layerConfig[zlevel - EL_AFTER_INCREMENTAL_INC], true);
  8877. }
  8878. if (virtual) {
  8879. layer.virtual = virtual;
  8880. }
  8881. this.insertLayer(zlevel, layer);
  8882. // Context is created after dom inserted to document
  8883. // Or excanvas will get 0px clientWidth and clientHeight
  8884. layer.initContext();
  8885. }
  8886. return layer;
  8887. },
  8888. insertLayer: function (zlevel, layer) {
  8889. var layersMap = this._layers;
  8890. var zlevelList = this._zlevelList;
  8891. var len = zlevelList.length;
  8892. var prevLayer = null;
  8893. var i = -1;
  8894. var domRoot = this._domRoot;
  8895. if (layersMap[zlevel]) {
  8896. logError$1('ZLevel ' + zlevel + ' has been used already');
  8897. return;
  8898. }
  8899. // Check if is a valid layer
  8900. if (!isLayerValid(layer)) {
  8901. logError$1('Layer of zlevel ' + zlevel + ' is not valid');
  8902. return;
  8903. }
  8904. if (len > 0 && zlevel > zlevelList[0]) {
  8905. for (i = 0; i < len - 1; i++) {
  8906. if (
  8907. zlevelList[i] < zlevel
  8908. && zlevelList[i + 1] > zlevel
  8909. ) {
  8910. break;
  8911. }
  8912. }
  8913. prevLayer = layersMap[zlevelList[i]];
  8914. }
  8915. zlevelList.splice(i + 1, 0, zlevel);
  8916. layersMap[zlevel] = layer;
  8917. // Vitual layer will not directly show on the screen.
  8918. // (It can be a WebGL layer and assigned to a ZImage element)
  8919. // But it still under management of zrender.
  8920. if (!layer.virtual) {
  8921. if (prevLayer) {
  8922. var prevDom = prevLayer.dom;
  8923. if (prevDom.nextSibling) {
  8924. domRoot.insertBefore(
  8925. layer.dom,
  8926. prevDom.nextSibling
  8927. );
  8928. }
  8929. else {
  8930. domRoot.appendChild(layer.dom);
  8931. }
  8932. }
  8933. else {
  8934. if (domRoot.firstChild) {
  8935. domRoot.insertBefore(layer.dom, domRoot.firstChild);
  8936. }
  8937. else {
  8938. domRoot.appendChild(layer.dom);
  8939. }
  8940. }
  8941. }
  8942. },
  8943. // Iterate each layer
  8944. eachLayer: function (cb, context) {
  8945. var zlevelList = this._zlevelList;
  8946. var z;
  8947. var i;
  8948. for (i = 0; i < zlevelList.length; i++) {
  8949. z = zlevelList[i];
  8950. cb.call(context, this._layers[z], z);
  8951. }
  8952. },
  8953. // Iterate each buildin layer
  8954. eachBuiltinLayer: function (cb, context) {
  8955. var zlevelList = this._zlevelList;
  8956. var layer;
  8957. var z;
  8958. var i;
  8959. for (i = 0; i < zlevelList.length; i++) {
  8960. z = zlevelList[i];
  8961. layer = this._layers[z];
  8962. if (layer.__builtin__) {
  8963. cb.call(context, layer, z);
  8964. }
  8965. }
  8966. },
  8967. // Iterate each other layer except buildin layer
  8968. eachOtherLayer: function (cb, context) {
  8969. var zlevelList = this._zlevelList;
  8970. var layer;
  8971. var z;
  8972. var i;
  8973. for (i = 0; i < zlevelList.length; i++) {
  8974. z = zlevelList[i];
  8975. layer = this._layers[z];
  8976. if (!layer.__builtin__) {
  8977. cb.call(context, layer, z);
  8978. }
  8979. }
  8980. },
  8981. /**
  8982. * 获取所有已创建的层
  8983. * @param {Array.<module:zrender/Layer>} [prevLayer]
  8984. */
  8985. getLayers: function () {
  8986. return this._layers;
  8987. },
  8988. _updateLayerStatus: function (list) {
  8989. this.eachBuiltinLayer(function (layer, z) {
  8990. layer.__dirty = layer.__used = false;
  8991. });
  8992. function updatePrevLayer(idx) {
  8993. if (prevLayer) {
  8994. if (prevLayer.__endIndex !== idx) {
  8995. prevLayer.__dirty = true;
  8996. }
  8997. prevLayer.__endIndex = idx;
  8998. }
  8999. }
  9000. if (this._singleCanvas) {
  9001. for (var i = 1; i < list.length; i++) {
  9002. var el = list[i];
  9003. if (el.zlevel !== list[i - 1].zlevel || el.incremental) {
  9004. this._needsManuallyCompositing = true;
  9005. break;
  9006. }
  9007. }
  9008. }
  9009. var prevLayer = null;
  9010. var incrementalLayerCount = 0;
  9011. var prevZlevel;
  9012. for (var i = 0; i < list.length; i++) {
  9013. var el = list[i];
  9014. var zlevel = el.zlevel;
  9015. var layer;
  9016. if (prevZlevel !== zlevel) {
  9017. prevZlevel = zlevel;
  9018. incrementalLayerCount = 0;
  9019. }
  9020. // TODO Not use magic number on zlevel.
  9021. // Each layer with increment element can be separated to 3 layers.
  9022. // (Other Element drawn after incremental element)
  9023. // -----------------zlevel + EL_AFTER_INCREMENTAL_INC--------------------
  9024. // (Incremental element)
  9025. // ----------------------zlevel + INCREMENTAL_INC------------------------
  9026. // (Element drawn before incremental element)
  9027. // --------------------------------zlevel--------------------------------
  9028. if (el.incremental) {
  9029. layer = this.getLayer(zlevel + INCREMENTAL_INC, this._needsManuallyCompositing);
  9030. layer.incremental = true;
  9031. incrementalLayerCount = 1;
  9032. }
  9033. else {
  9034. layer = this.getLayer(
  9035. zlevel + (incrementalLayerCount > 0 ? EL_AFTER_INCREMENTAL_INC : 0),
  9036. this._needsManuallyCompositing
  9037. );
  9038. }
  9039. if (!layer.__builtin__) {
  9040. logError$1('ZLevel ' + zlevel + ' has been used by unkown layer ' + layer.id);
  9041. }
  9042. if (layer !== prevLayer) {
  9043. layer.__used = true;
  9044. if (layer.__startIndex !== i) {
  9045. layer.__dirty = true;
  9046. }
  9047. layer.__startIndex = i;
  9048. if (!layer.incremental) {
  9049. layer.__drawIndex = i;
  9050. }
  9051. else {
  9052. // Mark layer draw index needs to update.
  9053. layer.__drawIndex = -1;
  9054. }
  9055. updatePrevLayer(i);
  9056. prevLayer = layer;
  9057. }
  9058. if (el.__dirty) {
  9059. layer.__dirty = true;
  9060. if (layer.incremental && layer.__drawIndex < 0) {
  9061. // Start draw from the first dirty element.
  9062. layer.__drawIndex = i;
  9063. }
  9064. }
  9065. }
  9066. updatePrevLayer(i);
  9067. this.eachBuiltinLayer(function (layer, z) {
  9068. // Used in last frame but not in this frame. Needs clear
  9069. if (!layer.__used && layer.getElementCount() > 0) {
  9070. layer.__dirty = true;
  9071. layer.__startIndex = layer.__endIndex = layer.__drawIndex = 0;
  9072. }
  9073. // For incremental layer. In case start index changed and no elements are dirty.
  9074. if (layer.__dirty && layer.__drawIndex < 0) {
  9075. layer.__drawIndex = layer.__startIndex;
  9076. }
  9077. });
  9078. },
  9079. /**
  9080. * 清除hover层外所有内容
  9081. */
  9082. clear: function () {
  9083. this.eachBuiltinLayer(this._clearLayer);
  9084. return this;
  9085. },
  9086. _clearLayer: function (layer) {
  9087. layer.clear();
  9088. },
  9089. setBackgroundColor: function (backgroundColor) {
  9090. this._backgroundColor = backgroundColor;
  9091. },
  9092. /**
  9093. * 修改指定zlevel的绘制参数
  9094. *
  9095. * @param {string} zlevel
  9096. * @param {Object} config 配置对象
  9097. * @param {string} [config.clearColor=0] 每次清空画布的颜色
  9098. * @param {string} [config.motionBlur=false] 是否开启动态模糊
  9099. * @param {number} [config.lastFrameAlpha=0.7]
  9100. * 在开启动态模糊的时候使用,与上一帧混合的alpha值,值越大尾迹越明显
  9101. */
  9102. configLayer: function (zlevel, config) {
  9103. if (config) {
  9104. var layerConfig = this._layerConfig;
  9105. if (!layerConfig[zlevel]) {
  9106. layerConfig[zlevel] = config;
  9107. }
  9108. else {
  9109. merge(layerConfig[zlevel], config, true);
  9110. }
  9111. for (var i = 0; i < this._zlevelList.length; i++) {
  9112. var _zlevel = this._zlevelList[i];
  9113. // TODO Remove EL_AFTER_INCREMENTAL_INC magic number
  9114. if (_zlevel === zlevel || _zlevel === zlevel + EL_AFTER_INCREMENTAL_INC) {
  9115. var layer = this._layers[_zlevel];
  9116. merge(layer, layerConfig[zlevel], true);
  9117. }
  9118. }
  9119. }
  9120. },
  9121. /**
  9122. * 删除指定层
  9123. * @param {number} zlevel 层所在的zlevel
  9124. */
  9125. delLayer: function (zlevel) {
  9126. var layers = this._layers;
  9127. var zlevelList = this._zlevelList;
  9128. var layer = layers[zlevel];
  9129. if (!layer) {
  9130. return;
  9131. }
  9132. layer.dom.parentNode.removeChild(layer.dom);
  9133. delete layers[zlevel];
  9134. zlevelList.splice(indexOf(zlevelList, zlevel), 1);
  9135. },
  9136. /**
  9137. * 区域大小变化后重绘
  9138. */
  9139. resize: function (width, height) {
  9140. if (!this._domRoot.style) { // Maybe in node or worker
  9141. if (width == null || height == null) {
  9142. return;
  9143. }
  9144. this._width = width;
  9145. this._height = height;
  9146. this.getLayer(CANVAS_ZLEVEL).resize(width, height);
  9147. }
  9148. else {
  9149. var domRoot = this._domRoot;
  9150. // FIXME Why ?
  9151. domRoot.style.display = 'none';
  9152. // Save input w/h
  9153. var opts = this._opts;
  9154. width != null && (opts.width = width);
  9155. height != null && (opts.height = height);
  9156. width = this._getSize(0);
  9157. height = this._getSize(1);
  9158. domRoot.style.display = '';
  9159. // 优化没有实际改变的resize
  9160. if (this._width !== width || height !== this._height) {
  9161. domRoot.style.width = width + 'px';
  9162. domRoot.style.height = height + 'px';
  9163. for (var id in this._layers) {
  9164. if (this._layers.hasOwnProperty(id)) {
  9165. this._layers[id].resize(width, height);
  9166. }
  9167. }
  9168. each(this._progressiveLayers, function (layer) {
  9169. layer.resize(width, height);
  9170. });
  9171. this.refresh(true);
  9172. }
  9173. this._width = width;
  9174. this._height = height;
  9175. }
  9176. return this;
  9177. },
  9178. /**
  9179. * 清除单独的一个层
  9180. * @param {number} zlevel
  9181. */
  9182. clearLayer: function (zlevel) {
  9183. var layer = this._layers[zlevel];
  9184. if (layer) {
  9185. layer.clear();
  9186. }
  9187. },
  9188. /**
  9189. * 释放
  9190. */
  9191. dispose: function () {
  9192. this.root.innerHTML = '';
  9193. this.root =
  9194. this.storage =
  9195. this._domRoot =
  9196. this._layers = null;
  9197. },
  9198. /**
  9199. * Get canvas which has all thing rendered
  9200. * @param {Object} opts
  9201. * @param {string} [opts.backgroundColor]
  9202. * @param {number} [opts.pixelRatio]
  9203. */
  9204. getRenderedCanvas: function (opts) {
  9205. opts = opts || {};
  9206. if (this._singleCanvas && !this._compositeManually) {
  9207. return this._layers[CANVAS_ZLEVEL].dom;
  9208. }
  9209. var imageLayer = new Layer('image', this, opts.pixelRatio || this.dpr);
  9210. imageLayer.initContext();
  9211. imageLayer.clear(false, opts.backgroundColor || this._backgroundColor);
  9212. if (opts.pixelRatio <= this.dpr) {
  9213. this.refresh();
  9214. var width = imageLayer.dom.width;
  9215. var height = imageLayer.dom.height;
  9216. var ctx = imageLayer.ctx;
  9217. this.eachLayer(function (layer) {
  9218. if (layer.__builtin__) {
  9219. ctx.drawImage(layer.dom, 0, 0, width, height);
  9220. }
  9221. else if (layer.renderToCanvas) {
  9222. imageLayer.ctx.save();
  9223. layer.renderToCanvas(imageLayer.ctx);
  9224. imageLayer.ctx.restore();
  9225. }
  9226. });
  9227. }
  9228. else {
  9229. // PENDING, echarts-gl and incremental rendering.
  9230. var scope = {};
  9231. var displayList = this.storage.getDisplayList(true);
  9232. for (var i = 0; i < displayList.length; i++) {
  9233. var el = displayList[i];
  9234. this._doPaintEl(el, imageLayer, true, scope);
  9235. }
  9236. }
  9237. return imageLayer.dom;
  9238. },
  9239. /**
  9240. * 获取绘图区域宽度
  9241. */
  9242. getWidth: function () {
  9243. return this._width;
  9244. },
  9245. /**
  9246. * 获取绘图区域高度
  9247. */
  9248. getHeight: function () {
  9249. return this._height;
  9250. },
  9251. _getSize: function (whIdx) {
  9252. var opts = this._opts;
  9253. var wh = ['width', 'height'][whIdx];
  9254. var cwh = ['clientWidth', 'clientHeight'][whIdx];
  9255. var plt = ['paddingLeft', 'paddingTop'][whIdx];
  9256. var prb = ['paddingRight', 'paddingBottom'][whIdx];
  9257. if (opts[wh] != null && opts[wh] !== 'auto') {
  9258. return parseFloat(opts[wh]);
  9259. }
  9260. var root = this.root;
  9261. // IE8 does not support getComputedStyle, but it use VML.
  9262. var stl = document.defaultView.getComputedStyle(root);
  9263. return (
  9264. (root[cwh] || parseInt10(stl[wh]) || parseInt10(root.style[wh]))
  9265. - (parseInt10(stl[plt]) || 0)
  9266. - (parseInt10(stl[prb]) || 0)
  9267. ) | 0;
  9268. },
  9269. pathToImage: function (path, dpr) {
  9270. dpr = dpr || this.dpr;
  9271. var canvas = document.createElement('canvas');
  9272. var ctx = canvas.getContext('2d');
  9273. var rect = path.getBoundingRect();
  9274. var style = path.style;
  9275. var shadowBlurSize = style.shadowBlur * dpr;
  9276. var shadowOffsetX = style.shadowOffsetX * dpr;
  9277. var shadowOffsetY = style.shadowOffsetY * dpr;
  9278. var lineWidth = style.hasStroke() ? style.lineWidth : 0;
  9279. var leftMargin = Math.max(lineWidth / 2, -shadowOffsetX + shadowBlurSize);
  9280. var rightMargin = Math.max(lineWidth / 2, shadowOffsetX + shadowBlurSize);
  9281. var topMargin = Math.max(lineWidth / 2, -shadowOffsetY + shadowBlurSize);
  9282. var bottomMargin = Math.max(lineWidth / 2, shadowOffsetY + shadowBlurSize);
  9283. var width = rect.width + leftMargin + rightMargin;
  9284. var height = rect.height + topMargin + bottomMargin;
  9285. canvas.width = width * dpr;
  9286. canvas.height = height * dpr;
  9287. ctx.scale(dpr, dpr);
  9288. ctx.clearRect(0, 0, width, height);
  9289. ctx.dpr = dpr;
  9290. var pathTransform = {
  9291. position: path.position,
  9292. rotation: path.rotation,
  9293. scale: path.scale
  9294. };
  9295. path.position = [leftMargin - rect.x, topMargin - rect.y];
  9296. path.rotation = 0;
  9297. path.scale = [1, 1];
  9298. path.updateTransform();
  9299. if (path) {
  9300. path.brush(ctx);
  9301. }
  9302. var ImageShape = ZImage;
  9303. var imgShape = new ImageShape({
  9304. style: {
  9305. x: 0,
  9306. y: 0,
  9307. image: canvas
  9308. }
  9309. });
  9310. if (pathTransform.position != null) {
  9311. imgShape.position = path.position = pathTransform.position;
  9312. }
  9313. if (pathTransform.rotation != null) {
  9314. imgShape.rotation = path.rotation = pathTransform.rotation;
  9315. }
  9316. if (pathTransform.scale != null) {
  9317. imgShape.scale = path.scale = pathTransform.scale;
  9318. }
  9319. return imgShape;
  9320. }
  9321. };
  9322. /**
  9323. * Animation main class, dispatch and manage all animation controllers
  9324. *
  9325. * @module zrender/animation/Animation
  9326. * @author pissang(https://github.com/pissang)
  9327. */
  9328. // TODO Additive animation
  9329. // http://iosoteric.com/additive-animations-animatewithduration-in-ios-8/
  9330. // https://developer.apple.com/videos/wwdc2014/#236
  9331. /**
  9332. * @typedef {Object} IZRenderStage
  9333. * @property {Function} update
  9334. */
  9335. /**
  9336. * @alias module:zrender/animation/Animation
  9337. * @constructor
  9338. * @param {Object} [options]
  9339. * @param {Function} [options.onframe]
  9340. * @param {IZRenderStage} [options.stage]
  9341. * @example
  9342. * var animation = new Animation();
  9343. * var obj = {
  9344. * x: 100,
  9345. * y: 100
  9346. * };
  9347. * animation.animate(node.position)
  9348. * .when(1000, {
  9349. * x: 500,
  9350. * y: 500
  9351. * })
  9352. * .when(2000, {
  9353. * x: 100,
  9354. * y: 100
  9355. * })
  9356. * .start('spline');
  9357. */
  9358. var Animation = function (options) {
  9359. options = options || {};
  9360. this.stage = options.stage || {};
  9361. this.onframe = options.onframe || function () {};
  9362. // private properties
  9363. this._clips = [];
  9364. this._running = false;
  9365. this._time;
  9366. this._pausedTime;
  9367. this._pauseStart;
  9368. this._paused = false;
  9369. Eventful.call(this);
  9370. };
  9371. Animation.prototype = {
  9372. constructor: Animation,
  9373. /**
  9374. * Add clip
  9375. * @param {module:zrender/animation/Clip} clip
  9376. */
  9377. addClip: function (clip) {
  9378. this._clips.push(clip);
  9379. },
  9380. /**
  9381. * Add animator
  9382. * @param {module:zrender/animation/Animator} animator
  9383. */
  9384. addAnimator: function (animator) {
  9385. animator.animation = this;
  9386. var clips = animator.getClips();
  9387. for (var i = 0; i < clips.length; i++) {
  9388. this.addClip(clips[i]);
  9389. }
  9390. },
  9391. /**
  9392. * Delete animation clip
  9393. * @param {module:zrender/animation/Clip} clip
  9394. */
  9395. removeClip: function (clip) {
  9396. var idx = indexOf(this._clips, clip);
  9397. if (idx >= 0) {
  9398. this._clips.splice(idx, 1);
  9399. }
  9400. },
  9401. /**
  9402. * Delete animation clip
  9403. * @param {module:zrender/animation/Animator} animator
  9404. */
  9405. removeAnimator: function (animator) {
  9406. var clips = animator.getClips();
  9407. for (var i = 0; i < clips.length; i++) {
  9408. this.removeClip(clips[i]);
  9409. }
  9410. animator.animation = null;
  9411. },
  9412. _update: function () {
  9413. var time = new Date().getTime() - this._pausedTime;
  9414. var delta = time - this._time;
  9415. var clips = this._clips;
  9416. var len = clips.length;
  9417. var deferredEvents = [];
  9418. var deferredClips = [];
  9419. for (var i = 0; i < len; i++) {
  9420. var clip = clips[i];
  9421. var e = clip.step(time, delta);
  9422. // Throw out the events need to be called after
  9423. // stage.update, like destroy
  9424. if (e) {
  9425. deferredEvents.push(e);
  9426. deferredClips.push(clip);
  9427. }
  9428. }
  9429. // Remove the finished clip
  9430. for (var i = 0; i < len;) {
  9431. if (clips[i]._needsRemove) {
  9432. clips[i] = clips[len - 1];
  9433. clips.pop();
  9434. len--;
  9435. }
  9436. else {
  9437. i++;
  9438. }
  9439. }
  9440. len = deferredEvents.length;
  9441. for (var i = 0; i < len; i++) {
  9442. deferredClips[i].fire(deferredEvents[i]);
  9443. }
  9444. this._time = time;
  9445. this.onframe(delta);
  9446. // 'frame' should be triggered before stage, because upper application
  9447. // depends on the sequence (e.g., echarts-stream and finish
  9448. // event judge)
  9449. this.trigger('frame', delta);
  9450. if (this.stage.update) {
  9451. this.stage.update();
  9452. }
  9453. },
  9454. _startLoop: function () {
  9455. var self = this;
  9456. this._running = true;
  9457. function step() {
  9458. if (self._running) {
  9459. requestAnimationFrame(step);
  9460. !self._paused && self._update();
  9461. }
  9462. }
  9463. requestAnimationFrame(step);
  9464. },
  9465. /**
  9466. * Start animation.
  9467. */
  9468. start: function () {
  9469. this._time = new Date().getTime();
  9470. this._pausedTime = 0;
  9471. this._startLoop();
  9472. },
  9473. /**
  9474. * Stop animation.
  9475. */
  9476. stop: function () {
  9477. this._running = false;
  9478. },
  9479. /**
  9480. * Pause animation.
  9481. */
  9482. pause: function () {
  9483. if (!this._paused) {
  9484. this._pauseStart = new Date().getTime();
  9485. this._paused = true;
  9486. }
  9487. },
  9488. /**
  9489. * Resume animation.
  9490. */
  9491. resume: function () {
  9492. if (this._paused) {
  9493. this._pausedTime += (new Date().getTime()) - this._pauseStart;
  9494. this._paused = false;
  9495. }
  9496. },
  9497. /**
  9498. * Clear animation.
  9499. */
  9500. clear: function () {
  9501. this._clips = [];
  9502. },
  9503. /**
  9504. * Whether animation finished.
  9505. */
  9506. isFinished: function () {
  9507. return !this._clips.length;
  9508. },
  9509. /**
  9510. * Creat animator for a target, whose props can be animated.
  9511. *
  9512. * @param {Object} target
  9513. * @param {Object} options
  9514. * @param {boolean} [options.loop=false] Whether loop animation.
  9515. * @param {Function} [options.getter=null] Get value from target.
  9516. * @param {Function} [options.setter=null] Set value to target.
  9517. * @return {module:zrender/animation/Animation~Animator}
  9518. */
  9519. // TODO Gap
  9520. animate: function (target, options) {
  9521. options = options || {};
  9522. var animator = new Animator(
  9523. target,
  9524. options.loop,
  9525. options.getter,
  9526. options.setter
  9527. );
  9528. this.addAnimator(animator);
  9529. return animator;
  9530. }
  9531. };
  9532. mixin(Animation, Eventful);
  9533. /* global document */
  9534. var TOUCH_CLICK_DELAY = 300;
  9535. var globalEventSupported = env$1.domSupported;
  9536. var localNativeListenerNames = (function () {
  9537. var mouseHandlerNames = [
  9538. 'click', 'dblclick', 'mousewheel', 'mouseout',
  9539. 'mouseup', 'mousedown', 'mousemove', 'contextmenu'
  9540. ];
  9541. var touchHandlerNames = [
  9542. 'touchstart', 'touchend', 'touchmove'
  9543. ];
  9544. var pointerEventNameMap = {
  9545. pointerdown: 1, pointerup: 1, pointermove: 1, pointerout: 1
  9546. };
  9547. var pointerHandlerNames = map(mouseHandlerNames, function (name) {
  9548. var nm = name.replace('mouse', 'pointer');
  9549. return pointerEventNameMap.hasOwnProperty(nm) ? nm : name;
  9550. });
  9551. return {
  9552. mouse: mouseHandlerNames,
  9553. touch: touchHandlerNames,
  9554. pointer: pointerHandlerNames
  9555. };
  9556. })();
  9557. var globalNativeListenerNames = {
  9558. mouse: ['mousemove', 'mouseup'],
  9559. pointer: ['pointermove', 'pointerup']
  9560. };
  9561. function eventNameFix(name) {
  9562. return (name === 'mousewheel' && env$1.browser.firefox) ? 'DOMMouseScroll' : name;
  9563. }
  9564. function isPointerFromTouch(event) {
  9565. var pointerType = event.pointerType;
  9566. return pointerType === 'pen' || pointerType === 'touch';
  9567. }
  9568. // function useMSGuesture(handlerProxy, event) {
  9569. // return isPointerFromTouch(event) && !!handlerProxy._msGesture;
  9570. // }
  9571. // function onMSGestureChange(proxy, event) {
  9572. // if (event.translationX || event.translationY) {
  9573. // // mousemove is carried by MSGesture to reduce the sensitivity.
  9574. // proxy.handler.dispatchToElement(event.target, 'mousemove', event);
  9575. // }
  9576. // if (event.scale !== 1) {
  9577. // event.pinchX = event.offsetX;
  9578. // event.pinchY = event.offsetY;
  9579. // event.pinchScale = event.scale;
  9580. // proxy.handler.dispatchToElement(event.target, 'pinch', event);
  9581. // }
  9582. // }
  9583. /**
  9584. * Prevent mouse event from being dispatched after Touch Events action
  9585. * @see <https://github.com/deltakosh/handjs/blob/master/src/hand.base.js>
  9586. * 1. Mobile browsers dispatch mouse events 300ms after touchend.
  9587. * 2. Chrome for Android dispatch mousedown for long-touch about 650ms
  9588. * Result: Blocking Mouse Events for 700ms.
  9589. *
  9590. * @param {DOMHandlerScope} scope
  9591. */
  9592. function setTouchTimer(scope) {
  9593. scope.touching = true;
  9594. if (scope.touchTimer != null) {
  9595. clearTimeout(scope.touchTimer);
  9596. scope.touchTimer = null;
  9597. }
  9598. scope.touchTimer = setTimeout(function () {
  9599. scope.touching = false;
  9600. scope.touchTimer = null;
  9601. }, 700);
  9602. }
  9603. // Mark touch, which is useful in distinguish touch and
  9604. // mouse event in upper applicatoin.
  9605. function markTouch(event) {
  9606. event && (event.zrByTouch = true);
  9607. }
  9608. // function markTriggeredFromLocal(event) {
  9609. // event && (event.__zrIsFromLocal = true);
  9610. // }
  9611. // function isTriggeredFromLocal(instance, event) {
  9612. // return !!(event && event.__zrIsFromLocal);
  9613. // }
  9614. function normalizeGlobalEvent(instance, event) {
  9615. // offsetX, offsetY still need to be calculated. They are necessary in the event
  9616. // handlers of the upper applications. Set `true` to force calculate them.
  9617. return normalizeEvent(instance.dom, new FakeGlobalEvent(instance, event), true);
  9618. }
  9619. /**
  9620. * Detect whether the given el is in `painterRoot`.
  9621. */
  9622. function isLocalEl(instance, el) {
  9623. var elTmp = el;
  9624. var isLocal = false;
  9625. while (elTmp && elTmp.nodeType !== 9
  9626. && !(
  9627. isLocal = elTmp.domBelongToZr
  9628. || (elTmp !== el && elTmp === instance.painterRoot)
  9629. )
  9630. ) {
  9631. elTmp = elTmp.parentNode;
  9632. }
  9633. return isLocal;
  9634. }
  9635. /**
  9636. * Make a fake event but not change the original event,
  9637. * becuase the global event probably be used by other
  9638. * listeners not belonging to zrender.
  9639. * @class
  9640. */
  9641. function FakeGlobalEvent(instance, event) {
  9642. this.type = event.type;
  9643. this.target = this.currentTarget = instance.dom;
  9644. this.pointerType = event.pointerType;
  9645. // Necessray for the force calculation of zrX, zrY
  9646. this.clientX = event.clientX;
  9647. this.clientY = event.clientY;
  9648. // Because we do not mount global listeners to touch events,
  9649. // we do not copy `targetTouches` and `changedTouches` here.
  9650. }
  9651. var fakeGlobalEventProto = FakeGlobalEvent.prototype;
  9652. // we make the default methods on the event do nothing,
  9653. // otherwise it is dangerous. See more details in
  9654. // [Drag outside] in `Handler.js`.
  9655. fakeGlobalEventProto.stopPropagation =
  9656. fakeGlobalEventProto.stopImmediatePropagation =
  9657. fakeGlobalEventProto.preventDefault = noop;
  9658. /**
  9659. * Local DOM Handlers
  9660. * @this {HandlerProxy}
  9661. */
  9662. var localDOMHandlers = {
  9663. mousedown: function (event) {
  9664. event = normalizeEvent(this.dom, event);
  9665. this._mayPointerCapture = [event.zrX, event.zrY];
  9666. this.trigger('mousedown', event);
  9667. },
  9668. mousemove: function (event) {
  9669. event = normalizeEvent(this.dom, event);
  9670. var downPoint = this._mayPointerCapture;
  9671. if (downPoint && (event.zrX !== downPoint[0] || event.zrY !== downPoint[1])) {
  9672. togglePointerCapture(this, true);
  9673. }
  9674. this.trigger('mousemove', event);
  9675. },
  9676. mouseup: function (event) {
  9677. event = normalizeEvent(this.dom, event);
  9678. togglePointerCapture(this, false);
  9679. this.trigger('mouseup', event);
  9680. },
  9681. mouseout: function (event) {
  9682. event = normalizeEvent(this.dom, event);
  9683. // Similarly to the browser did on `document` and touch event,
  9684. // `globalout` will be delayed to final pointer cature release.
  9685. if (this._pointerCapturing) {
  9686. event.zrEventControl = 'no_globalout';
  9687. }
  9688. // There might be some doms created by upper layer application
  9689. // at the same level of painter.getViewportRoot() (e.g., tooltip
  9690. // dom created by echarts), where 'globalout' event should not
  9691. // be triggered when mouse enters these doms. (But 'mouseout'
  9692. // should be triggered at the original hovered element as usual).
  9693. var element = event.toElement || event.relatedTarget;
  9694. event.zrIsToLocalDOM = isLocalEl(this, element);
  9695. this.trigger('mouseout', event);
  9696. },
  9697. touchstart: function (event) {
  9698. // Default mouse behaviour should not be disabled here.
  9699. // For example, page may needs to be slided.
  9700. event = normalizeEvent(this.dom, event);
  9701. markTouch(event);
  9702. this._lastTouchMoment = new Date();
  9703. this.handler.processGesture(event, 'start');
  9704. // For consistent event listener for both touch device and mouse device,
  9705. // we simulate "mouseover-->mousedown" in touch device. So we trigger
  9706. // `mousemove` here (to trigger `mouseover` inside), and then trigger
  9707. // `mousedown`.
  9708. localDOMHandlers.mousemove.call(this, event);
  9709. localDOMHandlers.mousedown.call(this, event);
  9710. },
  9711. touchmove: function (event) {
  9712. event = normalizeEvent(this.dom, event);
  9713. markTouch(event);
  9714. this.handler.processGesture(event, 'change');
  9715. // Mouse move should always be triggered no matter whether
  9716. // there is gestrue event, because mouse move and pinch may
  9717. // be used at the same time.
  9718. localDOMHandlers.mousemove.call(this, event);
  9719. },
  9720. touchend: function (event) {
  9721. event = normalizeEvent(this.dom, event);
  9722. markTouch(event);
  9723. this.handler.processGesture(event, 'end');
  9724. localDOMHandlers.mouseup.call(this, event);
  9725. // Do not trigger `mouseout` here, in spite of `mousemove`(`mouseover`) is
  9726. // triggered in `touchstart`. This seems to be illogical, but by this mechanism,
  9727. // we can conveniently implement "hover style" in both PC and touch device just
  9728. // by listening to `mouseover` to add "hover style" and listening to `mouseout`
  9729. // to remove "hover style" on an element, without any additional code for
  9730. // compatibility. (`mouseout` will not be triggered in `touchend`, so "hover
  9731. // style" will remain for user view)
  9732. // click event should always be triggered no matter whether
  9733. // there is gestrue event. System click can not be prevented.
  9734. if (+new Date() - this._lastTouchMoment < TOUCH_CLICK_DELAY) {
  9735. localDOMHandlers.click.call(this, event);
  9736. }
  9737. },
  9738. pointerdown: function (event) {
  9739. localDOMHandlers.mousedown.call(this, event);
  9740. // if (useMSGuesture(this, event)) {
  9741. // this._msGesture.addPointer(event.pointerId);
  9742. // }
  9743. },
  9744. pointermove: function (event) {
  9745. // FIXME
  9746. // pointermove is so sensitive that it always triggered when
  9747. // tap(click) on touch screen, which affect some judgement in
  9748. // upper application. So, we dont support mousemove on MS touch
  9749. // device yet.
  9750. if (!isPointerFromTouch(event)) {
  9751. localDOMHandlers.mousemove.call(this, event);
  9752. }
  9753. },
  9754. pointerup: function (event) {
  9755. localDOMHandlers.mouseup.call(this, event);
  9756. },
  9757. pointerout: function (event) {
  9758. // pointerout will be triggered when tap on touch screen
  9759. // (IE11+/Edge on MS Surface) after click event triggered,
  9760. // which is inconsistent with the mousout behavior we defined
  9761. // in touchend. So we unify them.
  9762. // (check localDOMHandlers.touchend for detailed explanation)
  9763. if (!isPointerFromTouch(event)) {
  9764. localDOMHandlers.mouseout.call(this, event);
  9765. }
  9766. }
  9767. };
  9768. /**
  9769. * Othere DOM UI Event handlers for zr dom.
  9770. * @this {HandlerProxy}
  9771. */
  9772. each(['click', 'mousewheel', 'dblclick', 'contextmenu'], function (name) {
  9773. localDOMHandlers[name] = function (event) {
  9774. event = normalizeEvent(this.dom, event);
  9775. this.trigger(name, event);
  9776. };
  9777. });
  9778. /**
  9779. * DOM UI Event handlers for global page.
  9780. *
  9781. * [Caution]:
  9782. * those handlers should both support in capture phase and bubble phase!
  9783. *
  9784. * @this {HandlerProxy}
  9785. */
  9786. var globalDOMHandlers = {
  9787. pointermove: function (event) {
  9788. // FIXME
  9789. // pointermove is so sensitive that it always triggered when
  9790. // tap(click) on touch screen, which affect some judgement in
  9791. // upper application. So, we dont support mousemove on MS touch
  9792. // device yet.
  9793. if (!isPointerFromTouch(event)) {
  9794. globalDOMHandlers.mousemove.call(this, event);
  9795. }
  9796. },
  9797. pointerup: function (event) {
  9798. globalDOMHandlers.mouseup.call(this, event);
  9799. },
  9800. mousemove: function (event) {
  9801. this.trigger('mousemove', event);
  9802. },
  9803. mouseup: function (event) {
  9804. var pointerCaptureReleasing = this._pointerCapturing;
  9805. togglePointerCapture(this, false);
  9806. this.trigger('mouseup', event);
  9807. if (pointerCaptureReleasing) {
  9808. event.zrEventControl = 'only_globalout';
  9809. this.trigger('mouseout', event);
  9810. }
  9811. }
  9812. };
  9813. /**
  9814. * @param {HandlerProxy} instance
  9815. * @param {DOMHandlerScope} scope
  9816. */
  9817. function mountLocalDOMEventListeners(instance, scope) {
  9818. var domHandlers = scope.domHandlers;
  9819. if (env$1.pointerEventsSupported) { // Only IE11+/Edge
  9820. // 1. On devices that both enable touch and mouse (e.g., MS Surface and lenovo X240),
  9821. // IE11+/Edge do not trigger touch event, but trigger pointer event and mouse event
  9822. // at the same time.
  9823. // 2. On MS Surface, it probablely only trigger mousedown but no mouseup when tap on
  9824. // screen, which do not occurs in pointer event.
  9825. // So we use pointer event to both detect touch gesture and mouse behavior.
  9826. each(localNativeListenerNames.pointer, function (nativeEventName) {
  9827. mountSingleDOMEventListener(scope, nativeEventName, function (event) {
  9828. // markTriggeredFromLocal(event);
  9829. domHandlers[nativeEventName].call(instance, event);
  9830. });
  9831. });
  9832. // FIXME
  9833. // Note: MS Gesture require CSS touch-action set. But touch-action is not reliable,
  9834. // which does not prevent defuault behavior occasionally (which may cause view port
  9835. // zoomed in but use can not zoom it back). And event.preventDefault() does not work.
  9836. // So we have to not to use MSGesture and not to support touchmove and pinch on MS
  9837. // touch screen. And we only support click behavior on MS touch screen now.
  9838. // MS Gesture Event is only supported on IE11+/Edge and on Windows 8+.
  9839. // We dont support touch on IE on win7.
  9840. // See <https://msdn.microsoft.com/en-us/library/dn433243(v=vs.85).aspx>
  9841. // if (typeof MSGesture === 'function') {
  9842. // (this._msGesture = new MSGesture()).target = dom; // jshint ignore:line
  9843. // dom.addEventListener('MSGestureChange', onMSGestureChange);
  9844. // }
  9845. }
  9846. else {
  9847. if (env$1.touchEventsSupported) {
  9848. each(localNativeListenerNames.touch, function (nativeEventName) {
  9849. mountSingleDOMEventListener(scope, nativeEventName, function (event) {
  9850. // markTriggeredFromLocal(event);
  9851. domHandlers[nativeEventName].call(instance, event);
  9852. setTouchTimer(scope);
  9853. });
  9854. });
  9855. // Handler of 'mouseout' event is needed in touch mode, which will be mounted below.
  9856. // addEventListener(root, 'mouseout', this._mouseoutHandler);
  9857. }
  9858. // 1. Considering some devices that both enable touch and mouse event (like on MS Surface
  9859. // and lenovo X240, @see #2350), we make mouse event be always listened, otherwise
  9860. // mouse event can not be handle in those devices.
  9861. // 2. On MS Surface, Chrome will trigger both touch event and mouse event. How to prevent
  9862. // mouseevent after touch event triggered, see `setTouchTimer`.
  9863. each(localNativeListenerNames.mouse, function (nativeEventName) {
  9864. mountSingleDOMEventListener(scope, nativeEventName, function (event) {
  9865. event = getNativeEvent(event);
  9866. if (!scope.touching) {
  9867. // markTriggeredFromLocal(event);
  9868. domHandlers[nativeEventName].call(instance, event);
  9869. }
  9870. });
  9871. });
  9872. }
  9873. }
  9874. /**
  9875. * @param {HandlerProxy} instance
  9876. * @param {DOMHandlerScope} scope
  9877. */
  9878. function mountGlobalDOMEventListeners(instance, scope) {
  9879. // Only IE11+/Edge. See the comment in `mountLocalDOMEventListeners`.
  9880. if (env$1.pointerEventsSupported) {
  9881. each(globalNativeListenerNames.pointer, mount);
  9882. }
  9883. // Touch event has implemented "drag outside" so we do not mount global listener for touch event.
  9884. // (see https://www.w3.org/TR/touch-events/#the-touchmove-event)
  9885. // We do not consider "both-support-touch-and-mouse device" for this feature (see the comment of
  9886. // `mountLocalDOMEventListeners`) to avoid bugs util some requirements come.
  9887. else if (!env$1.touchEventsSupported) {
  9888. each(globalNativeListenerNames.mouse, mount);
  9889. }
  9890. function mount(nativeEventName) {
  9891. function nativeEventListener(event) {
  9892. event = getNativeEvent(event);
  9893. // See the reason in [Drag outside] in `Handler.js`
  9894. // This checking supports both `useCapture` or not.
  9895. // PENDING: if there is performance issue in some devices,
  9896. // we probably can not use `useCapture` and change a easier
  9897. // to judes whether local (mark).
  9898. if (!isLocalEl(instance, event.target)) {
  9899. event = normalizeGlobalEvent(instance, event);
  9900. scope.domHandlers[nativeEventName].call(instance, event);
  9901. }
  9902. }
  9903. mountSingleDOMEventListener(
  9904. scope, nativeEventName, nativeEventListener,
  9905. {capture: true} // See [Drag Outside] in `Handler.js`
  9906. );
  9907. }
  9908. }
  9909. function mountSingleDOMEventListener(scope, nativeEventName, listener, opt) {
  9910. scope.mounted[nativeEventName] = listener;
  9911. scope.listenerOpts[nativeEventName] = opt;
  9912. addEventListener(scope.domTarget, eventNameFix(nativeEventName), listener, opt);
  9913. }
  9914. function unmountDOMEventListeners(scope) {
  9915. var mounted = scope.mounted;
  9916. for (var nativeEventName in mounted) {
  9917. if (mounted.hasOwnProperty(nativeEventName)) {
  9918. removeEventListener(
  9919. scope.domTarget, eventNameFix(nativeEventName), mounted[nativeEventName],
  9920. scope.listenerOpts[nativeEventName]
  9921. );
  9922. }
  9923. }
  9924. scope.mounted = {};
  9925. }
  9926. /**
  9927. * See [Drag Outside] in `Handler.js`.
  9928. * @implement
  9929. * @param {boolean} isPointerCapturing Should never be `null`/`undefined`.
  9930. * `true`: start to capture pointer if it is not capturing.
  9931. * `false`: end the capture if it is capturing.
  9932. */
  9933. function togglePointerCapture(instance, isPointerCapturing) {
  9934. instance._mayPointerCapture = null;
  9935. if (globalEventSupported && (instance._pointerCapturing ^ isPointerCapturing)) {
  9936. instance._pointerCapturing = isPointerCapturing;
  9937. var globalHandlerScope = instance._globalHandlerScope;
  9938. isPointerCapturing
  9939. ? mountGlobalDOMEventListeners(instance, globalHandlerScope)
  9940. : unmountDOMEventListeners(globalHandlerScope);
  9941. }
  9942. }
  9943. /**
  9944. * @inner
  9945. * @class
  9946. */
  9947. function DOMHandlerScope(domTarget, domHandlers) {
  9948. this.domTarget = domTarget;
  9949. this.domHandlers = domHandlers;
  9950. // Key: eventName, value: mounted handler funcitons.
  9951. // Used for unmount.
  9952. this.mounted = {};
  9953. this.listenerOpts = {};
  9954. this.touchTimer = null;
  9955. this.touching = false;
  9956. }
  9957. /**
  9958. * @public
  9959. * @class
  9960. */
  9961. function HandlerDomProxy(dom, painterRoot) {
  9962. Eventful.call(this);
  9963. this.dom = dom;
  9964. this.painterRoot = painterRoot;
  9965. this._localHandlerScope = new DOMHandlerScope(dom, localDOMHandlers);
  9966. if (globalEventSupported) {
  9967. this._globalHandlerScope = new DOMHandlerScope(document, globalDOMHandlers);
  9968. }
  9969. /**
  9970. * @type {boolean}
  9971. */
  9972. this._pointerCapturing = false;
  9973. /**
  9974. * @type {Array.<number>} [x, y] or null.
  9975. */
  9976. this._mayPointerCapture = null;
  9977. mountLocalDOMEventListeners(this, this._localHandlerScope);
  9978. }
  9979. var handlerDomProxyProto = HandlerDomProxy.prototype;
  9980. handlerDomProxyProto.dispose = function () {
  9981. unmountDOMEventListeners(this._localHandlerScope);
  9982. if (globalEventSupported) {
  9983. unmountDOMEventListeners(this._globalHandlerScope);
  9984. }
  9985. };
  9986. handlerDomProxyProto.setCursor = function (cursorStyle) {
  9987. this.dom.style && (this.dom.style.cursor = cursorStyle || 'default');
  9988. };
  9989. mixin(HandlerDomProxy, Eventful);
  9990. /*!
  9991. * ZRender, a high performance 2d drawing library.
  9992. *
  9993. * Copyright (c) 2013, Baidu Inc.
  9994. * All rights reserved.
  9995. *
  9996. * LICENSE
  9997. * https://github.com/ecomfe/zrender/blob/master/LICENSE.txt
  9998. */
  9999. var useVML = !env$1.canvasSupported;
  10000. var painterCtors = {
  10001. canvas: Painter
  10002. };
  10003. var instances = {}; // ZRender实例map索引
  10004. /**
  10005. * @type {string}
  10006. */
  10007. var version = '4.3.2';
  10008. /**
  10009. * Initializing a zrender instance
  10010. * @param {HTMLElement} dom
  10011. * @param {Object} [opts]
  10012. * @param {string} [opts.renderer='canvas'] 'canvas' or 'svg'
  10013. * @param {number} [opts.devicePixelRatio]
  10014. * @param {number|string} [opts.width] Can be 'auto' (the same as null/undefined)
  10015. * @param {number|string} [opts.height] Can be 'auto' (the same as null/undefined)
  10016. * @return {module:zrender/ZRender}
  10017. */
  10018. function init(dom, opts) {
  10019. var zr = new ZRender(guid(), dom, opts);
  10020. instances[zr.id] = zr;
  10021. return zr;
  10022. }
  10023. /**
  10024. * Dispose zrender instance
  10025. * @param {module:zrender/ZRender} zr
  10026. */
  10027. function dispose(zr) {
  10028. if (zr) {
  10029. zr.dispose();
  10030. }
  10031. else {
  10032. for (var key in instances) {
  10033. if (instances.hasOwnProperty(key)) {
  10034. instances[key].dispose();
  10035. }
  10036. }
  10037. instances = {};
  10038. }
  10039. return this;
  10040. }
  10041. /**
  10042. * Get zrender instance by id
  10043. * @param {string} id zrender instance id
  10044. * @return {module:zrender/ZRender}
  10045. */
  10046. function getInstance(id) {
  10047. return instances[id];
  10048. }
  10049. function registerPainter(name, Ctor) {
  10050. painterCtors[name] = Ctor;
  10051. }
  10052. function delInstance(id) {
  10053. delete instances[id];
  10054. }
  10055. /**
  10056. * @module zrender/ZRender
  10057. */
  10058. /**
  10059. * @constructor
  10060. * @alias module:zrender/ZRender
  10061. * @param {string} id
  10062. * @param {HTMLElement} dom
  10063. * @param {Object} opts
  10064. * @param {string} [opts.renderer='canvas'] 'canvas' or 'svg'
  10065. * @param {number} [opts.devicePixelRatio]
  10066. * @param {number} [opts.width] Can be 'auto' (the same as null/undefined)
  10067. * @param {number} [opts.height] Can be 'auto' (the same as null/undefined)
  10068. */
  10069. var ZRender = function (id, dom, opts) {
  10070. opts = opts || {};
  10071. /**
  10072. * @type {HTMLDomElement}
  10073. */
  10074. this.dom = dom;
  10075. /**
  10076. * @type {string}
  10077. */
  10078. this.id = id;
  10079. var self = this;
  10080. var storage = new Storage();
  10081. var rendererType = opts.renderer;
  10082. // TODO WebGL
  10083. if (useVML) {
  10084. if (!painterCtors.vml) {
  10085. throw new Error('You need to require \'zrender/vml/vml\' to support IE8');
  10086. }
  10087. rendererType = 'vml';
  10088. }
  10089. else if (!rendererType || !painterCtors[rendererType]) {
  10090. rendererType = 'canvas';
  10091. }
  10092. var painter = new painterCtors[rendererType](dom, storage, opts, id);
  10093. this.storage = storage;
  10094. this.painter = painter;
  10095. var handerProxy = (!env$1.node && !env$1.worker) ? new HandlerDomProxy(painter.getViewportRoot(), painter.root) : null;
  10096. this.handler = new Handler(storage, painter, handerProxy, painter.root);
  10097. /**
  10098. * @type {module:zrender/animation/Animation}
  10099. */
  10100. this.animation = new Animation({
  10101. stage: {
  10102. update: bind(this.flush, this)
  10103. }
  10104. });
  10105. this.animation.start();
  10106. /**
  10107. * @type {boolean}
  10108. * @private
  10109. */
  10110. this._needsRefresh;
  10111. // 修改 storage.delFromStorage, 每次删除元素之前删除动画
  10112. // FIXME 有点ugly
  10113. var oldDelFromStorage = storage.delFromStorage;
  10114. var oldAddToStorage = storage.addToStorage;
  10115. storage.delFromStorage = function (el) {
  10116. oldDelFromStorage.call(storage, el);
  10117. el && el.removeSelfFromZr(self);
  10118. };
  10119. storage.addToStorage = function (el) {
  10120. oldAddToStorage.call(storage, el);
  10121. el.addSelfToZr(self);
  10122. };
  10123. };
  10124. ZRender.prototype = {
  10125. constructor: ZRender,
  10126. /**
  10127. * 获取实例唯一标识
  10128. * @return {string}
  10129. */
  10130. getId: function () {
  10131. return this.id;
  10132. },
  10133. /**
  10134. * 添加元素
  10135. * @param {module:zrender/Element} el
  10136. */
  10137. add: function (el) {
  10138. this.storage.addRoot(el);
  10139. this._needsRefresh = true;
  10140. },
  10141. /**
  10142. * 删除元素
  10143. * @param {module:zrender/Element} el
  10144. */
  10145. remove: function (el) {
  10146. this.storage.delRoot(el);
  10147. this._needsRefresh = true;
  10148. },
  10149. /**
  10150. * Change configuration of layer
  10151. * @param {string} zLevel
  10152. * @param {Object} config
  10153. * @param {string} [config.clearColor=0] Clear color
  10154. * @param {string} [config.motionBlur=false] If enable motion blur
  10155. * @param {number} [config.lastFrameAlpha=0.7] Motion blur factor. Larger value cause longer trailer
  10156. */
  10157. configLayer: function (zLevel, config) {
  10158. if (this.painter.configLayer) {
  10159. this.painter.configLayer(zLevel, config);
  10160. }
  10161. this._needsRefresh = true;
  10162. },
  10163. /**
  10164. * Set background color
  10165. * @param {string} backgroundColor
  10166. */
  10167. setBackgroundColor: function (backgroundColor) {
  10168. if (this.painter.setBackgroundColor) {
  10169. this.painter.setBackgroundColor(backgroundColor);
  10170. }
  10171. this._needsRefresh = true;
  10172. },
  10173. /**
  10174. * Repaint the canvas immediately
  10175. */
  10176. refreshImmediately: function () {
  10177. // var start = new Date();
  10178. // Clear needsRefresh ahead to avoid something wrong happens in refresh
  10179. // Or it will cause zrender refreshes again and again.
  10180. this._needsRefresh = this._needsRefreshHover = false;
  10181. this.painter.refresh();
  10182. // Avoid trigger zr.refresh in Element#beforeUpdate hook
  10183. this._needsRefresh = this._needsRefreshHover = false;
  10184. // var end = new Date();
  10185. // var log = document.getElementById('log');
  10186. // if (log) {
  10187. // log.innerHTML = log.innerHTML + '<br>' + (end - start);
  10188. // }
  10189. },
  10190. /**
  10191. * Mark and repaint the canvas in the next frame of browser
  10192. */
  10193. refresh: function () {
  10194. this._needsRefresh = true;
  10195. },
  10196. /**
  10197. * Perform all refresh
  10198. */
  10199. flush: function () {
  10200. var triggerRendered;
  10201. if (this._needsRefresh) {
  10202. triggerRendered = true;
  10203. this.refreshImmediately();
  10204. }
  10205. if (this._needsRefreshHover) {
  10206. triggerRendered = true;
  10207. this.refreshHoverImmediately();
  10208. }
  10209. triggerRendered && this.trigger('rendered');
  10210. },
  10211. /**
  10212. * Add element to hover layer
  10213. * @param {module:zrender/Element} el
  10214. * @param {Object} style
  10215. */
  10216. addHover: function (el, style) {
  10217. if (this.painter.addHover) {
  10218. var elMirror = this.painter.addHover(el, style);
  10219. this.refreshHover();
  10220. return elMirror;
  10221. }
  10222. },
  10223. /**
  10224. * Add element from hover layer
  10225. * @param {module:zrender/Element} el
  10226. */
  10227. removeHover: function (el) {
  10228. if (this.painter.removeHover) {
  10229. this.painter.removeHover(el);
  10230. this.refreshHover();
  10231. }
  10232. },
  10233. /**
  10234. * Clear all hover elements in hover layer
  10235. * @param {module:zrender/Element} el
  10236. */
  10237. clearHover: function () {
  10238. if (this.painter.clearHover) {
  10239. this.painter.clearHover();
  10240. this.refreshHover();
  10241. }
  10242. },
  10243. /**
  10244. * Refresh hover in next frame
  10245. */
  10246. refreshHover: function () {
  10247. this._needsRefreshHover = true;
  10248. },
  10249. /**
  10250. * Refresh hover immediately
  10251. */
  10252. refreshHoverImmediately: function () {
  10253. this._needsRefreshHover = false;
  10254. this.painter.refreshHover && this.painter.refreshHover();
  10255. },
  10256. /**
  10257. * Resize the canvas.
  10258. * Should be invoked when container size is changed
  10259. * @param {Object} [opts]
  10260. * @param {number|string} [opts.width] Can be 'auto' (the same as null/undefined)
  10261. * @param {number|string} [opts.height] Can be 'auto' (the same as null/undefined)
  10262. */
  10263. resize: function (opts) {
  10264. opts = opts || {};
  10265. this.painter.resize(opts.width, opts.height);
  10266. this.handler.resize();
  10267. },
  10268. /**
  10269. * Stop and clear all animation immediately
  10270. */
  10271. clearAnimation: function () {
  10272. this.animation.clear();
  10273. },
  10274. /**
  10275. * Get container width
  10276. */
  10277. getWidth: function () {
  10278. return this.painter.getWidth();
  10279. },
  10280. /**
  10281. * Get container height
  10282. */
  10283. getHeight: function () {
  10284. return this.painter.getHeight();
  10285. },
  10286. /**
  10287. * Export the canvas as Base64 URL
  10288. * @param {string} type
  10289. * @param {string} [backgroundColor='#fff']
  10290. * @return {string} Base64 URL
  10291. */
  10292. // toDataURL: function(type, backgroundColor) {
  10293. // return this.painter.getRenderedCanvas({
  10294. // backgroundColor: backgroundColor
  10295. // }).toDataURL(type);
  10296. // },
  10297. /**
  10298. * Converting a path to image.
  10299. * It has much better performance of drawing image rather than drawing a vector path.
  10300. * @param {module:zrender/graphic/Path} e
  10301. * @param {number} width
  10302. * @param {number} height
  10303. */
  10304. pathToImage: function (e, dpr) {
  10305. return this.painter.pathToImage(e, dpr);
  10306. },
  10307. /**
  10308. * Set default cursor
  10309. * @param {string} [cursorStyle='default'] 例如 crosshair
  10310. */
  10311. setCursorStyle: function (cursorStyle) {
  10312. this.handler.setCursorStyle(cursorStyle);
  10313. },
  10314. /**
  10315. * Find hovered element
  10316. * @param {number} x
  10317. * @param {number} y
  10318. * @return {Object} {target, topTarget}
  10319. */
  10320. findHover: function (x, y) {
  10321. return this.handler.findHover(x, y);
  10322. },
  10323. /**
  10324. * Bind event
  10325. *
  10326. * @param {string} eventName Event name
  10327. * @param {Function} eventHandler Handler function
  10328. * @param {Object} [context] Context object
  10329. */
  10330. on: function (eventName, eventHandler, context) {
  10331. this.handler.on(eventName, eventHandler, context);
  10332. },
  10333. /**
  10334. * Unbind event
  10335. * @param {string} eventName Event name
  10336. * @param {Function} [eventHandler] Handler function
  10337. */
  10338. off: function (eventName, eventHandler) {
  10339. this.handler.off(eventName, eventHandler);
  10340. },
  10341. /**
  10342. * Trigger event manually
  10343. *
  10344. * @param {string} eventName Event name
  10345. * @param {event=} event Event object
  10346. */
  10347. trigger: function (eventName, event) {
  10348. this.handler.trigger(eventName, event);
  10349. },
  10350. /**
  10351. * Clear all objects and the canvas.
  10352. */
  10353. clear: function () {
  10354. this.storage.delRoot();
  10355. this.painter.clear();
  10356. },
  10357. /**
  10358. * Dispose self.
  10359. */
  10360. dispose: function () {
  10361. this.animation.stop();
  10362. this.clear();
  10363. this.storage.dispose();
  10364. this.painter.dispose();
  10365. this.handler.dispose();
  10366. this.animation =
  10367. this.storage =
  10368. this.painter =
  10369. this.handler = null;
  10370. delInstance(this.id);
  10371. }
  10372. };
  10373. /**
  10374. * 曲线辅助模块
  10375. * @module zrender/core/curve
  10376. * @author pissang(https://www.github.com/pissang)
  10377. */
  10378. var mathPow = Math.pow;
  10379. var mathSqrt$2 = Math.sqrt;
  10380. var EPSILON$1 = 1e-8;
  10381. var EPSILON_NUMERIC = 1e-4;
  10382. var THREE_SQRT = mathSqrt$2(3);
  10383. var ONE_THIRD = 1 / 3;
  10384. // 临时变量
  10385. var _v0 = create();
  10386. var _v1 = create();
  10387. var _v2 = create();
  10388. function isAroundZero(val) {
  10389. return val > -EPSILON$1 && val < EPSILON$1;
  10390. }
  10391. function isNotAroundZero$1(val) {
  10392. return val > EPSILON$1 || val < -EPSILON$1;
  10393. }
  10394. /**
  10395. * 计算三次贝塞尔值
  10396. * @memberOf module:zrender/core/curve
  10397. * @param {number} p0
  10398. * @param {number} p1
  10399. * @param {number} p2
  10400. * @param {number} p3
  10401. * @param {number} t
  10402. * @return {number}
  10403. */
  10404. function cubicAt(p0, p1, p2, p3, t) {
  10405. var onet = 1 - t;
  10406. return onet * onet * (onet * p0 + 3 * t * p1)
  10407. + t * t * (t * p3 + 3 * onet * p2);
  10408. }
  10409. /**
  10410. * 计算三次贝塞尔导数值
  10411. * @memberOf module:zrender/core/curve
  10412. * @param {number} p0
  10413. * @param {number} p1
  10414. * @param {number} p2
  10415. * @param {number} p3
  10416. * @param {number} t
  10417. * @return {number}
  10418. */
  10419. function cubicDerivativeAt(p0, p1, p2, p3, t) {
  10420. var onet = 1 - t;
  10421. return 3 * (
  10422. ((p1 - p0) * onet + 2 * (p2 - p1) * t) * onet
  10423. + (p3 - p2) * t * t
  10424. );
  10425. }
  10426. /**
  10427. * 计算三次贝塞尔方程根,使用盛金公式
  10428. * @memberOf module:zrender/core/curve
  10429. * @param {number} p0
  10430. * @param {number} p1
  10431. * @param {number} p2
  10432. * @param {number} p3
  10433. * @param {number} val
  10434. * @param {Array.<number>} roots
  10435. * @return {number} 有效根数目
  10436. */
  10437. function cubicRootAt(p0, p1, p2, p3, val, roots) {
  10438. // Evaluate roots of cubic functions
  10439. var a = p3 + 3 * (p1 - p2) - p0;
  10440. var b = 3 * (p2 - p1 * 2 + p0);
  10441. var c = 3 * (p1 - p0);
  10442. var d = p0 - val;
  10443. var A = b * b - 3 * a * c;
  10444. var B = b * c - 9 * a * d;
  10445. var C = c * c - 3 * b * d;
  10446. var n = 0;
  10447. if (isAroundZero(A) && isAroundZero(B)) {
  10448. if (isAroundZero(b)) {
  10449. roots[0] = 0;
  10450. }
  10451. else {
  10452. var t1 = -c / b; //t1, t2, t3, b is not zero
  10453. if (t1 >= 0 && t1 <= 1) {
  10454. roots[n++] = t1;
  10455. }
  10456. }
  10457. }
  10458. else {
  10459. var disc = B * B - 4 * A * C;
  10460. if (isAroundZero(disc)) {
  10461. var K = B / A;
  10462. var t1 = -b / a + K; // t1, a is not zero
  10463. var t2 = -K / 2; // t2, t3
  10464. if (t1 >= 0 && t1 <= 1) {
  10465. roots[n++] = t1;
  10466. }
  10467. if (t2 >= 0 && t2 <= 1) {
  10468. roots[n++] = t2;
  10469. }
  10470. }
  10471. else if (disc > 0) {
  10472. var discSqrt = mathSqrt$2(disc);
  10473. var Y1 = A * b + 1.5 * a * (-B + discSqrt);
  10474. var Y2 = A * b + 1.5 * a * (-B - discSqrt);
  10475. if (Y1 < 0) {
  10476. Y1 = -mathPow(-Y1, ONE_THIRD);
  10477. }
  10478. else {
  10479. Y1 = mathPow(Y1, ONE_THIRD);
  10480. }
  10481. if (Y2 < 0) {
  10482. Y2 = -mathPow(-Y2, ONE_THIRD);
  10483. }
  10484. else {
  10485. Y2 = mathPow(Y2, ONE_THIRD);
  10486. }
  10487. var t1 = (-b - (Y1 + Y2)) / (3 * a);
  10488. if (t1 >= 0 && t1 <= 1) {
  10489. roots[n++] = t1;
  10490. }
  10491. }
  10492. else {
  10493. var T = (2 * A * b - 3 * a * B) / (2 * mathSqrt$2(A * A * A));
  10494. var theta = Math.acos(T) / 3;
  10495. var ASqrt = mathSqrt$2(A);
  10496. var tmp = Math.cos(theta);
  10497. var t1 = (-b - 2 * ASqrt * tmp) / (3 * a);
  10498. var t2 = (-b + ASqrt * (tmp + THREE_SQRT * Math.sin(theta))) / (3 * a);
  10499. var t3 = (-b + ASqrt * (tmp - THREE_SQRT * Math.sin(theta))) / (3 * a);
  10500. if (t1 >= 0 && t1 <= 1) {
  10501. roots[n++] = t1;
  10502. }
  10503. if (t2 >= 0 && t2 <= 1) {
  10504. roots[n++] = t2;
  10505. }
  10506. if (t3 >= 0 && t3 <= 1) {
  10507. roots[n++] = t3;
  10508. }
  10509. }
  10510. }
  10511. return n;
  10512. }
  10513. /**
  10514. * 计算三次贝塞尔方程极限值的位置
  10515. * @memberOf module:zrender/core/curve
  10516. * @param {number} p0
  10517. * @param {number} p1
  10518. * @param {number} p2
  10519. * @param {number} p3
  10520. * @param {Array.<number>} extrema
  10521. * @return {number} 有效数目
  10522. */
  10523. function cubicExtrema(p0, p1, p2, p3, extrema) {
  10524. var b = 6 * p2 - 12 * p1 + 6 * p0;
  10525. var a = 9 * p1 + 3 * p3 - 3 * p0 - 9 * p2;
  10526. var c = 3 * p1 - 3 * p0;
  10527. var n = 0;
  10528. if (isAroundZero(a)) {
  10529. if (isNotAroundZero$1(b)) {
  10530. var t1 = -c / b;
  10531. if (t1 >= 0 && t1 <= 1) {
  10532. extrema[n++] = t1;
  10533. }
  10534. }
  10535. }
  10536. else {
  10537. var disc = b * b - 4 * a * c;
  10538. if (isAroundZero(disc)) {
  10539. extrema[0] = -b / (2 * a);
  10540. }
  10541. else if (disc > 0) {
  10542. var discSqrt = mathSqrt$2(disc);
  10543. var t1 = (-b + discSqrt) / (2 * a);
  10544. var t2 = (-b - discSqrt) / (2 * a);
  10545. if (t1 >= 0 && t1 <= 1) {
  10546. extrema[n++] = t1;
  10547. }
  10548. if (t2 >= 0 && t2 <= 1) {
  10549. extrema[n++] = t2;
  10550. }
  10551. }
  10552. }
  10553. return n;
  10554. }
  10555. /**
  10556. * 细分三次贝塞尔曲线
  10557. * @memberOf module:zrender/core/curve
  10558. * @param {number} p0
  10559. * @param {number} p1
  10560. * @param {number} p2
  10561. * @param {number} p3
  10562. * @param {number} t
  10563. * @param {Array.<number>} out
  10564. */
  10565. function cubicSubdivide(p0, p1, p2, p3, t, out) {
  10566. var p01 = (p1 - p0) * t + p0;
  10567. var p12 = (p2 - p1) * t + p1;
  10568. var p23 = (p3 - p2) * t + p2;
  10569. var p012 = (p12 - p01) * t + p01;
  10570. var p123 = (p23 - p12) * t + p12;
  10571. var p0123 = (p123 - p012) * t + p012;
  10572. // Seg0
  10573. out[0] = p0;
  10574. out[1] = p01;
  10575. out[2] = p012;
  10576. out[3] = p0123;
  10577. // Seg1
  10578. out[4] = p0123;
  10579. out[5] = p123;
  10580. out[6] = p23;
  10581. out[7] = p3;
  10582. }
  10583. /**
  10584. * 投射点到三次贝塞尔曲线上,返回投射距离。
  10585. * 投射点有可能会有一个或者多个,这里只返回其中距离最短的一个。
  10586. * @param {number} x0
  10587. * @param {number} y0
  10588. * @param {number} x1
  10589. * @param {number} y1
  10590. * @param {number} x2
  10591. * @param {number} y2
  10592. * @param {number} x3
  10593. * @param {number} y3
  10594. * @param {number} x
  10595. * @param {number} y
  10596. * @param {Array.<number>} [out] 投射点
  10597. * @return {number}
  10598. */
  10599. function cubicProjectPoint(
  10600. x0, y0, x1, y1, x2, y2, x3, y3,
  10601. x, y, out
  10602. ) {
  10603. // http://pomax.github.io/bezierinfo/#projections
  10604. var t;
  10605. var interval = 0.005;
  10606. var d = Infinity;
  10607. var prev;
  10608. var next;
  10609. var d1;
  10610. var d2;
  10611. _v0[0] = x;
  10612. _v0[1] = y;
  10613. // 先粗略估计一下可能的最小距离的 t 值
  10614. // PENDING
  10615. for (var _t = 0; _t < 1; _t += 0.05) {
  10616. _v1[0] = cubicAt(x0, x1, x2, x3, _t);
  10617. _v1[1] = cubicAt(y0, y1, y2, y3, _t);
  10618. d1 = distSquare(_v0, _v1);
  10619. if (d1 < d) {
  10620. t = _t;
  10621. d = d1;
  10622. }
  10623. }
  10624. d = Infinity;
  10625. // At most 32 iteration
  10626. for (var i = 0; i < 32; i++) {
  10627. if (interval < EPSILON_NUMERIC) {
  10628. break;
  10629. }
  10630. prev = t - interval;
  10631. next = t + interval;
  10632. // t - interval
  10633. _v1[0] = cubicAt(x0, x1, x2, x3, prev);
  10634. _v1[1] = cubicAt(y0, y1, y2, y3, prev);
  10635. d1 = distSquare(_v1, _v0);
  10636. if (prev >= 0 && d1 < d) {
  10637. t = prev;
  10638. d = d1;
  10639. }
  10640. else {
  10641. // t + interval
  10642. _v2[0] = cubicAt(x0, x1, x2, x3, next);
  10643. _v2[1] = cubicAt(y0, y1, y2, y3, next);
  10644. d2 = distSquare(_v2, _v0);
  10645. if (next <= 1 && d2 < d) {
  10646. t = next;
  10647. d = d2;
  10648. }
  10649. else {
  10650. interval *= 0.5;
  10651. }
  10652. }
  10653. }
  10654. // t
  10655. if (out) {
  10656. out[0] = cubicAt(x0, x1, x2, x3, t);
  10657. out[1] = cubicAt(y0, y1, y2, y3, t);
  10658. }
  10659. // console.log(interval, i);
  10660. return mathSqrt$2(d);
  10661. }
  10662. /**
  10663. * 计算二次方贝塞尔值
  10664. * @param {number} p0
  10665. * @param {number} p1
  10666. * @param {number} p2
  10667. * @param {number} t
  10668. * @return {number}
  10669. */
  10670. function quadraticAt(p0, p1, p2, t) {
  10671. var onet = 1 - t;
  10672. return onet * (onet * p0 + 2 * t * p1) + t * t * p2;
  10673. }
  10674. /**
  10675. * 计算二次方贝塞尔导数值
  10676. * @param {number} p0
  10677. * @param {number} p1
  10678. * @param {number} p2
  10679. * @param {number} t
  10680. * @return {number}
  10681. */
  10682. function quadraticDerivativeAt(p0, p1, p2, t) {
  10683. return 2 * ((1 - t) * (p1 - p0) + t * (p2 - p1));
  10684. }
  10685. /**
  10686. * 计算二次方贝塞尔方程根
  10687. * @param {number} p0
  10688. * @param {number} p1
  10689. * @param {number} p2
  10690. * @param {number} t
  10691. * @param {Array.<number>} roots
  10692. * @return {number} 有效根数目
  10693. */
  10694. function quadraticRootAt(p0, p1, p2, val, roots) {
  10695. var a = p0 - 2 * p1 + p2;
  10696. var b = 2 * (p1 - p0);
  10697. var c = p0 - val;
  10698. var n = 0;
  10699. if (isAroundZero(a)) {
  10700. if (isNotAroundZero$1(b)) {
  10701. var t1 = -c / b;
  10702. if (t1 >= 0 && t1 <= 1) {
  10703. roots[n++] = t1;
  10704. }
  10705. }
  10706. }
  10707. else {
  10708. var disc = b * b - 4 * a * c;
  10709. if (isAroundZero(disc)) {
  10710. var t1 = -b / (2 * a);
  10711. if (t1 >= 0 && t1 <= 1) {
  10712. roots[n++] = t1;
  10713. }
  10714. }
  10715. else if (disc > 0) {
  10716. var discSqrt = mathSqrt$2(disc);
  10717. var t1 = (-b + discSqrt) / (2 * a);
  10718. var t2 = (-b - discSqrt) / (2 * a);
  10719. if (t1 >= 0 && t1 <= 1) {
  10720. roots[n++] = t1;
  10721. }
  10722. if (t2 >= 0 && t2 <= 1) {
  10723. roots[n++] = t2;
  10724. }
  10725. }
  10726. }
  10727. return n;
  10728. }
  10729. /**
  10730. * 计算二次贝塞尔方程极限值
  10731. * @memberOf module:zrender/core/curve
  10732. * @param {number} p0
  10733. * @param {number} p1
  10734. * @param {number} p2
  10735. * @return {number}
  10736. */
  10737. function quadraticExtremum(p0, p1, p2) {
  10738. var divider = p0 + p2 - 2 * p1;
  10739. if (divider === 0) {
  10740. // p1 is center of p0 and p2
  10741. return 0.5;
  10742. }
  10743. else {
  10744. return (p0 - p1) / divider;
  10745. }
  10746. }
  10747. /**
  10748. * 细分二次贝塞尔曲线
  10749. * @memberOf module:zrender/core/curve
  10750. * @param {number} p0
  10751. * @param {number} p1
  10752. * @param {number} p2
  10753. * @param {number} t
  10754. * @param {Array.<number>} out
  10755. */
  10756. function quadraticSubdivide(p0, p1, p2, t, out) {
  10757. var p01 = (p1 - p0) * t + p0;
  10758. var p12 = (p2 - p1) * t + p1;
  10759. var p012 = (p12 - p01) * t + p01;
  10760. // Seg0
  10761. out[0] = p0;
  10762. out[1] = p01;
  10763. out[2] = p012;
  10764. // Seg1
  10765. out[3] = p012;
  10766. out[4] = p12;
  10767. out[5] = p2;
  10768. }
  10769. /**
  10770. * 投射点到二次贝塞尔曲线上,返回投射距离。
  10771. * 投射点有可能会有一个或者多个,这里只返回其中距离最短的一个。
  10772. * @param {number} x0
  10773. * @param {number} y0
  10774. * @param {number} x1
  10775. * @param {number} y1
  10776. * @param {number} x2
  10777. * @param {number} y2
  10778. * @param {number} x
  10779. * @param {number} y
  10780. * @param {Array.<number>} out 投射点
  10781. * @return {number}
  10782. */
  10783. function quadraticProjectPoint(
  10784. x0, y0, x1, y1, x2, y2,
  10785. x, y, out
  10786. ) {
  10787. // http://pomax.github.io/bezierinfo/#projections
  10788. var t;
  10789. var interval = 0.005;
  10790. var d = Infinity;
  10791. _v0[0] = x;
  10792. _v0[1] = y;
  10793. // 先粗略估计一下可能的最小距离的 t 值
  10794. // PENDING
  10795. for (var _t = 0; _t < 1; _t += 0.05) {
  10796. _v1[0] = quadraticAt(x0, x1, x2, _t);
  10797. _v1[1] = quadraticAt(y0, y1, y2, _t);
  10798. var d1 = distSquare(_v0, _v1);
  10799. if (d1 < d) {
  10800. t = _t;
  10801. d = d1;
  10802. }
  10803. }
  10804. d = Infinity;
  10805. // At most 32 iteration
  10806. for (var i = 0; i < 32; i++) {
  10807. if (interval < EPSILON_NUMERIC) {
  10808. break;
  10809. }
  10810. var prev = t - interval;
  10811. var next = t + interval;
  10812. // t - interval
  10813. _v1[0] = quadraticAt(x0, x1, x2, prev);
  10814. _v1[1] = quadraticAt(y0, y1, y2, prev);
  10815. var d1 = distSquare(_v1, _v0);
  10816. if (prev >= 0 && d1 < d) {
  10817. t = prev;
  10818. d = d1;
  10819. }
  10820. else {
  10821. // t + interval
  10822. _v2[0] = quadraticAt(x0, x1, x2, next);
  10823. _v2[1] = quadraticAt(y0, y1, y2, next);
  10824. var d2 = distSquare(_v2, _v0);
  10825. if (next <= 1 && d2 < d) {
  10826. t = next;
  10827. d = d2;
  10828. }
  10829. else {
  10830. interval *= 0.5;
  10831. }
  10832. }
  10833. }
  10834. // t
  10835. if (out) {
  10836. out[0] = quadraticAt(x0, x1, x2, t);
  10837. out[1] = quadraticAt(y0, y1, y2, t);
  10838. }
  10839. // console.log(interval, i);
  10840. return mathSqrt$2(d);
  10841. }
  10842. /**
  10843. * @author Yi Shen(https://github.com/pissang)
  10844. */
  10845. var mathMin$2 = Math.min;
  10846. var mathMax$2 = Math.max;
  10847. var mathSin$2 = Math.sin;
  10848. var mathCos$2 = Math.cos;
  10849. var PI2 = Math.PI * 2;
  10850. var start = create();
  10851. var end = create();
  10852. var extremity = create();
  10853. /**
  10854. * 从顶点数组中计算出最小包围盒,写入`min`和`max`中
  10855. * @module zrender/core/bbox
  10856. * @param {Array<Object>} points 顶点数组
  10857. * @param {number} min
  10858. * @param {number} max
  10859. */
  10860. /**
  10861. * @memberOf module:zrender/core/bbox
  10862. * @param {number} x0
  10863. * @param {number} y0
  10864. * @param {number} x1
  10865. * @param {number} y1
  10866. * @param {Array.<number>} min
  10867. * @param {Array.<number>} max
  10868. */
  10869. function fromLine(x0, y0, x1, y1, min$$1, max$$1) {
  10870. min$$1[0] = mathMin$2(x0, x1);
  10871. min$$1[1] = mathMin$2(y0, y1);
  10872. max$$1[0] = mathMax$2(x0, x1);
  10873. max$$1[1] = mathMax$2(y0, y1);
  10874. }
  10875. var xDim = [];
  10876. var yDim = [];
  10877. /**
  10878. * 从三阶贝塞尔曲线(p0, p1, p2, p3)中计算出最小包围盒,写入`min`和`max`中
  10879. * @memberOf module:zrender/core/bbox
  10880. * @param {number} x0
  10881. * @param {number} y0
  10882. * @param {number} x1
  10883. * @param {number} y1
  10884. * @param {number} x2
  10885. * @param {number} y2
  10886. * @param {number} x3
  10887. * @param {number} y3
  10888. * @param {Array.<number>} min
  10889. * @param {Array.<number>} max
  10890. */
  10891. function fromCubic(
  10892. x0, y0, x1, y1, x2, y2, x3, y3, min$$1, max$$1
  10893. ) {
  10894. var cubicExtrema$$1 = cubicExtrema;
  10895. var cubicAt$$1 = cubicAt;
  10896. var i;
  10897. var n = cubicExtrema$$1(x0, x1, x2, x3, xDim);
  10898. min$$1[0] = Infinity;
  10899. min$$1[1] = Infinity;
  10900. max$$1[0] = -Infinity;
  10901. max$$1[1] = -Infinity;
  10902. for (i = 0; i < n; i++) {
  10903. var x = cubicAt$$1(x0, x1, x2, x3, xDim[i]);
  10904. min$$1[0] = mathMin$2(x, min$$1[0]);
  10905. max$$1[0] = mathMax$2(x, max$$1[0]);
  10906. }
  10907. n = cubicExtrema$$1(y0, y1, y2, y3, yDim);
  10908. for (i = 0; i < n; i++) {
  10909. var y = cubicAt$$1(y0, y1, y2, y3, yDim[i]);
  10910. min$$1[1] = mathMin$2(y, min$$1[1]);
  10911. max$$1[1] = mathMax$2(y, max$$1[1]);
  10912. }
  10913. min$$1[0] = mathMin$2(x0, min$$1[0]);
  10914. max$$1[0] = mathMax$2(x0, max$$1[0]);
  10915. min$$1[0] = mathMin$2(x3, min$$1[0]);
  10916. max$$1[0] = mathMax$2(x3, max$$1[0]);
  10917. min$$1[1] = mathMin$2(y0, min$$1[1]);
  10918. max$$1[1] = mathMax$2(y0, max$$1[1]);
  10919. min$$1[1] = mathMin$2(y3, min$$1[1]);
  10920. max$$1[1] = mathMax$2(y3, max$$1[1]);
  10921. }
  10922. /**
  10923. * 从二阶贝塞尔曲线(p0, p1, p2)中计算出最小包围盒,写入`min`和`max`中
  10924. * @memberOf module:zrender/core/bbox
  10925. * @param {number} x0
  10926. * @param {number} y0
  10927. * @param {number} x1
  10928. * @param {number} y1
  10929. * @param {number} x2
  10930. * @param {number} y2
  10931. * @param {Array.<number>} min
  10932. * @param {Array.<number>} max
  10933. */
  10934. function fromQuadratic(x0, y0, x1, y1, x2, y2, min$$1, max$$1) {
  10935. var quadraticExtremum$$1 = quadraticExtremum;
  10936. var quadraticAt$$1 = quadraticAt;
  10937. // Find extremities, where derivative in x dim or y dim is zero
  10938. var tx =
  10939. mathMax$2(
  10940. mathMin$2(quadraticExtremum$$1(x0, x1, x2), 1), 0
  10941. );
  10942. var ty =
  10943. mathMax$2(
  10944. mathMin$2(quadraticExtremum$$1(y0, y1, y2), 1), 0
  10945. );
  10946. var x = quadraticAt$$1(x0, x1, x2, tx);
  10947. var y = quadraticAt$$1(y0, y1, y2, ty);
  10948. min$$1[0] = mathMin$2(x0, x2, x);
  10949. min$$1[1] = mathMin$2(y0, y2, y);
  10950. max$$1[0] = mathMax$2(x0, x2, x);
  10951. max$$1[1] = mathMax$2(y0, y2, y);
  10952. }
  10953. /**
  10954. * 从圆弧中计算出最小包围盒,写入`min`和`max`中
  10955. * @method
  10956. * @memberOf module:zrender/core/bbox
  10957. * @param {number} x
  10958. * @param {number} y
  10959. * @param {number} rx
  10960. * @param {number} ry
  10961. * @param {number} startAngle
  10962. * @param {number} endAngle
  10963. * @param {number} anticlockwise
  10964. * @param {Array.<number>} min
  10965. * @param {Array.<number>} max
  10966. */
  10967. function fromArc(
  10968. x, y, rx, ry, startAngle, endAngle, anticlockwise, min$$1, max$$1
  10969. ) {
  10970. var vec2Min = min;
  10971. var vec2Max = max;
  10972. var diff = Math.abs(startAngle - endAngle);
  10973. if (diff % PI2 < 1e-4 && diff > 1e-4) {
  10974. // Is a circle
  10975. min$$1[0] = x - rx;
  10976. min$$1[1] = y - ry;
  10977. max$$1[0] = x + rx;
  10978. max$$1[1] = y + ry;
  10979. return;
  10980. }
  10981. start[0] = mathCos$2(startAngle) * rx + x;
  10982. start[1] = mathSin$2(startAngle) * ry + y;
  10983. end[0] = mathCos$2(endAngle) * rx + x;
  10984. end[1] = mathSin$2(endAngle) * ry + y;
  10985. vec2Min(min$$1, start, end);
  10986. vec2Max(max$$1, start, end);
  10987. // Thresh to [0, Math.PI * 2]
  10988. startAngle = startAngle % (PI2);
  10989. if (startAngle < 0) {
  10990. startAngle = startAngle + PI2;
  10991. }
  10992. endAngle = endAngle % (PI2);
  10993. if (endAngle < 0) {
  10994. endAngle = endAngle + PI2;
  10995. }
  10996. if (startAngle > endAngle && !anticlockwise) {
  10997. endAngle += PI2;
  10998. }
  10999. else if (startAngle < endAngle && anticlockwise) {
  11000. startAngle += PI2;
  11001. }
  11002. if (anticlockwise) {
  11003. var tmp = endAngle;
  11004. endAngle = startAngle;
  11005. startAngle = tmp;
  11006. }
  11007. // var number = 0;
  11008. // var step = (anticlockwise ? -Math.PI : Math.PI) / 2;
  11009. for (var angle = 0; angle < endAngle; angle += Math.PI / 2) {
  11010. if (angle > startAngle) {
  11011. extremity[0] = mathCos$2(angle) * rx + x;
  11012. extremity[1] = mathSin$2(angle) * ry + y;
  11013. vec2Min(min$$1, extremity, min$$1);
  11014. vec2Max(max$$1, extremity, max$$1);
  11015. }
  11016. }
  11017. }
  11018. /**
  11019. * Path 代理,可以在`buildPath`中用于替代`ctx`, 会保存每个path操作的命令到pathCommands属性中
  11020. * 可以用于 isInsidePath 判断以及获取boundingRect
  11021. *
  11022. * @module zrender/core/PathProxy
  11023. * @author Yi Shen (http://www.github.com/pissang)
  11024. */
  11025. // TODO getTotalLength, getPointAtLength
  11026. /* global Float32Array */
  11027. var CMD = {
  11028. M: 1,
  11029. L: 2,
  11030. C: 3,
  11031. Q: 4,
  11032. A: 5,
  11033. Z: 6,
  11034. // Rect
  11035. R: 7
  11036. };
  11037. // var CMD_MEM_SIZE = {
  11038. // M: 3,
  11039. // L: 3,
  11040. // C: 7,
  11041. // Q: 5,
  11042. // A: 9,
  11043. // R: 5,
  11044. // Z: 1
  11045. // };
  11046. var min$1 = [];
  11047. var max$1 = [];
  11048. var min2 = [];
  11049. var max2 = [];
  11050. var mathMin$1 = Math.min;
  11051. var mathMax$1 = Math.max;
  11052. var mathCos$1 = Math.cos;
  11053. var mathSin$1 = Math.sin;
  11054. var mathSqrt$1 = Math.sqrt;
  11055. var mathAbs = Math.abs;
  11056. var hasTypedArray = typeof Float32Array !== 'undefined';
  11057. /**
  11058. * @alias module:zrender/core/PathProxy
  11059. * @constructor
  11060. */
  11061. var PathProxy = function (notSaveData) {
  11062. this._saveData = !(notSaveData || false);
  11063. if (this._saveData) {
  11064. /**
  11065. * Path data. Stored as flat array
  11066. * @type {Array.<Object>}
  11067. */
  11068. this.data = [];
  11069. }
  11070. this._ctx = null;
  11071. };
  11072. /**
  11073. * 快速计算Path包围盒(并不是最小包围盒)
  11074. * @return {Object}
  11075. */
  11076. PathProxy.prototype = {
  11077. constructor: PathProxy,
  11078. _xi: 0,
  11079. _yi: 0,
  11080. _x0: 0,
  11081. _y0: 0,
  11082. // Unit x, Unit y. Provide for avoiding drawing that too short line segment
  11083. _ux: 0,
  11084. _uy: 0,
  11085. _len: 0,
  11086. _lineDash: null,
  11087. _dashOffset: 0,
  11088. _dashIdx: 0,
  11089. _dashSum: 0,
  11090. /**
  11091. * @readOnly
  11092. */
  11093. setScale: function (sx, sy, segmentIgnoreThreshold) {
  11094. // Compat. Previously there is no segmentIgnoreThreshold.
  11095. segmentIgnoreThreshold = segmentIgnoreThreshold || 0;
  11096. this._ux = mathAbs(segmentIgnoreThreshold / devicePixelRatio / sx) || 0;
  11097. this._uy = mathAbs(segmentIgnoreThreshold / devicePixelRatio / sy) || 0;
  11098. },
  11099. getContext: function () {
  11100. return this._ctx;
  11101. },
  11102. /**
  11103. * @param {CanvasRenderingContext2D} ctx
  11104. * @return {module:zrender/core/PathProxy}
  11105. */
  11106. beginPath: function (ctx) {
  11107. this._ctx = ctx;
  11108. ctx && ctx.beginPath();
  11109. ctx && (this.dpr = ctx.dpr);
  11110. // Reset
  11111. if (this._saveData) {
  11112. this._len = 0;
  11113. }
  11114. if (this._lineDash) {
  11115. this._lineDash = null;
  11116. this._dashOffset = 0;
  11117. }
  11118. return this;
  11119. },
  11120. /**
  11121. * @param {number} x
  11122. * @param {number} y
  11123. * @return {module:zrender/core/PathProxy}
  11124. */
  11125. moveTo: function (x, y) {
  11126. this.addData(CMD.M, x, y);
  11127. this._ctx && this._ctx.moveTo(x, y);
  11128. // x0, y0, xi, yi 是记录在 _dashedXXXXTo 方法中使用
  11129. // xi, yi 记录当前点, x0, y0 在 closePath 的时候回到起始点。
  11130. // 有可能在 beginPath 之后直接调用 lineTo,这时候 x0, y0 需要
  11131. // 在 lineTo 方法中记录,这里先不考虑这种情况,dashed line 也只在 IE10- 中不支持
  11132. this._x0 = x;
  11133. this._y0 = y;
  11134. this._xi = x;
  11135. this._yi = y;
  11136. return this;
  11137. },
  11138. /**
  11139. * @param {number} x
  11140. * @param {number} y
  11141. * @return {module:zrender/core/PathProxy}
  11142. */
  11143. lineTo: function (x, y) {
  11144. var exceedUnit = mathAbs(x - this._xi) > this._ux
  11145. || mathAbs(y - this._yi) > this._uy
  11146. // Force draw the first segment
  11147. || this._len < 5;
  11148. this.addData(CMD.L, x, y);
  11149. if (this._ctx && exceedUnit) {
  11150. this._needsDash() ? this._dashedLineTo(x, y)
  11151. : this._ctx.lineTo(x, y);
  11152. }
  11153. if (exceedUnit) {
  11154. this._xi = x;
  11155. this._yi = y;
  11156. }
  11157. return this;
  11158. },
  11159. /**
  11160. * @param {number} x1
  11161. * @param {number} y1
  11162. * @param {number} x2
  11163. * @param {number} y2
  11164. * @param {number} x3
  11165. * @param {number} y3
  11166. * @return {module:zrender/core/PathProxy}
  11167. */
  11168. bezierCurveTo: function (x1, y1, x2, y2, x3, y3) {
  11169. this.addData(CMD.C, x1, y1, x2, y2, x3, y3);
  11170. if (this._ctx) {
  11171. this._needsDash() ? this._dashedBezierTo(x1, y1, x2, y2, x3, y3)
  11172. : this._ctx.bezierCurveTo(x1, y1, x2, y2, x3, y3);
  11173. }
  11174. this._xi = x3;
  11175. this._yi = y3;
  11176. return this;
  11177. },
  11178. /**
  11179. * @param {number} x1
  11180. * @param {number} y1
  11181. * @param {number} x2
  11182. * @param {number} y2
  11183. * @return {module:zrender/core/PathProxy}
  11184. */
  11185. quadraticCurveTo: function (x1, y1, x2, y2) {
  11186. this.addData(CMD.Q, x1, y1, x2, y2);
  11187. if (this._ctx) {
  11188. this._needsDash() ? this._dashedQuadraticTo(x1, y1, x2, y2)
  11189. : this._ctx.quadraticCurveTo(x1, y1, x2, y2);
  11190. }
  11191. this._xi = x2;
  11192. this._yi = y2;
  11193. return this;
  11194. },
  11195. /**
  11196. * @param {number} cx
  11197. * @param {number} cy
  11198. * @param {number} r
  11199. * @param {number} startAngle
  11200. * @param {number} endAngle
  11201. * @param {boolean} anticlockwise
  11202. * @return {module:zrender/core/PathProxy}
  11203. */
  11204. arc: function (cx, cy, r, startAngle, endAngle, anticlockwise) {
  11205. this.addData(
  11206. CMD.A, cx, cy, r, r, startAngle, endAngle - startAngle, 0, anticlockwise ? 0 : 1
  11207. );
  11208. this._ctx && this._ctx.arc(cx, cy, r, startAngle, endAngle, anticlockwise);
  11209. this._xi = mathCos$1(endAngle) * r + cx;
  11210. this._yi = mathSin$1(endAngle) * r + cy;
  11211. return this;
  11212. },
  11213. // TODO
  11214. arcTo: function (x1, y1, x2, y2, radius) {
  11215. if (this._ctx) {
  11216. this._ctx.arcTo(x1, y1, x2, y2, radius);
  11217. }
  11218. return this;
  11219. },
  11220. // TODO
  11221. rect: function (x, y, w, h) {
  11222. this._ctx && this._ctx.rect(x, y, w, h);
  11223. this.addData(CMD.R, x, y, w, h);
  11224. return this;
  11225. },
  11226. /**
  11227. * @return {module:zrender/core/PathProxy}
  11228. */
  11229. closePath: function () {
  11230. this.addData(CMD.Z);
  11231. var ctx = this._ctx;
  11232. var x0 = this._x0;
  11233. var y0 = this._y0;
  11234. if (ctx) {
  11235. this._needsDash() && this._dashedLineTo(x0, y0);
  11236. ctx.closePath();
  11237. }
  11238. this._xi = x0;
  11239. this._yi = y0;
  11240. return this;
  11241. },
  11242. /**
  11243. * Context 从外部传入,因为有可能是 rebuildPath 完之后再 fill。
  11244. * stroke 同样
  11245. * @param {CanvasRenderingContext2D} ctx
  11246. * @return {module:zrender/core/PathProxy}
  11247. */
  11248. fill: function (ctx) {
  11249. ctx && ctx.fill();
  11250. this.toStatic();
  11251. },
  11252. /**
  11253. * @param {CanvasRenderingContext2D} ctx
  11254. * @return {module:zrender/core/PathProxy}
  11255. */
  11256. stroke: function (ctx) {
  11257. ctx && ctx.stroke();
  11258. this.toStatic();
  11259. },
  11260. /**
  11261. * 必须在其它绘制命令前调用
  11262. * Must be invoked before all other path drawing methods
  11263. * @return {module:zrender/core/PathProxy}
  11264. */
  11265. setLineDash: function (lineDash) {
  11266. if (lineDash instanceof Array) {
  11267. this._lineDash = lineDash;
  11268. this._dashIdx = 0;
  11269. var lineDashSum = 0;
  11270. for (var i = 0; i < lineDash.length; i++) {
  11271. lineDashSum += lineDash[i];
  11272. }
  11273. this._dashSum = lineDashSum;
  11274. }
  11275. return this;
  11276. },
  11277. /**
  11278. * 必须在其它绘制命令前调用
  11279. * Must be invoked before all other path drawing methods
  11280. * @return {module:zrender/core/PathProxy}
  11281. */
  11282. setLineDashOffset: function (offset) {
  11283. this._dashOffset = offset;
  11284. return this;
  11285. },
  11286. /**
  11287. *
  11288. * @return {boolean}
  11289. */
  11290. len: function () {
  11291. return this._len;
  11292. },
  11293. /**
  11294. * 直接设置 Path 数据
  11295. */
  11296. setData: function (data) {
  11297. var len$$1 = data.length;
  11298. if (!(this.data && this.data.length === len$$1) && hasTypedArray) {
  11299. this.data = new Float32Array(len$$1);
  11300. }
  11301. for (var i = 0; i < len$$1; i++) {
  11302. this.data[i] = data[i];
  11303. }
  11304. this._len = len$$1;
  11305. },
  11306. /**
  11307. * 添加子路径
  11308. * @param {module:zrender/core/PathProxy|Array.<module:zrender/core/PathProxy>} path
  11309. */
  11310. appendPath: function (path) {
  11311. if (!(path instanceof Array)) {
  11312. path = [path];
  11313. }
  11314. var len$$1 = path.length;
  11315. var appendSize = 0;
  11316. var offset = this._len;
  11317. for (var i = 0; i < len$$1; i++) {
  11318. appendSize += path[i].len();
  11319. }
  11320. if (hasTypedArray && (this.data instanceof Float32Array)) {
  11321. this.data = new Float32Array(offset + appendSize);
  11322. }
  11323. for (var i = 0; i < len$$1; i++) {
  11324. var appendPathData = path[i].data;
  11325. for (var k = 0; k < appendPathData.length; k++) {
  11326. this.data[offset++] = appendPathData[k];
  11327. }
  11328. }
  11329. this._len = offset;
  11330. },
  11331. /**
  11332. * 填充 Path 数据。
  11333. * 尽量复用而不申明新的数组。大部分图形重绘的指令数据长度都是不变的。
  11334. */
  11335. addData: function (cmd) {
  11336. if (!this._saveData) {
  11337. return;
  11338. }
  11339. var data = this.data;
  11340. if (this._len + arguments.length > data.length) {
  11341. // 因为之前的数组已经转换成静态的 Float32Array
  11342. // 所以不够用时需要扩展一个新的动态数组
  11343. this._expandData();
  11344. data = this.data;
  11345. }
  11346. for (var i = 0; i < arguments.length; i++) {
  11347. data[this._len++] = arguments[i];
  11348. }
  11349. this._prevCmd = cmd;
  11350. },
  11351. _expandData: function () {
  11352. // Only if data is Float32Array
  11353. if (!(this.data instanceof Array)) {
  11354. var newData = [];
  11355. for (var i = 0; i < this._len; i++) {
  11356. newData[i] = this.data[i];
  11357. }
  11358. this.data = newData;
  11359. }
  11360. },
  11361. /**
  11362. * If needs js implemented dashed line
  11363. * @return {boolean}
  11364. * @private
  11365. */
  11366. _needsDash: function () {
  11367. return this._lineDash;
  11368. },
  11369. _dashedLineTo: function (x1, y1) {
  11370. var dashSum = this._dashSum;
  11371. var offset = this._dashOffset;
  11372. var lineDash = this._lineDash;
  11373. var ctx = this._ctx;
  11374. var x0 = this._xi;
  11375. var y0 = this._yi;
  11376. var dx = x1 - x0;
  11377. var dy = y1 - y0;
  11378. var dist$$1 = mathSqrt$1(dx * dx + dy * dy);
  11379. var x = x0;
  11380. var y = y0;
  11381. var dash;
  11382. var nDash = lineDash.length;
  11383. var idx;
  11384. dx /= dist$$1;
  11385. dy /= dist$$1;
  11386. if (offset < 0) {
  11387. // Convert to positive offset
  11388. offset = dashSum + offset;
  11389. }
  11390. offset %= dashSum;
  11391. x -= offset * dx;
  11392. y -= offset * dy;
  11393. while ((dx > 0 && x <= x1) || (dx < 0 && x >= x1)
  11394. || (dx === 0 && ((dy > 0 && y <= y1) || (dy < 0 && y >= y1)))) {
  11395. idx = this._dashIdx;
  11396. dash = lineDash[idx];
  11397. x += dx * dash;
  11398. y += dy * dash;
  11399. this._dashIdx = (idx + 1) % nDash;
  11400. // Skip positive offset
  11401. if ((dx > 0 && x < x0) || (dx < 0 && x > x0) || (dy > 0 && y < y0) || (dy < 0 && y > y0)) {
  11402. continue;
  11403. }
  11404. ctx[idx % 2 ? 'moveTo' : 'lineTo'](
  11405. dx >= 0 ? mathMin$1(x, x1) : mathMax$1(x, x1),
  11406. dy >= 0 ? mathMin$1(y, y1) : mathMax$1(y, y1)
  11407. );
  11408. }
  11409. // Offset for next lineTo
  11410. dx = x - x1;
  11411. dy = y - y1;
  11412. this._dashOffset = -mathSqrt$1(dx * dx + dy * dy);
  11413. },
  11414. // Not accurate dashed line to
  11415. _dashedBezierTo: function (x1, y1, x2, y2, x3, y3) {
  11416. var dashSum = this._dashSum;
  11417. var offset = this._dashOffset;
  11418. var lineDash = this._lineDash;
  11419. var ctx = this._ctx;
  11420. var x0 = this._xi;
  11421. var y0 = this._yi;
  11422. var t;
  11423. var dx;
  11424. var dy;
  11425. var cubicAt$$1 = cubicAt;
  11426. var bezierLen = 0;
  11427. var idx = this._dashIdx;
  11428. var nDash = lineDash.length;
  11429. var x;
  11430. var y;
  11431. var tmpLen = 0;
  11432. if (offset < 0) {
  11433. // Convert to positive offset
  11434. offset = dashSum + offset;
  11435. }
  11436. offset %= dashSum;
  11437. // Bezier approx length
  11438. for (t = 0; t < 1; t += 0.1) {
  11439. dx = cubicAt$$1(x0, x1, x2, x3, t + 0.1)
  11440. - cubicAt$$1(x0, x1, x2, x3, t);
  11441. dy = cubicAt$$1(y0, y1, y2, y3, t + 0.1)
  11442. - cubicAt$$1(y0, y1, y2, y3, t);
  11443. bezierLen += mathSqrt$1(dx * dx + dy * dy);
  11444. }
  11445. // Find idx after add offset
  11446. for (; idx < nDash; idx++) {
  11447. tmpLen += lineDash[idx];
  11448. if (tmpLen > offset) {
  11449. break;
  11450. }
  11451. }
  11452. t = (tmpLen - offset) / bezierLen;
  11453. while (t <= 1) {
  11454. x = cubicAt$$1(x0, x1, x2, x3, t);
  11455. y = cubicAt$$1(y0, y1, y2, y3, t);
  11456. // Use line to approximate dashed bezier
  11457. // Bad result if dash is long
  11458. idx % 2 ? ctx.moveTo(x, y)
  11459. : ctx.lineTo(x, y);
  11460. t += lineDash[idx] / bezierLen;
  11461. idx = (idx + 1) % nDash;
  11462. }
  11463. // Finish the last segment and calculate the new offset
  11464. (idx % 2 !== 0) && ctx.lineTo(x3, y3);
  11465. dx = x3 - x;
  11466. dy = y3 - y;
  11467. this._dashOffset = -mathSqrt$1(dx * dx + dy * dy);
  11468. },
  11469. _dashedQuadraticTo: function (x1, y1, x2, y2) {
  11470. // Convert quadratic to cubic using degree elevation
  11471. var x3 = x2;
  11472. var y3 = y2;
  11473. x2 = (x2 + 2 * x1) / 3;
  11474. y2 = (y2 + 2 * y1) / 3;
  11475. x1 = (this._xi + 2 * x1) / 3;
  11476. y1 = (this._yi + 2 * y1) / 3;
  11477. this._dashedBezierTo(x1, y1, x2, y2, x3, y3);
  11478. },
  11479. /**
  11480. * 转成静态的 Float32Array 减少堆内存占用
  11481. * Convert dynamic array to static Float32Array
  11482. */
  11483. toStatic: function () {
  11484. var data = this.data;
  11485. if (data instanceof Array) {
  11486. data.length = this._len;
  11487. if (hasTypedArray) {
  11488. this.data = new Float32Array(data);
  11489. }
  11490. }
  11491. },
  11492. /**
  11493. * @return {module:zrender/core/BoundingRect}
  11494. */
  11495. getBoundingRect: function () {
  11496. min$1[0] = min$1[1] = min2[0] = min2[1] = Number.MAX_VALUE;
  11497. max$1[0] = max$1[1] = max2[0] = max2[1] = -Number.MAX_VALUE;
  11498. var data = this.data;
  11499. var xi = 0;
  11500. var yi = 0;
  11501. var x0 = 0;
  11502. var y0 = 0;
  11503. for (var i = 0; i < data.length;) {
  11504. var cmd = data[i++];
  11505. if (i === 1) {
  11506. // 如果第一个命令是 L, C, Q
  11507. // 则 previous point 同绘制命令的第一个 point
  11508. //
  11509. // 第一个命令为 Arc 的情况下会在后面特殊处理
  11510. xi = data[i];
  11511. yi = data[i + 1];
  11512. x0 = xi;
  11513. y0 = yi;
  11514. }
  11515. switch (cmd) {
  11516. case CMD.M:
  11517. // moveTo 命令重新创建一个新的 subpath, 并且更新新的起点
  11518. // 在 closePath 的时候使用
  11519. x0 = data[i++];
  11520. y0 = data[i++];
  11521. xi = x0;
  11522. yi = y0;
  11523. min2[0] = x0;
  11524. min2[1] = y0;
  11525. max2[0] = x0;
  11526. max2[1] = y0;
  11527. break;
  11528. case CMD.L:
  11529. fromLine(xi, yi, data[i], data[i + 1], min2, max2);
  11530. xi = data[i++];
  11531. yi = data[i++];
  11532. break;
  11533. case CMD.C:
  11534. fromCubic(
  11535. xi, yi, data[i++], data[i++], data[i++], data[i++], data[i], data[i + 1],
  11536. min2, max2
  11537. );
  11538. xi = data[i++];
  11539. yi = data[i++];
  11540. break;
  11541. case CMD.Q:
  11542. fromQuadratic(
  11543. xi, yi, data[i++], data[i++], data[i], data[i + 1],
  11544. min2, max2
  11545. );
  11546. xi = data[i++];
  11547. yi = data[i++];
  11548. break;
  11549. case CMD.A:
  11550. // TODO Arc 判断的开销比较大
  11551. var cx = data[i++];
  11552. var cy = data[i++];
  11553. var rx = data[i++];
  11554. var ry = data[i++];
  11555. var startAngle = data[i++];
  11556. var endAngle = data[i++] + startAngle;
  11557. // TODO Arc 旋转
  11558. i += 1;
  11559. var anticlockwise = 1 - data[i++];
  11560. if (i === 1) {
  11561. // 直接使用 arc 命令
  11562. // 第一个命令起点还未定义
  11563. x0 = mathCos$1(startAngle) * rx + cx;
  11564. y0 = mathSin$1(startAngle) * ry + cy;
  11565. }
  11566. fromArc(
  11567. cx, cy, rx, ry, startAngle, endAngle,
  11568. anticlockwise, min2, max2
  11569. );
  11570. xi = mathCos$1(endAngle) * rx + cx;
  11571. yi = mathSin$1(endAngle) * ry + cy;
  11572. break;
  11573. case CMD.R:
  11574. x0 = xi = data[i++];
  11575. y0 = yi = data[i++];
  11576. var width = data[i++];
  11577. var height = data[i++];
  11578. // Use fromLine
  11579. fromLine(x0, y0, x0 + width, y0 + height, min2, max2);
  11580. break;
  11581. case CMD.Z:
  11582. xi = x0;
  11583. yi = y0;
  11584. break;
  11585. }
  11586. // Union
  11587. min(min$1, min$1, min2);
  11588. max(max$1, max$1, max2);
  11589. }
  11590. // No data
  11591. if (i === 0) {
  11592. min$1[0] = min$1[1] = max$1[0] = max$1[1] = 0;
  11593. }
  11594. return new BoundingRect(
  11595. min$1[0], min$1[1], max$1[0] - min$1[0], max$1[1] - min$1[1]
  11596. );
  11597. },
  11598. /**
  11599. * Rebuild path from current data
  11600. * Rebuild path will not consider javascript implemented line dash.
  11601. * @param {CanvasRenderingContext2D} ctx
  11602. */
  11603. rebuildPath: function (ctx) {
  11604. var d = this.data;
  11605. var x0;
  11606. var y0;
  11607. var xi;
  11608. var yi;
  11609. var x;
  11610. var y;
  11611. var ux = this._ux;
  11612. var uy = this._uy;
  11613. var len$$1 = this._len;
  11614. for (var i = 0; i < len$$1;) {
  11615. var cmd = d[i++];
  11616. if (i === 1) {
  11617. // 如果第一个命令是 L, C, Q
  11618. // 则 previous point 同绘制命令的第一个 point
  11619. //
  11620. // 第一个命令为 Arc 的情况下会在后面特殊处理
  11621. xi = d[i];
  11622. yi = d[i + 1];
  11623. x0 = xi;
  11624. y0 = yi;
  11625. }
  11626. switch (cmd) {
  11627. case CMD.M:
  11628. x0 = xi = d[i++];
  11629. y0 = yi = d[i++];
  11630. ctx.moveTo(xi, yi);
  11631. break;
  11632. case CMD.L:
  11633. x = d[i++];
  11634. y = d[i++];
  11635. // Not draw too small seg between
  11636. if (mathAbs(x - xi) > ux || mathAbs(y - yi) > uy || i === len$$1 - 1) {
  11637. ctx.lineTo(x, y);
  11638. xi = x;
  11639. yi = y;
  11640. }
  11641. break;
  11642. case CMD.C:
  11643. ctx.bezierCurveTo(
  11644. d[i++], d[i++], d[i++], d[i++], d[i++], d[i++]
  11645. );
  11646. xi = d[i - 2];
  11647. yi = d[i - 1];
  11648. break;
  11649. case CMD.Q:
  11650. ctx.quadraticCurveTo(d[i++], d[i++], d[i++], d[i++]);
  11651. xi = d[i - 2];
  11652. yi = d[i - 1];
  11653. break;
  11654. case CMD.A:
  11655. var cx = d[i++];
  11656. var cy = d[i++];
  11657. var rx = d[i++];
  11658. var ry = d[i++];
  11659. var theta = d[i++];
  11660. var dTheta = d[i++];
  11661. var psi = d[i++];
  11662. var fs = d[i++];
  11663. var r = (rx > ry) ? rx : ry;
  11664. var scaleX = (rx > ry) ? 1 : rx / ry;
  11665. var scaleY = (rx > ry) ? ry / rx : 1;
  11666. var isEllipse = Math.abs(rx - ry) > 1e-3;
  11667. var endAngle = theta + dTheta;
  11668. if (isEllipse) {
  11669. ctx.translate(cx, cy);
  11670. ctx.rotate(psi);
  11671. ctx.scale(scaleX, scaleY);
  11672. ctx.arc(0, 0, r, theta, endAngle, 1 - fs);
  11673. ctx.scale(1 / scaleX, 1 / scaleY);
  11674. ctx.rotate(-psi);
  11675. ctx.translate(-cx, -cy);
  11676. }
  11677. else {
  11678. ctx.arc(cx, cy, r, theta, endAngle, 1 - fs);
  11679. }
  11680. if (i === 1) {
  11681. // 直接使用 arc 命令
  11682. // 第一个命令起点还未定义
  11683. x0 = mathCos$1(theta) * rx + cx;
  11684. y0 = mathSin$1(theta) * ry + cy;
  11685. }
  11686. xi = mathCos$1(endAngle) * rx + cx;
  11687. yi = mathSin$1(endAngle) * ry + cy;
  11688. break;
  11689. case CMD.R:
  11690. x0 = xi = d[i];
  11691. y0 = yi = d[i + 1];
  11692. ctx.rect(d[i++], d[i++], d[i++], d[i++]);
  11693. break;
  11694. case CMD.Z:
  11695. ctx.closePath();
  11696. xi = x0;
  11697. yi = y0;
  11698. }
  11699. }
  11700. }
  11701. };
  11702. PathProxy.CMD = CMD;
  11703. /**
  11704. * 线段包含判断
  11705. * @param {number} x0
  11706. * @param {number} y0
  11707. * @param {number} x1
  11708. * @param {number} y1
  11709. * @param {number} lineWidth
  11710. * @param {number} x
  11711. * @param {number} y
  11712. * @return {boolean}
  11713. */
  11714. function containStroke$1(x0, y0, x1, y1, lineWidth, x, y) {
  11715. if (lineWidth === 0) {
  11716. return false;
  11717. }
  11718. var _l = lineWidth;
  11719. var _a = 0;
  11720. var _b = x0;
  11721. // Quick reject
  11722. if (
  11723. (y > y0 + _l && y > y1 + _l)
  11724. || (y < y0 - _l && y < y1 - _l)
  11725. || (x > x0 + _l && x > x1 + _l)
  11726. || (x < x0 - _l && x < x1 - _l)
  11727. ) {
  11728. return false;
  11729. }
  11730. if (x0 !== x1) {
  11731. _a = (y0 - y1) / (x0 - x1);
  11732. _b = (x0 * y1 - x1 * y0) / (x0 - x1);
  11733. }
  11734. else {
  11735. return Math.abs(x - x0) <= _l / 2;
  11736. }
  11737. var tmp = _a * x - y + _b;
  11738. var _s = tmp * tmp / (_a * _a + 1);
  11739. return _s <= _l / 2 * _l / 2;
  11740. }
  11741. /**
  11742. * 三次贝塞尔曲线描边包含判断
  11743. * @param {number} x0
  11744. * @param {number} y0
  11745. * @param {number} x1
  11746. * @param {number} y1
  11747. * @param {number} x2
  11748. * @param {number} y2
  11749. * @param {number} x3
  11750. * @param {number} y3
  11751. * @param {number} lineWidth
  11752. * @param {number} x
  11753. * @param {number} y
  11754. * @return {boolean}
  11755. */
  11756. function containStroke$2(x0, y0, x1, y1, x2, y2, x3, y3, lineWidth, x, y) {
  11757. if (lineWidth === 0) {
  11758. return false;
  11759. }
  11760. var _l = lineWidth;
  11761. // Quick reject
  11762. if (
  11763. (y > y0 + _l && y > y1 + _l && y > y2 + _l && y > y3 + _l)
  11764. || (y < y0 - _l && y < y1 - _l && y < y2 - _l && y < y3 - _l)
  11765. || (x > x0 + _l && x > x1 + _l && x > x2 + _l && x > x3 + _l)
  11766. || (x < x0 - _l && x < x1 - _l && x < x2 - _l && x < x3 - _l)
  11767. ) {
  11768. return false;
  11769. }
  11770. var d = cubicProjectPoint(
  11771. x0, y0, x1, y1, x2, y2, x3, y3,
  11772. x, y, null
  11773. );
  11774. return d <= _l / 2;
  11775. }
  11776. /**
  11777. * 二次贝塞尔曲线描边包含判断
  11778. * @param {number} x0
  11779. * @param {number} y0
  11780. * @param {number} x1
  11781. * @param {number} y1
  11782. * @param {number} x2
  11783. * @param {number} y2
  11784. * @param {number} lineWidth
  11785. * @param {number} x
  11786. * @param {number} y
  11787. * @return {boolean}
  11788. */
  11789. function containStroke$3(x0, y0, x1, y1, x2, y2, lineWidth, x, y) {
  11790. if (lineWidth === 0) {
  11791. return false;
  11792. }
  11793. var _l = lineWidth;
  11794. // Quick reject
  11795. if (
  11796. (y > y0 + _l && y > y1 + _l && y > y2 + _l)
  11797. || (y < y0 - _l && y < y1 - _l && y < y2 - _l)
  11798. || (x > x0 + _l && x > x1 + _l && x > x2 + _l)
  11799. || (x < x0 - _l && x < x1 - _l && x < x2 - _l)
  11800. ) {
  11801. return false;
  11802. }
  11803. var d = quadraticProjectPoint(
  11804. x0, y0, x1, y1, x2, y2,
  11805. x, y, null
  11806. );
  11807. return d <= _l / 2;
  11808. }
  11809. var PI2$3 = Math.PI * 2;
  11810. function normalizeRadian(angle) {
  11811. angle %= PI2$3;
  11812. if (angle < 0) {
  11813. angle += PI2$3;
  11814. }
  11815. return angle;
  11816. }
  11817. var PI2$2 = Math.PI * 2;
  11818. /**
  11819. * 圆弧描边包含判断
  11820. * @param {number} cx
  11821. * @param {number} cy
  11822. * @param {number} r
  11823. * @param {number} startAngle
  11824. * @param {number} endAngle
  11825. * @param {boolean} anticlockwise
  11826. * @param {number} lineWidth
  11827. * @param {number} x
  11828. * @param {number} y
  11829. * @return {Boolean}
  11830. */
  11831. function containStroke$4(
  11832. cx, cy, r, startAngle, endAngle, anticlockwise,
  11833. lineWidth, x, y
  11834. ) {
  11835. if (lineWidth === 0) {
  11836. return false;
  11837. }
  11838. var _l = lineWidth;
  11839. x -= cx;
  11840. y -= cy;
  11841. var d = Math.sqrt(x * x + y * y);
  11842. if ((d - _l > r) || (d + _l < r)) {
  11843. return false;
  11844. }
  11845. if (Math.abs(startAngle - endAngle) % PI2$2 < 1e-4) {
  11846. // Is a circle
  11847. return true;
  11848. }
  11849. if (anticlockwise) {
  11850. var tmp = startAngle;
  11851. startAngle = normalizeRadian(endAngle);
  11852. endAngle = normalizeRadian(tmp);
  11853. }
  11854. else {
  11855. startAngle = normalizeRadian(startAngle);
  11856. endAngle = normalizeRadian(endAngle);
  11857. }
  11858. if (startAngle > endAngle) {
  11859. endAngle += PI2$2;
  11860. }
  11861. var angle = Math.atan2(y, x);
  11862. if (angle < 0) {
  11863. angle += PI2$2;
  11864. }
  11865. return (angle >= startAngle && angle <= endAngle)
  11866. || (angle + PI2$2 >= startAngle && angle + PI2$2 <= endAngle);
  11867. }
  11868. function windingLine(x0, y0, x1, y1, x, y) {
  11869. if ((y > y0 && y > y1) || (y < y0 && y < y1)) {
  11870. return 0;
  11871. }
  11872. // Ignore horizontal line
  11873. if (y1 === y0) {
  11874. return 0;
  11875. }
  11876. var dir = y1 < y0 ? 1 : -1;
  11877. var t = (y - y0) / (y1 - y0);
  11878. // Avoid winding error when intersection point is the connect point of two line of polygon
  11879. if (t === 1 || t === 0) {
  11880. dir = y1 < y0 ? 0.5 : -0.5;
  11881. }
  11882. var x_ = t * (x1 - x0) + x0;
  11883. // If (x, y) on the line, considered as "contain".
  11884. return x_ === x ? Infinity : x_ > x ? dir : 0;
  11885. }
  11886. var CMD$1 = PathProxy.CMD;
  11887. var PI2$1 = Math.PI * 2;
  11888. var EPSILON$2 = 1e-4;
  11889. function isAroundEqual(a, b) {
  11890. return Math.abs(a - b) < EPSILON$2;
  11891. }
  11892. // 临时数组
  11893. var roots = [-1, -1, -1];
  11894. var extrema = [-1, -1];
  11895. function swapExtrema() {
  11896. var tmp = extrema[0];
  11897. extrema[0] = extrema[1];
  11898. extrema[1] = tmp;
  11899. }
  11900. function windingCubic(x0, y0, x1, y1, x2, y2, x3, y3, x, y) {
  11901. // Quick reject
  11902. if (
  11903. (y > y0 && y > y1 && y > y2 && y > y3)
  11904. || (y < y0 && y < y1 && y < y2 && y < y3)
  11905. ) {
  11906. return 0;
  11907. }
  11908. var nRoots = cubicRootAt(y0, y1, y2, y3, y, roots);
  11909. if (nRoots === 0) {
  11910. return 0;
  11911. }
  11912. else {
  11913. var w = 0;
  11914. var nExtrema = -1;
  11915. var y0_;
  11916. var y1_;
  11917. for (var i = 0; i < nRoots; i++) {
  11918. var t = roots[i];
  11919. // Avoid winding error when intersection point is the connect point of two line of polygon
  11920. var unit = (t === 0 || t === 1) ? 0.5 : 1;
  11921. var x_ = cubicAt(x0, x1, x2, x3, t);
  11922. if (x_ < x) { // Quick reject
  11923. continue;
  11924. }
  11925. if (nExtrema < 0) {
  11926. nExtrema = cubicExtrema(y0, y1, y2, y3, extrema);
  11927. if (extrema[1] < extrema[0] && nExtrema > 1) {
  11928. swapExtrema();
  11929. }
  11930. y0_ = cubicAt(y0, y1, y2, y3, extrema[0]);
  11931. if (nExtrema > 1) {
  11932. y1_ = cubicAt(y0, y1, y2, y3, extrema[1]);
  11933. }
  11934. }
  11935. if (nExtrema === 2) {
  11936. // 分成三段单调函数
  11937. if (t < extrema[0]) {
  11938. w += y0_ < y0 ? unit : -unit;
  11939. }
  11940. else if (t < extrema[1]) {
  11941. w += y1_ < y0_ ? unit : -unit;
  11942. }
  11943. else {
  11944. w += y3 < y1_ ? unit : -unit;
  11945. }
  11946. }
  11947. else {
  11948. // 分成两段单调函数
  11949. if (t < extrema[0]) {
  11950. w += y0_ < y0 ? unit : -unit;
  11951. }
  11952. else {
  11953. w += y3 < y0_ ? unit : -unit;
  11954. }
  11955. }
  11956. }
  11957. return w;
  11958. }
  11959. }
  11960. function windingQuadratic(x0, y0, x1, y1, x2, y2, x, y) {
  11961. // Quick reject
  11962. if (
  11963. (y > y0 && y > y1 && y > y2)
  11964. || (y < y0 && y < y1 && y < y2)
  11965. ) {
  11966. return 0;
  11967. }
  11968. var nRoots = quadraticRootAt(y0, y1, y2, y, roots);
  11969. if (nRoots === 0) {
  11970. return 0;
  11971. }
  11972. else {
  11973. var t = quadraticExtremum(y0, y1, y2);
  11974. if (t >= 0 && t <= 1) {
  11975. var w = 0;
  11976. var y_ = quadraticAt(y0, y1, y2, t);
  11977. for (var i = 0; i < nRoots; i++) {
  11978. // Remove one endpoint.
  11979. var unit = (roots[i] === 0 || roots[i] === 1) ? 0.5 : 1;
  11980. var x_ = quadraticAt(x0, x1, x2, roots[i]);
  11981. if (x_ < x) { // Quick reject
  11982. continue;
  11983. }
  11984. if (roots[i] < t) {
  11985. w += y_ < y0 ? unit : -unit;
  11986. }
  11987. else {
  11988. w += y2 < y_ ? unit : -unit;
  11989. }
  11990. }
  11991. return w;
  11992. }
  11993. else {
  11994. // Remove one endpoint.
  11995. var unit = (roots[0] === 0 || roots[0] === 1) ? 0.5 : 1;
  11996. var x_ = quadraticAt(x0, x1, x2, roots[0]);
  11997. if (x_ < x) { // Quick reject
  11998. return 0;
  11999. }
  12000. return y2 < y0 ? unit : -unit;
  12001. }
  12002. }
  12003. }
  12004. // TODO
  12005. // Arc 旋转
  12006. function windingArc(
  12007. cx, cy, r, startAngle, endAngle, anticlockwise, x, y
  12008. ) {
  12009. y -= cy;
  12010. if (y > r || y < -r) {
  12011. return 0;
  12012. }
  12013. var tmp = Math.sqrt(r * r - y * y);
  12014. roots[0] = -tmp;
  12015. roots[1] = tmp;
  12016. var diff = Math.abs(startAngle - endAngle);
  12017. if (diff < 1e-4) {
  12018. return 0;
  12019. }
  12020. if (diff % PI2$1 < 1e-4) {
  12021. // Is a circle
  12022. startAngle = 0;
  12023. endAngle = PI2$1;
  12024. var dir = anticlockwise ? 1 : -1;
  12025. if (x >= roots[0] + cx && x <= roots[1] + cx) {
  12026. return dir;
  12027. }
  12028. else {
  12029. return 0;
  12030. }
  12031. }
  12032. if (anticlockwise) {
  12033. var tmp = startAngle;
  12034. startAngle = normalizeRadian(endAngle);
  12035. endAngle = normalizeRadian(tmp);
  12036. }
  12037. else {
  12038. startAngle = normalizeRadian(startAngle);
  12039. endAngle = normalizeRadian(endAngle);
  12040. }
  12041. if (startAngle > endAngle) {
  12042. endAngle += PI2$1;
  12043. }
  12044. var w = 0;
  12045. for (var i = 0; i < 2; i++) {
  12046. var x_ = roots[i];
  12047. if (x_ + cx > x) {
  12048. var angle = Math.atan2(y, x_);
  12049. var dir = anticlockwise ? 1 : -1;
  12050. if (angle < 0) {
  12051. angle = PI2$1 + angle;
  12052. }
  12053. if (
  12054. (angle >= startAngle && angle <= endAngle)
  12055. || (angle + PI2$1 >= startAngle && angle + PI2$1 <= endAngle)
  12056. ) {
  12057. if (angle > Math.PI / 2 && angle < Math.PI * 1.5) {
  12058. dir = -dir;
  12059. }
  12060. w += dir;
  12061. }
  12062. }
  12063. }
  12064. return w;
  12065. }
  12066. function containPath(data, lineWidth, isStroke, x, y) {
  12067. var w = 0;
  12068. var xi = 0;
  12069. var yi = 0;
  12070. var x0 = 0;
  12071. var y0 = 0;
  12072. for (var i = 0; i < data.length;) {
  12073. var cmd = data[i++];
  12074. // Begin a new subpath
  12075. if (cmd === CMD$1.M && i > 1) {
  12076. // Close previous subpath
  12077. if (!isStroke) {
  12078. w += windingLine(xi, yi, x0, y0, x, y);
  12079. }
  12080. // 如果被任何一个 subpath 包含
  12081. // if (w !== 0) {
  12082. // return true;
  12083. // }
  12084. }
  12085. if (i === 1) {
  12086. // 如果第一个命令是 L, C, Q
  12087. // 则 previous point 同绘制命令的第一个 point
  12088. //
  12089. // 第一个命令为 Arc 的情况下会在后面特殊处理
  12090. xi = data[i];
  12091. yi = data[i + 1];
  12092. x0 = xi;
  12093. y0 = yi;
  12094. }
  12095. switch (cmd) {
  12096. case CMD$1.M:
  12097. // moveTo 命令重新创建一个新的 subpath, 并且更新新的起点
  12098. // 在 closePath 的时候使用
  12099. x0 = data[i++];
  12100. y0 = data[i++];
  12101. xi = x0;
  12102. yi = y0;
  12103. break;
  12104. case CMD$1.L:
  12105. if (isStroke) {
  12106. if (containStroke$1(xi, yi, data[i], data[i + 1], lineWidth, x, y)) {
  12107. return true;
  12108. }
  12109. }
  12110. else {
  12111. // NOTE 在第一个命令为 L, C, Q 的时候会计算出 NaN
  12112. w += windingLine(xi, yi, data[i], data[i + 1], x, y) || 0;
  12113. }
  12114. xi = data[i++];
  12115. yi = data[i++];
  12116. break;
  12117. case CMD$1.C:
  12118. if (isStroke) {
  12119. if (containStroke$2(xi, yi,
  12120. data[i++], data[i++], data[i++], data[i++], data[i], data[i + 1],
  12121. lineWidth, x, y
  12122. )) {
  12123. return true;
  12124. }
  12125. }
  12126. else {
  12127. w += windingCubic(
  12128. xi, yi,
  12129. data[i++], data[i++], data[i++], data[i++], data[i], data[i + 1],
  12130. x, y
  12131. ) || 0;
  12132. }
  12133. xi = data[i++];
  12134. yi = data[i++];
  12135. break;
  12136. case CMD$1.Q:
  12137. if (isStroke) {
  12138. if (containStroke$3(xi, yi,
  12139. data[i++], data[i++], data[i], data[i + 1],
  12140. lineWidth, x, y
  12141. )) {
  12142. return true;
  12143. }
  12144. }
  12145. else {
  12146. w += windingQuadratic(
  12147. xi, yi,
  12148. data[i++], data[i++], data[i], data[i + 1],
  12149. x, y
  12150. ) || 0;
  12151. }
  12152. xi = data[i++];
  12153. yi = data[i++];
  12154. break;
  12155. case CMD$1.A:
  12156. // TODO Arc 判断的开销比较大
  12157. var cx = data[i++];
  12158. var cy = data[i++];
  12159. var rx = data[i++];
  12160. var ry = data[i++];
  12161. var theta = data[i++];
  12162. var dTheta = data[i++];
  12163. // TODO Arc 旋转
  12164. i += 1;
  12165. var anticlockwise = 1 - data[i++];
  12166. var x1 = Math.cos(theta) * rx + cx;
  12167. var y1 = Math.sin(theta) * ry + cy;
  12168. // 不是直接使用 arc 命令
  12169. if (i > 1) {
  12170. w += windingLine(xi, yi, x1, y1, x, y);
  12171. }
  12172. else {
  12173. // 第一个命令起点还未定义
  12174. x0 = x1;
  12175. y0 = y1;
  12176. }
  12177. // zr 使用scale来模拟椭圆, 这里也对x做一定的缩放
  12178. var _x = (x - cx) * ry / rx + cx;
  12179. if (isStroke) {
  12180. if (containStroke$4(
  12181. cx, cy, ry, theta, theta + dTheta, anticlockwise,
  12182. lineWidth, _x, y
  12183. )) {
  12184. return true;
  12185. }
  12186. }
  12187. else {
  12188. w += windingArc(
  12189. cx, cy, ry, theta, theta + dTheta, anticlockwise,
  12190. _x, y
  12191. );
  12192. }
  12193. xi = Math.cos(theta + dTheta) * rx + cx;
  12194. yi = Math.sin(theta + dTheta) * ry + cy;
  12195. break;
  12196. case CMD$1.R:
  12197. x0 = xi = data[i++];
  12198. y0 = yi = data[i++];
  12199. var width = data[i++];
  12200. var height = data[i++];
  12201. var x1 = x0 + width;
  12202. var y1 = y0 + height;
  12203. if (isStroke) {
  12204. if (containStroke$1(x0, y0, x1, y0, lineWidth, x, y)
  12205. || containStroke$1(x1, y0, x1, y1, lineWidth, x, y)
  12206. || containStroke$1(x1, y1, x0, y1, lineWidth, x, y)
  12207. || containStroke$1(x0, y1, x0, y0, lineWidth, x, y)
  12208. ) {
  12209. return true;
  12210. }
  12211. }
  12212. else {
  12213. // FIXME Clockwise ?
  12214. w += windingLine(x1, y0, x1, y1, x, y);
  12215. w += windingLine(x0, y1, x0, y0, x, y);
  12216. }
  12217. break;
  12218. case CMD$1.Z:
  12219. if (isStroke) {
  12220. if (containStroke$1(
  12221. xi, yi, x0, y0, lineWidth, x, y
  12222. )) {
  12223. return true;
  12224. }
  12225. }
  12226. else {
  12227. // Close a subpath
  12228. w += windingLine(xi, yi, x0, y0, x, y);
  12229. // 如果被任何一个 subpath 包含
  12230. // FIXME subpaths may overlap
  12231. // if (w !== 0) {
  12232. // return true;
  12233. // }
  12234. }
  12235. xi = x0;
  12236. yi = y0;
  12237. break;
  12238. }
  12239. }
  12240. if (!isStroke && !isAroundEqual(yi, y0)) {
  12241. w += windingLine(xi, yi, x0, y0, x, y) || 0;
  12242. }
  12243. return w !== 0;
  12244. }
  12245. function contain(pathData, x, y) {
  12246. return containPath(pathData, 0, false, x, y);
  12247. }
  12248. function containStroke(pathData, lineWidth, x, y) {
  12249. return containPath(pathData, lineWidth, true, x, y);
  12250. }
  12251. var getCanvasPattern = Pattern.prototype.getCanvasPattern;
  12252. var abs = Math.abs;
  12253. var pathProxyForDraw = new PathProxy(true);
  12254. /**
  12255. * @alias module:zrender/graphic/Path
  12256. * @extends module:zrender/graphic/Displayable
  12257. * @constructor
  12258. * @param {Object} opts
  12259. */
  12260. function Path(opts) {
  12261. Displayable.call(this, opts);
  12262. /**
  12263. * @type {module:zrender/core/PathProxy}
  12264. * @readOnly
  12265. */
  12266. this.path = null;
  12267. }
  12268. Path.prototype = {
  12269. constructor: Path,
  12270. type: 'path',
  12271. __dirtyPath: true,
  12272. strokeContainThreshold: 5,
  12273. // This item default to be false. But in map series in echarts,
  12274. // in order to improve performance, it should be set to true,
  12275. // so the shorty segment won't draw.
  12276. segmentIgnoreThreshold: 0,
  12277. /**
  12278. * See `module:zrender/src/graphic/helper/subPixelOptimize`.
  12279. * @type {boolean}
  12280. */
  12281. subPixelOptimize: false,
  12282. brush: function (ctx, prevEl) {
  12283. var style = this.style;
  12284. var path = this.path || pathProxyForDraw;
  12285. var hasStroke = style.hasStroke();
  12286. var hasFill = style.hasFill();
  12287. var fill = style.fill;
  12288. var stroke = style.stroke;
  12289. var hasFillGradient = hasFill && !!(fill.colorStops);
  12290. var hasStrokeGradient = hasStroke && !!(stroke.colorStops);
  12291. var hasFillPattern = hasFill && !!(fill.image);
  12292. var hasStrokePattern = hasStroke && !!(stroke.image);
  12293. style.bind(ctx, this, prevEl);
  12294. this.setTransform(ctx);
  12295. if (this.__dirty) {
  12296. var rect;
  12297. // Update gradient because bounding rect may changed
  12298. if (hasFillGradient) {
  12299. rect = rect || this.getBoundingRect();
  12300. this._fillGradient = style.getGradient(ctx, fill, rect);
  12301. }
  12302. if (hasStrokeGradient) {
  12303. rect = rect || this.getBoundingRect();
  12304. this._strokeGradient = style.getGradient(ctx, stroke, rect);
  12305. }
  12306. }
  12307. // Use the gradient or pattern
  12308. if (hasFillGradient) {
  12309. // PENDING If may have affect the state
  12310. ctx.fillStyle = this._fillGradient;
  12311. }
  12312. else if (hasFillPattern) {
  12313. ctx.fillStyle = getCanvasPattern.call(fill, ctx);
  12314. }
  12315. if (hasStrokeGradient) {
  12316. ctx.strokeStyle = this._strokeGradient;
  12317. }
  12318. else if (hasStrokePattern) {
  12319. ctx.strokeStyle = getCanvasPattern.call(stroke, ctx);
  12320. }
  12321. var lineDash = style.lineDash;
  12322. var lineDashOffset = style.lineDashOffset;
  12323. var ctxLineDash = !!ctx.setLineDash;
  12324. // Update path sx, sy
  12325. var scale = this.getGlobalScale();
  12326. path.setScale(scale[0], scale[1], this.segmentIgnoreThreshold);
  12327. // Proxy context
  12328. // Rebuild path in following 2 cases
  12329. // 1. Path is dirty
  12330. // 2. Path needs javascript implemented lineDash stroking.
  12331. // In this case, lineDash information will not be saved in PathProxy
  12332. if (this.__dirtyPath
  12333. || (lineDash && !ctxLineDash && hasStroke)
  12334. ) {
  12335. path.beginPath(ctx);
  12336. // Setting line dash before build path
  12337. if (lineDash && !ctxLineDash) {
  12338. path.setLineDash(lineDash);
  12339. path.setLineDashOffset(lineDashOffset);
  12340. }
  12341. this.buildPath(path, this.shape, false);
  12342. // Clear path dirty flag
  12343. if (this.path) {
  12344. this.__dirtyPath = false;
  12345. }
  12346. }
  12347. else {
  12348. // Replay path building
  12349. ctx.beginPath();
  12350. this.path.rebuildPath(ctx);
  12351. }
  12352. if (hasFill) {
  12353. if (style.fillOpacity != null) {
  12354. var originalGlobalAlpha = ctx.globalAlpha;
  12355. ctx.globalAlpha = style.fillOpacity * style.opacity;
  12356. path.fill(ctx);
  12357. ctx.globalAlpha = originalGlobalAlpha;
  12358. }
  12359. else {
  12360. path.fill(ctx);
  12361. }
  12362. }
  12363. if (lineDash && ctxLineDash) {
  12364. ctx.setLineDash(lineDash);
  12365. ctx.lineDashOffset = lineDashOffset;
  12366. }
  12367. if (hasStroke) {
  12368. if (style.strokeOpacity != null) {
  12369. var originalGlobalAlpha = ctx.globalAlpha;
  12370. ctx.globalAlpha = style.strokeOpacity * style.opacity;
  12371. path.stroke(ctx);
  12372. ctx.globalAlpha = originalGlobalAlpha;
  12373. }
  12374. else {
  12375. path.stroke(ctx);
  12376. }
  12377. }
  12378. if (lineDash && ctxLineDash) {
  12379. // PENDING
  12380. // Remove lineDash
  12381. ctx.setLineDash([]);
  12382. }
  12383. // Draw rect text
  12384. if (style.text != null) {
  12385. // Only restore transform when needs draw text.
  12386. this.restoreTransform(ctx);
  12387. this.drawRectText(ctx, this.getBoundingRect());
  12388. }
  12389. },
  12390. // When bundling path, some shape may decide if use moveTo to begin a new subpath or closePath
  12391. // Like in circle
  12392. buildPath: function (ctx, shapeCfg, inBundle) {},
  12393. createPathProxy: function () {
  12394. this.path = new PathProxy();
  12395. },
  12396. getBoundingRect: function () {
  12397. var rect = this._rect;
  12398. var style = this.style;
  12399. var needsUpdateRect = !rect;
  12400. if (needsUpdateRect) {
  12401. var path = this.path;
  12402. if (!path) {
  12403. // Create path on demand.
  12404. path = this.path = new PathProxy();
  12405. }
  12406. if (this.__dirtyPath) {
  12407. path.beginPath();
  12408. this.buildPath(path, this.shape, false);
  12409. }
  12410. rect = path.getBoundingRect();
  12411. }
  12412. this._rect = rect;
  12413. if (style.hasStroke()) {
  12414. // Needs update rect with stroke lineWidth when
  12415. // 1. Element changes scale or lineWidth
  12416. // 2. Shape is changed
  12417. var rectWithStroke = this._rectWithStroke || (this._rectWithStroke = rect.clone());
  12418. if (this.__dirty || needsUpdateRect) {
  12419. rectWithStroke.copy(rect);
  12420. // FIXME Must after updateTransform
  12421. var w = style.lineWidth;
  12422. // PENDING, Min line width is needed when line is horizontal or vertical
  12423. var lineScale = style.strokeNoScale ? this.getLineScale() : 1;
  12424. // Only add extra hover lineWidth when there are no fill
  12425. if (!style.hasFill()) {
  12426. w = Math.max(w, this.strokeContainThreshold || 4);
  12427. }
  12428. // Consider line width
  12429. // Line scale can't be 0;
  12430. if (lineScale > 1e-10) {
  12431. rectWithStroke.width += w / lineScale;
  12432. rectWithStroke.height += w / lineScale;
  12433. rectWithStroke.x -= w / lineScale / 2;
  12434. rectWithStroke.y -= w / lineScale / 2;
  12435. }
  12436. }
  12437. // Return rect with stroke
  12438. return rectWithStroke;
  12439. }
  12440. return rect;
  12441. },
  12442. contain: function (x, y) {
  12443. var localPos = this.transformCoordToLocal(x, y);
  12444. var rect = this.getBoundingRect();
  12445. var style = this.style;
  12446. x = localPos[0];
  12447. y = localPos[1];
  12448. if (rect.contain(x, y)) {
  12449. var pathData = this.path.data;
  12450. if (style.hasStroke()) {
  12451. var lineWidth = style.lineWidth;
  12452. var lineScale = style.strokeNoScale ? this.getLineScale() : 1;
  12453. // Line scale can't be 0;
  12454. if (lineScale > 1e-10) {
  12455. // Only add extra hover lineWidth when there are no fill
  12456. if (!style.hasFill()) {
  12457. lineWidth = Math.max(lineWidth, this.strokeContainThreshold);
  12458. }
  12459. if (containStroke(
  12460. pathData, lineWidth / lineScale, x, y
  12461. )) {
  12462. return true;
  12463. }
  12464. }
  12465. }
  12466. if (style.hasFill()) {
  12467. return contain(pathData, x, y);
  12468. }
  12469. }
  12470. return false;
  12471. },
  12472. /**
  12473. * @param {boolean} dirtyPath
  12474. */
  12475. dirty: function (dirtyPath) {
  12476. if (dirtyPath == null) {
  12477. dirtyPath = true;
  12478. }
  12479. // Only mark dirty, not mark clean
  12480. if (dirtyPath) {
  12481. this.__dirtyPath = dirtyPath;
  12482. this._rect = null;
  12483. }
  12484. this.__dirty = this.__dirtyText = true;
  12485. this.__zr && this.__zr.refresh();
  12486. // Used as a clipping path
  12487. if (this.__clipTarget) {
  12488. this.__clipTarget.dirty();
  12489. }
  12490. },
  12491. /**
  12492. * Alias for animate('shape')
  12493. * @param {boolean} loop
  12494. */
  12495. animateShape: function (loop) {
  12496. return this.animate('shape', loop);
  12497. },
  12498. // Overwrite attrKV
  12499. attrKV: function (key, value) {
  12500. // FIXME
  12501. if (key === 'shape') {
  12502. this.setShape(value);
  12503. this.__dirtyPath = true;
  12504. this._rect = null;
  12505. }
  12506. else {
  12507. Displayable.prototype.attrKV.call(this, key, value);
  12508. }
  12509. },
  12510. /**
  12511. * @param {Object|string} key
  12512. * @param {*} value
  12513. */
  12514. setShape: function (key, value) {
  12515. var shape = this.shape;
  12516. // Path from string may not have shape
  12517. if (shape) {
  12518. if (isObject(key)) {
  12519. for (var name in key) {
  12520. if (key.hasOwnProperty(name)) {
  12521. shape[name] = key[name];
  12522. }
  12523. }
  12524. }
  12525. else {
  12526. shape[key] = value;
  12527. }
  12528. this.dirty(true);
  12529. }
  12530. return this;
  12531. },
  12532. getLineScale: function () {
  12533. var m = this.transform;
  12534. // Get the line scale.
  12535. // Determinant of `m` means how much the area is enlarged by the
  12536. // transformation. So its square root can be used as a scale factor
  12537. // for width.
  12538. return m && abs(m[0] - 1) > 1e-10 && abs(m[3] - 1) > 1e-10
  12539. ? Math.sqrt(abs(m[0] * m[3] - m[2] * m[1]))
  12540. : 1;
  12541. }
  12542. };
  12543. /**
  12544. * 扩展一个 Path element, 比如星形,圆等。
  12545. * Extend a path element
  12546. * @param {Object} props
  12547. * @param {string} props.type Path type
  12548. * @param {Function} props.init Initialize
  12549. * @param {Function} props.buildPath Overwrite buildPath method
  12550. * @param {Object} [props.style] Extended default style config
  12551. * @param {Object} [props.shape] Extended default shape config
  12552. */
  12553. Path.extend = function (defaults$$1) {
  12554. var Sub = function (opts) {
  12555. Path.call(this, opts);
  12556. if (defaults$$1.style) {
  12557. // Extend default style
  12558. this.style.extendFrom(defaults$$1.style, false);
  12559. }
  12560. // Extend default shape
  12561. var defaultShape = defaults$$1.shape;
  12562. if (defaultShape) {
  12563. this.shape = this.shape || {};
  12564. var thisShape = this.shape;
  12565. for (var name in defaultShape) {
  12566. if (
  12567. !thisShape.hasOwnProperty(name)
  12568. && defaultShape.hasOwnProperty(name)
  12569. ) {
  12570. thisShape[name] = defaultShape[name];
  12571. }
  12572. }
  12573. }
  12574. defaults$$1.init && defaults$$1.init.call(this, opts);
  12575. };
  12576. inherits(Sub, Path);
  12577. // FIXME 不能 extend position, rotation 等引用对象
  12578. for (var name in defaults$$1) {
  12579. // Extending prototype values and methods
  12580. if (name !== 'style' && name !== 'shape') {
  12581. Sub.prototype[name] = defaults$$1[name];
  12582. }
  12583. }
  12584. return Sub;
  12585. };
  12586. inherits(Path, Displayable);
  12587. var CMD$2 = PathProxy.CMD;
  12588. var points = [[], [], []];
  12589. var mathSqrt$3 = Math.sqrt;
  12590. var mathAtan2 = Math.atan2;
  12591. var transformPath = function (path, m) {
  12592. var data = path.data;
  12593. var cmd;
  12594. var nPoint;
  12595. var i;
  12596. var j;
  12597. var k;
  12598. var p;
  12599. var M = CMD$2.M;
  12600. var C = CMD$2.C;
  12601. var L = CMD$2.L;
  12602. var R = CMD$2.R;
  12603. var A = CMD$2.A;
  12604. var Q = CMD$2.Q;
  12605. for (i = 0, j = 0; i < data.length;) {
  12606. cmd = data[i++];
  12607. j = i;
  12608. nPoint = 0;
  12609. switch (cmd) {
  12610. case M:
  12611. nPoint = 1;
  12612. break;
  12613. case L:
  12614. nPoint = 1;
  12615. break;
  12616. case C:
  12617. nPoint = 3;
  12618. break;
  12619. case Q:
  12620. nPoint = 2;
  12621. break;
  12622. case A:
  12623. var x = m[4];
  12624. var y = m[5];
  12625. var sx = mathSqrt$3(m[0] * m[0] + m[1] * m[1]);
  12626. var sy = mathSqrt$3(m[2] * m[2] + m[3] * m[3]);
  12627. var angle = mathAtan2(-m[1] / sy, m[0] / sx);
  12628. // cx
  12629. data[i] *= sx;
  12630. data[i++] += x;
  12631. // cy
  12632. data[i] *= sy;
  12633. data[i++] += y;
  12634. // Scale rx and ry
  12635. // FIXME Assume psi is 0 here
  12636. data[i++] *= sx;
  12637. data[i++] *= sy;
  12638. // Start angle
  12639. data[i++] += angle;
  12640. // end angle
  12641. data[i++] += angle;
  12642. // FIXME psi
  12643. i += 2;
  12644. j = i;
  12645. break;
  12646. case R:
  12647. // x0, y0
  12648. p[0] = data[i++];
  12649. p[1] = data[i++];
  12650. applyTransform(p, p, m);
  12651. data[j++] = p[0];
  12652. data[j++] = p[1];
  12653. // x1, y1
  12654. p[0] += data[i++];
  12655. p[1] += data[i++];
  12656. applyTransform(p, p, m);
  12657. data[j++] = p[0];
  12658. data[j++] = p[1];
  12659. }
  12660. for (k = 0; k < nPoint; k++) {
  12661. var p = points[k];
  12662. p[0] = data[i++];
  12663. p[1] = data[i++];
  12664. applyTransform(p, p, m);
  12665. // Write back
  12666. data[j++] = p[0];
  12667. data[j++] = p[1];
  12668. }
  12669. }
  12670. };
  12671. // command chars
  12672. // var cc = [
  12673. // 'm', 'M', 'l', 'L', 'v', 'V', 'h', 'H', 'z', 'Z',
  12674. // 'c', 'C', 'q', 'Q', 't', 'T', 's', 'S', 'a', 'A'
  12675. // ];
  12676. var mathSqrt = Math.sqrt;
  12677. var mathSin = Math.sin;
  12678. var mathCos = Math.cos;
  12679. var PI = Math.PI;
  12680. var vMag = function (v) {
  12681. return Math.sqrt(v[0] * v[0] + v[1] * v[1]);
  12682. };
  12683. var vRatio = function (u, v) {
  12684. return (u[0] * v[0] + u[1] * v[1]) / (vMag(u) * vMag(v));
  12685. };
  12686. var vAngle = function (u, v) {
  12687. return (u[0] * v[1] < u[1] * v[0] ? -1 : 1)
  12688. * Math.acos(vRatio(u, v));
  12689. };
  12690. function processArc(x1, y1, x2, y2, fa, fs, rx, ry, psiDeg, cmd, path) {
  12691. var psi = psiDeg * (PI / 180.0);
  12692. var xp = mathCos(psi) * (x1 - x2) / 2.0
  12693. + mathSin(psi) * (y1 - y2) / 2.0;
  12694. var yp = -1 * mathSin(psi) * (x1 - x2) / 2.0
  12695. + mathCos(psi) * (y1 - y2) / 2.0;
  12696. var lambda = (xp * xp) / (rx * rx) + (yp * yp) / (ry * ry);
  12697. if (lambda > 1) {
  12698. rx *= mathSqrt(lambda);
  12699. ry *= mathSqrt(lambda);
  12700. }
  12701. var f = (fa === fs ? -1 : 1)
  12702. * mathSqrt((((rx * rx) * (ry * ry))
  12703. - ((rx * rx) * (yp * yp))
  12704. - ((ry * ry) * (xp * xp))) / ((rx * rx) * (yp * yp)
  12705. + (ry * ry) * (xp * xp))
  12706. ) || 0;
  12707. var cxp = f * rx * yp / ry;
  12708. var cyp = f * -ry * xp / rx;
  12709. var cx = (x1 + x2) / 2.0
  12710. + mathCos(psi) * cxp
  12711. - mathSin(psi) * cyp;
  12712. var cy = (y1 + y2) / 2.0
  12713. + mathSin(psi) * cxp
  12714. + mathCos(psi) * cyp;
  12715. var theta = vAngle([ 1, 0 ], [ (xp - cxp) / rx, (yp - cyp) / ry ]);
  12716. var u = [ (xp - cxp) / rx, (yp - cyp) / ry ];
  12717. var v = [ (-1 * xp - cxp) / rx, (-1 * yp - cyp) / ry ];
  12718. var dTheta = vAngle(u, v);
  12719. if (vRatio(u, v) <= -1) {
  12720. dTheta = PI;
  12721. }
  12722. if (vRatio(u, v) >= 1) {
  12723. dTheta = 0;
  12724. }
  12725. if (fs === 0 && dTheta > 0) {
  12726. dTheta = dTheta - 2 * PI;
  12727. }
  12728. if (fs === 1 && dTheta < 0) {
  12729. dTheta = dTheta + 2 * PI;
  12730. }
  12731. path.addData(cmd, cx, cy, rx, ry, theta, dTheta, psi, fs);
  12732. }
  12733. var commandReg = /([mlvhzcqtsa])([^mlvhzcqtsa]*)/ig;
  12734. // Consider case:
  12735. // (1) delimiter can be comma or space, where continuous commas
  12736. // or spaces should be seen as one comma.
  12737. // (2) value can be like:
  12738. // '2e-4', 'l.5.9' (ignore 0), 'M-10-10', 'l-2.43e-1,34.9983',
  12739. // 'l-.5E1,54', '121-23-44-11' (no delimiter)
  12740. var numberReg = /-?([0-9]*\.)?[0-9]+([eE]-?[0-9]+)?/g;
  12741. // var valueSplitReg = /[\s,]+/;
  12742. function createPathProxyFromString(data) {
  12743. if (!data) {
  12744. return new PathProxy();
  12745. }
  12746. // var data = data.replace(/-/g, ' -')
  12747. // .replace(/ /g, ' ')
  12748. // .replace(/ /g, ',')
  12749. // .replace(/,,/g, ',');
  12750. // var n;
  12751. // create pipes so that we can split the data
  12752. // for (n = 0; n < cc.length; n++) {
  12753. // cs = cs.replace(new RegExp(cc[n], 'g'), '|' + cc[n]);
  12754. // }
  12755. // data = data.replace(/-/g, ',-');
  12756. // create array
  12757. // var arr = cs.split('|');
  12758. // init context point
  12759. var cpx = 0;
  12760. var cpy = 0;
  12761. var subpathX = cpx;
  12762. var subpathY = cpy;
  12763. var prevCmd;
  12764. var path = new PathProxy();
  12765. var CMD = PathProxy.CMD;
  12766. // commandReg.lastIndex = 0;
  12767. // var cmdResult;
  12768. // while ((cmdResult = commandReg.exec(data)) != null) {
  12769. // var cmdStr = cmdResult[1];
  12770. // var cmdContent = cmdResult[2];
  12771. var cmdList = data.match(commandReg);
  12772. for (var l = 0; l < cmdList.length; l++) {
  12773. var cmdText = cmdList[l];
  12774. var cmdStr = cmdText.charAt(0);
  12775. var cmd;
  12776. // String#split is faster a little bit than String#replace or RegExp#exec.
  12777. // var p = cmdContent.split(valueSplitReg);
  12778. // var pLen = 0;
  12779. // for (var i = 0; i < p.length; i++) {
  12780. // // '' and other invalid str => NaN
  12781. // var val = parseFloat(p[i]);
  12782. // !isNaN(val) && (p[pLen++] = val);
  12783. // }
  12784. var p = cmdText.match(numberReg) || [];
  12785. var pLen = p.length;
  12786. for (var i = 0; i < pLen; i++) {
  12787. p[i] = parseFloat(p[i]);
  12788. }
  12789. var off = 0;
  12790. while (off < pLen) {
  12791. var ctlPtx;
  12792. var ctlPty;
  12793. var rx;
  12794. var ry;
  12795. var psi;
  12796. var fa;
  12797. var fs;
  12798. var x1 = cpx;
  12799. var y1 = cpy;
  12800. // convert l, H, h, V, and v to L
  12801. switch (cmdStr) {
  12802. case 'l':
  12803. cpx += p[off++];
  12804. cpy += p[off++];
  12805. cmd = CMD.L;
  12806. path.addData(cmd, cpx, cpy);
  12807. break;
  12808. case 'L':
  12809. cpx = p[off++];
  12810. cpy = p[off++];
  12811. cmd = CMD.L;
  12812. path.addData(cmd, cpx, cpy);
  12813. break;
  12814. case 'm':
  12815. cpx += p[off++];
  12816. cpy += p[off++];
  12817. cmd = CMD.M;
  12818. path.addData(cmd, cpx, cpy);
  12819. subpathX = cpx;
  12820. subpathY = cpy;
  12821. cmdStr = 'l';
  12822. break;
  12823. case 'M':
  12824. cpx = p[off++];
  12825. cpy = p[off++];
  12826. cmd = CMD.M;
  12827. path.addData(cmd, cpx, cpy);
  12828. subpathX = cpx;
  12829. subpathY = cpy;
  12830. cmdStr = 'L';
  12831. break;
  12832. case 'h':
  12833. cpx += p[off++];
  12834. cmd = CMD.L;
  12835. path.addData(cmd, cpx, cpy);
  12836. break;
  12837. case 'H':
  12838. cpx = p[off++];
  12839. cmd = CMD.L;
  12840. path.addData(cmd, cpx, cpy);
  12841. break;
  12842. case 'v':
  12843. cpy += p[off++];
  12844. cmd = CMD.L;
  12845. path.addData(cmd, cpx, cpy);
  12846. break;
  12847. case 'V':
  12848. cpy = p[off++];
  12849. cmd = CMD.L;
  12850. path.addData(cmd, cpx, cpy);
  12851. break;
  12852. case 'C':
  12853. cmd = CMD.C;
  12854. path.addData(
  12855. cmd, p[off++], p[off++], p[off++], p[off++], p[off++], p[off++]
  12856. );
  12857. cpx = p[off - 2];
  12858. cpy = p[off - 1];
  12859. break;
  12860. case 'c':
  12861. cmd = CMD.C;
  12862. path.addData(
  12863. cmd,
  12864. p[off++] + cpx, p[off++] + cpy,
  12865. p[off++] + cpx, p[off++] + cpy,
  12866. p[off++] + cpx, p[off++] + cpy
  12867. );
  12868. cpx += p[off - 2];
  12869. cpy += p[off - 1];
  12870. break;
  12871. case 'S':
  12872. ctlPtx = cpx;
  12873. ctlPty = cpy;
  12874. var len = path.len();
  12875. var pathData = path.data;
  12876. if (prevCmd === CMD.C) {
  12877. ctlPtx += cpx - pathData[len - 4];
  12878. ctlPty += cpy - pathData[len - 3];
  12879. }
  12880. cmd = CMD.C;
  12881. x1 = p[off++];
  12882. y1 = p[off++];
  12883. cpx = p[off++];
  12884. cpy = p[off++];
  12885. path.addData(cmd, ctlPtx, ctlPty, x1, y1, cpx, cpy);
  12886. break;
  12887. case 's':
  12888. ctlPtx = cpx;
  12889. ctlPty = cpy;
  12890. var len = path.len();
  12891. var pathData = path.data;
  12892. if (prevCmd === CMD.C) {
  12893. ctlPtx += cpx - pathData[len - 4];
  12894. ctlPty += cpy - pathData[len - 3];
  12895. }
  12896. cmd = CMD.C;
  12897. x1 = cpx + p[off++];
  12898. y1 = cpy + p[off++];
  12899. cpx += p[off++];
  12900. cpy += p[off++];
  12901. path.addData(cmd, ctlPtx, ctlPty, x1, y1, cpx, cpy);
  12902. break;
  12903. case 'Q':
  12904. x1 = p[off++];
  12905. y1 = p[off++];
  12906. cpx = p[off++];
  12907. cpy = p[off++];
  12908. cmd = CMD.Q;
  12909. path.addData(cmd, x1, y1, cpx, cpy);
  12910. break;
  12911. case 'q':
  12912. x1 = p[off++] + cpx;
  12913. y1 = p[off++] + cpy;
  12914. cpx += p[off++];
  12915. cpy += p[off++];
  12916. cmd = CMD.Q;
  12917. path.addData(cmd, x1, y1, cpx, cpy);
  12918. break;
  12919. case 'T':
  12920. ctlPtx = cpx;
  12921. ctlPty = cpy;
  12922. var len = path.len();
  12923. var pathData = path.data;
  12924. if (prevCmd === CMD.Q) {
  12925. ctlPtx += cpx - pathData[len - 4];
  12926. ctlPty += cpy - pathData[len - 3];
  12927. }
  12928. cpx = p[off++];
  12929. cpy = p[off++];
  12930. cmd = CMD.Q;
  12931. path.addData(cmd, ctlPtx, ctlPty, cpx, cpy);
  12932. break;
  12933. case 't':
  12934. ctlPtx = cpx;
  12935. ctlPty = cpy;
  12936. var len = path.len();
  12937. var pathData = path.data;
  12938. if (prevCmd === CMD.Q) {
  12939. ctlPtx += cpx - pathData[len - 4];
  12940. ctlPty += cpy - pathData[len - 3];
  12941. }
  12942. cpx += p[off++];
  12943. cpy += p[off++];
  12944. cmd = CMD.Q;
  12945. path.addData(cmd, ctlPtx, ctlPty, cpx, cpy);
  12946. break;
  12947. case 'A':
  12948. rx = p[off++];
  12949. ry = p[off++];
  12950. psi = p[off++];
  12951. fa = p[off++];
  12952. fs = p[off++];
  12953. x1 = cpx, y1 = cpy;
  12954. cpx = p[off++];
  12955. cpy = p[off++];
  12956. cmd = CMD.A;
  12957. processArc(
  12958. x1, y1, cpx, cpy, fa, fs, rx, ry, psi, cmd, path
  12959. );
  12960. break;
  12961. case 'a':
  12962. rx = p[off++];
  12963. ry = p[off++];
  12964. psi = p[off++];
  12965. fa = p[off++];
  12966. fs = p[off++];
  12967. x1 = cpx, y1 = cpy;
  12968. cpx += p[off++];
  12969. cpy += p[off++];
  12970. cmd = CMD.A;
  12971. processArc(
  12972. x1, y1, cpx, cpy, fa, fs, rx, ry, psi, cmd, path
  12973. );
  12974. break;
  12975. }
  12976. }
  12977. if (cmdStr === 'z' || cmdStr === 'Z') {
  12978. cmd = CMD.Z;
  12979. path.addData(cmd);
  12980. // z may be in the middle of the path.
  12981. cpx = subpathX;
  12982. cpy = subpathY;
  12983. }
  12984. prevCmd = cmd;
  12985. }
  12986. path.toStatic();
  12987. return path;
  12988. }
  12989. // TODO Optimize double memory cost problem
  12990. function createPathOptions(str, opts) {
  12991. var pathProxy = createPathProxyFromString(str);
  12992. opts = opts || {};
  12993. opts.buildPath = function (path) {
  12994. if (path.setData) {
  12995. path.setData(pathProxy.data);
  12996. // Svg and vml renderer don't have context
  12997. var ctx = path.getContext();
  12998. if (ctx) {
  12999. path.rebuildPath(ctx);
  13000. }
  13001. }
  13002. else {
  13003. var ctx = path;
  13004. pathProxy.rebuildPath(ctx);
  13005. }
  13006. };
  13007. opts.applyTransform = function (m) {
  13008. transformPath(pathProxy, m);
  13009. this.dirty(true);
  13010. };
  13011. return opts;
  13012. }
  13013. /**
  13014. * Create a Path object from path string data
  13015. * http://www.w3.org/TR/SVG/paths.html#PathData
  13016. * @param {Object} opts Other options
  13017. */
  13018. function createFromString(str, opts) {
  13019. return new Path(createPathOptions(str, opts));
  13020. }
  13021. /**
  13022. * Create a Path class from path string data
  13023. * @param {string} str
  13024. * @param {Object} opts Other options
  13025. */
  13026. function extendFromString(str, opts) {
  13027. return Path.extend(createPathOptions(str, opts));
  13028. }
  13029. /**
  13030. * Merge multiple paths
  13031. */
  13032. // TODO Apply transform
  13033. // TODO stroke dash
  13034. // TODO Optimize double memory cost problem
  13035. function mergePath(pathEls, opts) {
  13036. var pathList = [];
  13037. var len = pathEls.length;
  13038. for (var i = 0; i < len; i++) {
  13039. var pathEl = pathEls[i];
  13040. if (!pathEl.path) {
  13041. pathEl.createPathProxy();
  13042. }
  13043. if (pathEl.__dirtyPath) {
  13044. pathEl.buildPath(pathEl.path, pathEl.shape, true);
  13045. }
  13046. pathList.push(pathEl.path);
  13047. }
  13048. var pathBundle = new Path(opts);
  13049. // Need path proxy.
  13050. pathBundle.createPathProxy();
  13051. pathBundle.buildPath = function (path) {
  13052. path.appendPath(pathList);
  13053. // Svg and vml renderer don't have context
  13054. var ctx = path.getContext();
  13055. if (ctx) {
  13056. path.rebuildPath(ctx);
  13057. }
  13058. };
  13059. return pathBundle;
  13060. }
  13061. var path = (Object.freeze || Object)({
  13062. createFromString: createFromString,
  13063. extendFromString: extendFromString,
  13064. mergePath: mergePath
  13065. });
  13066. /**
  13067. * @alias zrender/graphic/Text
  13068. * @extends module:zrender/graphic/Displayable
  13069. * @constructor
  13070. * @param {Object} opts
  13071. */
  13072. var Text = function (opts) { // jshint ignore:line
  13073. Displayable.call(this, opts);
  13074. };
  13075. Text.prototype = {
  13076. constructor: Text,
  13077. type: 'text',
  13078. brush: function (ctx, prevEl) {
  13079. var style = this.style;
  13080. // Optimize, avoid normalize every time.
  13081. this.__dirty && normalizeTextStyle(style, true);
  13082. // Use props with prefix 'text'.
  13083. style.fill = style.stroke = style.shadowBlur = style.shadowColor =
  13084. style.shadowOffsetX = style.shadowOffsetY = null;
  13085. var text = style.text;
  13086. // Convert to string
  13087. text != null && (text += '');
  13088. // Do not apply style.bind in Text node. Because the real bind job
  13089. // is in textHelper.renderText, and performance of text render should
  13090. // be considered.
  13091. // style.bind(ctx, this, prevEl);
  13092. if (!needDrawText(text, style)) {
  13093. // The current el.style is not applied
  13094. // and should not be used as cache.
  13095. ctx.__attrCachedBy = ContextCachedBy.NONE;
  13096. return;
  13097. }
  13098. this.setTransform(ctx);
  13099. renderText(this, ctx, text, style, null, prevEl);
  13100. this.restoreTransform(ctx);
  13101. },
  13102. getBoundingRect: function () {
  13103. var style = this.style;
  13104. // Optimize, avoid normalize every time.
  13105. this.__dirty && normalizeTextStyle(style, true);
  13106. if (!this._rect) {
  13107. var text = style.text;
  13108. text != null ? (text += '') : (text = '');
  13109. var rect = getBoundingRect(
  13110. style.text + '',
  13111. style.font,
  13112. style.textAlign,
  13113. style.textVerticalAlign,
  13114. style.textPadding,
  13115. style.textLineHeight,
  13116. style.rich
  13117. );
  13118. rect.x += style.x || 0;
  13119. rect.y += style.y || 0;
  13120. if (getStroke(style.textStroke, style.textStrokeWidth)) {
  13121. var w = style.textStrokeWidth;
  13122. rect.x -= w / 2;
  13123. rect.y -= w / 2;
  13124. rect.width += w;
  13125. rect.height += w;
  13126. }
  13127. this._rect = rect;
  13128. }
  13129. return this._rect;
  13130. }
  13131. };
  13132. inherits(Text, Displayable);
  13133. /**
  13134. * 圆形
  13135. * @module zrender/shape/Circle
  13136. */
  13137. var Circle = Path.extend({
  13138. type: 'circle',
  13139. shape: {
  13140. cx: 0,
  13141. cy: 0,
  13142. r: 0
  13143. },
  13144. buildPath: function (ctx, shape, inBundle) {
  13145. // Better stroking in ShapeBundle
  13146. // Always do it may have performence issue ( fill may be 2x more cost)
  13147. if (inBundle) {
  13148. ctx.moveTo(shape.cx + shape.r, shape.cy);
  13149. }
  13150. // else {
  13151. // if (ctx.allocate && !ctx.data.length) {
  13152. // ctx.allocate(ctx.CMD_MEM_SIZE.A);
  13153. // }
  13154. // }
  13155. // Better stroking in ShapeBundle
  13156. // ctx.moveTo(shape.cx + shape.r, shape.cy);
  13157. ctx.arc(shape.cx, shape.cy, shape.r, 0, Math.PI * 2, true);
  13158. }
  13159. });
  13160. /**
  13161. * Sub-pixel optimize for canvas rendering, prevent from blur
  13162. * when rendering a thin vertical/horizontal line.
  13163. */
  13164. var round = Math.round;
  13165. /**
  13166. * Sub pixel optimize line for canvas
  13167. *
  13168. * @param {Object} outputShape The modification will be performed on `outputShape`.
  13169. * `outputShape` and `inputShape` can be the same object.
  13170. * `outputShape` object can be used repeatly, because all of
  13171. * the `x1`, `x2`, `y1`, `y2` will be assigned in this method.
  13172. * @param {Object} [inputShape]
  13173. * @param {number} [inputShape.x1]
  13174. * @param {number} [inputShape.y1]
  13175. * @param {number} [inputShape.x2]
  13176. * @param {number} [inputShape.y2]
  13177. * @param {Object} [style]
  13178. * @param {number} [style.lineWidth] If `null`/`undefined`/`0`, do not optimize.
  13179. */
  13180. function subPixelOptimizeLine(outputShape, inputShape, style) {
  13181. if (!inputShape) {
  13182. return;
  13183. }
  13184. var x1 = inputShape.x1;
  13185. var x2 = inputShape.x2;
  13186. var y1 = inputShape.y1;
  13187. var y2 = inputShape.y2;
  13188. outputShape.x1 = x1;
  13189. outputShape.x2 = x2;
  13190. outputShape.y1 = y1;
  13191. outputShape.y2 = y2;
  13192. var lineWidth = style && style.lineWidth;
  13193. if (!lineWidth) {
  13194. return;
  13195. }
  13196. if (round(x1 * 2) === round(x2 * 2)) {
  13197. outputShape.x1 = outputShape.x2 = subPixelOptimize(x1, lineWidth, true);
  13198. }
  13199. if (round(y1 * 2) === round(y2 * 2)) {
  13200. outputShape.y1 = outputShape.y2 = subPixelOptimize(y1, lineWidth, true);
  13201. }
  13202. }
  13203. /**
  13204. * Sub pixel optimize rect for canvas
  13205. *
  13206. * @param {Object} outputShape The modification will be performed on `outputShape`.
  13207. * `outputShape` and `inputShape` can be the same object.
  13208. * `outputShape` object can be used repeatly, because all of
  13209. * the `x`, `y`, `width`, `height` will be assigned in this method.
  13210. * @param {Object} [inputShape]
  13211. * @param {number} [inputShape.x]
  13212. * @param {number} [inputShape.y]
  13213. * @param {number} [inputShape.width]
  13214. * @param {number} [inputShape.height]
  13215. * @param {Object} [style]
  13216. * @param {number} [style.lineWidth] If `null`/`undefined`/`0`, do not optimize.
  13217. */
  13218. function subPixelOptimizeRect(outputShape, inputShape, style) {
  13219. if (!inputShape) {
  13220. return;
  13221. }
  13222. var originX = inputShape.x;
  13223. var originY = inputShape.y;
  13224. var originWidth = inputShape.width;
  13225. var originHeight = inputShape.height;
  13226. outputShape.x = originX;
  13227. outputShape.y = originY;
  13228. outputShape.width = originWidth;
  13229. outputShape.height = originHeight;
  13230. var lineWidth = style && style.lineWidth;
  13231. if (!lineWidth) {
  13232. return;
  13233. }
  13234. outputShape.x = subPixelOptimize(originX, lineWidth, true);
  13235. outputShape.y = subPixelOptimize(originY, lineWidth, true);
  13236. outputShape.width = Math.max(
  13237. subPixelOptimize(originX + originWidth, lineWidth, false) - outputShape.x,
  13238. originWidth === 0 ? 0 : 1
  13239. );
  13240. outputShape.height = Math.max(
  13241. subPixelOptimize(originY + originHeight, lineWidth, false) - outputShape.y,
  13242. originHeight === 0 ? 0 : 1
  13243. );
  13244. }
  13245. /**
  13246. * Sub pixel optimize for canvas
  13247. *
  13248. * @param {number} position Coordinate, such as x, y
  13249. * @param {number} lineWidth If `null`/`undefined`/`0`, do not optimize.
  13250. * @param {boolean=} positiveOrNegative Default false (negative).
  13251. * @return {number} Optimized position.
  13252. */
  13253. function subPixelOptimize(position, lineWidth, positiveOrNegative) {
  13254. if (!lineWidth) {
  13255. return position;
  13256. }
  13257. // Assure that (position + lineWidth / 2) is near integer edge,
  13258. // otherwise line will be fuzzy in canvas.
  13259. var doubledPosition = round(position * 2);
  13260. return (doubledPosition + round(lineWidth)) % 2 === 0
  13261. ? doubledPosition / 2
  13262. : (doubledPosition + (positiveOrNegative ? 1 : -1)) / 2;
  13263. }
  13264. /**
  13265. * 矩形
  13266. * @module zrender/graphic/shape/Rect
  13267. */
  13268. // Avoid create repeatly.
  13269. var subPixelOptimizeOutputShape = {};
  13270. var Rect = Path.extend({
  13271. type: 'rect',
  13272. shape: {
  13273. // 左上、右上、右下、左下角的半径依次为r1、r2、r3、r4
  13274. // r缩写为1 相当于 [1, 1, 1, 1]
  13275. // r缩写为[1] 相当于 [1, 1, 1, 1]
  13276. // r缩写为[1, 2] 相当于 [1, 2, 1, 2]
  13277. // r缩写为[1, 2, 3] 相当于 [1, 2, 3, 2]
  13278. r: 0,
  13279. x: 0,
  13280. y: 0,
  13281. width: 0,
  13282. height: 0
  13283. },
  13284. buildPath: function (ctx, shape) {
  13285. var x;
  13286. var y;
  13287. var width;
  13288. var height;
  13289. if (this.subPixelOptimize) {
  13290. subPixelOptimizeRect(subPixelOptimizeOutputShape, shape, this.style);
  13291. x = subPixelOptimizeOutputShape.x;
  13292. y = subPixelOptimizeOutputShape.y;
  13293. width = subPixelOptimizeOutputShape.width;
  13294. height = subPixelOptimizeOutputShape.height;
  13295. subPixelOptimizeOutputShape.r = shape.r;
  13296. shape = subPixelOptimizeOutputShape;
  13297. }
  13298. else {
  13299. x = shape.x;
  13300. y = shape.y;
  13301. width = shape.width;
  13302. height = shape.height;
  13303. }
  13304. if (!shape.r) {
  13305. ctx.rect(x, y, width, height);
  13306. }
  13307. else {
  13308. buildPath(ctx, shape);
  13309. }
  13310. ctx.closePath();
  13311. return;
  13312. }
  13313. });
  13314. /**
  13315. * 椭圆形状
  13316. * @module zrender/graphic/shape/Ellipse
  13317. */
  13318. var Ellipse = Path.extend({
  13319. type: 'ellipse',
  13320. shape: {
  13321. cx: 0, cy: 0,
  13322. rx: 0, ry: 0
  13323. },
  13324. buildPath: function (ctx, shape) {
  13325. var k = 0.5522848;
  13326. var x = shape.cx;
  13327. var y = shape.cy;
  13328. var a = shape.rx;
  13329. var b = shape.ry;
  13330. var ox = a * k; // 水平控制点偏移量
  13331. var oy = b * k; // 垂直控制点偏移量
  13332. // 从椭圆的左端点开始顺时针绘制四条三次贝塞尔曲线
  13333. ctx.moveTo(x - a, y);
  13334. ctx.bezierCurveTo(x - a, y - oy, x - ox, y - b, x, y - b);
  13335. ctx.bezierCurveTo(x + ox, y - b, x + a, y - oy, x + a, y);
  13336. ctx.bezierCurveTo(x + a, y + oy, x + ox, y + b, x, y + b);
  13337. ctx.bezierCurveTo(x - ox, y + b, x - a, y + oy, x - a, y);
  13338. ctx.closePath();
  13339. }
  13340. });
  13341. /**
  13342. * 直线
  13343. * @module zrender/graphic/shape/Line
  13344. */
  13345. // Avoid create repeatly.
  13346. var subPixelOptimizeOutputShape$1 = {};
  13347. var Line = Path.extend({
  13348. type: 'line',
  13349. shape: {
  13350. // Start point
  13351. x1: 0,
  13352. y1: 0,
  13353. // End point
  13354. x2: 0,
  13355. y2: 0,
  13356. percent: 1
  13357. },
  13358. style: {
  13359. stroke: '#000',
  13360. fill: null
  13361. },
  13362. buildPath: function (ctx, shape) {
  13363. var x1;
  13364. var y1;
  13365. var x2;
  13366. var y2;
  13367. if (this.subPixelOptimize) {
  13368. subPixelOptimizeLine(subPixelOptimizeOutputShape$1, shape, this.style);
  13369. x1 = subPixelOptimizeOutputShape$1.x1;
  13370. y1 = subPixelOptimizeOutputShape$1.y1;
  13371. x2 = subPixelOptimizeOutputShape$1.x2;
  13372. y2 = subPixelOptimizeOutputShape$1.y2;
  13373. }
  13374. else {
  13375. x1 = shape.x1;
  13376. y1 = shape.y1;
  13377. x2 = shape.x2;
  13378. y2 = shape.y2;
  13379. }
  13380. var percent = shape.percent;
  13381. if (percent === 0) {
  13382. return;
  13383. }
  13384. ctx.moveTo(x1, y1);
  13385. if (percent < 1) {
  13386. x2 = x1 * (1 - percent) + x2 * percent;
  13387. y2 = y1 * (1 - percent) + y2 * percent;
  13388. }
  13389. ctx.lineTo(x2, y2);
  13390. },
  13391. /**
  13392. * Get point at percent
  13393. * @param {number} percent
  13394. * @return {Array.<number>}
  13395. */
  13396. pointAt: function (p) {
  13397. var shape = this.shape;
  13398. return [
  13399. shape.x1 * (1 - p) + shape.x2 * p,
  13400. shape.y1 * (1 - p) + shape.y2 * p
  13401. ];
  13402. }
  13403. });
  13404. /**
  13405. * Catmull-Rom spline 插值折线
  13406. * @module zrender/shape/util/smoothSpline
  13407. * @author pissang (https://www.github.com/pissang)
  13408. * Kener (@Kener-林峰, kener.linfeng@gmail.com)
  13409. * errorrik (errorrik@gmail.com)
  13410. */
  13411. /**
  13412. * @inner
  13413. */
  13414. function interpolate(p0, p1, p2, p3, t, t2, t3) {
  13415. var v0 = (p2 - p0) * 0.5;
  13416. var v1 = (p3 - p1) * 0.5;
  13417. return (2 * (p1 - p2) + v0 + v1) * t3
  13418. + (-3 * (p1 - p2) - 2 * v0 - v1) * t2
  13419. + v0 * t + p1;
  13420. }
  13421. /**
  13422. * @alias module:zrender/shape/util/smoothSpline
  13423. * @param {Array} points 线段顶点数组
  13424. * @param {boolean} isLoop
  13425. * @return {Array}
  13426. */
  13427. var smoothSpline = function (points, isLoop) {
  13428. var len$$1 = points.length;
  13429. var ret = [];
  13430. var distance$$1 = 0;
  13431. for (var i = 1; i < len$$1; i++) {
  13432. distance$$1 += distance(points[i - 1], points[i]);
  13433. }
  13434. var segs = distance$$1 / 2;
  13435. segs = segs < len$$1 ? len$$1 : segs;
  13436. for (var i = 0; i < segs; i++) {
  13437. var pos = i / (segs - 1) * (isLoop ? len$$1 : len$$1 - 1);
  13438. var idx = Math.floor(pos);
  13439. var w = pos - idx;
  13440. var p0;
  13441. var p1 = points[idx % len$$1];
  13442. var p2;
  13443. var p3;
  13444. if (!isLoop) {
  13445. p0 = points[idx === 0 ? idx : idx - 1];
  13446. p2 = points[idx > len$$1 - 2 ? len$$1 - 1 : idx + 1];
  13447. p3 = points[idx > len$$1 - 3 ? len$$1 - 1 : idx + 2];
  13448. }
  13449. else {
  13450. p0 = points[(idx - 1 + len$$1) % len$$1];
  13451. p2 = points[(idx + 1) % len$$1];
  13452. p3 = points[(idx + 2) % len$$1];
  13453. }
  13454. var w2 = w * w;
  13455. var w3 = w * w2;
  13456. ret.push([
  13457. interpolate(p0[0], p1[0], p2[0], p3[0], w, w2, w3),
  13458. interpolate(p0[1], p1[1], p2[1], p3[1], w, w2, w3)
  13459. ]);
  13460. }
  13461. return ret;
  13462. };
  13463. /**
  13464. * 贝塞尔平滑曲线
  13465. * @module zrender/shape/util/smoothBezier
  13466. * @author pissang (https://www.github.com/pissang)
  13467. * Kener (@Kener-林峰, kener.linfeng@gmail.com)
  13468. * errorrik (errorrik@gmail.com)
  13469. */
  13470. /**
  13471. * 贝塞尔平滑曲线
  13472. * @alias module:zrender/shape/util/smoothBezier
  13473. * @param {Array} points 线段顶点数组
  13474. * @param {number} smooth 平滑等级, 0-1
  13475. * @param {boolean} isLoop
  13476. * @param {Array} constraint 将计算出来的控制点约束在一个包围盒内
  13477. * 比如 [[0, 0], [100, 100]], 这个包围盒会与
  13478. * 整个折线的包围盒做一个并集用来约束控制点。
  13479. * @param {Array} 计算出来的控制点数组
  13480. */
  13481. var smoothBezier = function (points, smooth, isLoop, constraint) {
  13482. var cps = [];
  13483. var v = [];
  13484. var v1 = [];
  13485. var v2 = [];
  13486. var prevPoint;
  13487. var nextPoint;
  13488. var min$$1;
  13489. var max$$1;
  13490. if (constraint) {
  13491. min$$1 = [Infinity, Infinity];
  13492. max$$1 = [-Infinity, -Infinity];
  13493. for (var i = 0, len$$1 = points.length; i < len$$1; i++) {
  13494. min(min$$1, min$$1, points[i]);
  13495. max(max$$1, max$$1, points[i]);
  13496. }
  13497. // 与指定的包围盒做并集
  13498. min(min$$1, min$$1, constraint[0]);
  13499. max(max$$1, max$$1, constraint[1]);
  13500. }
  13501. for (var i = 0, len$$1 = points.length; i < len$$1; i++) {
  13502. var point = points[i];
  13503. if (isLoop) {
  13504. prevPoint = points[i ? i - 1 : len$$1 - 1];
  13505. nextPoint = points[(i + 1) % len$$1];
  13506. }
  13507. else {
  13508. if (i === 0 || i === len$$1 - 1) {
  13509. cps.push(clone$1(points[i]));
  13510. continue;
  13511. }
  13512. else {
  13513. prevPoint = points[i - 1];
  13514. nextPoint = points[i + 1];
  13515. }
  13516. }
  13517. sub(v, nextPoint, prevPoint);
  13518. // use degree to scale the handle length
  13519. scale(v, v, smooth);
  13520. var d0 = distance(point, prevPoint);
  13521. var d1 = distance(point, nextPoint);
  13522. var sum = d0 + d1;
  13523. if (sum !== 0) {
  13524. d0 /= sum;
  13525. d1 /= sum;
  13526. }
  13527. scale(v1, v, -d0);
  13528. scale(v2, v, d1);
  13529. var cp0 = add([], point, v1);
  13530. var cp1 = add([], point, v2);
  13531. if (constraint) {
  13532. max(cp0, cp0, min$$1);
  13533. min(cp0, cp0, max$$1);
  13534. max(cp1, cp1, min$$1);
  13535. min(cp1, cp1, max$$1);
  13536. }
  13537. cps.push(cp0);
  13538. cps.push(cp1);
  13539. }
  13540. if (isLoop) {
  13541. cps.push(cps.shift());
  13542. }
  13543. return cps;
  13544. };
  13545. function buildPath$1(ctx, shape, closePath) {
  13546. var points = shape.points;
  13547. var smooth = shape.smooth;
  13548. if (points && points.length >= 2) {
  13549. if (smooth && smooth !== 'spline') {
  13550. var controlPoints = smoothBezier(
  13551. points, smooth, closePath, shape.smoothConstraint
  13552. );
  13553. ctx.moveTo(points[0][0], points[0][1]);
  13554. var len = points.length;
  13555. for (var i = 0; i < (closePath ? len : len - 1); i++) {
  13556. var cp1 = controlPoints[i * 2];
  13557. var cp2 = controlPoints[i * 2 + 1];
  13558. var p = points[(i + 1) % len];
  13559. ctx.bezierCurveTo(
  13560. cp1[0], cp1[1], cp2[0], cp2[1], p[0], p[1]
  13561. );
  13562. }
  13563. }
  13564. else {
  13565. if (smooth === 'spline') {
  13566. points = smoothSpline(points, closePath);
  13567. }
  13568. ctx.moveTo(points[0][0], points[0][1]);
  13569. for (var i = 1, l = points.length; i < l; i++) {
  13570. ctx.lineTo(points[i][0], points[i][1]);
  13571. }
  13572. }
  13573. closePath && ctx.closePath();
  13574. }
  13575. }
  13576. /**
  13577. * 多边形
  13578. * @module zrender/shape/Polygon
  13579. */
  13580. var Polygon = Path.extend({
  13581. type: 'polygon',
  13582. shape: {
  13583. points: null,
  13584. smooth: false,
  13585. smoothConstraint: null
  13586. },
  13587. buildPath: function (ctx, shape) {
  13588. buildPath$1(ctx, shape, true);
  13589. }
  13590. });
  13591. /**
  13592. * @module zrender/graphic/shape/Polyline
  13593. */
  13594. var Polyline = Path.extend({
  13595. type: 'polyline',
  13596. shape: {
  13597. points: null,
  13598. smooth: false,
  13599. smoothConstraint: null
  13600. },
  13601. style: {
  13602. stroke: '#000',
  13603. fill: null
  13604. },
  13605. buildPath: function (ctx, shape) {
  13606. buildPath$1(ctx, shape, false);
  13607. }
  13608. });
  13609. /**
  13610. * @param {Array.<Object>} colorStops
  13611. */
  13612. var Gradient = function (colorStops) {
  13613. this.colorStops = colorStops || [];
  13614. };
  13615. Gradient.prototype = {
  13616. constructor: Gradient,
  13617. addColorStop: function (offset, color) {
  13618. this.colorStops.push({
  13619. offset: offset,
  13620. color: color
  13621. });
  13622. }
  13623. };
  13624. /**
  13625. * x, y, x2, y2 are all percent from 0 to 1
  13626. * @param {number} [x=0]
  13627. * @param {number} [y=0]
  13628. * @param {number} [x2=1]
  13629. * @param {number} [y2=0]
  13630. * @param {Array.<Object>} colorStops
  13631. * @param {boolean} [globalCoord=false]
  13632. */
  13633. var LinearGradient = function (x, y, x2, y2, colorStops, globalCoord) {
  13634. // Should do nothing more in this constructor. Because gradient can be
  13635. // declard by `color: {type: 'linear', colorStops: ...}`, where
  13636. // this constructor will not be called.
  13637. this.x = x == null ? 0 : x;
  13638. this.y = y == null ? 0 : y;
  13639. this.x2 = x2 == null ? 1 : x2;
  13640. this.y2 = y2 == null ? 0 : y2;
  13641. // Can be cloned
  13642. this.type = 'linear';
  13643. // If use global coord
  13644. this.global = globalCoord || false;
  13645. Gradient.call(this, colorStops);
  13646. };
  13647. LinearGradient.prototype = {
  13648. constructor: LinearGradient
  13649. };
  13650. inherits(LinearGradient, Gradient);
  13651. // import RadialGradient from '../graphic/RadialGradient';
  13652. // import Pattern from '../graphic/Pattern';
  13653. // import * as vector from '../core/vector';
  13654. // Most of the values can be separated by comma and/or white space.
  13655. var DILIMITER_REG = /[\s,]+/;
  13656. /**
  13657. * For big svg string, this method might be time consuming.
  13658. *
  13659. * @param {string} svg xml string
  13660. * @return {Object} xml root.
  13661. */
  13662. function parseXML(svg) {
  13663. if (isString(svg)) {
  13664. var parser = new DOMParser();
  13665. svg = parser.parseFromString(svg, 'text/xml');
  13666. }
  13667. // Document node. If using $.get, doc node may be input.
  13668. if (svg.nodeType === 9) {
  13669. svg = svg.firstChild;
  13670. }
  13671. // nodeName of <!DOCTYPE svg> is also 'svg'.
  13672. while (svg.nodeName.toLowerCase() !== 'svg' || svg.nodeType !== 1) {
  13673. svg = svg.nextSibling;
  13674. }
  13675. return svg;
  13676. }
  13677. function SVGParser() {
  13678. this._defs = {};
  13679. this._root = null;
  13680. this._isDefine = false;
  13681. this._isText = false;
  13682. }
  13683. SVGParser.prototype.parse = function (xml, opt) {
  13684. opt = opt || {};
  13685. var svg = parseXML(xml);
  13686. if (!svg) {
  13687. throw new Error('Illegal svg');
  13688. }
  13689. var root = new Group();
  13690. this._root = root;
  13691. // parse view port
  13692. var viewBox = svg.getAttribute('viewBox') || '';
  13693. // If width/height not specified, means "100%" of `opt.width/height`.
  13694. // TODO: Other percent value not supported yet.
  13695. var width = parseFloat(svg.getAttribute('width') || opt.width);
  13696. var height = parseFloat(svg.getAttribute('height') || opt.height);
  13697. // If width/height not specified, set as null for output.
  13698. isNaN(width) && (width = null);
  13699. isNaN(height) && (height = null);
  13700. // Apply inline style on svg element.
  13701. parseAttributes(svg, root, null, true);
  13702. var child = svg.firstChild;
  13703. while (child) {
  13704. this._parseNode(child, root);
  13705. child = child.nextSibling;
  13706. }
  13707. var viewBoxRect;
  13708. var viewBoxTransform;
  13709. if (viewBox) {
  13710. var viewBoxArr = trim(viewBox).split(DILIMITER_REG);
  13711. // Some invalid case like viewBox: 'none'.
  13712. if (viewBoxArr.length >= 4) {
  13713. viewBoxRect = {
  13714. x: parseFloat(viewBoxArr[0] || 0),
  13715. y: parseFloat(viewBoxArr[1] || 0),
  13716. width: parseFloat(viewBoxArr[2]),
  13717. height: parseFloat(viewBoxArr[3])
  13718. };
  13719. }
  13720. }
  13721. if (viewBoxRect && width != null && height != null) {
  13722. viewBoxTransform = makeViewBoxTransform(viewBoxRect, width, height);
  13723. if (!opt.ignoreViewBox) {
  13724. // If set transform on the output group, it probably bring trouble when
  13725. // some users only intend to show the clipped content inside the viewBox,
  13726. // but not intend to transform the output group. So we keep the output
  13727. // group no transform. If the user intend to use the viewBox as a
  13728. // camera, just set `opt.ignoreViewBox` as `true` and set transfrom
  13729. // manually according to the viewBox info in the output of this method.
  13730. var elRoot = root;
  13731. root = new Group();
  13732. root.add(elRoot);
  13733. elRoot.scale = viewBoxTransform.scale.slice();
  13734. elRoot.position = viewBoxTransform.position.slice();
  13735. }
  13736. }
  13737. // Some shapes might be overflow the viewport, which should be
  13738. // clipped despite whether the viewBox is used, as the SVG does.
  13739. if (!opt.ignoreRootClip && width != null && height != null) {
  13740. root.setClipPath(new Rect({
  13741. shape: {x: 0, y: 0, width: width, height: height}
  13742. }));
  13743. }
  13744. // Set width/height on group just for output the viewport size.
  13745. return {
  13746. root: root,
  13747. width: width,
  13748. height: height,
  13749. viewBoxRect: viewBoxRect,
  13750. viewBoxTransform: viewBoxTransform
  13751. };
  13752. };
  13753. SVGParser.prototype._parseNode = function (xmlNode, parentGroup) {
  13754. var nodeName = xmlNode.nodeName.toLowerCase();
  13755. // TODO
  13756. // support <style>...</style> in svg, where nodeName is 'style',
  13757. // CSS classes is defined globally wherever the style tags are declared.
  13758. if (nodeName === 'defs') {
  13759. // define flag
  13760. this._isDefine = true;
  13761. }
  13762. else if (nodeName === 'text') {
  13763. this._isText = true;
  13764. }
  13765. var el;
  13766. if (this._isDefine) {
  13767. var parser = defineParsers[nodeName];
  13768. if (parser) {
  13769. var def = parser.call(this, xmlNode);
  13770. var id = xmlNode.getAttribute('id');
  13771. if (id) {
  13772. this._defs[id] = def;
  13773. }
  13774. }
  13775. }
  13776. else {
  13777. var parser = nodeParsers[nodeName];
  13778. if (parser) {
  13779. el = parser.call(this, xmlNode, parentGroup);
  13780. parentGroup.add(el);
  13781. }
  13782. }
  13783. var child = xmlNode.firstChild;
  13784. while (child) {
  13785. if (child.nodeType === 1) {
  13786. this._parseNode(child, el);
  13787. }
  13788. // Is text
  13789. if (child.nodeType === 3 && this._isText) {
  13790. this._parseText(child, el);
  13791. }
  13792. child = child.nextSibling;
  13793. }
  13794. // Quit define
  13795. if (nodeName === 'defs') {
  13796. this._isDefine = false;
  13797. }
  13798. else if (nodeName === 'text') {
  13799. this._isText = false;
  13800. }
  13801. };
  13802. SVGParser.prototype._parseText = function (xmlNode, parentGroup) {
  13803. if (xmlNode.nodeType === 1) {
  13804. var dx = xmlNode.getAttribute('dx') || 0;
  13805. var dy = xmlNode.getAttribute('dy') || 0;
  13806. this._textX += parseFloat(dx);
  13807. this._textY += parseFloat(dy);
  13808. }
  13809. var text = new Text({
  13810. style: {
  13811. text: xmlNode.textContent,
  13812. transformText: true
  13813. },
  13814. position: [this._textX || 0, this._textY || 0]
  13815. });
  13816. inheritStyle(parentGroup, text);
  13817. parseAttributes(xmlNode, text, this._defs);
  13818. var fontSize = text.style.fontSize;
  13819. if (fontSize && fontSize < 9) {
  13820. // PENDING
  13821. text.style.fontSize = 9;
  13822. text.scale = text.scale || [1, 1];
  13823. text.scale[0] *= fontSize / 9;
  13824. text.scale[1] *= fontSize / 9;
  13825. }
  13826. var rect = text.getBoundingRect();
  13827. this._textX += rect.width;
  13828. parentGroup.add(text);
  13829. return text;
  13830. };
  13831. var nodeParsers = {
  13832. 'g': function (xmlNode, parentGroup) {
  13833. var g = new Group();
  13834. inheritStyle(parentGroup, g);
  13835. parseAttributes(xmlNode, g, this._defs);
  13836. return g;
  13837. },
  13838. 'rect': function (xmlNode, parentGroup) {
  13839. var rect = new Rect();
  13840. inheritStyle(parentGroup, rect);
  13841. parseAttributes(xmlNode, rect, this._defs);
  13842. rect.setShape({
  13843. x: parseFloat(xmlNode.getAttribute('x') || 0),
  13844. y: parseFloat(xmlNode.getAttribute('y') || 0),
  13845. width: parseFloat(xmlNode.getAttribute('width') || 0),
  13846. height: parseFloat(xmlNode.getAttribute('height') || 0)
  13847. });
  13848. // console.log(xmlNode.getAttribute('transform'));
  13849. // console.log(rect.transform);
  13850. return rect;
  13851. },
  13852. 'circle': function (xmlNode, parentGroup) {
  13853. var circle = new Circle();
  13854. inheritStyle(parentGroup, circle);
  13855. parseAttributes(xmlNode, circle, this._defs);
  13856. circle.setShape({
  13857. cx: parseFloat(xmlNode.getAttribute('cx') || 0),
  13858. cy: parseFloat(xmlNode.getAttribute('cy') || 0),
  13859. r: parseFloat(xmlNode.getAttribute('r') || 0)
  13860. });
  13861. return circle;
  13862. },
  13863. 'line': function (xmlNode, parentGroup) {
  13864. var line = new Line();
  13865. inheritStyle(parentGroup, line);
  13866. parseAttributes(xmlNode, line, this._defs);
  13867. line.setShape({
  13868. x1: parseFloat(xmlNode.getAttribute('x1') || 0),
  13869. y1: parseFloat(xmlNode.getAttribute('y1') || 0),
  13870. x2: parseFloat(xmlNode.getAttribute('x2') || 0),
  13871. y2: parseFloat(xmlNode.getAttribute('y2') || 0)
  13872. });
  13873. return line;
  13874. },
  13875. 'ellipse': function (xmlNode, parentGroup) {
  13876. var ellipse = new Ellipse();
  13877. inheritStyle(parentGroup, ellipse);
  13878. parseAttributes(xmlNode, ellipse, this._defs);
  13879. ellipse.setShape({
  13880. cx: parseFloat(xmlNode.getAttribute('cx') || 0),
  13881. cy: parseFloat(xmlNode.getAttribute('cy') || 0),
  13882. rx: parseFloat(xmlNode.getAttribute('rx') || 0),
  13883. ry: parseFloat(xmlNode.getAttribute('ry') || 0)
  13884. });
  13885. return ellipse;
  13886. },
  13887. 'polygon': function (xmlNode, parentGroup) {
  13888. var points = xmlNode.getAttribute('points');
  13889. if (points) {
  13890. points = parsePoints(points);
  13891. }
  13892. var polygon = new Polygon({
  13893. shape: {
  13894. points: points || []
  13895. }
  13896. });
  13897. inheritStyle(parentGroup, polygon);
  13898. parseAttributes(xmlNode, polygon, this._defs);
  13899. return polygon;
  13900. },
  13901. 'polyline': function (xmlNode, parentGroup) {
  13902. var path = new Path();
  13903. inheritStyle(parentGroup, path);
  13904. parseAttributes(xmlNode, path, this._defs);
  13905. var points = xmlNode.getAttribute('points');
  13906. if (points) {
  13907. points = parsePoints(points);
  13908. }
  13909. var polyline = new Polyline({
  13910. shape: {
  13911. points: points || []
  13912. }
  13913. });
  13914. return polyline;
  13915. },
  13916. 'image': function (xmlNode, parentGroup) {
  13917. var img = new ZImage();
  13918. inheritStyle(parentGroup, img);
  13919. parseAttributes(xmlNode, img, this._defs);
  13920. img.setStyle({
  13921. image: xmlNode.getAttribute('xlink:href'),
  13922. x: xmlNode.getAttribute('x'),
  13923. y: xmlNode.getAttribute('y'),
  13924. width: xmlNode.getAttribute('width'),
  13925. height: xmlNode.getAttribute('height')
  13926. });
  13927. return img;
  13928. },
  13929. 'text': function (xmlNode, parentGroup) {
  13930. var x = xmlNode.getAttribute('x') || 0;
  13931. var y = xmlNode.getAttribute('y') || 0;
  13932. var dx = xmlNode.getAttribute('dx') || 0;
  13933. var dy = xmlNode.getAttribute('dy') || 0;
  13934. this._textX = parseFloat(x) + parseFloat(dx);
  13935. this._textY = parseFloat(y) + parseFloat(dy);
  13936. var g = new Group();
  13937. inheritStyle(parentGroup, g);
  13938. parseAttributes(xmlNode, g, this._defs);
  13939. return g;
  13940. },
  13941. 'tspan': function (xmlNode, parentGroup) {
  13942. var x = xmlNode.getAttribute('x');
  13943. var y = xmlNode.getAttribute('y');
  13944. if (x != null) {
  13945. // new offset x
  13946. this._textX = parseFloat(x);
  13947. }
  13948. if (y != null) {
  13949. // new offset y
  13950. this._textY = parseFloat(y);
  13951. }
  13952. var dx = xmlNode.getAttribute('dx') || 0;
  13953. var dy = xmlNode.getAttribute('dy') || 0;
  13954. var g = new Group();
  13955. inheritStyle(parentGroup, g);
  13956. parseAttributes(xmlNode, g, this._defs);
  13957. this._textX += dx;
  13958. this._textY += dy;
  13959. return g;
  13960. },
  13961. 'path': function (xmlNode, parentGroup) {
  13962. // TODO svg fill rule
  13963. // https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/fill-rule
  13964. // path.style.globalCompositeOperation = 'xor';
  13965. var d = xmlNode.getAttribute('d') || '';
  13966. // Performance sensitive.
  13967. var path = createFromString(d);
  13968. inheritStyle(parentGroup, path);
  13969. parseAttributes(xmlNode, path, this._defs);
  13970. return path;
  13971. }
  13972. };
  13973. var defineParsers = {
  13974. 'lineargradient': function (xmlNode) {
  13975. var x1 = parseInt(xmlNode.getAttribute('x1') || 0, 10);
  13976. var y1 = parseInt(xmlNode.getAttribute('y1') || 0, 10);
  13977. var x2 = parseInt(xmlNode.getAttribute('x2') || 10, 10);
  13978. var y2 = parseInt(xmlNode.getAttribute('y2') || 0, 10);
  13979. var gradient = new LinearGradient(x1, y1, x2, y2);
  13980. _parseGradientColorStops(xmlNode, gradient);
  13981. return gradient;
  13982. },
  13983. 'radialgradient': function (xmlNode) {
  13984. }
  13985. };
  13986. function _parseGradientColorStops(xmlNode, gradient) {
  13987. var stop = xmlNode.firstChild;
  13988. while (stop) {
  13989. if (stop.nodeType === 1) {
  13990. var offset = stop.getAttribute('offset');
  13991. if (offset.indexOf('%') > 0) { // percentage
  13992. offset = parseInt(offset, 10) / 100;
  13993. }
  13994. else if (offset) { // number from 0 to 1
  13995. offset = parseFloat(offset);
  13996. }
  13997. else {
  13998. offset = 0;
  13999. }
  14000. var stopColor = stop.getAttribute('stop-color') || '#000000';
  14001. gradient.addColorStop(offset, stopColor);
  14002. }
  14003. stop = stop.nextSibling;
  14004. }
  14005. }
  14006. function inheritStyle(parent, child) {
  14007. if (parent && parent.__inheritedStyle) {
  14008. if (!child.__inheritedStyle) {
  14009. child.__inheritedStyle = {};
  14010. }
  14011. defaults(child.__inheritedStyle, parent.__inheritedStyle);
  14012. }
  14013. }
  14014. function parsePoints(pointsString) {
  14015. var list = trim(pointsString).split(DILIMITER_REG);
  14016. var points = [];
  14017. for (var i = 0; i < list.length; i += 2) {
  14018. var x = parseFloat(list[i]);
  14019. var y = parseFloat(list[i + 1]);
  14020. points.push([x, y]);
  14021. }
  14022. return points;
  14023. }
  14024. var attributesMap = {
  14025. 'fill': 'fill',
  14026. 'stroke': 'stroke',
  14027. 'stroke-width': 'lineWidth',
  14028. 'opacity': 'opacity',
  14029. 'fill-opacity': 'fillOpacity',
  14030. 'stroke-opacity': 'strokeOpacity',
  14031. 'stroke-dasharray': 'lineDash',
  14032. 'stroke-dashoffset': 'lineDashOffset',
  14033. 'stroke-linecap': 'lineCap',
  14034. 'stroke-linejoin': 'lineJoin',
  14035. 'stroke-miterlimit': 'miterLimit',
  14036. 'font-family': 'fontFamily',
  14037. 'font-size': 'fontSize',
  14038. 'font-style': 'fontStyle',
  14039. 'font-weight': 'fontWeight',
  14040. 'text-align': 'textAlign',
  14041. 'alignment-baseline': 'textBaseline'
  14042. };
  14043. function parseAttributes(xmlNode, el, defs, onlyInlineStyle) {
  14044. var zrStyle = el.__inheritedStyle || {};
  14045. var isTextEl = el.type === 'text';
  14046. // TODO Shadow
  14047. if (xmlNode.nodeType === 1) {
  14048. parseTransformAttribute(xmlNode, el);
  14049. extend(zrStyle, parseStyleAttribute(xmlNode));
  14050. if (!onlyInlineStyle) {
  14051. for (var svgAttrName in attributesMap) {
  14052. if (attributesMap.hasOwnProperty(svgAttrName)) {
  14053. var attrValue = xmlNode.getAttribute(svgAttrName);
  14054. if (attrValue != null) {
  14055. zrStyle[attributesMap[svgAttrName]] = attrValue;
  14056. }
  14057. }
  14058. }
  14059. }
  14060. }
  14061. var elFillProp = isTextEl ? 'textFill' : 'fill';
  14062. var elStrokeProp = isTextEl ? 'textStroke' : 'stroke';
  14063. el.style = el.style || new Style();
  14064. var elStyle = el.style;
  14065. zrStyle.fill != null && elStyle.set(elFillProp, getPaint(zrStyle.fill, defs));
  14066. zrStyle.stroke != null && elStyle.set(elStrokeProp, getPaint(zrStyle.stroke, defs));
  14067. each([
  14068. 'lineWidth', 'opacity', 'fillOpacity', 'strokeOpacity', 'miterLimit', 'fontSize'
  14069. ], function (propName) {
  14070. var elPropName = (propName === 'lineWidth' && isTextEl) ? 'textStrokeWidth' : propName;
  14071. zrStyle[propName] != null && elStyle.set(elPropName, parseFloat(zrStyle[propName]));
  14072. });
  14073. if (!zrStyle.textBaseline || zrStyle.textBaseline === 'auto') {
  14074. zrStyle.textBaseline = 'alphabetic';
  14075. }
  14076. if (zrStyle.textBaseline === 'alphabetic') {
  14077. zrStyle.textBaseline = 'bottom';
  14078. }
  14079. if (zrStyle.textAlign === 'start') {
  14080. zrStyle.textAlign = 'left';
  14081. }
  14082. if (zrStyle.textAlign === 'end') {
  14083. zrStyle.textAlign = 'right';
  14084. }
  14085. each(['lineDashOffset', 'lineCap', 'lineJoin',
  14086. 'fontWeight', 'fontFamily', 'fontStyle', 'textAlign', 'textBaseline'
  14087. ], function (propName) {
  14088. zrStyle[propName] != null && elStyle.set(propName, zrStyle[propName]);
  14089. });
  14090. if (zrStyle.lineDash) {
  14091. el.style.lineDash = trim(zrStyle.lineDash).split(DILIMITER_REG);
  14092. }
  14093. if (elStyle[elStrokeProp] && elStyle[elStrokeProp] !== 'none') {
  14094. // enable stroke
  14095. el[elStrokeProp] = true;
  14096. }
  14097. el.__inheritedStyle = zrStyle;
  14098. }
  14099. var urlRegex = /url\(\s*#(.*?)\)/;
  14100. function getPaint(str, defs) {
  14101. // if (str === 'none') {
  14102. // return;
  14103. // }
  14104. var urlMatch = defs && str && str.match(urlRegex);
  14105. if (urlMatch) {
  14106. var url = trim(urlMatch[1]);
  14107. var def = defs[url];
  14108. return def;
  14109. }
  14110. return str;
  14111. }
  14112. var transformRegex = /(translate|scale|rotate|skewX|skewY|matrix)\(([\-\s0-9\.e,]*)\)/g;
  14113. function parseTransformAttribute(xmlNode, node) {
  14114. var transform = xmlNode.getAttribute('transform');
  14115. if (transform) {
  14116. transform = transform.replace(/,/g, ' ');
  14117. var m = null;
  14118. var transformOps = [];
  14119. transform.replace(transformRegex, function (str, type, value) {
  14120. transformOps.push(type, value);
  14121. });
  14122. for (var i = transformOps.length - 1; i > 0; i -= 2) {
  14123. var value = transformOps[i];
  14124. var type = transformOps[i - 1];
  14125. m = m || create$1();
  14126. switch (type) {
  14127. case 'translate':
  14128. value = trim(value).split(DILIMITER_REG);
  14129. translate(m, m, [parseFloat(value[0]), parseFloat(value[1] || 0)]);
  14130. break;
  14131. case 'scale':
  14132. value = trim(value).split(DILIMITER_REG);
  14133. scale$1(m, m, [parseFloat(value[0]), parseFloat(value[1] || value[0])]);
  14134. break;
  14135. case 'rotate':
  14136. value = trim(value).split(DILIMITER_REG);
  14137. rotate(m, m, parseFloat(value[0]));
  14138. break;
  14139. case 'skew':
  14140. value = trim(value).split(DILIMITER_REG);
  14141. console.warn('Skew transform is not supported yet');
  14142. break;
  14143. case 'matrix':
  14144. var value = trim(value).split(DILIMITER_REG);
  14145. m[0] = parseFloat(value[0]);
  14146. m[1] = parseFloat(value[1]);
  14147. m[2] = parseFloat(value[2]);
  14148. m[3] = parseFloat(value[3]);
  14149. m[4] = parseFloat(value[4]);
  14150. m[5] = parseFloat(value[5]);
  14151. break;
  14152. }
  14153. }
  14154. node.setLocalTransform(m);
  14155. }
  14156. }
  14157. // Value may contain space.
  14158. var styleRegex = /([^\s:;]+)\s*:\s*([^:;]+)/g;
  14159. function parseStyleAttribute(xmlNode) {
  14160. var style = xmlNode.getAttribute('style');
  14161. var result = {};
  14162. if (!style) {
  14163. return result;
  14164. }
  14165. var styleList = {};
  14166. styleRegex.lastIndex = 0;
  14167. var styleRegResult;
  14168. while ((styleRegResult = styleRegex.exec(style)) != null) {
  14169. styleList[styleRegResult[1]] = styleRegResult[2];
  14170. }
  14171. for (var svgAttrName in attributesMap) {
  14172. if (attributesMap.hasOwnProperty(svgAttrName) && styleList[svgAttrName] != null) {
  14173. result[attributesMap[svgAttrName]] = styleList[svgAttrName];
  14174. }
  14175. }
  14176. return result;
  14177. }
  14178. /**
  14179. * @param {Array.<number>} viewBoxRect
  14180. * @param {number} width
  14181. * @param {number} height
  14182. * @return {Object} {scale, position}
  14183. */
  14184. function makeViewBoxTransform(viewBoxRect, width, height) {
  14185. var scaleX = width / viewBoxRect.width;
  14186. var scaleY = height / viewBoxRect.height;
  14187. var scale = Math.min(scaleX, scaleY);
  14188. // preserveAspectRatio 'xMidYMid'
  14189. var viewBoxScale = [scale, scale];
  14190. var viewBoxPosition = [
  14191. -(viewBoxRect.x + viewBoxRect.width / 2) * scale + width / 2,
  14192. -(viewBoxRect.y + viewBoxRect.height / 2) * scale + height / 2
  14193. ];
  14194. return {
  14195. scale: viewBoxScale,
  14196. position: viewBoxPosition
  14197. };
  14198. }
  14199. /**
  14200. * @param {string|XMLElement} xml
  14201. * @param {Object} [opt]
  14202. * @param {number} [opt.width] Default width if svg width not specified or is a percent value.
  14203. * @param {number} [opt.height] Default height if svg height not specified or is a percent value.
  14204. * @param {boolean} [opt.ignoreViewBox]
  14205. * @param {boolean} [opt.ignoreRootClip]
  14206. * @return {Object} result:
  14207. * {
  14208. * root: Group, The root of the the result tree of zrender shapes,
  14209. * width: number, the viewport width of the SVG,
  14210. * height: number, the viewport height of the SVG,
  14211. * viewBoxRect: {x, y, width, height}, the declared viewBox rect of the SVG, if exists,
  14212. * viewBoxTransform: the {scale, position} calculated by viewBox and viewport, is exists.
  14213. * }
  14214. */
  14215. function parseSVG(xml, opt) {
  14216. var parser = new SVGParser();
  14217. return parser.parse(xml, opt);
  14218. }
  14219. // CompoundPath to improve performance
  14220. var CompoundPath = Path.extend({
  14221. type: 'compound',
  14222. shape: {
  14223. paths: null
  14224. },
  14225. _updatePathDirty: function () {
  14226. var dirtyPath = this.__dirtyPath;
  14227. var paths = this.shape.paths;
  14228. for (var i = 0; i < paths.length; i++) {
  14229. // Mark as dirty if any subpath is dirty
  14230. dirtyPath = dirtyPath || paths[i].__dirtyPath;
  14231. }
  14232. this.__dirtyPath = dirtyPath;
  14233. this.__dirty = this.__dirty || dirtyPath;
  14234. },
  14235. beforeBrush: function () {
  14236. this._updatePathDirty();
  14237. var paths = this.shape.paths || [];
  14238. var scale = this.getGlobalScale();
  14239. // Update path scale
  14240. for (var i = 0; i < paths.length; i++) {
  14241. if (!paths[i].path) {
  14242. paths[i].createPathProxy();
  14243. }
  14244. paths[i].path.setScale(scale[0], scale[1], paths[i].segmentIgnoreThreshold);
  14245. }
  14246. },
  14247. buildPath: function (ctx, shape) {
  14248. var paths = shape.paths || [];
  14249. for (var i = 0; i < paths.length; i++) {
  14250. paths[i].buildPath(ctx, paths[i].shape, true);
  14251. }
  14252. },
  14253. afterBrush: function () {
  14254. var paths = this.shape.paths || [];
  14255. for (var i = 0; i < paths.length; i++) {
  14256. paths[i].__dirtyPath = false;
  14257. }
  14258. },
  14259. getBoundingRect: function () {
  14260. this._updatePathDirty();
  14261. return Path.prototype.getBoundingRect.call(this);
  14262. }
  14263. });
  14264. /**
  14265. * Displayable for incremental rendering. It will be rendered in a separate layer
  14266. * IncrementalDisplay have two main methods. `clearDisplayables` and `addDisplayables`
  14267. * addDisplayables will render the added displayables incremetally.
  14268. *
  14269. * It use a not clearFlag to tell the painter don't clear the layer if it's the first element.
  14270. */
  14271. // TODO Style override ?
  14272. function IncrementalDisplayble(opts) {
  14273. Displayable.call(this, opts);
  14274. this._displayables = [];
  14275. this._temporaryDisplayables = [];
  14276. this._cursor = 0;
  14277. this.notClear = true;
  14278. }
  14279. IncrementalDisplayble.prototype.incremental = true;
  14280. IncrementalDisplayble.prototype.clearDisplaybles = function () {
  14281. this._displayables = [];
  14282. this._temporaryDisplayables = [];
  14283. this._cursor = 0;
  14284. this.dirty();
  14285. this.notClear = false;
  14286. };
  14287. IncrementalDisplayble.prototype.addDisplayable = function (displayable, notPersistent) {
  14288. if (notPersistent) {
  14289. this._temporaryDisplayables.push(displayable);
  14290. }
  14291. else {
  14292. this._displayables.push(displayable);
  14293. }
  14294. this.dirty();
  14295. };
  14296. IncrementalDisplayble.prototype.addDisplayables = function (displayables, notPersistent) {
  14297. notPersistent = notPersistent || false;
  14298. for (var i = 0; i < displayables.length; i++) {
  14299. this.addDisplayable(displayables[i], notPersistent);
  14300. }
  14301. };
  14302. IncrementalDisplayble.prototype.eachPendingDisplayable = function (cb) {
  14303. for (var i = this._cursor; i < this._displayables.length; i++) {
  14304. cb && cb(this._displayables[i]);
  14305. }
  14306. for (var i = 0; i < this._temporaryDisplayables.length; i++) {
  14307. cb && cb(this._temporaryDisplayables[i]);
  14308. }
  14309. };
  14310. IncrementalDisplayble.prototype.update = function () {
  14311. this.updateTransform();
  14312. for (var i = this._cursor; i < this._displayables.length; i++) {
  14313. var displayable = this._displayables[i];
  14314. // PENDING
  14315. displayable.parent = this;
  14316. displayable.update();
  14317. displayable.parent = null;
  14318. }
  14319. for (var i = 0; i < this._temporaryDisplayables.length; i++) {
  14320. var displayable = this._temporaryDisplayables[i];
  14321. // PENDING
  14322. displayable.parent = this;
  14323. displayable.update();
  14324. displayable.parent = null;
  14325. }
  14326. };
  14327. IncrementalDisplayble.prototype.brush = function (ctx, prevEl) {
  14328. // Render persistant displayables.
  14329. for (var i = this._cursor; i < this._displayables.length; i++) {
  14330. var displayable = this._displayables[i];
  14331. displayable.beforeBrush && displayable.beforeBrush(ctx);
  14332. displayable.brush(ctx, i === this._cursor ? null : this._displayables[i - 1]);
  14333. displayable.afterBrush && displayable.afterBrush(ctx);
  14334. }
  14335. this._cursor = i;
  14336. // Render temporary displayables.
  14337. for (var i = 0; i < this._temporaryDisplayables.length; i++) {
  14338. var displayable = this._temporaryDisplayables[i];
  14339. displayable.beforeBrush && displayable.beforeBrush(ctx);
  14340. displayable.brush(ctx, i === 0 ? null : this._temporaryDisplayables[i - 1]);
  14341. displayable.afterBrush && displayable.afterBrush(ctx);
  14342. }
  14343. this._temporaryDisplayables = [];
  14344. this.notClear = true;
  14345. };
  14346. var m = [];
  14347. IncrementalDisplayble.prototype.getBoundingRect = function () {
  14348. if (!this._rect) {
  14349. var rect = new BoundingRect(Infinity, Infinity, -Infinity, -Infinity);
  14350. for (var i = 0; i < this._displayables.length; i++) {
  14351. var displayable = this._displayables[i];
  14352. var childRect = displayable.getBoundingRect().clone();
  14353. if (displayable.needLocalTransform()) {
  14354. childRect.applyTransform(displayable.getLocalTransform(m));
  14355. }
  14356. rect.union(childRect);
  14357. }
  14358. this._rect = rect;
  14359. }
  14360. return this._rect;
  14361. };
  14362. IncrementalDisplayble.prototype.contain = function (x, y) {
  14363. var localPos = this.transformCoordToLocal(x, y);
  14364. var rect = this.getBoundingRect();
  14365. if (rect.contain(localPos[0], localPos[1])) {
  14366. for (var i = 0; i < this._displayables.length; i++) {
  14367. var displayable = this._displayables[i];
  14368. if (displayable.contain(x, y)) {
  14369. return true;
  14370. }
  14371. }
  14372. }
  14373. return false;
  14374. };
  14375. inherits(IncrementalDisplayble, Displayable);
  14376. /**
  14377. * 圆弧
  14378. * @module zrender/graphic/shape/Arc
  14379. */
  14380. var Arc = Path.extend({
  14381. type: 'arc',
  14382. shape: {
  14383. cx: 0,
  14384. cy: 0,
  14385. r: 0,
  14386. startAngle: 0,
  14387. endAngle: Math.PI * 2,
  14388. clockwise: true
  14389. },
  14390. style: {
  14391. stroke: '#000',
  14392. fill: null
  14393. },
  14394. buildPath: function (ctx, shape) {
  14395. var x = shape.cx;
  14396. var y = shape.cy;
  14397. var r = Math.max(shape.r, 0);
  14398. var startAngle = shape.startAngle;
  14399. var endAngle = shape.endAngle;
  14400. var clockwise = shape.clockwise;
  14401. var unitX = Math.cos(startAngle);
  14402. var unitY = Math.sin(startAngle);
  14403. ctx.moveTo(unitX * r + x, unitY * r + y);
  14404. ctx.arc(x, y, r, startAngle, endAngle, !clockwise);
  14405. }
  14406. });
  14407. /**
  14408. * 贝塞尔曲线
  14409. * @module zrender/shape/BezierCurve
  14410. */
  14411. var out = [];
  14412. function someVectorAt(shape, t, isTangent) {
  14413. var cpx2 = shape.cpx2;
  14414. var cpy2 = shape.cpy2;
  14415. if (cpx2 === null || cpy2 === null) {
  14416. return [
  14417. (isTangent ? cubicDerivativeAt : cubicAt)(shape.x1, shape.cpx1, shape.cpx2, shape.x2, t),
  14418. (isTangent ? cubicDerivativeAt : cubicAt)(shape.y1, shape.cpy1, shape.cpy2, shape.y2, t)
  14419. ];
  14420. }
  14421. else {
  14422. return [
  14423. (isTangent ? quadraticDerivativeAt : quadraticAt)(shape.x1, shape.cpx1, shape.x2, t),
  14424. (isTangent ? quadraticDerivativeAt : quadraticAt)(shape.y1, shape.cpy1, shape.y2, t)
  14425. ];
  14426. }
  14427. }
  14428. var BezierCurve = Path.extend({
  14429. type: 'bezier-curve',
  14430. shape: {
  14431. x1: 0,
  14432. y1: 0,
  14433. x2: 0,
  14434. y2: 0,
  14435. cpx1: 0,
  14436. cpy1: 0,
  14437. // cpx2: 0,
  14438. // cpy2: 0
  14439. // Curve show percent, for animating
  14440. percent: 1
  14441. },
  14442. style: {
  14443. stroke: '#000',
  14444. fill: null
  14445. },
  14446. buildPath: function (ctx, shape) {
  14447. var x1 = shape.x1;
  14448. var y1 = shape.y1;
  14449. var x2 = shape.x2;
  14450. var y2 = shape.y2;
  14451. var cpx1 = shape.cpx1;
  14452. var cpy1 = shape.cpy1;
  14453. var cpx2 = shape.cpx2;
  14454. var cpy2 = shape.cpy2;
  14455. var percent = shape.percent;
  14456. if (percent === 0) {
  14457. return;
  14458. }
  14459. ctx.moveTo(x1, y1);
  14460. if (cpx2 == null || cpy2 == null) {
  14461. if (percent < 1) {
  14462. quadraticSubdivide(
  14463. x1, cpx1, x2, percent, out
  14464. );
  14465. cpx1 = out[1];
  14466. x2 = out[2];
  14467. quadraticSubdivide(
  14468. y1, cpy1, y2, percent, out
  14469. );
  14470. cpy1 = out[1];
  14471. y2 = out[2];
  14472. }
  14473. ctx.quadraticCurveTo(
  14474. cpx1, cpy1,
  14475. x2, y2
  14476. );
  14477. }
  14478. else {
  14479. if (percent < 1) {
  14480. cubicSubdivide(
  14481. x1, cpx1, cpx2, x2, percent, out
  14482. );
  14483. cpx1 = out[1];
  14484. cpx2 = out[2];
  14485. x2 = out[3];
  14486. cubicSubdivide(
  14487. y1, cpy1, cpy2, y2, percent, out
  14488. );
  14489. cpy1 = out[1];
  14490. cpy2 = out[2];
  14491. y2 = out[3];
  14492. }
  14493. ctx.bezierCurveTo(
  14494. cpx1, cpy1,
  14495. cpx2, cpy2,
  14496. x2, y2
  14497. );
  14498. }
  14499. },
  14500. /**
  14501. * Get point at percent
  14502. * @param {number} t
  14503. * @return {Array.<number>}
  14504. */
  14505. pointAt: function (t) {
  14506. return someVectorAt(this.shape, t, false);
  14507. },
  14508. /**
  14509. * Get tangent at percent
  14510. * @param {number} t
  14511. * @return {Array.<number>}
  14512. */
  14513. tangentAt: function (t) {
  14514. var p = someVectorAt(this.shape, t, true);
  14515. return normalize(p, p);
  14516. }
  14517. });
  14518. /**
  14519. * 水滴形状
  14520. * @module zrender/graphic/shape/Droplet
  14521. */
  14522. var Droplet = Path.extend({
  14523. type: 'droplet',
  14524. shape: {
  14525. cx: 0, cy: 0,
  14526. width: 0, height: 0
  14527. },
  14528. buildPath: function (ctx, shape) {
  14529. var x = shape.cx;
  14530. var y = shape.cy;
  14531. var a = shape.width;
  14532. var b = shape.height;
  14533. ctx.moveTo(x, y + a);
  14534. ctx.bezierCurveTo(
  14535. x + a,
  14536. y + a,
  14537. x + a * 3 / 2,
  14538. y - a / 3,
  14539. x,
  14540. y - b
  14541. );
  14542. ctx.bezierCurveTo(
  14543. x - a * 3 / 2,
  14544. y - a / 3,
  14545. x - a,
  14546. y + a,
  14547. x,
  14548. y + a
  14549. );
  14550. ctx.closePath();
  14551. }
  14552. });
  14553. /**
  14554. * 心形
  14555. * @module zrender/graphic/shape/Heart
  14556. */
  14557. var Heart = Path.extend({
  14558. type: 'heart',
  14559. shape: {
  14560. cx: 0,
  14561. cy: 0,
  14562. width: 0,
  14563. height: 0
  14564. },
  14565. buildPath: function (ctx, shape) {
  14566. var x = shape.cx;
  14567. var y = shape.cy;
  14568. var a = shape.width;
  14569. var b = shape.height;
  14570. ctx.moveTo(x, y);
  14571. ctx.bezierCurveTo(
  14572. x + a / 2, y - b * 2 / 3,
  14573. x + a * 2, y + b / 3,
  14574. x, y + b
  14575. );
  14576. ctx.bezierCurveTo(
  14577. x - a * 2, y + b / 3,
  14578. x - a / 2, y - b * 2 / 3,
  14579. x, y
  14580. );
  14581. }
  14582. });
  14583. /**
  14584. * 正多边形
  14585. * @module zrender/shape/Isogon
  14586. */
  14587. var PI$1 = Math.PI;
  14588. var sin = Math.sin;
  14589. var cos = Math.cos;
  14590. var Isogon = Path.extend({
  14591. type: 'isogon',
  14592. shape: {
  14593. x: 0, y: 0,
  14594. r: 0, n: 0
  14595. },
  14596. buildPath: function (ctx, shape) {
  14597. var n = shape.n;
  14598. if (!n || n < 2) {
  14599. return;
  14600. }
  14601. var x = shape.x;
  14602. var y = shape.y;
  14603. var r = shape.r;
  14604. var dStep = 2 * PI$1 / n;
  14605. var deg = -PI$1 / 2;
  14606. ctx.moveTo(x + r * cos(deg), y + r * sin(deg));
  14607. for (var i = 0, end = n - 1; i < end; i++) {
  14608. deg += dStep;
  14609. ctx.lineTo(x + r * cos(deg), y + r * sin(deg));
  14610. }
  14611. ctx.closePath();
  14612. return;
  14613. }
  14614. });
  14615. /**
  14616. * 圆环
  14617. * @module zrender/graphic/shape/Ring
  14618. */
  14619. var Ring = Path.extend({
  14620. type: 'ring',
  14621. shape: {
  14622. cx: 0,
  14623. cy: 0,
  14624. r: 0,
  14625. r0: 0
  14626. },
  14627. buildPath: function (ctx, shape) {
  14628. var x = shape.cx;
  14629. var y = shape.cy;
  14630. var PI2 = Math.PI * 2;
  14631. ctx.moveTo(x + shape.r, y);
  14632. ctx.arc(x, y, shape.r, 0, PI2, false);
  14633. ctx.moveTo(x + shape.r0, y);
  14634. ctx.arc(x, y, shape.r0, 0, PI2, true);
  14635. }
  14636. });
  14637. /**
  14638. * 玫瑰线
  14639. * @module zrender/graphic/shape/Rose
  14640. */
  14641. var sin$1 = Math.sin;
  14642. var cos$1 = Math.cos;
  14643. var radian = Math.PI / 180;
  14644. var Rose = Path.extend({
  14645. type: 'rose',
  14646. shape: {
  14647. cx: 0,
  14648. cy: 0,
  14649. r: [],
  14650. k: 0,
  14651. n: 1
  14652. },
  14653. style: {
  14654. stroke: '#000',
  14655. fill: null
  14656. },
  14657. buildPath: function (ctx, shape) {
  14658. var x;
  14659. var y;
  14660. var R = shape.r;
  14661. var r;
  14662. var k = shape.k;
  14663. var n = shape.n;
  14664. var x0 = shape.cx;
  14665. var y0 = shape.cy;
  14666. ctx.moveTo(x0, y0);
  14667. for (var i = 0, len = R.length; i < len; i++) {
  14668. r = R[i];
  14669. for (var j = 0; j <= 360 * n; j++) {
  14670. x = r
  14671. * sin$1(k / n * j % 360 * radian)
  14672. * cos$1(j * radian)
  14673. + x0;
  14674. y = r
  14675. * sin$1(k / n * j % 360 * radian)
  14676. * sin$1(j * radian)
  14677. + y0;
  14678. ctx.lineTo(x, y);
  14679. }
  14680. }
  14681. }
  14682. });
  14683. // Fix weird bug in some version of IE11 (like 11.0.9600.178**),
  14684. // where exception "unexpected call to method or property access"
  14685. // might be thrown when calling ctx.fill or ctx.stroke after a path
  14686. // whose area size is zero is drawn and ctx.clip() is called and
  14687. // shadowBlur is set. See #4572, #3112, #5777.
  14688. // (e.g.,
  14689. // ctx.moveTo(10, 10);
  14690. // ctx.lineTo(20, 10);
  14691. // ctx.closePath();
  14692. // ctx.clip();
  14693. // ctx.shadowBlur = 10;
  14694. // ...
  14695. // ctx.fill();
  14696. // )
  14697. var shadowTemp = [
  14698. ['shadowBlur', 0],
  14699. ['shadowColor', '#000'],
  14700. ['shadowOffsetX', 0],
  14701. ['shadowOffsetY', 0]
  14702. ];
  14703. var fixClipWithShadow = function (orignalBrush) {
  14704. // version string can be: '11.0'
  14705. return (env$1.browser.ie && env$1.browser.version >= 11)
  14706. ? function () {
  14707. var clipPaths = this.__clipPaths;
  14708. var style = this.style;
  14709. var modified;
  14710. if (clipPaths) {
  14711. for (var i = 0; i < clipPaths.length; i++) {
  14712. var clipPath = clipPaths[i];
  14713. var shape = clipPath && clipPath.shape;
  14714. var type = clipPath && clipPath.type;
  14715. if (shape && (
  14716. (type === 'sector' && shape.startAngle === shape.endAngle)
  14717. || (type === 'rect' && (!shape.width || !shape.height))
  14718. )) {
  14719. for (var j = 0; j < shadowTemp.length; j++) {
  14720. // It is save to put shadowTemp static, because shadowTemp
  14721. // will be all modified each item brush called.
  14722. shadowTemp[j][2] = style[shadowTemp[j][0]];
  14723. style[shadowTemp[j][0]] = shadowTemp[j][1];
  14724. }
  14725. modified = true;
  14726. break;
  14727. }
  14728. }
  14729. }
  14730. orignalBrush.apply(this, arguments);
  14731. if (modified) {
  14732. for (var j = 0; j < shadowTemp.length; j++) {
  14733. style[shadowTemp[j][0]] = shadowTemp[j][2];
  14734. }
  14735. }
  14736. }
  14737. : orignalBrush;
  14738. };
  14739. /**
  14740. * 扇形
  14741. * @module zrender/graphic/shape/Sector
  14742. */
  14743. var Sector = Path.extend({
  14744. type: 'sector',
  14745. shape: {
  14746. cx: 0,
  14747. cy: 0,
  14748. r0: 0,
  14749. r: 0,
  14750. startAngle: 0,
  14751. endAngle: Math.PI * 2,
  14752. clockwise: true
  14753. },
  14754. brush: fixClipWithShadow(Path.prototype.brush),
  14755. buildPath: function (ctx, shape) {
  14756. var x = shape.cx;
  14757. var y = shape.cy;
  14758. var r0 = Math.max(shape.r0 || 0, 0);
  14759. var r = Math.max(shape.r, 0);
  14760. var startAngle = shape.startAngle;
  14761. var endAngle = shape.endAngle;
  14762. var clockwise = shape.clockwise;
  14763. var unitX = Math.cos(startAngle);
  14764. var unitY = Math.sin(startAngle);
  14765. ctx.moveTo(unitX * r0 + x, unitY * r0 + y);
  14766. ctx.lineTo(unitX * r + x, unitY * r + y);
  14767. ctx.arc(x, y, r, startAngle, endAngle, !clockwise);
  14768. ctx.lineTo(
  14769. Math.cos(endAngle) * r0 + x,
  14770. Math.sin(endAngle) * r0 + y
  14771. );
  14772. if (r0 !== 0) {
  14773. ctx.arc(x, y, r0, endAngle, startAngle, clockwise);
  14774. }
  14775. ctx.closePath();
  14776. }
  14777. });
  14778. /**
  14779. * n角星(n>3)
  14780. * @module zrender/graphic/shape/Star
  14781. */
  14782. var PI$2 = Math.PI;
  14783. var cos$2 = Math.cos;
  14784. var sin$2 = Math.sin;
  14785. var Star = Path.extend({
  14786. type: 'star',
  14787. shape: {
  14788. cx: 0,
  14789. cy: 0,
  14790. n: 3,
  14791. r0: null,
  14792. r: 0
  14793. },
  14794. buildPath: function (ctx, shape) {
  14795. var n = shape.n;
  14796. if (!n || n < 2) {
  14797. return;
  14798. }
  14799. var x = shape.cx;
  14800. var y = shape.cy;
  14801. var r = shape.r;
  14802. var r0 = shape.r0;
  14803. // 如果未指定内部顶点外接圆半径,则自动计算
  14804. if (r0 == null) {
  14805. r0 = n > 4
  14806. // 相隔的外部顶点的连线的交点,
  14807. // 被取为内部交点,以此计算r0
  14808. ? r * cos$2(2 * PI$2 / n) / cos$2(PI$2 / n)
  14809. // 二三四角星的特殊处理
  14810. : r / 3;
  14811. }
  14812. var dStep = PI$2 / n;
  14813. var deg = -PI$2 / 2;
  14814. var xStart = x + r * cos$2(deg);
  14815. var yStart = y + r * sin$2(deg);
  14816. deg += dStep;
  14817. // 记录边界点,用于判断inside
  14818. ctx.moveTo(xStart, yStart);
  14819. for (var i = 0, end = n * 2 - 1, ri; i < end; i++) {
  14820. ri = i % 2 === 0 ? r0 : r;
  14821. ctx.lineTo(x + ri * cos$2(deg), y + ri * sin$2(deg));
  14822. deg += dStep;
  14823. }
  14824. ctx.closePath();
  14825. }
  14826. });
  14827. /**
  14828. * 内外旋轮曲线
  14829. * @module zrender/graphic/shape/Trochold
  14830. */
  14831. var cos$3 = Math.cos;
  14832. var sin$3 = Math.sin;
  14833. var Trochoid = Path.extend({
  14834. type: 'trochoid',
  14835. shape: {
  14836. cx: 0,
  14837. cy: 0,
  14838. r: 0,
  14839. r0: 0,
  14840. d: 0,
  14841. location: 'out'
  14842. },
  14843. style: {
  14844. stroke: '#000',
  14845. fill: null
  14846. },
  14847. buildPath: function (ctx, shape) {
  14848. var x1;
  14849. var y1;
  14850. var x2;
  14851. var y2;
  14852. var R = shape.r;
  14853. var r = shape.r0;
  14854. var d = shape.d;
  14855. var offsetX = shape.cx;
  14856. var offsetY = shape.cy;
  14857. var delta = shape.location === 'out' ? 1 : -1;
  14858. if (shape.location && R <= r) {
  14859. return;
  14860. }
  14861. var num = 0;
  14862. var i = 1;
  14863. var theta;
  14864. x1 = (R + delta * r) * cos$3(0)
  14865. - delta * d * cos$3(0) + offsetX;
  14866. y1 = (R + delta * r) * sin$3(0)
  14867. - d * sin$3(0) + offsetY;
  14868. ctx.moveTo(x1, y1);
  14869. // 计算结束时的i
  14870. do {
  14871. num++;
  14872. }
  14873. while ((r * num) % (R + delta * r) !== 0);
  14874. do {
  14875. theta = Math.PI / 180 * i;
  14876. x2 = (R + delta * r) * cos$3(theta)
  14877. - delta * d * cos$3((R / r + delta) * theta)
  14878. + offsetX;
  14879. y2 = (R + delta * r) * sin$3(theta)
  14880. - d * sin$3((R / r + delta) * theta)
  14881. + offsetY;
  14882. ctx.lineTo(x2, y2);
  14883. i++;
  14884. }
  14885. while (i <= (r * num) / (R + delta * r) * 360);
  14886. }
  14887. });
  14888. /**
  14889. * x, y, r are all percent from 0 to 1
  14890. * @param {number} [x=0.5]
  14891. * @param {number} [y=0.5]
  14892. * @param {number} [r=0.5]
  14893. * @param {Array.<Object>} [colorStops]
  14894. * @param {boolean} [globalCoord=false]
  14895. */
  14896. var RadialGradient = function (x, y, r, colorStops, globalCoord) {
  14897. // Should do nothing more in this constructor. Because gradient can be
  14898. // declard by `color: {type: 'radial', colorStops: ...}`, where
  14899. // this constructor will not be called.
  14900. this.x = x == null ? 0.5 : x;
  14901. this.y = y == null ? 0.5 : y;
  14902. this.r = r == null ? 0.5 : r;
  14903. // Can be cloned
  14904. this.type = 'radial';
  14905. // If use global coord
  14906. this.global = globalCoord || false;
  14907. Gradient.call(this, colorStops);
  14908. };
  14909. RadialGradient.prototype = {
  14910. constructor: RadialGradient
  14911. };
  14912. inherits(RadialGradient, Gradient);
  14913. /**
  14914. * Do not mount those modules on 'src/zrender' for better tree shaking.
  14915. */
  14916. var svgURI = 'http://www.w3.org/2000/svg';
  14917. function createElement(name) {
  14918. return document.createElementNS(svgURI, name);
  14919. }
  14920. // TODO
  14921. // 1. shadow
  14922. // 2. Image: sx, sy, sw, sh
  14923. var CMD$3 = PathProxy.CMD;
  14924. var arrayJoin = Array.prototype.join;
  14925. var NONE = 'none';
  14926. var mathRound = Math.round;
  14927. var mathSin$3 = Math.sin;
  14928. var mathCos$3 = Math.cos;
  14929. var PI$3 = Math.PI;
  14930. var PI2$4 = Math.PI * 2;
  14931. var degree = 180 / PI$3;
  14932. var EPSILON$3 = 1e-4;
  14933. function round4(val) {
  14934. return mathRound(val * 1e4) / 1e4;
  14935. }
  14936. function isAroundZero$1(val) {
  14937. return val < EPSILON$3 && val > -EPSILON$3;
  14938. }
  14939. function pathHasFill(style, isText) {
  14940. var fill = isText ? style.textFill : style.fill;
  14941. return fill != null && fill !== NONE;
  14942. }
  14943. function pathHasStroke(style, isText) {
  14944. var stroke = isText ? style.textStroke : style.stroke;
  14945. return stroke != null && stroke !== NONE;
  14946. }
  14947. function setTransform(svgEl, m) {
  14948. if (m) {
  14949. attr(svgEl, 'transform', 'matrix(' + arrayJoin.call(m, ',') + ')');
  14950. }
  14951. }
  14952. function attr(el, key, val) {
  14953. if (!val || val.type !== 'linear' && val.type !== 'radial') {
  14954. // Don't set attribute for gradient, since it need new dom nodes
  14955. el.setAttribute(key, val);
  14956. }
  14957. }
  14958. function attrXLink(el, key, val) {
  14959. el.setAttributeNS('http://www.w3.org/1999/xlink', key, val);
  14960. }
  14961. function bindStyle(svgEl, style, isText, el) {
  14962. if (pathHasFill(style, isText)) {
  14963. var fill = isText ? style.textFill : style.fill;
  14964. fill = fill === 'transparent' ? NONE : fill;
  14965. attr(svgEl, 'fill', fill);
  14966. attr(svgEl, 'fill-opacity', style.fillOpacity != null ? style.fillOpacity * style.opacity : style.opacity);
  14967. }
  14968. else {
  14969. attr(svgEl, 'fill', NONE);
  14970. }
  14971. if (pathHasStroke(style, isText)) {
  14972. var stroke = isText ? style.textStroke : style.stroke;
  14973. stroke = stroke === 'transparent' ? NONE : stroke;
  14974. attr(svgEl, 'stroke', stroke);
  14975. var strokeWidth = isText
  14976. ? style.textStrokeWidth
  14977. : style.lineWidth;
  14978. var strokeScale = !isText && style.strokeNoScale
  14979. ? el.getLineScale()
  14980. : 1;
  14981. attr(svgEl, 'stroke-width', strokeWidth / strokeScale);
  14982. // stroke then fill for text; fill then stroke for others
  14983. attr(svgEl, 'paint-order', isText ? 'stroke' : 'fill');
  14984. attr(svgEl, 'stroke-opacity', style.strokeOpacity != null ? style.strokeOpacity : style.opacity);
  14985. var lineDash = style.lineDash;
  14986. if (lineDash) {
  14987. attr(svgEl, 'stroke-dasharray', style.lineDash.join(','));
  14988. attr(svgEl, 'stroke-dashoffset', mathRound(style.lineDashOffset || 0));
  14989. }
  14990. else {
  14991. attr(svgEl, 'stroke-dasharray', '');
  14992. }
  14993. // PENDING
  14994. style.lineCap && attr(svgEl, 'stroke-linecap', style.lineCap);
  14995. style.lineJoin && attr(svgEl, 'stroke-linejoin', style.lineJoin);
  14996. style.miterLimit && attr(svgEl, 'stroke-miterlimit', style.miterLimit);
  14997. }
  14998. else {
  14999. attr(svgEl, 'stroke', NONE);
  15000. }
  15001. }
  15002. /***************************************************
  15003. * PATH
  15004. **************************************************/
  15005. function pathDataToString(path) {
  15006. var str = [];
  15007. var data = path.data;
  15008. var dataLength = path.len();
  15009. for (var i = 0; i < dataLength;) {
  15010. var cmd = data[i++];
  15011. var cmdStr = '';
  15012. var nData = 0;
  15013. switch (cmd) {
  15014. case CMD$3.M:
  15015. cmdStr = 'M';
  15016. nData = 2;
  15017. break;
  15018. case CMD$3.L:
  15019. cmdStr = 'L';
  15020. nData = 2;
  15021. break;
  15022. case CMD$3.Q:
  15023. cmdStr = 'Q';
  15024. nData = 4;
  15025. break;
  15026. case CMD$3.C:
  15027. cmdStr = 'C';
  15028. nData = 6;
  15029. break;
  15030. case CMD$3.A:
  15031. var cx = data[i++];
  15032. var cy = data[i++];
  15033. var rx = data[i++];
  15034. var ry = data[i++];
  15035. var theta = data[i++];
  15036. var dTheta = data[i++];
  15037. var psi = data[i++];
  15038. var clockwise = data[i++];
  15039. var dThetaPositive = Math.abs(dTheta);
  15040. var isCircle = isAroundZero$1(dThetaPositive - PI2$4)
  15041. || (clockwise ? dTheta >= PI2$4 : -dTheta >= PI2$4);
  15042. // Mapping to 0~2PI
  15043. var unifiedTheta = dTheta > 0 ? dTheta % PI2$4 : (dTheta % PI2$4 + PI2$4);
  15044. var large = false;
  15045. if (isCircle) {
  15046. large = true;
  15047. }
  15048. else if (isAroundZero$1(dThetaPositive)) {
  15049. large = false;
  15050. }
  15051. else {
  15052. large = (unifiedTheta >= PI$3) === !!clockwise;
  15053. }
  15054. var x0 = round4(cx + rx * mathCos$3(theta));
  15055. var y0 = round4(cy + ry * mathSin$3(theta));
  15056. // It will not draw if start point and end point are exactly the same
  15057. // We need to shift the end point with a small value
  15058. // FIXME A better way to draw circle ?
  15059. if (isCircle) {
  15060. if (clockwise) {
  15061. dTheta = PI2$4 - 1e-4;
  15062. }
  15063. else {
  15064. dTheta = -PI2$4 + 1e-4;
  15065. }
  15066. large = true;
  15067. if (i === 9) {
  15068. // Move to (x0, y0) only when CMD.A comes at the
  15069. // first position of a shape.
  15070. // For instance, when drawing a ring, CMD.A comes
  15071. // after CMD.M, so it's unnecessary to move to
  15072. // (x0, y0).
  15073. str.push('M', x0, y0);
  15074. }
  15075. }
  15076. var x = round4(cx + rx * mathCos$3(theta + dTheta));
  15077. var y = round4(cy + ry * mathSin$3(theta + dTheta));
  15078. // FIXME Ellipse
  15079. str.push('A', round4(rx), round4(ry),
  15080. mathRound(psi * degree), +large, +clockwise, x, y);
  15081. break;
  15082. case CMD$3.Z:
  15083. cmdStr = 'Z';
  15084. break;
  15085. case CMD$3.R:
  15086. var x = round4(data[i++]);
  15087. var y = round4(data[i++]);
  15088. var w = round4(data[i++]);
  15089. var h = round4(data[i++]);
  15090. str.push(
  15091. 'M', x, y,
  15092. 'L', x + w, y,
  15093. 'L', x + w, y + h,
  15094. 'L', x, y + h,
  15095. 'L', x, y
  15096. );
  15097. break;
  15098. }
  15099. cmdStr && str.push(cmdStr);
  15100. for (var j = 0; j < nData; j++) {
  15101. // PENDING With scale
  15102. str.push(round4(data[i++]));
  15103. }
  15104. }
  15105. return str.join(' ');
  15106. }
  15107. var svgPath = {};
  15108. svgPath.brush = function (el) {
  15109. var style = el.style;
  15110. var svgEl = el.__svgEl;
  15111. if (!svgEl) {
  15112. svgEl = createElement('path');
  15113. el.__svgEl = svgEl;
  15114. }
  15115. if (!el.path) {
  15116. el.createPathProxy();
  15117. }
  15118. var path = el.path;
  15119. if (el.__dirtyPath) {
  15120. path.beginPath();
  15121. path.subPixelOptimize = false;
  15122. el.buildPath(path, el.shape);
  15123. el.__dirtyPath = false;
  15124. var pathStr = pathDataToString(path);
  15125. if (pathStr.indexOf('NaN') < 0) {
  15126. // Ignore illegal path, which may happen such in out-of-range
  15127. // data in Calendar series.
  15128. attr(svgEl, 'd', pathStr);
  15129. }
  15130. }
  15131. bindStyle(svgEl, style, false, el);
  15132. setTransform(svgEl, el.transform);
  15133. if (style.text != null) {
  15134. svgTextDrawRectText(el, el.getBoundingRect());
  15135. }
  15136. else {
  15137. removeOldTextNode(el);
  15138. }
  15139. };
  15140. /***************************************************
  15141. * IMAGE
  15142. **************************************************/
  15143. var svgImage = {};
  15144. svgImage.brush = function (el) {
  15145. var style = el.style;
  15146. var image = style.image;
  15147. if (image instanceof HTMLImageElement) {
  15148. var src = image.src;
  15149. image = src;
  15150. }
  15151. if (!image) {
  15152. return;
  15153. }
  15154. var x = style.x || 0;
  15155. var y = style.y || 0;
  15156. var dw = style.width;
  15157. var dh = style.height;
  15158. var svgEl = el.__svgEl;
  15159. if (!svgEl) {
  15160. svgEl = createElement('image');
  15161. el.__svgEl = svgEl;
  15162. }
  15163. if (image !== el.__imageSrc) {
  15164. attrXLink(svgEl, 'href', image);
  15165. // Caching image src
  15166. el.__imageSrc = image;
  15167. }
  15168. attr(svgEl, 'width', dw);
  15169. attr(svgEl, 'height', dh);
  15170. attr(svgEl, 'x', x);
  15171. attr(svgEl, 'y', y);
  15172. setTransform(svgEl, el.transform);
  15173. if (style.text != null) {
  15174. svgTextDrawRectText(el, el.getBoundingRect());
  15175. }
  15176. else {
  15177. removeOldTextNode(el);
  15178. }
  15179. };
  15180. /***************************************************
  15181. * TEXT
  15182. **************************************************/
  15183. var svgText = {};
  15184. var _tmpTextHostRect = new BoundingRect();
  15185. var _tmpTextBoxPos = {};
  15186. var _tmpTextTransform = [];
  15187. var TEXT_ALIGN_TO_ANCHRO = {
  15188. left: 'start',
  15189. right: 'end',
  15190. center: 'middle',
  15191. middle: 'middle'
  15192. };
  15193. /**
  15194. * @param {module:zrender/Element} el
  15195. * @param {Object|boolean} [hostRect] {x, y, width, height}
  15196. * If set false, rect text is not used.
  15197. */
  15198. var svgTextDrawRectText = function (el, hostRect) {
  15199. var style = el.style;
  15200. var elTransform = el.transform;
  15201. var needTransformTextByHostEl = el instanceof Text || style.transformText;
  15202. el.__dirty && normalizeTextStyle(style, true);
  15203. var text = style.text;
  15204. // Convert to string
  15205. text != null && (text += '');
  15206. if (!needDrawText(text, style)) {
  15207. return;
  15208. }
  15209. // render empty text for svg if no text but need draw text.
  15210. text == null && (text = '');
  15211. // Follow the setting in the canvas renderer, if not transform the
  15212. // text, transform the hostRect, by which the text is located.
  15213. if (!needTransformTextByHostEl && elTransform) {
  15214. _tmpTextHostRect.copy(hostRect);
  15215. _tmpTextHostRect.applyTransform(elTransform);
  15216. hostRect = _tmpTextHostRect;
  15217. }
  15218. var textSvgEl = el.__textSvgEl;
  15219. if (!textSvgEl) {
  15220. textSvgEl = createElement('text');
  15221. el.__textSvgEl = textSvgEl;
  15222. }
  15223. // style.font has been normalized by `normalizeTextStyle`.
  15224. var textSvgElStyle = textSvgEl.style;
  15225. var font = style.font || DEFAULT_FONT$1;
  15226. var computedFont = textSvgEl.__computedFont;
  15227. if (font !== textSvgEl.__styleFont) {
  15228. textSvgElStyle.font = textSvgEl.__styleFont = font;
  15229. // The computedFont might not be the orginal font if it is illegal font.
  15230. computedFont = textSvgEl.__computedFont = textSvgElStyle.font;
  15231. }
  15232. var textPadding = style.textPadding;
  15233. var textLineHeight = style.textLineHeight;
  15234. var contentBlock = el.__textCotentBlock;
  15235. if (!contentBlock || el.__dirtyText) {
  15236. contentBlock = el.__textCotentBlock = parsePlainText(
  15237. text, computedFont, textPadding, textLineHeight, style.truncate
  15238. );
  15239. }
  15240. var outerHeight = contentBlock.outerHeight;
  15241. var lineHeight = contentBlock.lineHeight;
  15242. getBoxPosition(_tmpTextBoxPos, el, style, hostRect);
  15243. var baseX = _tmpTextBoxPos.baseX;
  15244. var baseY = _tmpTextBoxPos.baseY;
  15245. var textAlign = _tmpTextBoxPos.textAlign || 'left';
  15246. var textVerticalAlign = _tmpTextBoxPos.textVerticalAlign;
  15247. setTextTransform(
  15248. textSvgEl, needTransformTextByHostEl, elTransform, style, hostRect, baseX, baseY
  15249. );
  15250. var boxY = adjustTextY(baseY, outerHeight, textVerticalAlign);
  15251. var textX = baseX;
  15252. var textY = boxY;
  15253. // TODO needDrawBg
  15254. if (textPadding) {
  15255. textX = getTextXForPadding$1(baseX, textAlign, textPadding);
  15256. textY += textPadding[0];
  15257. }
  15258. // `textBaseline` is set as 'middle'.
  15259. textY += lineHeight / 2;
  15260. bindStyle(textSvgEl, style, true, el);
  15261. // FIXME
  15262. // Add a <style> to reset all of the text font as inherit?
  15263. // otherwise the outer <style> may set the unexpected style.
  15264. // Font may affect position of each tspan elements
  15265. var canCacheByTextString = contentBlock.canCacheByTextString;
  15266. var tspanList = el.__tspanList || (el.__tspanList = []);
  15267. var tspanOriginLen = tspanList.length;
  15268. // Optimize for most cases, just compare text string to determine change.
  15269. if (canCacheByTextString && el.__canCacheByTextString && el.__text === text) {
  15270. if (el.__dirtyText && tspanOriginLen) {
  15271. for (var idx = 0; idx < tspanOriginLen; ++idx) {
  15272. updateTextLocation(tspanList[idx], textAlign, textX, textY + idx * lineHeight);
  15273. }
  15274. }
  15275. }
  15276. else {
  15277. el.__text = text;
  15278. el.__canCacheByTextString = canCacheByTextString;
  15279. var textLines = contentBlock.lines;
  15280. var nTextLines = textLines.length;
  15281. var idx = 0;
  15282. for (; idx < nTextLines; idx++) {
  15283. // Using cached tspan elements
  15284. var tspan = tspanList[idx];
  15285. var singleLineText = textLines[idx];
  15286. if (!tspan) {
  15287. tspan = tspanList[idx] = createElement('tspan');
  15288. textSvgEl.appendChild(tspan);
  15289. tspan.appendChild(document.createTextNode(singleLineText));
  15290. }
  15291. else if (tspan.__zrText !== singleLineText) {
  15292. tspan.innerHTML = '';
  15293. tspan.appendChild(document.createTextNode(singleLineText));
  15294. }
  15295. updateTextLocation(tspan, textAlign, textX, textY + idx * lineHeight);
  15296. }
  15297. // Remove unused tspan elements
  15298. if (tspanOriginLen > nTextLines) {
  15299. for (; idx < tspanOriginLen; idx++) {
  15300. textSvgEl.removeChild(tspanList[idx]);
  15301. }
  15302. tspanList.length = nTextLines;
  15303. }
  15304. }
  15305. };
  15306. function setTextTransform(textSvgEl, needTransformTextByHostEl, elTransform, style, hostRect, baseX, baseY) {
  15307. identity(_tmpTextTransform);
  15308. if (needTransformTextByHostEl && elTransform) {
  15309. copy$1(_tmpTextTransform, elTransform);
  15310. }
  15311. // textRotation only apply in RectText.
  15312. var textRotation = style.textRotation;
  15313. if (hostRect && textRotation) {
  15314. var origin = style.textOrigin;
  15315. if (origin === 'center') {
  15316. baseX = hostRect.width / 2 + hostRect.x;
  15317. baseY = hostRect.height / 2 + hostRect.y;
  15318. }
  15319. else if (origin) {
  15320. baseX = origin[0] + hostRect.x;
  15321. baseY = origin[1] + hostRect.y;
  15322. }
  15323. _tmpTextTransform[4] -= baseX;
  15324. _tmpTextTransform[5] -= baseY;
  15325. // Positive: anticlockwise
  15326. rotate(_tmpTextTransform, _tmpTextTransform, textRotation);
  15327. _tmpTextTransform[4] += baseX;
  15328. _tmpTextTransform[5] += baseY;
  15329. }
  15330. // See the definition in `Style.js#textOrigin`, the default
  15331. // origin is from the result of `getBoxPosition`.
  15332. setTransform(textSvgEl, _tmpTextTransform);
  15333. }
  15334. // FIXME merge the same code with `helper/text.js#getTextXForPadding`;
  15335. function getTextXForPadding$1(x, textAlign, textPadding) {
  15336. return textAlign === 'right'
  15337. ? (x - textPadding[1])
  15338. : textAlign === 'center'
  15339. ? (x + textPadding[3] / 2 - textPadding[1] / 2)
  15340. : (x + textPadding[3]);
  15341. }
  15342. function updateTextLocation(tspan, textAlign, x, y) {
  15343. // Consider different font display differently in vertial align, we always
  15344. // set vertialAlign as 'middle', and use 'y' to locate text vertically.
  15345. attr(tspan, 'dominant-baseline', 'middle');
  15346. attr(tspan, 'text-anchor', TEXT_ALIGN_TO_ANCHRO[textAlign]);
  15347. attr(tspan, 'x', x);
  15348. attr(tspan, 'y', y);
  15349. }
  15350. function removeOldTextNode(el) {
  15351. if (el && el.__textSvgEl) {
  15352. // textSvgEl may has no parentNode if el has been removed temporary.
  15353. if (el.__textSvgEl.parentNode) {
  15354. el.__textSvgEl.parentNode.removeChild(el.__textSvgEl);
  15355. }
  15356. el.__textSvgEl = null;
  15357. el.__tspanList = [];
  15358. el.__text = null;
  15359. }
  15360. }
  15361. svgText.drawRectText = svgTextDrawRectText;
  15362. svgText.brush = function (el) {
  15363. var style = el.style;
  15364. if (style.text != null) {
  15365. svgTextDrawRectText(el, false);
  15366. }
  15367. else {
  15368. removeOldTextNode(el);
  15369. }
  15370. };
  15371. // Myers' Diff Algorithm
  15372. // Modified from https://github.com/kpdecker/jsdiff/blob/master/src/diff/base.js
  15373. function Diff() {}
  15374. Diff.prototype = {
  15375. diff: function (oldArr, newArr, equals) {
  15376. if (!equals) {
  15377. equals = function (a, b) {
  15378. return a === b;
  15379. };
  15380. }
  15381. this.equals = equals;
  15382. var self = this;
  15383. oldArr = oldArr.slice();
  15384. newArr = newArr.slice();
  15385. // Allow subclasses to massage the input prior to running
  15386. var newLen = newArr.length;
  15387. var oldLen = oldArr.length;
  15388. var editLength = 1;
  15389. var maxEditLength = newLen + oldLen;
  15390. var bestPath = [{ newPos: -1, components: [] }];
  15391. // Seed editLength = 0, i.e. the content starts with the same values
  15392. var oldPos = this.extractCommon(bestPath[0], newArr, oldArr, 0);
  15393. if (bestPath[0].newPos + 1 >= newLen && oldPos + 1 >= oldLen) {
  15394. var indices = [];
  15395. for (var i = 0; i < newArr.length; i++) {
  15396. indices.push(i);
  15397. }
  15398. // Identity per the equality and tokenizer
  15399. return [{
  15400. indices: indices, count: newArr.length
  15401. }];
  15402. }
  15403. // Main worker method. checks all permutations of a given edit length for acceptance.
  15404. function execEditLength() {
  15405. for (var diagonalPath = -1 * editLength; diagonalPath <= editLength; diagonalPath += 2) {
  15406. var basePath;
  15407. var addPath = bestPath[diagonalPath - 1];
  15408. var removePath = bestPath[diagonalPath + 1];
  15409. var oldPos = (removePath ? removePath.newPos : 0) - diagonalPath;
  15410. if (addPath) {
  15411. // No one else is going to attempt to use this value, clear it
  15412. bestPath[diagonalPath - 1] = undefined;
  15413. }
  15414. var canAdd = addPath && addPath.newPos + 1 < newLen;
  15415. var canRemove = removePath && 0 <= oldPos && oldPos < oldLen;
  15416. if (!canAdd && !canRemove) {
  15417. // If this path is a terminal then prune
  15418. bestPath[diagonalPath] = undefined;
  15419. continue;
  15420. }
  15421. // Select the diagonal that we want to branch from. We select the prior
  15422. // path whose position in the new string is the farthest from the origin
  15423. // and does not pass the bounds of the diff graph
  15424. if (!canAdd || (canRemove && addPath.newPos < removePath.newPos)) {
  15425. basePath = clonePath(removePath);
  15426. self.pushComponent(basePath.components, undefined, true);
  15427. }
  15428. else {
  15429. basePath = addPath; // No need to clone, we've pulled it from the list
  15430. basePath.newPos++;
  15431. self.pushComponent(basePath.components, true, undefined);
  15432. }
  15433. oldPos = self.extractCommon(basePath, newArr, oldArr, diagonalPath);
  15434. // If we have hit the end of both strings, then we are done
  15435. if (basePath.newPos + 1 >= newLen && oldPos + 1 >= oldLen) {
  15436. return buildValues(self, basePath.components, newArr, oldArr);
  15437. }
  15438. else {
  15439. // Otherwise track this path as a potential candidate and continue.
  15440. bestPath[diagonalPath] = basePath;
  15441. }
  15442. }
  15443. editLength++;
  15444. }
  15445. while (editLength <= maxEditLength) {
  15446. var ret = execEditLength();
  15447. if (ret) {
  15448. return ret;
  15449. }
  15450. }
  15451. },
  15452. pushComponent: function (components, added, removed) {
  15453. var last = components[components.length - 1];
  15454. if (last && last.added === added && last.removed === removed) {
  15455. // We need to clone here as the component clone operation is just
  15456. // as shallow array clone
  15457. components[components.length - 1] = {count: last.count + 1, added: added, removed: removed };
  15458. }
  15459. else {
  15460. components.push({count: 1, added: added, removed: removed });
  15461. }
  15462. },
  15463. extractCommon: function (basePath, newArr, oldArr, diagonalPath) {
  15464. var newLen = newArr.length;
  15465. var oldLen = oldArr.length;
  15466. var newPos = basePath.newPos;
  15467. var oldPos = newPos - diagonalPath;
  15468. var commonCount = 0;
  15469. while (newPos + 1 < newLen && oldPos + 1 < oldLen && this.equals(newArr[newPos + 1], oldArr[oldPos + 1])) {
  15470. newPos++;
  15471. oldPos++;
  15472. commonCount++;
  15473. }
  15474. if (commonCount) {
  15475. basePath.components.push({count: commonCount});
  15476. }
  15477. basePath.newPos = newPos;
  15478. return oldPos;
  15479. },
  15480. tokenize: function (value) {
  15481. return value.slice();
  15482. },
  15483. join: function (value) {
  15484. return value.slice();
  15485. }
  15486. };
  15487. function buildValues(diff, components, newArr, oldArr) {
  15488. var componentPos = 0;
  15489. var componentLen = components.length;
  15490. var newPos = 0;
  15491. var oldPos = 0;
  15492. for (; componentPos < componentLen; componentPos++) {
  15493. var component = components[componentPos];
  15494. if (!component.removed) {
  15495. var indices = [];
  15496. for (var i = newPos; i < newPos + component.count; i++) {
  15497. indices.push(i);
  15498. }
  15499. component.indices = indices;
  15500. newPos += component.count;
  15501. // Common case
  15502. if (!component.added) {
  15503. oldPos += component.count;
  15504. }
  15505. }
  15506. else {
  15507. var indices = [];
  15508. for (var i = oldPos; i < oldPos + component.count; i++) {
  15509. indices.push(i);
  15510. }
  15511. component.indices = indices;
  15512. oldPos += component.count;
  15513. }
  15514. }
  15515. return components;
  15516. }
  15517. function clonePath(path) {
  15518. return { newPos: path.newPos, components: path.components.slice(0) };
  15519. }
  15520. var arrayDiff = new Diff();
  15521. var arrayDiff$1 = function (oldArr, newArr, callback) {
  15522. return arrayDiff.diff(oldArr, newArr, callback);
  15523. };
  15524. /**
  15525. * @file Manages elements that can be defined in <defs> in SVG,
  15526. * e.g., gradients, clip path, etc.
  15527. * @author Zhang Wenli
  15528. */
  15529. var MARK_UNUSED = '0';
  15530. var MARK_USED = '1';
  15531. /**
  15532. * Manages elements that can be defined in <defs> in SVG,
  15533. * e.g., gradients, clip path, etc.
  15534. *
  15535. * @class
  15536. * @param {number} zrId zrender instance id
  15537. * @param {SVGElement} svgRoot root of SVG document
  15538. * @param {string|string[]} tagNames possible tag names
  15539. * @param {string} markLabel label name to make if the element
  15540. * is used
  15541. */
  15542. function Definable(
  15543. zrId,
  15544. svgRoot,
  15545. tagNames,
  15546. markLabel,
  15547. domName
  15548. ) {
  15549. this._zrId = zrId;
  15550. this._svgRoot = svgRoot;
  15551. this._tagNames = typeof tagNames === 'string' ? [tagNames] : tagNames;
  15552. this._markLabel = markLabel;
  15553. this._domName = domName || '_dom';
  15554. this.nextId = 0;
  15555. }
  15556. Definable.prototype.createElement = createElement;
  15557. /**
  15558. * Get the <defs> tag for svgRoot; optionally creates one if not exists.
  15559. *
  15560. * @param {boolean} isForceCreating if need to create when not exists
  15561. * @return {SVGDefsElement} SVG <defs> element, null if it doesn't
  15562. * exist and isForceCreating is false
  15563. */
  15564. Definable.prototype.getDefs = function (isForceCreating) {
  15565. var svgRoot = this._svgRoot;
  15566. var defs = this._svgRoot.getElementsByTagName('defs');
  15567. if (defs.length === 0) {
  15568. // Not exist
  15569. if (isForceCreating) {
  15570. defs = svgRoot.insertBefore(
  15571. this.createElement('defs'), // Create new tag
  15572. svgRoot.firstChild // Insert in the front of svg
  15573. );
  15574. if (!defs.contains) {
  15575. // IE doesn't support contains method
  15576. defs.contains = function (el) {
  15577. var children = defs.children;
  15578. if (!children) {
  15579. return false;
  15580. }
  15581. for (var i = children.length - 1; i >= 0; --i) {
  15582. if (children[i] === el) {
  15583. return true;
  15584. }
  15585. }
  15586. return false;
  15587. };
  15588. }
  15589. return defs;
  15590. }
  15591. else {
  15592. return null;
  15593. }
  15594. }
  15595. else {
  15596. return defs[0];
  15597. }
  15598. };
  15599. /**
  15600. * Update DOM element if necessary.
  15601. *
  15602. * @param {Object|string} element style element. e.g., for gradient,
  15603. * it may be '#ccc' or {type: 'linear', ...}
  15604. * @param {Function|undefined} onUpdate update callback
  15605. */
  15606. Definable.prototype.update = function (element, onUpdate) {
  15607. if (!element) {
  15608. return;
  15609. }
  15610. var defs = this.getDefs(false);
  15611. if (element[this._domName] && defs.contains(element[this._domName])) {
  15612. // Update DOM
  15613. if (typeof onUpdate === 'function') {
  15614. onUpdate(element);
  15615. }
  15616. }
  15617. else {
  15618. // No previous dom, create new
  15619. var dom = this.add(element);
  15620. if (dom) {
  15621. element[this._domName] = dom;
  15622. }
  15623. }
  15624. };
  15625. /**
  15626. * Add gradient dom to defs
  15627. *
  15628. * @param {SVGElement} dom DOM to be added to <defs>
  15629. */
  15630. Definable.prototype.addDom = function (dom) {
  15631. var defs = this.getDefs(true);
  15632. defs.appendChild(dom);
  15633. };
  15634. /**
  15635. * Remove DOM of a given element.
  15636. *
  15637. * @param {SVGElement} element element to remove dom
  15638. */
  15639. Definable.prototype.removeDom = function (element) {
  15640. var defs = this.getDefs(false);
  15641. if (defs && element[this._domName]) {
  15642. defs.removeChild(element[this._domName]);
  15643. element[this._domName] = null;
  15644. }
  15645. };
  15646. /**
  15647. * Get DOMs of this element.
  15648. *
  15649. * @return {HTMLDomElement} doms of this defineable elements in <defs>
  15650. */
  15651. Definable.prototype.getDoms = function () {
  15652. var defs = this.getDefs(false);
  15653. if (!defs) {
  15654. // No dom when defs is not defined
  15655. return [];
  15656. }
  15657. var doms = [];
  15658. each(this._tagNames, function (tagName) {
  15659. var tags = defs.getElementsByTagName(tagName);
  15660. // Note that tags is HTMLCollection, which is array-like
  15661. // rather than real array.
  15662. // So `doms.concat(tags)` add tags as one object.
  15663. doms = doms.concat([].slice.call(tags));
  15664. });
  15665. return doms;
  15666. };
  15667. /**
  15668. * Mark DOMs to be unused before painting, and clear unused ones at the end
  15669. * of the painting.
  15670. */
  15671. Definable.prototype.markAllUnused = function () {
  15672. var doms = this.getDoms();
  15673. var that = this;
  15674. each(doms, function (dom) {
  15675. dom[that._markLabel] = MARK_UNUSED;
  15676. });
  15677. };
  15678. /**
  15679. * Mark a single DOM to be used.
  15680. *
  15681. * @param {SVGElement} dom DOM to mark
  15682. */
  15683. Definable.prototype.markUsed = function (dom) {
  15684. if (dom) {
  15685. dom[this._markLabel] = MARK_USED;
  15686. }
  15687. };
  15688. /**
  15689. * Remove unused DOMs defined in <defs>
  15690. */
  15691. Definable.prototype.removeUnused = function () {
  15692. var defs = this.getDefs(false);
  15693. if (!defs) {
  15694. // Nothing to remove
  15695. return;
  15696. }
  15697. var doms = this.getDoms();
  15698. var that = this;
  15699. each(doms, function (dom) {
  15700. if (dom[that._markLabel] !== MARK_USED) {
  15701. // Remove gradient
  15702. defs.removeChild(dom);
  15703. }
  15704. });
  15705. };
  15706. /**
  15707. * Get SVG proxy.
  15708. *
  15709. * @param {Displayable} displayable displayable element
  15710. * @return {Path|Image|Text} svg proxy of given element
  15711. */
  15712. Definable.prototype.getSvgProxy = function (displayable) {
  15713. if (displayable instanceof Path) {
  15714. return svgPath;
  15715. }
  15716. else if (displayable instanceof ZImage) {
  15717. return svgImage;
  15718. }
  15719. else if (displayable instanceof Text) {
  15720. return svgText;
  15721. }
  15722. else {
  15723. return svgPath;
  15724. }
  15725. };
  15726. /**
  15727. * Get text SVG element.
  15728. *
  15729. * @param {Displayable} displayable displayable element
  15730. * @return {SVGElement} SVG element of text
  15731. */
  15732. Definable.prototype.getTextSvgElement = function (displayable) {
  15733. return displayable.__textSvgEl;
  15734. };
  15735. /**
  15736. * Get SVG element.
  15737. *
  15738. * @param {Displayable} displayable displayable element
  15739. * @return {SVGElement} SVG element
  15740. */
  15741. Definable.prototype.getSvgElement = function (displayable) {
  15742. return displayable.__svgEl;
  15743. };
  15744. /**
  15745. * @file Manages SVG gradient elements.
  15746. * @author Zhang Wenli
  15747. */
  15748. /**
  15749. * Manages SVG gradient elements.
  15750. *
  15751. * @class
  15752. * @extends Definable
  15753. * @param {number} zrId zrender instance id
  15754. * @param {SVGElement} svgRoot root of SVG document
  15755. */
  15756. function GradientManager(zrId, svgRoot) {
  15757. Definable.call(
  15758. this,
  15759. zrId,
  15760. svgRoot,
  15761. ['linearGradient', 'radialGradient'],
  15762. '__gradient_in_use__'
  15763. );
  15764. }
  15765. inherits(GradientManager, Definable);
  15766. /**
  15767. * Create new gradient DOM for fill or stroke if not exist,
  15768. * but will not update gradient if exists.
  15769. *
  15770. * @param {SvgElement} svgElement SVG element to paint
  15771. * @param {Displayable} displayable zrender displayable element
  15772. */
  15773. GradientManager.prototype.addWithoutUpdate = function (
  15774. svgElement,
  15775. displayable
  15776. ) {
  15777. if (displayable && displayable.style) {
  15778. var that = this;
  15779. each(['fill', 'stroke'], function (fillOrStroke) {
  15780. if (displayable.style[fillOrStroke]
  15781. && (displayable.style[fillOrStroke].type === 'linear'
  15782. || displayable.style[fillOrStroke].type === 'radial')
  15783. ) {
  15784. var gradient = displayable.style[fillOrStroke];
  15785. var defs = that.getDefs(true);
  15786. // Create dom in <defs> if not exists
  15787. var dom;
  15788. if (gradient._dom) {
  15789. // Gradient exists
  15790. dom = gradient._dom;
  15791. if (!defs.contains(gradient._dom)) {
  15792. // _dom is no longer in defs, recreate
  15793. that.addDom(dom);
  15794. }
  15795. }
  15796. else {
  15797. // New dom
  15798. dom = that.add(gradient);
  15799. }
  15800. that.markUsed(displayable);
  15801. var id = dom.getAttribute('id');
  15802. svgElement.setAttribute(fillOrStroke, 'url(#' + id + ')');
  15803. }
  15804. });
  15805. }
  15806. };
  15807. /**
  15808. * Add a new gradient tag in <defs>
  15809. *
  15810. * @param {Gradient} gradient zr gradient instance
  15811. * @return {SVGLinearGradientElement | SVGRadialGradientElement}
  15812. * created DOM
  15813. */
  15814. GradientManager.prototype.add = function (gradient) {
  15815. var dom;
  15816. if (gradient.type === 'linear') {
  15817. dom = this.createElement('linearGradient');
  15818. }
  15819. else if (gradient.type === 'radial') {
  15820. dom = this.createElement('radialGradient');
  15821. }
  15822. else {
  15823. logError$1('Illegal gradient type.');
  15824. return null;
  15825. }
  15826. // Set dom id with gradient id, since each gradient instance
  15827. // will have no more than one dom element.
  15828. // id may exists before for those dirty elements, in which case
  15829. // id should remain the same, and other attributes should be
  15830. // updated.
  15831. gradient.id = gradient.id || this.nextId++;
  15832. dom.setAttribute('id', 'zr' + this._zrId
  15833. + '-gradient-' + gradient.id);
  15834. this.updateDom(gradient, dom);
  15835. this.addDom(dom);
  15836. return dom;
  15837. };
  15838. /**
  15839. * Update gradient.
  15840. *
  15841. * @param {Gradient} gradient zr gradient instance
  15842. */
  15843. GradientManager.prototype.update = function (gradient) {
  15844. var that = this;
  15845. Definable.prototype.update.call(this, gradient, function () {
  15846. var type = gradient.type;
  15847. var tagName = gradient._dom.tagName;
  15848. if (type === 'linear' && tagName === 'linearGradient'
  15849. || type === 'radial' && tagName === 'radialGradient'
  15850. ) {
  15851. // Gradient type is not changed, update gradient
  15852. that.updateDom(gradient, gradient._dom);
  15853. }
  15854. else {
  15855. // Remove and re-create if type is changed
  15856. that.removeDom(gradient);
  15857. that.add(gradient);
  15858. }
  15859. });
  15860. };
  15861. /**
  15862. * Update gradient dom
  15863. *
  15864. * @param {Gradient} gradient zr gradient instance
  15865. * @param {SVGLinearGradientElement | SVGRadialGradientElement} dom
  15866. * DOM to update
  15867. */
  15868. GradientManager.prototype.updateDom = function (gradient, dom) {
  15869. if (gradient.type === 'linear') {
  15870. dom.setAttribute('x1', gradient.x);
  15871. dom.setAttribute('y1', gradient.y);
  15872. dom.setAttribute('x2', gradient.x2);
  15873. dom.setAttribute('y2', gradient.y2);
  15874. }
  15875. else if (gradient.type === 'radial') {
  15876. dom.setAttribute('cx', gradient.x);
  15877. dom.setAttribute('cy', gradient.y);
  15878. dom.setAttribute('r', gradient.r);
  15879. }
  15880. else {
  15881. logError$1('Illegal gradient type.');
  15882. return;
  15883. }
  15884. if (gradient.global) {
  15885. // x1, x2, y1, y2 in range of 0 to canvas width or height
  15886. dom.setAttribute('gradientUnits', 'userSpaceOnUse');
  15887. }
  15888. else {
  15889. // x1, x2, y1, y2 in range of 0 to 1
  15890. dom.setAttribute('gradientUnits', 'objectBoundingBox');
  15891. }
  15892. // Remove color stops if exists
  15893. dom.innerHTML = '';
  15894. // Add color stops
  15895. var colors = gradient.colorStops;
  15896. for (var i = 0, len = colors.length; i < len; ++i) {
  15897. var stop = this.createElement('stop');
  15898. stop.setAttribute('offset', colors[i].offset * 100 + '%');
  15899. var color = colors[i].color;
  15900. if (color.indexOf('rgba') > -1) {
  15901. // Fix Safari bug that stop-color not recognizing alpha #9014
  15902. var opacity = parse(color)[3];
  15903. var hex = toHex(color);
  15904. // stop-color cannot be color, since:
  15905. // The opacity value used for the gradient calculation is the
  15906. // *product* of the value of stop-opacity and the opacity of the
  15907. // value of stop-color.
  15908. // See https://www.w3.org/TR/SVG2/pservers.html#StopOpacityProperty
  15909. stop.setAttribute('stop-color', '#' + hex);
  15910. stop.setAttribute('stop-opacity', opacity);
  15911. }
  15912. else {
  15913. stop.setAttribute('stop-color', colors[i].color);
  15914. }
  15915. dom.appendChild(stop);
  15916. }
  15917. // Store dom element in gradient, to avoid creating multiple
  15918. // dom instances for the same gradient element
  15919. gradient._dom = dom;
  15920. };
  15921. /**
  15922. * Mark a single gradient to be used
  15923. *
  15924. * @param {Displayable} displayable displayable element
  15925. */
  15926. GradientManager.prototype.markUsed = function (displayable) {
  15927. if (displayable.style) {
  15928. var gradient = displayable.style.fill;
  15929. if (gradient && gradient._dom) {
  15930. Definable.prototype.markUsed.call(this, gradient._dom);
  15931. }
  15932. gradient = displayable.style.stroke;
  15933. if (gradient && gradient._dom) {
  15934. Definable.prototype.markUsed.call(this, gradient._dom);
  15935. }
  15936. }
  15937. };
  15938. /**
  15939. * @file Manages SVG clipPath elements.
  15940. * @author Zhang Wenli
  15941. */
  15942. /**
  15943. * Manages SVG clipPath elements.
  15944. *
  15945. * @class
  15946. * @extends Definable
  15947. * @param {number} zrId zrender instance id
  15948. * @param {SVGElement} svgRoot root of SVG document
  15949. */
  15950. function ClippathManager(zrId, svgRoot) {
  15951. Definable.call(this, zrId, svgRoot, 'clipPath', '__clippath_in_use__');
  15952. }
  15953. inherits(ClippathManager, Definable);
  15954. /**
  15955. * Update clipPath.
  15956. *
  15957. * @param {Displayable} displayable displayable element
  15958. */
  15959. ClippathManager.prototype.update = function (displayable) {
  15960. var svgEl = this.getSvgElement(displayable);
  15961. if (svgEl) {
  15962. this.updateDom(svgEl, displayable.__clipPaths, false);
  15963. }
  15964. var textEl = this.getTextSvgElement(displayable);
  15965. if (textEl) {
  15966. // Make another clipPath for text, since it's transform
  15967. // matrix is not the same with svgElement
  15968. this.updateDom(textEl, displayable.__clipPaths, true);
  15969. }
  15970. this.markUsed(displayable);
  15971. };
  15972. /**
  15973. * Create an SVGElement of displayable and create a <clipPath> of its
  15974. * clipPath
  15975. *
  15976. * @param {Displayable} parentEl parent element
  15977. * @param {ClipPath[]} clipPaths clipPaths of parent element
  15978. * @param {boolean} isText if parent element is Text
  15979. */
  15980. ClippathManager.prototype.updateDom = function (
  15981. parentEl,
  15982. clipPaths,
  15983. isText
  15984. ) {
  15985. if (clipPaths && clipPaths.length > 0) {
  15986. // Has clipPath, create <clipPath> with the first clipPath
  15987. var defs = this.getDefs(true);
  15988. var clipPath = clipPaths[0];
  15989. var clipPathEl;
  15990. var id;
  15991. var dom = isText ? '_textDom' : '_dom';
  15992. if (clipPath[dom]) {
  15993. // Use a dom that is already in <defs>
  15994. id = clipPath[dom].getAttribute('id');
  15995. clipPathEl = clipPath[dom];
  15996. // Use a dom that is already in <defs>
  15997. if (!defs.contains(clipPathEl)) {
  15998. // This happens when set old clipPath that has
  15999. // been previously removed
  16000. defs.appendChild(clipPathEl);
  16001. }
  16002. }
  16003. else {
  16004. // New <clipPath>
  16005. id = 'zr' + this._zrId + '-clip-' + this.nextId;
  16006. ++this.nextId;
  16007. clipPathEl = this.createElement('clipPath');
  16008. clipPathEl.setAttribute('id', id);
  16009. defs.appendChild(clipPathEl);
  16010. clipPath[dom] = clipPathEl;
  16011. }
  16012. // Build path and add to <clipPath>
  16013. var svgProxy = this.getSvgProxy(clipPath);
  16014. if (clipPath.transform
  16015. && clipPath.parent.invTransform
  16016. && !isText
  16017. ) {
  16018. /**
  16019. * If a clipPath has a parent with transform, the transform
  16020. * of parent should not be considered when setting transform
  16021. * of clipPath. So we need to transform back from parent's
  16022. * transform, which is done by multiplying parent's inverse
  16023. * transform.
  16024. */
  16025. // Store old transform
  16026. var transform = Array.prototype.slice.call(
  16027. clipPath.transform
  16028. );
  16029. // Transform back from parent, and brush path
  16030. mul$1(
  16031. clipPath.transform,
  16032. clipPath.parent.invTransform,
  16033. clipPath.transform
  16034. );
  16035. svgProxy.brush(clipPath);
  16036. // Set back transform of clipPath
  16037. clipPath.transform = transform;
  16038. }
  16039. else {
  16040. svgProxy.brush(clipPath);
  16041. }
  16042. var pathEl = this.getSvgElement(clipPath);
  16043. clipPathEl.innerHTML = '';
  16044. /**
  16045. * Use `cloneNode()` here to appendChild to multiple parents,
  16046. * which may happend when Text and other shapes are using the same
  16047. * clipPath. Since Text will create an extra clipPath DOM due to
  16048. * different transform rules.
  16049. */
  16050. clipPathEl.appendChild(pathEl.cloneNode());
  16051. parentEl.setAttribute('clip-path', 'url(#' + id + ')');
  16052. if (clipPaths.length > 1) {
  16053. // Make the other clipPaths recursively
  16054. this.updateDom(clipPathEl, clipPaths.slice(1), isText);
  16055. }
  16056. }
  16057. else {
  16058. // No clipPath
  16059. if (parentEl) {
  16060. parentEl.setAttribute('clip-path', 'none');
  16061. }
  16062. }
  16063. };
  16064. /**
  16065. * Mark a single clipPath to be used
  16066. *
  16067. * @param {Displayable} displayable displayable element
  16068. */
  16069. ClippathManager.prototype.markUsed = function (displayable) {
  16070. var that = this;
  16071. // displayable.__clipPaths can only be `null`/`undefined` or an non-empty array.
  16072. if (displayable.__clipPaths) {
  16073. each(displayable.__clipPaths, function (clipPath) {
  16074. if (clipPath._dom) {
  16075. Definable.prototype.markUsed.call(that, clipPath._dom);
  16076. }
  16077. if (clipPath._textDom) {
  16078. Definable.prototype.markUsed.call(that, clipPath._textDom);
  16079. }
  16080. });
  16081. }
  16082. };
  16083. /**
  16084. * @file Manages SVG shadow elements.
  16085. * @author Zhang Wenli
  16086. */
  16087. /**
  16088. * Manages SVG shadow elements.
  16089. *
  16090. * @class
  16091. * @extends Definable
  16092. * @param {number} zrId zrender instance id
  16093. * @param {SVGElement} svgRoot root of SVG document
  16094. */
  16095. function ShadowManager(zrId, svgRoot) {
  16096. Definable.call(
  16097. this,
  16098. zrId,
  16099. svgRoot,
  16100. ['filter'],
  16101. '__filter_in_use__',
  16102. '_shadowDom'
  16103. );
  16104. }
  16105. inherits(ShadowManager, Definable);
  16106. /**
  16107. * Create new shadow DOM for fill or stroke if not exist,
  16108. * but will not update shadow if exists.
  16109. *
  16110. * @param {SvgElement} svgElement SVG element to paint
  16111. * @param {Displayable} displayable zrender displayable element
  16112. */
  16113. ShadowManager.prototype.addWithoutUpdate = function (
  16114. svgElement,
  16115. displayable
  16116. ) {
  16117. if (displayable && hasShadow(displayable.style)) {
  16118. // Create dom in <defs> if not exists
  16119. var dom;
  16120. if (displayable._shadowDom) {
  16121. // Gradient exists
  16122. dom = displayable._shadowDom;
  16123. var defs = this.getDefs(true);
  16124. if (!defs.contains(displayable._shadowDom)) {
  16125. // _shadowDom is no longer in defs, recreate
  16126. this.addDom(dom);
  16127. }
  16128. }
  16129. else {
  16130. // New dom
  16131. dom = this.add(displayable);
  16132. }
  16133. this.markUsed(displayable);
  16134. var id = dom.getAttribute('id');
  16135. svgElement.style.filter = 'url(#' + id + ')';
  16136. }
  16137. };
  16138. /**
  16139. * Add a new shadow tag in <defs>
  16140. *
  16141. * @param {Displayable} displayable zrender displayable element
  16142. * @return {SVGFilterElement} created DOM
  16143. */
  16144. ShadowManager.prototype.add = function (displayable) {
  16145. var dom = this.createElement('filter');
  16146. // Set dom id with shadow id, since each shadow instance
  16147. // will have no more than one dom element.
  16148. // id may exists before for those dirty elements, in which case
  16149. // id should remain the same, and other attributes should be
  16150. // updated.
  16151. displayable._shadowDomId = displayable._shadowDomId || this.nextId++;
  16152. dom.setAttribute('id', 'zr' + this._zrId
  16153. + '-shadow-' + displayable._shadowDomId);
  16154. this.updateDom(displayable, dom);
  16155. this.addDom(dom);
  16156. return dom;
  16157. };
  16158. /**
  16159. * Update shadow.
  16160. *
  16161. * @param {Displayable} displayable zrender displayable element
  16162. */
  16163. ShadowManager.prototype.update = function (svgElement, displayable) {
  16164. var style = displayable.style;
  16165. if (hasShadow(style)) {
  16166. var that = this;
  16167. Definable.prototype.update.call(this, displayable, function () {
  16168. that.updateDom(displayable, displayable._shadowDom);
  16169. });
  16170. }
  16171. else {
  16172. // Remove shadow
  16173. this.remove(svgElement, displayable);
  16174. }
  16175. };
  16176. /**
  16177. * Remove DOM and clear parent filter
  16178. */
  16179. ShadowManager.prototype.remove = function (svgElement, displayable) {
  16180. if (displayable._shadowDomId != null) {
  16181. this.removeDom(svgElement);
  16182. svgElement.style.filter = '';
  16183. }
  16184. };
  16185. /**
  16186. * Update shadow dom
  16187. *
  16188. * @param {Displayable} displayable zrender displayable element
  16189. * @param {SVGFilterElement} dom DOM to update
  16190. */
  16191. ShadowManager.prototype.updateDom = function (displayable, dom) {
  16192. var domChild = dom.getElementsByTagName('feDropShadow');
  16193. if (domChild.length === 0) {
  16194. domChild = this.createElement('feDropShadow');
  16195. }
  16196. else {
  16197. domChild = domChild[0];
  16198. }
  16199. var style = displayable.style;
  16200. var scaleX = displayable.scale ? (displayable.scale[0] || 1) : 1;
  16201. var scaleY = displayable.scale ? (displayable.scale[1] || 1) : 1;
  16202. // TODO: textBoxShadowBlur is not supported yet
  16203. var offsetX;
  16204. var offsetY;
  16205. var blur;
  16206. var color;
  16207. if (style.shadowBlur || style.shadowOffsetX || style.shadowOffsetY) {
  16208. offsetX = style.shadowOffsetX || 0;
  16209. offsetY = style.shadowOffsetY || 0;
  16210. blur = style.shadowBlur;
  16211. color = style.shadowColor;
  16212. }
  16213. else if (style.textShadowBlur) {
  16214. offsetX = style.textShadowOffsetX || 0;
  16215. offsetY = style.textShadowOffsetY || 0;
  16216. blur = style.textShadowBlur;
  16217. color = style.textShadowColor;
  16218. }
  16219. else {
  16220. // Remove shadow
  16221. this.removeDom(dom, style);
  16222. return;
  16223. }
  16224. domChild.setAttribute('dx', offsetX / scaleX);
  16225. domChild.setAttribute('dy', offsetY / scaleY);
  16226. domChild.setAttribute('flood-color', color);
  16227. // Divide by two here so that it looks the same as in canvas
  16228. // See: https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-shadowblur
  16229. var stdDx = blur / 2 / scaleX;
  16230. var stdDy = blur / 2 / scaleY;
  16231. var stdDeviation = stdDx + ' ' + stdDy;
  16232. domChild.setAttribute('stdDeviation', stdDeviation);
  16233. // Fix filter clipping problem
  16234. dom.setAttribute('x', '-100%');
  16235. dom.setAttribute('y', '-100%');
  16236. dom.setAttribute('width', Math.ceil(blur / 2 * 200) + '%');
  16237. dom.setAttribute('height', Math.ceil(blur / 2 * 200) + '%');
  16238. dom.appendChild(domChild);
  16239. // Store dom element in shadow, to avoid creating multiple
  16240. // dom instances for the same shadow element
  16241. displayable._shadowDom = dom;
  16242. };
  16243. /**
  16244. * Mark a single shadow to be used
  16245. *
  16246. * @param {Displayable} displayable displayable element
  16247. */
  16248. ShadowManager.prototype.markUsed = function (displayable) {
  16249. if (displayable._shadowDom) {
  16250. Definable.prototype.markUsed.call(this, displayable._shadowDom);
  16251. }
  16252. };
  16253. function hasShadow(style) {
  16254. // TODO: textBoxShadowBlur is not supported yet
  16255. return style
  16256. && (style.shadowBlur || style.shadowOffsetX || style.shadowOffsetY
  16257. || style.textShadowBlur || style.textShadowOffsetX
  16258. || style.textShadowOffsetY);
  16259. }
  16260. /**
  16261. * SVG Painter
  16262. * @module zrender/svg/Painter
  16263. */
  16264. function parseInt10$1(val) {
  16265. return parseInt(val, 10);
  16266. }
  16267. function getSvgProxy(el) {
  16268. if (el instanceof Path) {
  16269. return svgPath;
  16270. }
  16271. else if (el instanceof ZImage) {
  16272. return svgImage;
  16273. }
  16274. else if (el instanceof Text) {
  16275. return svgText;
  16276. }
  16277. else {
  16278. return svgPath;
  16279. }
  16280. }
  16281. function checkParentAvailable(parent, child) {
  16282. return child && parent && child.parentNode !== parent;
  16283. }
  16284. function insertAfter(parent, child, prevSibling) {
  16285. if (checkParentAvailable(parent, child) && prevSibling) {
  16286. var nextSibling = prevSibling.nextSibling;
  16287. nextSibling ? parent.insertBefore(child, nextSibling)
  16288. : parent.appendChild(child);
  16289. }
  16290. }
  16291. function prepend(parent, child) {
  16292. if (checkParentAvailable(parent, child)) {
  16293. var firstChild = parent.firstChild;
  16294. firstChild ? parent.insertBefore(child, firstChild)
  16295. : parent.appendChild(child);
  16296. }
  16297. }
  16298. // function append(parent, child) {
  16299. // if (checkParentAvailable(parent, child)) {
  16300. // parent.appendChild(child);
  16301. // }
  16302. // }
  16303. function remove(parent, child) {
  16304. if (child && parent && child.parentNode === parent) {
  16305. parent.removeChild(child);
  16306. }
  16307. }
  16308. function getTextSvgElement(displayable) {
  16309. return displayable.__textSvgEl;
  16310. }
  16311. function getSvgElement(displayable) {
  16312. return displayable.__svgEl;
  16313. }
  16314. /**
  16315. * @alias module:zrender/svg/Painter
  16316. * @constructor
  16317. * @param {HTMLElement} root 绘图容器
  16318. * @param {module:zrender/Storage} storage
  16319. * @param {Object} opts
  16320. */
  16321. var SVGPainter = function (root, storage, opts, zrId) {
  16322. this.root = root;
  16323. this.storage = storage;
  16324. this._opts = opts = extend({}, opts || {});
  16325. var svgDom = createElement('svg');
  16326. svgDom.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
  16327. svgDom.setAttribute('version', '1.1');
  16328. svgDom.setAttribute('baseProfile', 'full');
  16329. svgDom.style.cssText = 'user-select:none;position:absolute;left:0;top:0;';
  16330. var bgRoot = createElement('g');
  16331. svgDom.appendChild(bgRoot);
  16332. var svgRoot = createElement('g');
  16333. svgDom.appendChild(svgRoot);
  16334. this.gradientManager = new GradientManager(zrId, svgRoot);
  16335. this.clipPathManager = new ClippathManager(zrId, svgRoot);
  16336. this.shadowManager = new ShadowManager(zrId, svgRoot);
  16337. var viewport = document.createElement('div');
  16338. viewport.style.cssText = 'overflow:hidden;position:relative';
  16339. this._svgDom = svgDom;
  16340. this._svgRoot = svgRoot;
  16341. this._backgroundRoot = bgRoot;
  16342. this._viewport = viewport;
  16343. root.appendChild(viewport);
  16344. viewport.appendChild(svgDom);
  16345. this.resize(opts.width, opts.height);
  16346. this._visibleList = [];
  16347. };
  16348. SVGPainter.prototype = {
  16349. constructor: SVGPainter,
  16350. getType: function () {
  16351. return 'svg';
  16352. },
  16353. getViewportRoot: function () {
  16354. return this._viewport;
  16355. },
  16356. getSvgDom: function () {
  16357. return this._svgDom;
  16358. },
  16359. getSvgRoot: function () {
  16360. return this._svgRoot;
  16361. },
  16362. getViewportRootOffset: function () {
  16363. var viewportRoot = this.getViewportRoot();
  16364. if (viewportRoot) {
  16365. return {
  16366. offsetLeft: viewportRoot.offsetLeft || 0,
  16367. offsetTop: viewportRoot.offsetTop || 0
  16368. };
  16369. }
  16370. },
  16371. refresh: function () {
  16372. var list = this.storage.getDisplayList(true);
  16373. this._paintList(list);
  16374. },
  16375. setBackgroundColor: function (backgroundColor) {
  16376. // TODO gradient
  16377. // Insert a bg rect instead of setting background to viewport.
  16378. // Otherwise, the exported SVG don't have background.
  16379. if (this._backgroundRoot && this._backgroundNode) {
  16380. this._backgroundRoot.removeChild(this._backgroundNode);
  16381. }
  16382. var bgNode = createElement('rect');
  16383. bgNode.setAttribute('width', this.getWidth());
  16384. bgNode.setAttribute('height', this.getHeight());
  16385. bgNode.setAttribute('x', 0);
  16386. bgNode.setAttribute('y', 0);
  16387. bgNode.setAttribute('id', 0);
  16388. bgNode.style.fill = backgroundColor;
  16389. this._backgroundRoot.appendChild(bgNode);
  16390. this._backgroundNode = bgNode;
  16391. },
  16392. _paintList: function (list) {
  16393. this.gradientManager.markAllUnused();
  16394. this.clipPathManager.markAllUnused();
  16395. this.shadowManager.markAllUnused();
  16396. var svgRoot = this._svgRoot;
  16397. var visibleList = this._visibleList;
  16398. var listLen = list.length;
  16399. var newVisibleList = [];
  16400. var i;
  16401. for (i = 0; i < listLen; i++) {
  16402. var displayable = list[i];
  16403. var svgProxy = getSvgProxy(displayable);
  16404. var svgElement = getSvgElement(displayable)
  16405. || getTextSvgElement(displayable);
  16406. if (!displayable.invisible) {
  16407. if (displayable.__dirty) {
  16408. svgProxy && svgProxy.brush(displayable);
  16409. // Update clipPath
  16410. this.clipPathManager.update(displayable);
  16411. // Update gradient and shadow
  16412. if (displayable.style) {
  16413. this.gradientManager
  16414. .update(displayable.style.fill);
  16415. this.gradientManager
  16416. .update(displayable.style.stroke);
  16417. this.shadowManager
  16418. .update(svgElement, displayable);
  16419. }
  16420. displayable.__dirty = false;
  16421. }
  16422. newVisibleList.push(displayable);
  16423. }
  16424. }
  16425. var diff = arrayDiff$1(visibleList, newVisibleList);
  16426. var prevSvgElement;
  16427. // First do remove, in case element moved to the head and do remove
  16428. // after add
  16429. for (i = 0; i < diff.length; i++) {
  16430. var item = diff[i];
  16431. if (item.removed) {
  16432. for (var k = 0; k < item.count; k++) {
  16433. var displayable = visibleList[item.indices[k]];
  16434. var svgElement = getSvgElement(displayable);
  16435. var textSvgElement = getTextSvgElement(displayable);
  16436. remove(svgRoot, svgElement);
  16437. remove(svgRoot, textSvgElement);
  16438. }
  16439. }
  16440. }
  16441. for (i = 0; i < diff.length; i++) {
  16442. var item = diff[i];
  16443. if (item.added) {
  16444. for (var k = 0; k < item.count; k++) {
  16445. var displayable = newVisibleList[item.indices[k]];
  16446. var svgElement = getSvgElement(displayable);
  16447. var textSvgElement = getTextSvgElement(displayable);
  16448. prevSvgElement
  16449. ? insertAfter(svgRoot, svgElement, prevSvgElement)
  16450. : prepend(svgRoot, svgElement);
  16451. if (svgElement) {
  16452. insertAfter(svgRoot, textSvgElement, svgElement);
  16453. }
  16454. else if (prevSvgElement) {
  16455. insertAfter(
  16456. svgRoot, textSvgElement, prevSvgElement
  16457. );
  16458. }
  16459. else {
  16460. prepend(svgRoot, textSvgElement);
  16461. }
  16462. // Insert text
  16463. insertAfter(svgRoot, textSvgElement, svgElement);
  16464. prevSvgElement = textSvgElement || svgElement
  16465. || prevSvgElement;
  16466. // zrender.Text only create textSvgElement.
  16467. this.gradientManager
  16468. .addWithoutUpdate(svgElement || textSvgElement, displayable);
  16469. this.shadowManager
  16470. .addWithoutUpdate(svgElement || textSvgElement, displayable);
  16471. this.clipPathManager.markUsed(displayable);
  16472. }
  16473. }
  16474. else if (!item.removed) {
  16475. for (var k = 0; k < item.count; k++) {
  16476. var displayable = newVisibleList[item.indices[k]];
  16477. var svgElement = getSvgElement(displayable);
  16478. var textSvgElement = getTextSvgElement(displayable);
  16479. var svgElement = getSvgElement(displayable);
  16480. var textSvgElement = getTextSvgElement(displayable);
  16481. this.gradientManager.markUsed(displayable);
  16482. this.gradientManager
  16483. .addWithoutUpdate(svgElement || textSvgElement, displayable);
  16484. this.shadowManager.markUsed(displayable);
  16485. this.shadowManager
  16486. .addWithoutUpdate(svgElement || textSvgElement, displayable);
  16487. this.clipPathManager.markUsed(displayable);
  16488. if (textSvgElement) { // Insert text.
  16489. insertAfter(svgRoot, textSvgElement, svgElement);
  16490. }
  16491. prevSvgElement = svgElement
  16492. || textSvgElement || prevSvgElement;
  16493. }
  16494. }
  16495. }
  16496. this.gradientManager.removeUnused();
  16497. this.clipPathManager.removeUnused();
  16498. this.shadowManager.removeUnused();
  16499. this._visibleList = newVisibleList;
  16500. },
  16501. _getDefs: function (isForceCreating) {
  16502. var svgRoot = this._svgDom;
  16503. var defs = svgRoot.getElementsByTagName('defs');
  16504. if (defs.length === 0) {
  16505. // Not exist
  16506. if (isForceCreating) {
  16507. var defs = svgRoot.insertBefore(
  16508. createElement('defs'), // Create new tag
  16509. svgRoot.firstChild // Insert in the front of svg
  16510. );
  16511. if (!defs.contains) {
  16512. // IE doesn't support contains method
  16513. defs.contains = function (el) {
  16514. var children = defs.children;
  16515. if (!children) {
  16516. return false;
  16517. }
  16518. for (var i = children.length - 1; i >= 0; --i) {
  16519. if (children[i] === el) {
  16520. return true;
  16521. }
  16522. }
  16523. return false;
  16524. };
  16525. }
  16526. return defs;
  16527. }
  16528. else {
  16529. return null;
  16530. }
  16531. }
  16532. else {
  16533. return defs[0];
  16534. }
  16535. },
  16536. resize: function (width, height) {
  16537. var viewport = this._viewport;
  16538. // FIXME Why ?
  16539. viewport.style.display = 'none';
  16540. // Save input w/h
  16541. var opts = this._opts;
  16542. width != null && (opts.width = width);
  16543. height != null && (opts.height = height);
  16544. width = this._getSize(0);
  16545. height = this._getSize(1);
  16546. viewport.style.display = '';
  16547. if (this._width !== width || this._height !== height) {
  16548. this._width = width;
  16549. this._height = height;
  16550. var viewportStyle = viewport.style;
  16551. viewportStyle.width = width + 'px';
  16552. viewportStyle.height = height + 'px';
  16553. var svgRoot = this._svgDom;
  16554. // Set width by 'svgRoot.width = width' is invalid
  16555. svgRoot.setAttribute('width', width);
  16556. svgRoot.setAttribute('height', height);
  16557. }
  16558. if (this._backgroundNode) {
  16559. this._backgroundNode.setAttribute('width', width);
  16560. this._backgroundNode.setAttribute('height', height);
  16561. }
  16562. },
  16563. /**
  16564. * 获取绘图区域宽度
  16565. */
  16566. getWidth: function () {
  16567. return this._width;
  16568. },
  16569. /**
  16570. * 获取绘图区域高度
  16571. */
  16572. getHeight: function () {
  16573. return this._height;
  16574. },
  16575. _getSize: function (whIdx) {
  16576. var opts = this._opts;
  16577. var wh = ['width', 'height'][whIdx];
  16578. var cwh = ['clientWidth', 'clientHeight'][whIdx];
  16579. var plt = ['paddingLeft', 'paddingTop'][whIdx];
  16580. var prb = ['paddingRight', 'paddingBottom'][whIdx];
  16581. if (opts[wh] != null && opts[wh] !== 'auto') {
  16582. return parseFloat(opts[wh]);
  16583. }
  16584. var root = this.root;
  16585. // IE8 does not support getComputedStyle, but it use VML.
  16586. var stl = document.defaultView.getComputedStyle(root);
  16587. return (
  16588. (root[cwh] || parseInt10$1(stl[wh]) || parseInt10$1(root.style[wh]))
  16589. - (parseInt10$1(stl[plt]) || 0)
  16590. - (parseInt10$1(stl[prb]) || 0)
  16591. ) | 0;
  16592. },
  16593. dispose: function () {
  16594. this.root.innerHTML = '';
  16595. this._svgRoot
  16596. = this._backgroundRoot
  16597. = this._svgDom
  16598. = this._backgroundNode
  16599. = this._viewport
  16600. = this.storage
  16601. = null;
  16602. },
  16603. clear: function () {
  16604. if (this._viewport) {
  16605. this.root.removeChild(this._viewport);
  16606. }
  16607. },
  16608. toDataURL: function () {
  16609. this.refresh();
  16610. var html = encodeURIComponent(this._svgDom.outerHTML.replace(/></g, '>\n\r<'));
  16611. return 'data:image/svg+xml;charset=UTF-8,' + html;
  16612. }
  16613. };
  16614. // Not supported methods
  16615. function createMethodNotSupport(method) {
  16616. return function () {
  16617. logError$1('In SVG mode painter not support method "' + method + '"');
  16618. };
  16619. }
  16620. // Unsuppoted methods
  16621. each([
  16622. 'getLayer', 'insertLayer', 'eachLayer', 'eachBuiltinLayer',
  16623. 'eachOtherLayer', 'getLayers', 'modLayer', 'delLayer', 'clearLayer',
  16624. 'pathToImage'
  16625. ], function (name) {
  16626. SVGPainter.prototype[name] = createMethodNotSupport(name);
  16627. });
  16628. registerPainter('svg', SVGPainter);
  16629. var urn = 'urn:schemas-microsoft-com:vml';
  16630. var win = typeof window === 'undefined' ? null : window;
  16631. var vmlInited = false;
  16632. var doc = win && win.document;
  16633. function createNode(tagName) {
  16634. return doCreateNode(tagName);
  16635. }
  16636. // Avoid assign to an exported variable, for transforming to cjs.
  16637. var doCreateNode;
  16638. if (doc && !env$1.canvasSupported) {
  16639. try {
  16640. !doc.namespaces.zrvml && doc.namespaces.add('zrvml', urn);
  16641. doCreateNode = function (tagName) {
  16642. return doc.createElement('<zrvml:' + tagName + ' class="zrvml">');
  16643. };
  16644. }
  16645. catch (e) {
  16646. doCreateNode = function (tagName) {
  16647. return doc.createElement('<' + tagName + ' xmlns="' + urn + '" class="zrvml">');
  16648. };
  16649. }
  16650. }
  16651. // From raphael
  16652. function initVML() {
  16653. if (vmlInited || !doc) {
  16654. return;
  16655. }
  16656. vmlInited = true;
  16657. var styleSheets = doc.styleSheets;
  16658. if (styleSheets.length < 31) {
  16659. doc.createStyleSheet().addRule('.zrvml', 'behavior:url(#default#VML)');
  16660. }
  16661. else {
  16662. // http://msdn.microsoft.com/en-us/library/ms531194%28VS.85%29.aspx
  16663. styleSheets[0].addRule('.zrvml', 'behavior:url(#default#VML)');
  16664. }
  16665. }
  16666. // http://www.w3.org/TR/NOTE-VML
  16667. // TODO Use proxy like svg instead of overwrite brush methods
  16668. var CMD$4 = PathProxy.CMD;
  16669. var round$1 = Math.round;
  16670. var sqrt = Math.sqrt;
  16671. var abs$1 = Math.abs;
  16672. var cos$4 = Math.cos;
  16673. var sin$4 = Math.sin;
  16674. var mathMax$3 = Math.max;
  16675. if (!env$1.canvasSupported) {
  16676. var comma = ',';
  16677. var imageTransformPrefix = 'progid:DXImageTransform.Microsoft';
  16678. var Z = 21600;
  16679. var Z2 = Z / 2;
  16680. var ZLEVEL_BASE = 100000;
  16681. var Z_BASE = 1000;
  16682. var initRootElStyle = function (el) {
  16683. el.style.cssText = 'position:absolute;left:0;top:0;width:1px;height:1px;';
  16684. el.coordsize = Z + ',' + Z;
  16685. el.coordorigin = '0,0';
  16686. };
  16687. var encodeHtmlAttribute = function (s) {
  16688. return String(s).replace(/&/g, '&amp;').replace(/"/g, '&quot;');
  16689. };
  16690. var rgb2Str = function (r, g, b) {
  16691. return 'rgb(' + [r, g, b].join(',') + ')';
  16692. };
  16693. var append = function (parent, child) {
  16694. if (child && parent && child.parentNode !== parent) {
  16695. parent.appendChild(child);
  16696. }
  16697. };
  16698. var remove$1 = function (parent, child) {
  16699. if (child && parent && child.parentNode === parent) {
  16700. parent.removeChild(child);
  16701. }
  16702. };
  16703. var getZIndex = function (zlevel, z, z2) {
  16704. // z 的取值范围为 [0, 1000]
  16705. return (parseFloat(zlevel) || 0) * ZLEVEL_BASE + (parseFloat(z) || 0) * Z_BASE + z2;
  16706. };
  16707. var parsePercent$1 = parsePercent;
  16708. /***************************************************
  16709. * PATH
  16710. **************************************************/
  16711. var setColorAndOpacity = function (el, color, opacity) {
  16712. var colorArr = parse(color);
  16713. opacity = +opacity;
  16714. if (isNaN(opacity)) {
  16715. opacity = 1;
  16716. }
  16717. if (colorArr) {
  16718. el.color = rgb2Str(colorArr[0], colorArr[1], colorArr[2]);
  16719. el.opacity = opacity * colorArr[3];
  16720. }
  16721. };
  16722. var getColorAndAlpha = function (color) {
  16723. var colorArr = parse(color);
  16724. return [
  16725. rgb2Str(colorArr[0], colorArr[1], colorArr[2]),
  16726. colorArr[3]
  16727. ];
  16728. };
  16729. var updateFillNode = function (el, style, zrEl) {
  16730. // TODO pattern
  16731. var fill = style.fill;
  16732. if (fill != null) {
  16733. // Modified from excanvas
  16734. if (fill instanceof Gradient) {
  16735. var gradientType;
  16736. var angle = 0;
  16737. var focus = [0, 0];
  16738. // additional offset
  16739. var shift = 0;
  16740. // scale factor for offset
  16741. var expansion = 1;
  16742. var rect = zrEl.getBoundingRect();
  16743. var rectWidth = rect.width;
  16744. var rectHeight = rect.height;
  16745. if (fill.type === 'linear') {
  16746. gradientType = 'gradient';
  16747. var transform = zrEl.transform;
  16748. var p0 = [fill.x * rectWidth, fill.y * rectHeight];
  16749. var p1 = [fill.x2 * rectWidth, fill.y2 * rectHeight];
  16750. if (transform) {
  16751. applyTransform(p0, p0, transform);
  16752. applyTransform(p1, p1, transform);
  16753. }
  16754. var dx = p1[0] - p0[0];
  16755. var dy = p1[1] - p0[1];
  16756. angle = Math.atan2(dx, dy) * 180 / Math.PI;
  16757. // The angle should be a non-negative number.
  16758. if (angle < 0) {
  16759. angle += 360;
  16760. }
  16761. // Very small angles produce an unexpected result because they are
  16762. // converted to a scientific notation string.
  16763. if (angle < 1e-6) {
  16764. angle = 0;
  16765. }
  16766. }
  16767. else {
  16768. gradientType = 'gradientradial';
  16769. var p0 = [fill.x * rectWidth, fill.y * rectHeight];
  16770. var transform = zrEl.transform;
  16771. var scale$$1 = zrEl.scale;
  16772. var width = rectWidth;
  16773. var height = rectHeight;
  16774. focus = [
  16775. // Percent in bounding rect
  16776. (p0[0] - rect.x) / width,
  16777. (p0[1] - rect.y) / height
  16778. ];
  16779. if (transform) {
  16780. applyTransform(p0, p0, transform);
  16781. }
  16782. width /= scale$$1[0] * Z;
  16783. height /= scale$$1[1] * Z;
  16784. var dimension = mathMax$3(width, height);
  16785. shift = 2 * 0 / dimension;
  16786. expansion = 2 * fill.r / dimension - shift;
  16787. }
  16788. // We need to sort the color stops in ascending order by offset,
  16789. // otherwise IE won't interpret it correctly.
  16790. var stops = fill.colorStops.slice();
  16791. stops.sort(function (cs1, cs2) {
  16792. return cs1.offset - cs2.offset;
  16793. });
  16794. var length$$1 = stops.length;
  16795. // Color and alpha list of first and last stop
  16796. var colorAndAlphaList = [];
  16797. var colors = [];
  16798. for (var i = 0; i < length$$1; i++) {
  16799. var stop = stops[i];
  16800. var colorAndAlpha = getColorAndAlpha(stop.color);
  16801. colors.push(stop.offset * expansion + shift + ' ' + colorAndAlpha[0]);
  16802. if (i === 0 || i === length$$1 - 1) {
  16803. colorAndAlphaList.push(colorAndAlpha);
  16804. }
  16805. }
  16806. if (length$$1 >= 2) {
  16807. var color1 = colorAndAlphaList[0][0];
  16808. var color2 = colorAndAlphaList[1][0];
  16809. var opacity1 = colorAndAlphaList[0][1] * style.opacity;
  16810. var opacity2 = colorAndAlphaList[1][1] * style.opacity;
  16811. el.type = gradientType;
  16812. el.method = 'none';
  16813. el.focus = '100%';
  16814. el.angle = angle;
  16815. el.color = color1;
  16816. el.color2 = color2;
  16817. el.colors = colors.join(',');
  16818. // When colors attribute is used, the meanings of opacity and o:opacity2
  16819. // are reversed.
  16820. el.opacity = opacity2;
  16821. // FIXME g_o_:opacity ?
  16822. el.opacity2 = opacity1;
  16823. }
  16824. if (gradientType === 'radial') {
  16825. el.focusposition = focus.join(',');
  16826. }
  16827. }
  16828. else {
  16829. // FIXME Change from Gradient fill to color fill
  16830. setColorAndOpacity(el, fill, style.opacity);
  16831. }
  16832. }
  16833. };
  16834. var updateStrokeNode = function (el, style) {
  16835. // if (style.lineJoin != null) {
  16836. // el.joinstyle = style.lineJoin;
  16837. // }
  16838. // if (style.miterLimit != null) {
  16839. // el.miterlimit = style.miterLimit * Z;
  16840. // }
  16841. // if (style.lineCap != null) {
  16842. // el.endcap = style.lineCap;
  16843. // }
  16844. if (style.lineDash) {
  16845. el.dashstyle = style.lineDash.join(' ');
  16846. }
  16847. if (style.stroke != null && !(style.stroke instanceof Gradient)) {
  16848. setColorAndOpacity(el, style.stroke, style.opacity);
  16849. }
  16850. };
  16851. var updateFillAndStroke = function (vmlEl, type, style, zrEl) {
  16852. var isFill = type === 'fill';
  16853. var el = vmlEl.getElementsByTagName(type)[0];
  16854. // Stroke must have lineWidth
  16855. if (style[type] != null && style[type] !== 'none' && (isFill || (!isFill && style.lineWidth))) {
  16856. vmlEl[isFill ? 'filled' : 'stroked'] = 'true';
  16857. // FIXME Remove before updating, or set `colors` will throw error
  16858. if (style[type] instanceof Gradient) {
  16859. remove$1(vmlEl, el);
  16860. }
  16861. if (!el) {
  16862. el = createNode(type);
  16863. }
  16864. isFill ? updateFillNode(el, style, zrEl) : updateStrokeNode(el, style);
  16865. append(vmlEl, el);
  16866. }
  16867. else {
  16868. vmlEl[isFill ? 'filled' : 'stroked'] = 'false';
  16869. remove$1(vmlEl, el);
  16870. }
  16871. };
  16872. var points$1 = [[], [], []];
  16873. var pathDataToString$1 = function (path, m) {
  16874. var M = CMD$4.M;
  16875. var C = CMD$4.C;
  16876. var L = CMD$4.L;
  16877. var A = CMD$4.A;
  16878. var Q = CMD$4.Q;
  16879. var str = [];
  16880. var nPoint;
  16881. var cmdStr;
  16882. var cmd;
  16883. var i;
  16884. var xi;
  16885. var yi;
  16886. var data = path.data;
  16887. var dataLength = path.len();
  16888. for (i = 0; i < dataLength;) {
  16889. cmd = data[i++];
  16890. cmdStr = '';
  16891. nPoint = 0;
  16892. switch (cmd) {
  16893. case M:
  16894. cmdStr = ' m ';
  16895. nPoint = 1;
  16896. xi = data[i++];
  16897. yi = data[i++];
  16898. points$1[0][0] = xi;
  16899. points$1[0][1] = yi;
  16900. break;
  16901. case L:
  16902. cmdStr = ' l ';
  16903. nPoint = 1;
  16904. xi = data[i++];
  16905. yi = data[i++];
  16906. points$1[0][0] = xi;
  16907. points$1[0][1] = yi;
  16908. break;
  16909. case Q:
  16910. case C:
  16911. cmdStr = ' c ';
  16912. nPoint = 3;
  16913. var x1 = data[i++];
  16914. var y1 = data[i++];
  16915. var x2 = data[i++];
  16916. var y2 = data[i++];
  16917. var x3;
  16918. var y3;
  16919. if (cmd === Q) {
  16920. // Convert quadratic to cubic using degree elevation
  16921. x3 = x2;
  16922. y3 = y2;
  16923. x2 = (x2 + 2 * x1) / 3;
  16924. y2 = (y2 + 2 * y1) / 3;
  16925. x1 = (xi + 2 * x1) / 3;
  16926. y1 = (yi + 2 * y1) / 3;
  16927. }
  16928. else {
  16929. x3 = data[i++];
  16930. y3 = data[i++];
  16931. }
  16932. points$1[0][0] = x1;
  16933. points$1[0][1] = y1;
  16934. points$1[1][0] = x2;
  16935. points$1[1][1] = y2;
  16936. points$1[2][0] = x3;
  16937. points$1[2][1] = y3;
  16938. xi = x3;
  16939. yi = y3;
  16940. break;
  16941. case A:
  16942. var x = 0;
  16943. var y = 0;
  16944. var sx = 1;
  16945. var sy = 1;
  16946. var angle = 0;
  16947. if (m) {
  16948. // Extract SRT from matrix
  16949. x = m[4];
  16950. y = m[5];
  16951. sx = sqrt(m[0] * m[0] + m[1] * m[1]);
  16952. sy = sqrt(m[2] * m[2] + m[3] * m[3]);
  16953. angle = Math.atan2(-m[1] / sy, m[0] / sx);
  16954. }
  16955. var cx = data[i++];
  16956. var cy = data[i++];
  16957. var rx = data[i++];
  16958. var ry = data[i++];
  16959. var startAngle = data[i++] + angle;
  16960. var endAngle = data[i++] + startAngle + angle;
  16961. // FIXME
  16962. // var psi = data[i++];
  16963. i++;
  16964. var clockwise = data[i++];
  16965. var x0 = cx + cos$4(startAngle) * rx;
  16966. var y0 = cy + sin$4(startAngle) * ry;
  16967. var x1 = cx + cos$4(endAngle) * rx;
  16968. var y1 = cy + sin$4(endAngle) * ry;
  16969. var type = clockwise ? ' wa ' : ' at ';
  16970. if (Math.abs(x0 - x1) < 1e-4) {
  16971. // IE won't render arches drawn counter clockwise if x0 == x1.
  16972. if (Math.abs(endAngle - startAngle) > 1e-2) {
  16973. // Offset x0 by 1/80 of a pixel. Use something
  16974. // that can be represented in binary
  16975. if (clockwise) {
  16976. x0 += 270 / Z;
  16977. }
  16978. }
  16979. else {
  16980. // Avoid case draw full circle
  16981. if (Math.abs(y0 - cy) < 1e-4) {
  16982. if ((clockwise && x0 < cx) || (!clockwise && x0 > cx)) {
  16983. y1 -= 270 / Z;
  16984. }
  16985. else {
  16986. y1 += 270 / Z;
  16987. }
  16988. }
  16989. else if ((clockwise && y0 < cy) || (!clockwise && y0 > cy)) {
  16990. x1 += 270 / Z;
  16991. }
  16992. else {
  16993. x1 -= 270 / Z;
  16994. }
  16995. }
  16996. }
  16997. str.push(
  16998. type,
  16999. round$1(((cx - rx) * sx + x) * Z - Z2), comma,
  17000. round$1(((cy - ry) * sy + y) * Z - Z2), comma,
  17001. round$1(((cx + rx) * sx + x) * Z - Z2), comma,
  17002. round$1(((cy + ry) * sy + y) * Z - Z2), comma,
  17003. round$1((x0 * sx + x) * Z - Z2), comma,
  17004. round$1((y0 * sy + y) * Z - Z2), comma,
  17005. round$1((x1 * sx + x) * Z - Z2), comma,
  17006. round$1((y1 * sy + y) * Z - Z2)
  17007. );
  17008. xi = x1;
  17009. yi = y1;
  17010. break;
  17011. case CMD$4.R:
  17012. var p0 = points$1[0];
  17013. var p1 = points$1[1];
  17014. // x0, y0
  17015. p0[0] = data[i++];
  17016. p0[1] = data[i++];
  17017. // x1, y1
  17018. p1[0] = p0[0] + data[i++];
  17019. p1[1] = p0[1] + data[i++];
  17020. if (m) {
  17021. applyTransform(p0, p0, m);
  17022. applyTransform(p1, p1, m);
  17023. }
  17024. p0[0] = round$1(p0[0] * Z - Z2);
  17025. p1[0] = round$1(p1[0] * Z - Z2);
  17026. p0[1] = round$1(p0[1] * Z - Z2);
  17027. p1[1] = round$1(p1[1] * Z - Z2);
  17028. str.push(
  17029. // x0, y0
  17030. ' m ', p0[0], comma, p0[1],
  17031. // x1, y0
  17032. ' l ', p1[0], comma, p0[1],
  17033. // x1, y1
  17034. ' l ', p1[0], comma, p1[1],
  17035. // x0, y1
  17036. ' l ', p0[0], comma, p1[1]
  17037. );
  17038. break;
  17039. case CMD$4.Z:
  17040. // FIXME Update xi, yi
  17041. str.push(' x ');
  17042. }
  17043. if (nPoint > 0) {
  17044. str.push(cmdStr);
  17045. for (var k = 0; k < nPoint; k++) {
  17046. var p = points$1[k];
  17047. m && applyTransform(p, p, m);
  17048. // 不 round 会非常慢
  17049. str.push(
  17050. round$1(p[0] * Z - Z2), comma, round$1(p[1] * Z - Z2),
  17051. k < nPoint - 1 ? comma : ''
  17052. );
  17053. }
  17054. }
  17055. }
  17056. return str.join('');
  17057. };
  17058. // Rewrite the original path method
  17059. Path.prototype.brushVML = function (vmlRoot) {
  17060. var style = this.style;
  17061. var vmlEl = this._vmlEl;
  17062. if (!vmlEl) {
  17063. vmlEl = createNode('shape');
  17064. initRootElStyle(vmlEl);
  17065. this._vmlEl = vmlEl;
  17066. }
  17067. updateFillAndStroke(vmlEl, 'fill', style, this);
  17068. updateFillAndStroke(vmlEl, 'stroke', style, this);
  17069. var m = this.transform;
  17070. var needTransform = m != null;
  17071. var strokeEl = vmlEl.getElementsByTagName('stroke')[0];
  17072. if (strokeEl) {
  17073. var lineWidth = style.lineWidth;
  17074. // Get the line scale.
  17075. // Determinant of this.m_ means how much the area is enlarged by the
  17076. // transformation. So its square root can be used as a scale factor
  17077. // for width.
  17078. if (needTransform && !style.strokeNoScale) {
  17079. var det = m[0] * m[3] - m[1] * m[2];
  17080. lineWidth *= sqrt(abs$1(det));
  17081. }
  17082. strokeEl.weight = lineWidth + 'px';
  17083. }
  17084. var path = this.path || (this.path = new PathProxy());
  17085. if (this.__dirtyPath) {
  17086. path.beginPath();
  17087. path.subPixelOptimize = false;
  17088. this.buildPath(path, this.shape);
  17089. path.toStatic();
  17090. this.__dirtyPath = false;
  17091. }
  17092. vmlEl.path = pathDataToString$1(path, this.transform);
  17093. vmlEl.style.zIndex = getZIndex(this.zlevel, this.z, this.z2);
  17094. // Append to root
  17095. append(vmlRoot, vmlEl);
  17096. // Text
  17097. if (style.text != null) {
  17098. this.drawRectText(vmlRoot, this.getBoundingRect());
  17099. }
  17100. else {
  17101. this.removeRectText(vmlRoot);
  17102. }
  17103. };
  17104. Path.prototype.onRemove = function (vmlRoot) {
  17105. remove$1(vmlRoot, this._vmlEl);
  17106. this.removeRectText(vmlRoot);
  17107. };
  17108. Path.prototype.onAdd = function (vmlRoot) {
  17109. append(vmlRoot, this._vmlEl);
  17110. this.appendRectText(vmlRoot);
  17111. };
  17112. /***************************************************
  17113. * IMAGE
  17114. **************************************************/
  17115. var isImage = function (img) {
  17116. // FIXME img instanceof Image 如果 img 是一个字符串的时候,IE8 下会报错
  17117. return (typeof img === 'object') && img.tagName && img.tagName.toUpperCase() === 'IMG';
  17118. // return img instanceof Image;
  17119. };
  17120. // Rewrite the original path method
  17121. ZImage.prototype.brushVML = function (vmlRoot) {
  17122. var style = this.style;
  17123. var image = style.image;
  17124. // Image original width, height
  17125. var ow;
  17126. var oh;
  17127. if (isImage(image)) {
  17128. var src = image.src;
  17129. if (src === this._imageSrc) {
  17130. ow = this._imageWidth;
  17131. oh = this._imageHeight;
  17132. }
  17133. else {
  17134. var imageRuntimeStyle = image.runtimeStyle;
  17135. var oldRuntimeWidth = imageRuntimeStyle.width;
  17136. var oldRuntimeHeight = imageRuntimeStyle.height;
  17137. imageRuntimeStyle.width = 'auto';
  17138. imageRuntimeStyle.height = 'auto';
  17139. // get the original size
  17140. ow = image.width;
  17141. oh = image.height;
  17142. // and remove overides
  17143. imageRuntimeStyle.width = oldRuntimeWidth;
  17144. imageRuntimeStyle.height = oldRuntimeHeight;
  17145. // Caching image original width, height and src
  17146. this._imageSrc = src;
  17147. this._imageWidth = ow;
  17148. this._imageHeight = oh;
  17149. }
  17150. image = src;
  17151. }
  17152. else {
  17153. if (image === this._imageSrc) {
  17154. ow = this._imageWidth;
  17155. oh = this._imageHeight;
  17156. }
  17157. }
  17158. if (!image) {
  17159. return;
  17160. }
  17161. var x = style.x || 0;
  17162. var y = style.y || 0;
  17163. var dw = style.width;
  17164. var dh = style.height;
  17165. var sw = style.sWidth;
  17166. var sh = style.sHeight;
  17167. var sx = style.sx || 0;
  17168. var sy = style.sy || 0;
  17169. var hasCrop = sw && sh;
  17170. var vmlEl = this._vmlEl;
  17171. if (!vmlEl) {
  17172. // FIXME 使用 group 在 left, top 都不是 0 的时候就无法显示了。
  17173. // vmlEl = vmlCore.createNode('group');
  17174. vmlEl = doc.createElement('div');
  17175. initRootElStyle(vmlEl);
  17176. this._vmlEl = vmlEl;
  17177. }
  17178. var vmlElStyle = vmlEl.style;
  17179. var hasRotation = false;
  17180. var m;
  17181. var scaleX = 1;
  17182. var scaleY = 1;
  17183. if (this.transform) {
  17184. m = this.transform;
  17185. scaleX = sqrt(m[0] * m[0] + m[1] * m[1]);
  17186. scaleY = sqrt(m[2] * m[2] + m[3] * m[3]);
  17187. hasRotation = m[1] || m[2];
  17188. }
  17189. if (hasRotation) {
  17190. // If filters are necessary (rotation exists), create them
  17191. // filters are bog-slow, so only create them if abbsolutely necessary
  17192. // The following check doesn't account for skews (which don't exist
  17193. // in the canvas spec (yet) anyway.
  17194. // From excanvas
  17195. var p0 = [x, y];
  17196. var p1 = [x + dw, y];
  17197. var p2 = [x, y + dh];
  17198. var p3 = [x + dw, y + dh];
  17199. applyTransform(p0, p0, m);
  17200. applyTransform(p1, p1, m);
  17201. applyTransform(p2, p2, m);
  17202. applyTransform(p3, p3, m);
  17203. var maxX = mathMax$3(p0[0], p1[0], p2[0], p3[0]);
  17204. var maxY = mathMax$3(p0[1], p1[1], p2[1], p3[1]);
  17205. var transformFilter = [];
  17206. transformFilter.push('M11=', m[0] / scaleX, comma,
  17207. 'M12=', m[2] / scaleY, comma,
  17208. 'M21=', m[1] / scaleX, comma,
  17209. 'M22=', m[3] / scaleY, comma,
  17210. 'Dx=', round$1(x * scaleX + m[4]), comma,
  17211. 'Dy=', round$1(y * scaleY + m[5]));
  17212. vmlElStyle.padding = '0 ' + round$1(maxX) + 'px ' + round$1(maxY) + 'px 0';
  17213. // FIXME DXImageTransform 在 IE11 的兼容模式下不起作用
  17214. vmlElStyle.filter = imageTransformPrefix + '.Matrix('
  17215. + transformFilter.join('') + ', SizingMethod=clip)';
  17216. }
  17217. else {
  17218. if (m) {
  17219. x = x * scaleX + m[4];
  17220. y = y * scaleY + m[5];
  17221. }
  17222. vmlElStyle.filter = '';
  17223. vmlElStyle.left = round$1(x) + 'px';
  17224. vmlElStyle.top = round$1(y) + 'px';
  17225. }
  17226. var imageEl = this._imageEl;
  17227. var cropEl = this._cropEl;
  17228. if (!imageEl) {
  17229. imageEl = doc.createElement('div');
  17230. this._imageEl = imageEl;
  17231. }
  17232. var imageELStyle = imageEl.style;
  17233. if (hasCrop) {
  17234. // Needs know image original width and height
  17235. if (!(ow && oh)) {
  17236. var tmpImage = new Image();
  17237. var self = this;
  17238. tmpImage.onload = function () {
  17239. tmpImage.onload = null;
  17240. ow = tmpImage.width;
  17241. oh = tmpImage.height;
  17242. // Adjust image width and height to fit the ratio destinationSize / sourceSize
  17243. imageELStyle.width = round$1(scaleX * ow * dw / sw) + 'px';
  17244. imageELStyle.height = round$1(scaleY * oh * dh / sh) + 'px';
  17245. // Caching image original width, height and src
  17246. self._imageWidth = ow;
  17247. self._imageHeight = oh;
  17248. self._imageSrc = image;
  17249. };
  17250. tmpImage.src = image;
  17251. }
  17252. else {
  17253. imageELStyle.width = round$1(scaleX * ow * dw / sw) + 'px';
  17254. imageELStyle.height = round$1(scaleY * oh * dh / sh) + 'px';
  17255. }
  17256. if (!cropEl) {
  17257. cropEl = doc.createElement('div');
  17258. cropEl.style.overflow = 'hidden';
  17259. this._cropEl = cropEl;
  17260. }
  17261. var cropElStyle = cropEl.style;
  17262. cropElStyle.width = round$1((dw + sx * dw / sw) * scaleX);
  17263. cropElStyle.height = round$1((dh + sy * dh / sh) * scaleY);
  17264. cropElStyle.filter = imageTransformPrefix + '.Matrix(Dx='
  17265. + (-sx * dw / sw * scaleX) + ',Dy=' + (-sy * dh / sh * scaleY) + ')';
  17266. if (!cropEl.parentNode) {
  17267. vmlEl.appendChild(cropEl);
  17268. }
  17269. if (imageEl.parentNode !== cropEl) {
  17270. cropEl.appendChild(imageEl);
  17271. }
  17272. }
  17273. else {
  17274. imageELStyle.width = round$1(scaleX * dw) + 'px';
  17275. imageELStyle.height = round$1(scaleY * dh) + 'px';
  17276. vmlEl.appendChild(imageEl);
  17277. if (cropEl && cropEl.parentNode) {
  17278. vmlEl.removeChild(cropEl);
  17279. this._cropEl = null;
  17280. }
  17281. }
  17282. var filterStr = '';
  17283. var alpha = style.opacity;
  17284. if (alpha < 1) {
  17285. filterStr += '.Alpha(opacity=' + round$1(alpha * 100) + ') ';
  17286. }
  17287. filterStr += imageTransformPrefix + '.AlphaImageLoader(src=' + image + ', SizingMethod=scale)';
  17288. imageELStyle.filter = filterStr;
  17289. vmlEl.style.zIndex = getZIndex(this.zlevel, this.z, this.z2);
  17290. // Append to root
  17291. append(vmlRoot, vmlEl);
  17292. // Text
  17293. if (style.text != null) {
  17294. this.drawRectText(vmlRoot, this.getBoundingRect());
  17295. }
  17296. };
  17297. ZImage.prototype.onRemove = function (vmlRoot) {
  17298. remove$1(vmlRoot, this._vmlEl);
  17299. this._vmlEl = null;
  17300. this._cropEl = null;
  17301. this._imageEl = null;
  17302. this.removeRectText(vmlRoot);
  17303. };
  17304. ZImage.prototype.onAdd = function (vmlRoot) {
  17305. append(vmlRoot, this._vmlEl);
  17306. this.appendRectText(vmlRoot);
  17307. };
  17308. /***************************************************
  17309. * TEXT
  17310. **************************************************/
  17311. var DEFAULT_STYLE_NORMAL = 'normal';
  17312. var fontStyleCache = {};
  17313. var fontStyleCacheCount = 0;
  17314. var MAX_FONT_CACHE_SIZE = 100;
  17315. var fontEl = document.createElement('div');
  17316. var getFontStyle = function (fontString) {
  17317. var fontStyle = fontStyleCache[fontString];
  17318. if (!fontStyle) {
  17319. // Clear cache
  17320. if (fontStyleCacheCount > MAX_FONT_CACHE_SIZE) {
  17321. fontStyleCacheCount = 0;
  17322. fontStyleCache = {};
  17323. }
  17324. var style = fontEl.style;
  17325. var fontFamily;
  17326. try {
  17327. style.font = fontString;
  17328. fontFamily = style.fontFamily.split(',')[0];
  17329. }
  17330. catch (e) {
  17331. }
  17332. fontStyle = {
  17333. style: style.fontStyle || DEFAULT_STYLE_NORMAL,
  17334. variant: style.fontVariant || DEFAULT_STYLE_NORMAL,
  17335. weight: style.fontWeight || DEFAULT_STYLE_NORMAL,
  17336. size: parseFloat(style.fontSize || 12) | 0,
  17337. family: fontFamily || 'Microsoft YaHei'
  17338. };
  17339. fontStyleCache[fontString] = fontStyle;
  17340. fontStyleCacheCount++;
  17341. }
  17342. return fontStyle;
  17343. };
  17344. var textMeasureEl;
  17345. // Overwrite measure text method
  17346. $override$1('measureText', function (text, textFont) {
  17347. var doc$$1 = doc;
  17348. if (!textMeasureEl) {
  17349. textMeasureEl = doc$$1.createElement('div');
  17350. textMeasureEl.style.cssText = 'position:absolute;top:-20000px;left:0;'
  17351. + 'padding:0;margin:0;border:none;white-space:pre;';
  17352. doc.body.appendChild(textMeasureEl);
  17353. }
  17354. try {
  17355. textMeasureEl.style.font = textFont;
  17356. }
  17357. catch (ex) {
  17358. // Ignore failures to set to invalid font.
  17359. }
  17360. textMeasureEl.innerHTML = '';
  17361. // Don't use innerHTML or innerText because they allow markup/whitespace.
  17362. textMeasureEl.appendChild(doc$$1.createTextNode(text));
  17363. return {
  17364. width: textMeasureEl.offsetWidth
  17365. };
  17366. });
  17367. var tmpRect$2 = new BoundingRect();
  17368. var drawRectText = function (vmlRoot, rect, textRect, fromTextEl) {
  17369. var style = this.style;
  17370. // Optimize, avoid normalize every time.
  17371. this.__dirty && normalizeTextStyle(style, true);
  17372. var text = style.text;
  17373. // Convert to string
  17374. text != null && (text += '');
  17375. if (!text) {
  17376. return;
  17377. }
  17378. // Convert rich text to plain text. Rich text is not supported in
  17379. // IE8-, but tags in rich text template will be removed.
  17380. if (style.rich) {
  17381. var contentBlock = parseRichText(text, style);
  17382. text = [];
  17383. for (var i = 0; i < contentBlock.lines.length; i++) {
  17384. var tokens = contentBlock.lines[i].tokens;
  17385. var textLine = [];
  17386. for (var j = 0; j < tokens.length; j++) {
  17387. textLine.push(tokens[j].text);
  17388. }
  17389. text.push(textLine.join(''));
  17390. }
  17391. text = text.join('\n');
  17392. }
  17393. var x;
  17394. var y;
  17395. var align = style.textAlign;
  17396. var verticalAlign = style.textVerticalAlign;
  17397. var fontStyle = getFontStyle(style.font);
  17398. // FIXME encodeHtmlAttribute ?
  17399. var font = fontStyle.style + ' ' + fontStyle.variant + ' ' + fontStyle.weight + ' '
  17400. + fontStyle.size + 'px "' + fontStyle.family + '"';
  17401. textRect = textRect || getBoundingRect(
  17402. text, font, align, verticalAlign, style.textPadding, style.textLineHeight
  17403. );
  17404. // Transform rect to view space
  17405. var m = this.transform;
  17406. // Ignore transform for text in other element
  17407. if (m && !fromTextEl) {
  17408. tmpRect$2.copy(rect);
  17409. tmpRect$2.applyTransform(m);
  17410. rect = tmpRect$2;
  17411. }
  17412. if (!fromTextEl) {
  17413. var textPosition = style.textPosition;
  17414. // Text position represented by coord
  17415. if (textPosition instanceof Array) {
  17416. x = rect.x + parsePercent$1(textPosition[0], rect.width);
  17417. y = rect.y + parsePercent$1(textPosition[1], rect.height);
  17418. align = align || 'left';
  17419. }
  17420. else {
  17421. var res = this.calculateTextPosition
  17422. ? this.calculateTextPosition({}, style, rect)
  17423. : calculateTextPosition({}, style, rect);
  17424. x = res.x;
  17425. y = res.y;
  17426. // Default align and baseline when has textPosition
  17427. align = align || res.textAlign;
  17428. verticalAlign = verticalAlign || res.textVerticalAlign;
  17429. }
  17430. }
  17431. else {
  17432. x = rect.x;
  17433. y = rect.y;
  17434. }
  17435. x = adjustTextX(x, textRect.width, align);
  17436. y = adjustTextY(y, textRect.height, verticalAlign);
  17437. // Force baseline 'middle'
  17438. y += textRect.height / 2;
  17439. // var fontSize = fontStyle.size;
  17440. // 1.75 is an arbitrary number, as there is no info about the text baseline
  17441. // switch (baseline) {
  17442. // case 'hanging':
  17443. // case 'top':
  17444. // y += fontSize / 1.75;
  17445. // break;
  17446. // case 'middle':
  17447. // break;
  17448. // default:
  17449. // // case null:
  17450. // // case 'alphabetic':
  17451. // // case 'ideographic':
  17452. // // case 'bottom':
  17453. // y -= fontSize / 2.25;
  17454. // break;
  17455. // }
  17456. // switch (align) {
  17457. // case 'left':
  17458. // break;
  17459. // case 'center':
  17460. // x -= textRect.width / 2;
  17461. // break;
  17462. // case 'right':
  17463. // x -= textRect.width;
  17464. // break;
  17465. // case 'end':
  17466. // align = elementStyle.direction == 'ltr' ? 'right' : 'left';
  17467. // break;
  17468. // case 'start':
  17469. // align = elementStyle.direction == 'rtl' ? 'right' : 'left';
  17470. // break;
  17471. // default:
  17472. // align = 'left';
  17473. // }
  17474. var createNode$$1 = createNode;
  17475. var textVmlEl = this._textVmlEl;
  17476. var pathEl;
  17477. var textPathEl;
  17478. var skewEl;
  17479. if (!textVmlEl) {
  17480. textVmlEl = createNode$$1('line');
  17481. pathEl = createNode$$1('path');
  17482. textPathEl = createNode$$1('textpath');
  17483. skewEl = createNode$$1('skew');
  17484. // FIXME Why here is not cammel case
  17485. // Align 'center' seems wrong
  17486. textPathEl.style['v-text-align'] = 'left';
  17487. initRootElStyle(textVmlEl);
  17488. pathEl.textpathok = true;
  17489. textPathEl.on = true;
  17490. textVmlEl.from = '0 0';
  17491. textVmlEl.to = '1000 0.05';
  17492. append(textVmlEl, skewEl);
  17493. append(textVmlEl, pathEl);
  17494. append(textVmlEl, textPathEl);
  17495. this._textVmlEl = textVmlEl;
  17496. }
  17497. else {
  17498. // 这里是在前面 appendChild 保证顺序的前提下
  17499. skewEl = textVmlEl.firstChild;
  17500. pathEl = skewEl.nextSibling;
  17501. textPathEl = pathEl.nextSibling;
  17502. }
  17503. var coords = [x, y];
  17504. var textVmlElStyle = textVmlEl.style;
  17505. // Ignore transform for text in other element
  17506. if (m && fromTextEl) {
  17507. applyTransform(coords, coords, m);
  17508. skewEl.on = true;
  17509. skewEl.matrix = m[0].toFixed(3) + comma + m[2].toFixed(3) + comma
  17510. + m[1].toFixed(3) + comma + m[3].toFixed(3) + ',0,0';
  17511. // Text position
  17512. skewEl.offset = (round$1(coords[0]) || 0) + ',' + (round$1(coords[1]) || 0);
  17513. // Left top point as origin
  17514. skewEl.origin = '0 0';
  17515. textVmlElStyle.left = '0px';
  17516. textVmlElStyle.top = '0px';
  17517. }
  17518. else {
  17519. skewEl.on = false;
  17520. textVmlElStyle.left = round$1(x) + 'px';
  17521. textVmlElStyle.top = round$1(y) + 'px';
  17522. }
  17523. textPathEl.string = encodeHtmlAttribute(text);
  17524. // TODO
  17525. try {
  17526. textPathEl.style.font = font;
  17527. }
  17528. // Error font format
  17529. catch (e) {}
  17530. updateFillAndStroke(textVmlEl, 'fill', {
  17531. fill: style.textFill,
  17532. opacity: style.opacity
  17533. }, this);
  17534. updateFillAndStroke(textVmlEl, 'stroke', {
  17535. stroke: style.textStroke,
  17536. opacity: style.opacity,
  17537. lineDash: style.lineDash || null // style.lineDash can be `false`.
  17538. }, this);
  17539. textVmlEl.style.zIndex = getZIndex(this.zlevel, this.z, this.z2);
  17540. // Attached to root
  17541. append(vmlRoot, textVmlEl);
  17542. };
  17543. var removeRectText = function (vmlRoot) {
  17544. remove$1(vmlRoot, this._textVmlEl);
  17545. this._textVmlEl = null;
  17546. };
  17547. var appendRectText = function (vmlRoot) {
  17548. append(vmlRoot, this._textVmlEl);
  17549. };
  17550. var list = [RectText, Displayable, ZImage, Path, Text];
  17551. // In case Displayable has been mixed in RectText
  17552. for (var i$1 = 0; i$1 < list.length; i$1++) {
  17553. var proto = list[i$1].prototype;
  17554. proto.drawRectText = drawRectText;
  17555. proto.removeRectText = removeRectText;
  17556. proto.appendRectText = appendRectText;
  17557. }
  17558. Text.prototype.brushVML = function (vmlRoot) {
  17559. var style = this.style;
  17560. if (style.text != null) {
  17561. this.drawRectText(vmlRoot, {
  17562. x: style.x || 0, y: style.y || 0,
  17563. width: 0, height: 0
  17564. }, this.getBoundingRect(), true);
  17565. }
  17566. else {
  17567. this.removeRectText(vmlRoot);
  17568. }
  17569. };
  17570. Text.prototype.onRemove = function (vmlRoot) {
  17571. this.removeRectText(vmlRoot);
  17572. };
  17573. Text.prototype.onAdd = function (vmlRoot) {
  17574. this.appendRectText(vmlRoot);
  17575. };
  17576. }
  17577. /**
  17578. * VML Painter.
  17579. *
  17580. * @module zrender/vml/Painter
  17581. */
  17582. function parseInt10$2(val) {
  17583. return parseInt(val, 10);
  17584. }
  17585. /**
  17586. * @alias module:zrender/vml/Painter
  17587. */
  17588. function VMLPainter(root, storage) {
  17589. initVML();
  17590. this.root = root;
  17591. this.storage = storage;
  17592. var vmlViewport = document.createElement('div');
  17593. var vmlRoot = document.createElement('div');
  17594. vmlViewport.style.cssText = 'display:inline-block;overflow:hidden;position:relative;width:300px;height:150px;';
  17595. vmlRoot.style.cssText = 'position:absolute;left:0;top:0;';
  17596. root.appendChild(vmlViewport);
  17597. this._vmlRoot = vmlRoot;
  17598. this._vmlViewport = vmlViewport;
  17599. this.resize();
  17600. // Modify storage
  17601. var oldDelFromStorage = storage.delFromStorage;
  17602. var oldAddToStorage = storage.addToStorage;
  17603. storage.delFromStorage = function (el) {
  17604. oldDelFromStorage.call(storage, el);
  17605. if (el) {
  17606. el.onRemove && el.onRemove(vmlRoot);
  17607. }
  17608. };
  17609. storage.addToStorage = function (el) {
  17610. // Displayable already has a vml node
  17611. el.onAdd && el.onAdd(vmlRoot);
  17612. oldAddToStorage.call(storage, el);
  17613. };
  17614. this._firstPaint = true;
  17615. }
  17616. VMLPainter.prototype = {
  17617. constructor: VMLPainter,
  17618. getType: function () {
  17619. return 'vml';
  17620. },
  17621. /**
  17622. * @return {HTMLDivElement}
  17623. */
  17624. getViewportRoot: function () {
  17625. return this._vmlViewport;
  17626. },
  17627. getViewportRootOffset: function () {
  17628. var viewportRoot = this.getViewportRoot();
  17629. if (viewportRoot) {
  17630. return {
  17631. offsetLeft: viewportRoot.offsetLeft || 0,
  17632. offsetTop: viewportRoot.offsetTop || 0
  17633. };
  17634. }
  17635. },
  17636. /**
  17637. * 刷新
  17638. */
  17639. refresh: function () {
  17640. var list = this.storage.getDisplayList(true, true);
  17641. this._paintList(list);
  17642. },
  17643. _paintList: function (list) {
  17644. var vmlRoot = this._vmlRoot;
  17645. for (var i = 0; i < list.length; i++) {
  17646. var el = list[i];
  17647. if (el.invisible || el.ignore) {
  17648. if (!el.__alreadyNotVisible) {
  17649. el.onRemove(vmlRoot);
  17650. }
  17651. // Set as already invisible
  17652. el.__alreadyNotVisible = true;
  17653. }
  17654. else {
  17655. if (el.__alreadyNotVisible) {
  17656. el.onAdd(vmlRoot);
  17657. }
  17658. el.__alreadyNotVisible = false;
  17659. if (el.__dirty) {
  17660. el.beforeBrush && el.beforeBrush();
  17661. (el.brushVML || el.brush).call(el, vmlRoot);
  17662. el.afterBrush && el.afterBrush();
  17663. }
  17664. }
  17665. el.__dirty = false;
  17666. }
  17667. if (this._firstPaint) {
  17668. // Detached from document at first time
  17669. // to avoid page refreshing too many times
  17670. // FIXME 如果每次都先 removeChild 可能会导致一些填充和描边的效果改变
  17671. this._vmlViewport.appendChild(vmlRoot);
  17672. this._firstPaint = false;
  17673. }
  17674. },
  17675. resize: function (width, height) {
  17676. var width = width == null ? this._getWidth() : width;
  17677. var height = height == null ? this._getHeight() : height;
  17678. if (this._width !== width || this._height !== height) {
  17679. this._width = width;
  17680. this._height = height;
  17681. var vmlViewportStyle = this._vmlViewport.style;
  17682. vmlViewportStyle.width = width + 'px';
  17683. vmlViewportStyle.height = height + 'px';
  17684. }
  17685. },
  17686. dispose: function () {
  17687. this.root.innerHTML = '';
  17688. this._vmlRoot =
  17689. this._vmlViewport =
  17690. this.storage = null;
  17691. },
  17692. getWidth: function () {
  17693. return this._width;
  17694. },
  17695. getHeight: function () {
  17696. return this._height;
  17697. },
  17698. clear: function () {
  17699. if (this._vmlViewport) {
  17700. this.root.removeChild(this._vmlViewport);
  17701. }
  17702. },
  17703. _getWidth: function () {
  17704. var root = this.root;
  17705. var stl = root.currentStyle;
  17706. return ((root.clientWidth || parseInt10$2(stl.width))
  17707. - parseInt10$2(stl.paddingLeft)
  17708. - parseInt10$2(stl.paddingRight)) | 0;
  17709. },
  17710. _getHeight: function () {
  17711. var root = this.root;
  17712. var stl = root.currentStyle;
  17713. return ((root.clientHeight || parseInt10$2(stl.height))
  17714. - parseInt10$2(stl.paddingTop)
  17715. - parseInt10$2(stl.paddingBottom)) | 0;
  17716. }
  17717. };
  17718. // Not supported methods
  17719. function createMethodNotSupport$1(method) {
  17720. return function () {
  17721. logError$1('In IE8.0 VML mode painter not support method "' + method + '"');
  17722. };
  17723. }
  17724. // Unsupported methods
  17725. each([
  17726. 'getLayer', 'insertLayer', 'eachLayer', 'eachBuiltinLayer', 'eachOtherLayer', 'getLayers',
  17727. 'modLayer', 'delLayer', 'clearLayer', 'toDataURL', 'pathToImage'
  17728. ], function (name) {
  17729. VMLPainter.prototype[name] = createMethodNotSupport$1(name);
  17730. });
  17731. registerPainter('vml', VMLPainter);
  17732. exports.version = version;
  17733. exports.init = init;
  17734. exports.dispose = dispose;
  17735. exports.getInstance = getInstance;
  17736. exports.registerPainter = registerPainter;
  17737. exports.matrix = matrix;
  17738. exports.vector = vector;
  17739. exports.color = color;
  17740. exports.path = path;
  17741. exports.util = util;
  17742. exports.parseSVG = parseSVG;
  17743. exports.Group = Group;
  17744. exports.Path = Path;
  17745. exports.Image = ZImage;
  17746. exports.CompoundPath = CompoundPath;
  17747. exports.Text = Text;
  17748. exports.IncrementalDisplayable = IncrementalDisplayble;
  17749. exports.Arc = Arc;
  17750. exports.BezierCurve = BezierCurve;
  17751. exports.Circle = Circle;
  17752. exports.Droplet = Droplet;
  17753. exports.Ellipse = Ellipse;
  17754. exports.Heart = Heart;
  17755. exports.Isogon = Isogon;
  17756. exports.Line = Line;
  17757. exports.Polygon = Polygon;
  17758. exports.Polyline = Polyline;
  17759. exports.Rect = Rect;
  17760. exports.Ring = Ring;
  17761. exports.Rose = Rose;
  17762. exports.Sector = Sector;
  17763. exports.Star = Star;
  17764. exports.Trochoid = Trochoid;
  17765. exports.LinearGradient = LinearGradient;
  17766. exports.RadialGradient = RadialGradient;
  17767. exports.Pattern = Pattern;
  17768. exports.BoundingRect = BoundingRect;
  17769. })));
  17770. //# sourceMappingURL=zrender.js.map