@aarsteinmedia/dotlottie-player 2.1.6 → 2.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.js +1199 -2
- package/dist/custom-elements.json +12 -0
- package/dist/esm/index.js +1199 -2
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -2
- package/package.json +7 -6
package/dist/esm/index.js
CHANGED
|
@@ -1,2 +1,1199 @@
|
|
|
1
|
-
import
|
|
2
|
-
${(null==e?void 0:e.length)?"":"- animations\n"} ${t?"":"- manifest \n"}`);let n=addExt("lottie",r)||`${useId()}.lottie`,a={"manifest.json":[strToU8(JSON.stringify(t)),{level:0}]};for(let[r,o]of e.entries()){if(null===(i=o.assets)||void 0===i?void 0:i.length)for(let e of o.assets){let{id:t,p:r}=e;if(t&&r){let o=getExtFromB64(r);e.p=`${t}.${o}`,e.e=0,a[`images/${t}.${o}`]=[base64ToU8(r),{level:9}]}}a[`animations/${t.animations[r].id}.json`]=[strToU8(JSON.stringify(o)),{level:9}]}let s=yield getArrayBuffer(a);return o?download(s,{name:n,mimeType:"application/zip"}):s}catch(e){console.error(`❌ ${handleErrors(e).message}`)}});return function(t,r,o){return e.apply(this,arguments)}}(),download=(e,t)=>{let r=new Blob([e],{type:null==t?void 0:t.mimeType}),o=(null==t?void 0:t.name)||useId(),i=URL.createObjectURL(r),n=document.createElement("a");n.href=i,n.download=o,n.hidden=!0,document.body.appendChild(n),n.click(),setTimeout(()=>{n.remove(),URL.revokeObjectURL(i)},1e3)},handleErrors=e=>{let t={message:"Unknown error",status:isServer()?500:400};return e&&"object"==typeof e&&("message"in e&&"string"==typeof e.message&&(t.message=e.message),"status"in e&&(t.status=Number(e.status))),t},frameOutput=e=>((null!=e?e:0)+1).toString().padStart(3,"0"),getAnimationData=function(){var e=_async_to_generator$1(function*(e){try{if(!e||"string"!=typeof e&&"object"!=typeof e)throw Error("Broken file or invalid file format");if("string"!=typeof e){let t=Array.isArray(e)?e:[e];return{animations:t,manifest:null}}let t=yield fetch(e);if(!t.ok){let e=new CustomError(t.statusText);throw e.status=t.status,e}let r=getExt(e);if("json"===r||!r){if(r){let e=yield t.json();return{animations:[e],manifest:null}}let e=yield t.clone().text();try{let t=JSON.parse(e);return{animations:[t],manifest:null}}catch(e){}}let{data:o,manifest:i}=yield getLottieJSON(t);return{animations:o,manifest:i,isDotLottie:!0}}catch(e){return console.error(`❌ ${handleErrors(e).message}`),{animations:null,manifest:null}}});return function(t){return e.apply(this,arguments)}}(),getArrayBuffer=function(){var e=_async_to_generator$1(function*(e){let t=yield new Promise((t,r)=>{c(e,{level:9},(e,o)=>{if(e){r(e);return}t(o.buffer)})});return t});return function(t){return e.apply(this,arguments)}}(),getExt=e=>{var t;if(e&&hasExt(e))return null===(t=e.split(".").pop())||void 0===t?void 0:t.toLowerCase()},getExtFromB64=e=>{let t=e.split(":")[1].split(";")[0];return t.split("/")[1].split("+")[0]},getFilename=(e,t)=>{let r=getExt(e);return`${e.replace(/\.[^.]*$/,"").replace(/\W+/g,"")}${t&&r?`.${r}`:""}`.toLowerCase()},getLottieJSON=function(){var e=_async_to_generator$1(function*(e){let t=yield unzip(e),r=getManifest(t),o=[];for(let{id:e}of r.animations){let r=d(t[`animations/${e}.json`]),i=JSON.parse(r);yield resolveAssets(t,i.assets),o.push(i)}return{data:o,manifest:r}});return function(t){return e.apply(this,arguments)}}(),getManifest=e=>{let t=d(e["manifest.json"],!1),r=JSON.parse(t);if(!("animations"in r))throw Error("Manifest not found");if(!r.animations.length)throw Error("No animations listed in manifest");return r},getMimeFromExt=e=>{switch(e){case"svg":case"svg+xml":return"image/svg+xml";case"jpg":case"jpeg":return"image/jpeg";case"png":case"gif":case"webp":return`image/${e}`;case"mp3":case"mpeg":case"wav":return`audio/${e}`;default:return""}},hasExt=e=>{var t;let r=null==e?void 0:null===(t=e.split("/").pop())||void 0===t?void 0:t.lastIndexOf(".");return(null!=r?r:0)>1&&e&&e.length-1>(null!=r?r:0)},isAudio=e=>!("h"in e)&&!("w"in e)&&"p"in e&&"e"in e&&"u"in e&&"id"in e,isImage=e=>"w"in e&&"h"in e&&!("xt"in e)&&"p"in e,isServer=()=>!("undefined"!=typeof window&&window.document),strToU8=e=>{let t=new Uint8Array(e.length);for(let r=0;r<e.length;r++)t[r]=e.charCodeAt(r);return t},resolveAssets=function(){var e=_async_to_generator$1(function*(e,t){if(!Array.isArray(t))return;let r=[];for(let o of t){if(!isAudio(o)&&!isImage(o))continue;let t=isImage(o)?"images":"audio",i=null==e?void 0:e[`${t}/${o.p}`];i&&r.push(new Promise(e=>{let t=isServer()?Buffer.from(i).toString("base64"):btoa(i.reduce((e,t)=>e+String.fromCharCode(t),""));o.p=`data:${getMimeFromExt(getExt(o.p))};base64,${t}`,o.e=1,o.u="",e()}))}yield Promise.all(r)});return function(t,r){return e.apply(this,arguments)}}(),unzip=function(){var e=_async_to_generator$1(function*(e){let t=new Uint8Array((yield e.arrayBuffer())),r=yield new Promise((e,r)=>{p(t,(t,o)=>{t&&r(t),e(o)})});return r});return function(t){return e.apply(this,arguments)}}(),useId=e=>{let t=()=>((1+Math.random())*65536|0).toString(16).substring(1);return`${null!=e?e:`:${t()}`}-${t()}`};var PlayerState,PlayMode,PlayerEvents,name="@aarsteinmedia/dotlottie-player",version="2.1.6",description="Web Component for playing Lottie animations in your web app. Previously @johanaarstein/dotlottie-player",exports={".":{import:"./dist/esm/index.js",node:"./dist/esm/index.js",require:"./dist/cjs/index.js",types:"./dist/index.d.ts"}},main="./dist/index.js",unpkg="./dist/index.js",module="./dist/esm/index.js",types="./dist/index.d.ts",type="module",homepage="https://www.aarstein.media/en/dotlottie-player",repository={url:"https://github.com/aarsteinmedia/dotlottie-player.git",type:"git"},bugs="https://github.com/aarsteinmedia/dotlottie-player/issues",author={name:"Johan Martin Aarstein",email:"johan@aarstein.media",url:"https://www.aarstein.media",organization:"Aarstein Media"},license="GPL-2.0-or-later",scripts={build:"pnpm cleanup && rollup -c","build:types":"pnpm cleanup:types && tsc","build:cem":"npx cem analyze --config 'cem.config.mjs'",prod:"pnpm build:types && pnpm build && pnpm build:cem",dev:"pnpm cleanup && rollup -c --watch",lint:"tsc && eslint . --ext .ts,.js","lint:fix":"eslint . --ext .ts,.js --fix","cleanup:types":"shx rm -rf types && shx mkdir types",cleanup:"shx rm -rf dist && shx mkdir dist"},dependencies={fflate:"^0.8.1",lit:"^2.8.0","lottie-web":"^5.12.2"},peerDependencies={"@types/react":"^18.2.28"},devDependencies={"@custom-elements-manifest/analyzer":"^0.6.9","@rollup/plugin-commonjs":"^25.0.7","@rollup/plugin-json":"^6.0.1","@rollup/plugin-node-resolve":"^15.2.3","@rollup/plugin-replace":"^5.0.4","@swc/core":"1.3.75","@types/node":"^20.8.8","@typescript-eslint/eslint-plugin":"^5.62.0","@typescript-eslint/parser":"^5.62.0",eslint:"^8.52.0",rollup:"^3.29.4","rollup-plugin-dts":"^6.1.0","rollup-plugin-html-literals":"^1.1.8","rollup-plugin-summary":"^2.0.0","rollup-plugin-swc3":"^0.9.1",shx:"^0.3.4","ts-lit-plugin":"^1.2.1",typescript:"^5.2.2"},customElements="dist/custom-elements.json",files=["dist","README.md"],keywords=["lottie","dotlottie","animation","web component","component","lit-element","svg","vector","player"],publishConfig={access:"public"},engines={node:">= 7.6.0"},funding={type:"paypal",url:"https://www.paypal.com/donate/?hosted_button_id=E7C7DMN8KSQ6A"},pkg={name:name,version:version,description:description,exports:exports,main:main,unpkg:unpkg,module:module,types:types,type:type,homepage:homepage,repository:repository,bugs:bugs,author:author,license:license,scripts:scripts,dependencies:dependencies,peerDependencies:peerDependencies,devDependencies:devDependencies,customElements:customElements,files:files,keywords:keywords,publishConfig:publishConfig,engines:engines,funding:funding},styles=e`*{box-sizing:border-box}:host{--lottie-player-toolbar-height:35px;--lottie-player-toolbar-background-color:#FFF;--lottie-player-toolbar-icon-color:#000;--lottie-player-toolbar-icon-hover-color:#000;--lottie-player-toolbar-icon-active-color:#4285f4;--lottie-player-seeker-track-color:rgba(0, 0, 0, 0.2);--lottie-player-seeker-thumb-color:#4285f4;--lottie-player-seeker-display:block;display:block;width:100%;height:100%}@media (prefers-color-scheme:dark){:host{--lottie-player-toolbar-background-color:#000;--lottie-player-toolbar-icon-color:#FFF;--lottie-player-toolbar-icon-hover-color:#FFF;--lottie-player-seeker-track-color:rgba(255, 255, 255, 0.6)}}.main{display:flex;flex-direction:column;height:100%;width:100%;margin:0}.animation{width:100%;height:100%;display:flex}[data-controls=true] .animation{height:calc(100% - 35px)}.animation-container{position:relative}.popover{position:absolute;right:5px;bottom:40px;background-color:var(--lottie-player-toolbar-background-color);border-radius:5px;padding:10px 15px;border:solid 2px var(--lottie-player-toolbar-icon-color);animation:fadeIn .2s ease-in-out}.popover::before{content:"";right:10px;border:7px solid transparent;border-top-color:transparent;margin-right:-7px;height:0;width:0;position:absolute;pointer-events:none;top:100%;border-top-color:var(--lottie-player-toolbar-icon-color)}.toolbar{display:flex;align-items:center;justify-items:center;background:var(--lottie-player-toolbar-background-color);margin:0;height:35px;padding:5px;border-radius:5px;gap:5px}.toolbar.has-error{pointer-events:none;opacity:.5}.toolbar button{cursor:pointer;fill:var(--lottie-player-toolbar-icon-color);color:var(--lottie-player-toolbar-icon-color);display:flex;background:0 0;border:0;padding:0;outline:0;height:100%;margin:0;align-items:center;gap:5px;opacity:.9}.toolbar button:hover{opacity:1}.toolbar button[data-active=true]{opacity:1;fill:var(--lottie-player-toolbar-icon-active-color)}.toolbar button:disabled{opacity:.5}.toolbar button:focus{outline:0}.toolbar button svg{pointer-events:none}.toolbar button svg>*{fill:inherit}.toolbar button.disabled svg{display:none}.progress-container{position:relative;width:100%}.progress-container.simple{margin-right:12px}.seeker,.seeker::-webkit-slider-runnable-track,.seeker::-webkit-slider-thumb,progress{-webkit-appearance:none;appearance:none;outline:0}.seeker{width:100%;height:20px;border-radius:3px;border:0;cursor:pointer;background-color:transparent;display:var(--lottie-player-seeker-display);color:var(--lottie-player-seeker-thumb-color);margin:0;padding:7.5px 0;position:relative;z-index:1}progress{position:absolute;width:100%;height:5px;border-radius:3px;border:0;top:0;left:0;margin:7.5px 0;background-color:var(--lottie-player-seeker-track-color);pointer-events:none}::-moz-progress-bar{background-color:var(--lottie-player-seeker-thumb-color)}::-webkit-progress-inner-element{border-radius:3px;overflow:hidden}::-webkit-slider-runnable-track{background-color:transparent}::-webkit-progress-value{background-color:var(--lottie-player-seeker-thumb-color)}.seeker::-webkit-slider-thumb{height:15px;width:15px;border-radius:50%;border:0;background-color:var(--lottie-player-seeker-thumb-color);cursor:pointer;transition:transform .2s ease-in-out;transform:scale(0)}.seeker:focus::-webkit-slider-thumb,.seeker:hover::-webkit-slider-thumb{transform:scale(1)}.seeker::-moz-range-progress{background-color:var(--lottie-player-seeker-thumb-color);height:5px;border-radius:3px}.seeker::-moz-range-thumb{height:15px;width:15px;border-radius:50%;background-color:var(--lottie-player-seeker-thumb-color);border:0;cursor:pointer;transition:transform .2s ease-in-out;transform:scale(0)}.seeker:focus::-moz-range-thumb,.seeker:hover::-moz-range-thumb{transform:scale(1)}.seeker::-ms-track{width:100%;height:5px;cursor:pointer;background:0 0;border-color:transparent;color:transparent}.seeker::-ms-fill-upper{background:var(--lottie-player-seeker-track-color);border-radius:3px}.seeker::-ms-fill-lower{background-color:var(--lottie-player-seeker-thumb-color);border-radius:3px}.seeker::-ms-thumb{border:0;height:15px;width:15px;border-radius:50%;background:var(--lottie-player-seeker-thumb-color);cursor:pointer;transition:transform .2s ease-in-out;transform:scale(0)}.seeker:focus::-ms-thumb,.seeker:hover::-ms-thumb{transform:scale(1)}.seeker:focus::-ms-fill-lower{background:var(--lottie-player-seeker-track-color)}.seeker:focus::-ms-fill-upper{background:var(--lottie-player-seeker-track-color)}.error{display:flex;margin:auto;justify-content:center;height:100%;align-items:center}.error svg{width:100%;height:auto}@keyframes fadeIn{0%{opacity:0}100%{opacity:1}}`;function asyncGeneratorStep(e,t,r,o,i,n,a){try{var s=e[n](a),l=s.value}catch(e){r(e);return}s.done?t(l):Promise.resolve(l).then(o,i)}function _async_to_generator(e){return function(){var t=this,r=arguments;return new Promise(function(o,i){var n=e.apply(t,r);function a(e){asyncGeneratorStep(n,o,i,a,s,"next",e)}function s(e){asyncGeneratorStep(n,o,i,a,s,"throw",e)}a(void 0)})}}function _define_property(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function _object_spread(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{},o=Object.keys(r);"function"==typeof Object.getOwnPropertySymbols&&(o=o.concat(Object.getOwnPropertySymbols(r).filter(function(e){return Object.getOwnPropertyDescriptor(r,e).enumerable}))),o.forEach(function(t){_define_property(e,t,r[t])})}return e}function ownKeys(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);t&&(o=o.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),r.push.apply(r,o)}return r}function _object_spread_props(e,t){return t=null!=t?t:{},Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):ownKeys(Object(t)).forEach(function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}),e}function _ts_decorate(e,t,r,o){for(var i,n=arguments.length,a=n<3?t:null===o?o=Object.getOwnPropertyDescriptor(t,r):o,s=e.length-1;s>=0;s--)(i=e[s])&&(a=(n<3?i(a):n>3?i(t,r,a):i(t,r))||a);return n>3&&a&&Object.defineProperty(t,r,a),a}let DotLottiePlayer=class extends t{_getOptions(){var e;let t=null!==(e=this.preserveAspectRatio)&&void 0!==e?e:this.objectfit&&aspectRatio(this.objectfit),r=!this.segment||this.segment.some(e=>e<0)?void 0:this.segment.every(e=>e>0)?[this.segment[0]-1,this.segment[1]-1]:this.segment,o={container:this.container,loop:!!this.loop,autoplay:!!this.autoplay,renderer:this.renderer,initialSegment:r,rendererSettings:{imagePreserveAspectRatio:t}};switch(this.renderer){case"svg":o.rendererSettings=_object_spread_props(_object_spread({},o.rendererSettings),{hideOnTransparent:!0,preserveAspectRatio:t,progressiveLoad:!0});break;case"canvas":o.rendererSettings=_object_spread_props(_object_spread({},o.rendererSettings),{clearCanvas:!0,preserveAspectRatio:t,progressiveLoad:!0});break;case"html":o.rendererSettings=_object_spread_props(_object_spread({},o.rendererSettings),{hideOnTransparent:!0})}return o}load(e){var t=this;return _async_to_generator(function*(){var r;if(t.shadowRoot){try{let{animations:r,manifest:o,isDotLottie:i}=yield getAnimationData(e);if(!r||r.some(e=>!t._isLottie(e)))throw Error("Broken or corrupted file");t._isDotLottie=!!i,t._animations=r,t._manifest=null!=o?o:{animations:[{id:useId(),autoplay:t.autoplay,loop:t.loop,direction:t.direction,mode:t.mode,speed:t.speed}]},t._lottieInstance&&t._lottieInstance.destroy(),t._lottieInstance=l.loadAnimation(_object_spread_props(_object_spread({},t._getOptions()),{animationData:r[t._currentAnimation]}))}catch(e){t._errorMessage=handleErrors(e).message,t.currentState=PlayerState.Error,t.dispatchEvent(new CustomEvent(PlayerEvents.Error));return}t._addEventListeners(),t.setSpeed(t.speed),t.setDirection(null!==(r=t.direction)&&void 0!==r?r:1),t.setSubframe(!!t.subframe),t.autoplay&&(-1===t.direction&&t.seek("99%"),t.play())}})()}_addEventListeners(){this._lottieInstance&&(this._lottieInstance.addEventListener("enterFrame",()=>{if(this._lottieInstance){let{currentFrame:e,totalFrames:t}=this._lottieInstance;this._seeker=Math.floor(e/t*100),this.dispatchEvent(new CustomEvent(PlayerEvents.Frame,{detail:{frame:e,seeker:this._seeker}}))}}),this._lottieInstance.addEventListener("complete",()=>{var e,t;this.currentState=PlayerState.Completed,this.dispatchEvent(new CustomEvent(PlayerEvents.Complete)),(null===(e=this._animations)||void 0===e?void 0:e.length)>1&&this.autoplay&&this._currentAnimation<(null===(t=this._animations)||void 0===t?void 0:t.length)-1&&this.next()}),this._lottieInstance.addEventListener("loopComplete",()=>{var e,t,r;if(!this._lottieInstance)return;let{firstFrame:o,totalFrames:i,playDirection:n}=this._lottieInstance;if(this.count&&(this.mode===PlayMode.Bounce?this._playerState.count+=1:this._playerState.count+=.5,this._playerState.count>=this.count)){this.setLooping(!1),this.currentState=PlayerState.Completed,this.dispatchEvent(new CustomEvent(PlayerEvents.Complete));return}return(this.dispatchEvent(new CustomEvent(PlayerEvents.Loop)),this.mode===PlayMode.Bounce)?(null===(t=this._lottieInstance)||void 0===t||t.goToAndStop(-1===n?o:.99*i,!0),null===(r=this._lottieInstance)||void 0===r||r.setDirection(-1*n),setTimeout(()=>{var e;null===(e=this._lottieInstance)||void 0===e||e.play()},this.intermission)):(null===(e=this._lottieInstance)||void 0===e||e.goToAndStop(-1===n?.99*i:o,!0),setTimeout(()=>{var e;null===(e=this._lottieInstance)||void 0===e||e.play()},this.intermission))}),this._lottieInstance.addEventListener("DOMLoaded",()=>{this._playerState.loaded=!0,this.dispatchEvent(new CustomEvent(PlayerEvents.Ready))}),this._lottieInstance.addEventListener("data_ready",()=>{this.dispatchEvent(new CustomEvent(PlayerEvents.Load))}),this._lottieInstance.addEventListener("data_failed",()=>{this.currentState=PlayerState.Error,this.dispatchEvent(new CustomEvent(PlayerEvents.Error))}),this.container&&(this.container.addEventListener("mouseenter",()=>{this.hover&&this.currentState!==PlayerState.Playing&&this.play()}),this.container.addEventListener("mouseleave",()=>{this.hover&&this.currentState===PlayerState.Playing&&this.stop()})))}_onVisibilityChange(){document.hidden&&this.currentState===PlayerState.Playing?this._freeze():this.currentState===PlayerState.Frozen&&this.play()}_handleSeekChange({target:e}){!(!(e instanceof HTMLInputElement)||!this._lottieInstance||isNaN(Number(e.value)))&&(this.seek(Math.floor(Number(e.value)/100*this._lottieInstance.totalFrames)),setTimeout(()=>{e.parentElement instanceof HTMLFormElement&&e.parentElement.reset()},100))}_isLottie(e){return["v","ip","op","layers","fr","w","h"].every(t=>Object.prototype.hasOwnProperty.call(e,t))}addAnimation(e,t,r=!0){var o=this;return _async_to_generator(function*(){try{let i=o._manifest||{animations:[]},n=_object_spread_props(_object_spread({},i),{generator:pkg.name}),a=o._animations||[];for(let t of e){let{url:e}=t,{animations:r}=yield getAnimationData(e);if(!r)throw Error("No animation loaded");if(n.animations.some(({id:e})=>e===t.id))throw Error("Duplicate id for animation");n=_object_spread_props(_object_spread({},n),{animations:[...n.animations,t]}),a=[...a,...r]}return createDotLottie(a,n,t,r)}catch(e){console.error(handleErrors(e).message)}})()}getLottie(){return this._lottieInstance}play(){this._lottieInstance&&(this.currentState&&(this._playerState.prev=this.currentState),this._lottieInstance.play(),setTimeout(()=>{this.currentState=PlayerState.Playing},0),this.dispatchEvent(new CustomEvent(PlayerEvents.Play)))}pause(){this._lottieInstance&&(this.currentState&&(this._playerState.prev=this.currentState),this._lottieInstance.pause(),setTimeout(()=>{this.currentState=PlayerState.Paused},0),this.dispatchEvent(new CustomEvent(PlayerEvents.Pause)))}stop(){this._lottieInstance&&(this.currentState&&(this._playerState.prev=this.currentState),this._playerState.count=0,this._lottieInstance.stop(),setTimeout(()=>{this.currentState=PlayerState.Stopped},0),this.dispatchEvent(new CustomEvent(PlayerEvents.Stop)))}destroy(){this._lottieInstance&&(this.currentState=PlayerState.Destroyed,this._lottieInstance.destroy(),this._lottieInstance=null,this.dispatchEvent(new CustomEvent(PlayerEvents.Destroyed)),this.remove())}seek(e){if(!this._lottieInstance)return;let t=e.toString().match(/^([0-9]+)(%?)$/);if(!t)return;let r=Math.floor("%"===t[2]?this._lottieInstance.totalFrames*Number(t[1])/100:Number(t[1]));this._seeker=r,this.currentState===PlayerState.Playing||this.currentState===PlayerState.Frozen&&this._playerState.prev===PlayerState.Playing?(this._lottieInstance.goToAndPlay(r,!0),this.currentState=PlayerState.Playing):(this._lottieInstance.goToAndStop(r,!0),this._lottieInstance.pause())}snapshot(){if(!this.shadowRoot)return;let e=this.shadowRoot.querySelector(".animation svg"),t=e instanceof Node?new XMLSerializer().serializeToString(e):null;if(!t){console.error("Could not serialize data");return}return download(t,{name:`${getFilename(this.src)}-${frameOutput(this._seeker)}.svg`,mimeType:"image/svg+xml"}),t}setSubframe(e){this._lottieInstance&&(this.subframe=e,this._lottieInstance.setSubframe(e))}_freeze(){this._lottieInstance&&(this.currentState&&(this._playerState.prev=this.currentState),this._lottieInstance.pause(),setTimeout(()=>{this.currentState=PlayerState.Frozen},0),this.dispatchEvent(new CustomEvent(PlayerEvents.Freeze)))}reload(){var e=this;return _async_to_generator(function*(){e._lottieInstance&&(e._lottieInstance.destroy(),e.src&&(yield e.load(e.src)))})()}setSpeed(e=1){this._lottieInstance&&(this.speed=e,this._lottieInstance.setSpeed(e))}setDirection(e){this._lottieInstance&&(this.direction=e,this._lottieInstance.setDirection(e))}setLooping(e){this._lottieInstance&&(this.loop=e,this._lottieInstance.setLoop(e))}togglePlay(){if(!this._lottieInstance)return;let{currentFrame:e,playDirection:t,totalFrames:r}=this._lottieInstance;return this.currentState===PlayerState.Playing?this.pause():this.currentState===PlayerState.Completed?(this.currentState=PlayerState.Playing,this.mode===PlayMode.Bounce)?(this.setDirection(-1*t),this._lottieInstance.goToAndPlay(e,!0)):-1===t?this._lottieInstance.goToAndPlay(r,!0):this._lottieInstance.goToAndPlay(0,!0):this.play()}toggleLooping(){this.setLooping(!this.loop)}toggleBoomerang(){this.mode===PlayMode.Normal?this.mode=PlayMode.Bounce:this.mode=PlayMode.Normal}_toggleSettings(e){void 0===e?this._isSettingsOpen=!this._isSettingsOpen:this._isSettingsOpen=e}_switchInstance(){this._lottieInstance&&this._lottieInstance.destroy(),this._lottieInstance=l.loadAnimation(_object_spread_props(_object_spread({},this._getOptions()),{animationData:this._animations[this._currentAnimation]})),this._addEventListeners(),this.autoplay?(this._lottieInstance.goToAndPlay(0,!0),this.currentState=PlayerState.Playing):this._lottieInstance.goToAndStop(0,!0)}next(){this._currentAnimation++,this._switchInstance()}prev(){this._currentAnimation--,this._switchInstance()}convert(e,t,r,o,i=!0){if(e||this._isDotLottie)return;let n=t||this._manifest,a=_object_spread_props(_object_spread({},n),{generator:pkg.name});return createDotLottie(r||this._animations,a,`${getFilename(o||this.src)}.lottie`,i)}static get styles(){return styles}connectedCallback(){super.connectedCallback(),void 0!==document.hidden&&document.addEventListener("visibilitychange",this._onVisibilityChange)}firstUpdated(){var e=this;return _async_to_generator(function*(){"IntersectionObserver"in window&&(e._intersectionObserver=new IntersectionObserver(t=>{t[0].isIntersecting?document.hidden||e.currentState!==PlayerState.Frozen||e.play():e.currentState===PlayerState.Playing&&e._freeze()}),e._intersectionObserver.observe(e.container)),e.src&&(yield e.load(e.src)),e.dispatchEvent(new CustomEvent(PlayerEvents.Rendered))})()}disconnectedCallback(){super.disconnectedCallback(),this._intersectionObserver&&(this._intersectionObserver.disconnect(),this._intersectionObserver=void 0),this._lottieInstance&&this._lottieInstance.destroy(),document.removeEventListener("visibilitychange",this._onVisibilityChange)}renderControls(){var e,t;let i=this.currentState===PlayerState.Playing,n=this.currentState===PlayerState.Paused,a=this.currentState===PlayerState.Stopped,s=this.currentState===PlayerState.Error;return r`<div class="${`lottie-controls toolbar ${s?"has-error":""}`}" aria-label="Lottie Animation controls"><button @click="${this.togglePlay}" data-active="${i||n}" tabindex="0" aria-label="Toggle Play/Pause">${i?r`<svg width="24" height="24" aria-hidden="true" focusable="false"><path d="M14.016 5.016H18v13.969h-3.984V5.016zM6 18.984V5.015h3.984v13.969H6z"/></svg>`:r`<svg width="24" height="24" aria-hidden="true" focusable="false"><path d="M8.016 5.016L18.985 12 8.016 18.984V5.015z"/></svg>`}</button> <button @click="${this.stop}" data-active="${a}" tabindex="0" aria-label="Stop"><svg width="24" height="24" aria-hidden="true" focusable="false"><path d="M6 6h12v12H6V6z"/></svg></button> ${(null===(e=this._animations)||void 0===e?void 0:e.length)>1?r`${this._currentAnimation>0?r`<button @click="${this.prev}" tabindex="0" aria-label="Previous animation"><svg width="24" height="24" aria-hidden="true" focusable="false"><path d="M17.9 18.2 8.1 12l9.8-6.2v12.4zm-10.3 0H6.1V5.8h1.5v12.4z"/></svg></button>`:o} ${this._currentAnimation+1<(null===(t=this._animations)||void 0===t?void 0:t.length)?r`<button @click="${this.next}" tabindex="0" aria-label="Next animation"><svg width="24" height="24" aria-hidden="true" focusable="false"><path d="m6.1 5.8 9.8 6.2-9.8 6.2V5.8zM16.4 5.8h1.5v12.4h-1.5z"/></svg></button>`:o}`:o}<form class="progress-container${this.simple?" simple":""}"><input class="seeker" type="range" min="0" max="100" step="1" value="${this._seeker}" @change="${this._handleSeekChange}" @mousedown="${this._freeze}" aria-valuemin="0" aria-valuemax="100" role="slider" aria-valuenow="${this._seeker}" tabindex="0" aria-label="Slider for search"><progress min="0" max="100" value="${this._seeker}"></progress></form>${this.simple?o:r`<button @click="${this.toggleLooping}" data-active="${this.loop}" tabindex="0" aria-label="Toggle looping"><svg width="24" height="24" aria-hidden="true" focusable="false"><path d="M17.016 17.016v-4.031h1.969v6h-12v3l-3.984-3.984 3.984-3.984v3h10.031zM6.984 6.984v4.031H5.015v-6h12v-3l3.984 3.984-3.984 3.984v-3H6.984z"/></svg></button> <button @click="${this.toggleBoomerang}" data-active="${this.mode===PlayMode.Bounce}" aria-label="Toggle boomerang" tabindex="0"><svg width="24" height="24" aria-hidden="true" focusable="false"><path d="m11.8 13.2-.3.3c-.5.5-1.1 1.1-1.7 1.5-.5.4-1 .6-1.5.8-.5.2-1.1.3-1.6.3s-1-.1-1.5-.3c-.6-.2-1-.5-1.4-1-.5-.6-.8-1.2-.9-1.9-.2-.9-.1-1.8.3-2.6.3-.7.8-1.2 1.3-1.6.3-.2.6-.4 1-.5.2-.2.5-.2.8-.3.3 0 .7-.1 1 0 .3 0 .6.1.9.2.9.3 1.7.9 2.4 1.5.4.4.8.7 1.1 1.1l.1.1.4-.4c.6-.6 1.2-1.2 1.9-1.6.5-.3 1-.6 1.5-.7.4-.1.7-.2 1-.2h.9c1 .1 1.9.5 2.6 1.4.4.5.7 1.1.8 1.8.2.9.1 1.7-.2 2.5-.4.9-1 1.5-1.8 2-.4.2-.7.4-1.1.4-.4.1-.8.1-1.2.1-.5 0-.9-.1-1.3-.3-.8-.3-1.5-.9-2.1-1.5-.4-.4-.8-.7-1.1-1.1h-.3zm-1.1-1.1c-.1-.1-.1-.1 0 0-.3-.3-.6-.6-.8-.9-.5-.5-1-.9-1.6-1.2-.4-.3-.8-.4-1.3-.4-.4 0-.8 0-1.1.2-.5.2-.9.6-1.1 1-.2.3-.3.7-.3 1.1 0 .3 0 .6.1.9.1.5.4.9.8 1.2.5.4 1.1.5 1.7.5.5 0 1-.2 1.5-.5.6-.4 1.1-.8 1.6-1.3.1-.3.3-.5.5-.6zM13 12c.5.5 1 1 1.5 1.4.5.5 1.1.9 1.9 1 .4.1.8 0 1.2-.1.3-.1.6-.3.9-.5.4-.4.7-.9.8-1.4.1-.5 0-.9-.1-1.4-.3-.8-.8-1.2-1.7-1.4-.4-.1-.8-.1-1.2 0-.5.1-1 .4-1.4.7-.5.4-1 .8-1.4 1.2-.2.2-.4.3-.5.5z"/></svg></button> <button @click="${({target:e})=>{this._toggleSettings(),e instanceof HTMLElement&&e.focus()}}" @blur="${()=>setTimeout(()=>this._toggleSettings(!1),200)}" aria-label="Settings" aria-haspopup="true" aria-expanded="${!!this._isSettingsOpen}" aria-controls="${`${this._identifier}-settings`}"><svg width="24" height="24" aria-hidden="true" focusable="false"><circle cx="12" cy="5.4" r="2.5"/><circle cx="12" cy="12" r="2.5"/><circle cx="12" cy="18.6" r="2.5"/></svg></button><div id="${`${this._identifier}-settings`}" class="popover" style="display:${this._isSettingsOpen?"block":"none"}">${this._isDotLottie?o:r`<button @click="${this.convert}" aria-label="Convert JSON animation to dotLottie format" tabindex="0"><svg width="24" height="24" aria-hidden="true" focusable="false"><path d="M17.016 17.016v-4.031h1.969v6h-12v3l-3.984-3.984 3.984-3.984v3h10.031zM6.984 6.984v4.031H5.015v-6h12v-3l3.984 3.984-3.984 3.984v-3H6.984z"/></svg> Convert to dotLottie</button>`} <button @click="${this.snapshot}" aria-label="Download still image" tabindex="0"><svg width="24" height="24" aria-hidden="true" focusable="false"><path d="M16.8 10.8 12 15.6l-4.8-4.8h3V3.6h3.6v7.2h3zM12 15.6H3v4.8h18v-4.8h-9zm7.8 2.4h-2.4v-1.2h2.4V18z"/></svg> Download still image</button></div>`}</div>`}render(){var e,t,i;return r`<figure class="${"animation-container main"}" data-controls="${this.controls}" lang="${this.description?null===(t=document)||void 0===t?void 0:null===(e=t.documentElement)||void 0===e?void 0:e.lang:"en"}" role="img" aria-label="${null!==(i=this.description)&&void 0!==i?i:"Lottie animation"}" data-loaded="${this._playerState.loaded}"><div class="animation" style="background:${this.background}">${this.currentState===PlayerState.Error?r`<div class="error"><svg preserveAspectRatio="xMidYMid slice" xmlns="http://www.w3.org/2000/svg" xml:space="preserve" width="1920" height="1080" viewBox="0 0 1920 1080"><path fill="#fff" d="M0 0h1920v1080H0z"/><path fill="#3a6d8b" d="M1190.2 531 1007 212.4c-22-38.2-77.2-38-98.8.5L729.5 531.3c-21.3 37.9 6.1 84.6 49.5 84.6l361.9.3c43.7 0 71.1-47.3 49.3-85.2zM937.3 288.7c.2-7.5 3.3-23.9 23.2-23.9 16.3 0 23 16.1 23 23.5 0 55.3-10.7 197.2-12.2 214.5-.1 1-.9 1.7-1.9 1.7h-18.3c-1 0-1.8-.7-1.9-1.7-1.4-17.5-13.4-162.9-11.9-214.1zm24.2 283.8c-13.1 0-23.7-10.6-23.7-23.7s10.6-23.7 23.7-23.7 23.7 10.6 23.7 23.7-10.6 23.7-23.7 23.7zM722.1 644h112.6v34.4h-70.4V698h58.8v31.7h-58.8v22.6h72.4v36.2H722.1V644zm162 57.1h.6c8.3-12.9 18.2-17.8 31.3-17.8 3 0 5.1.4 6.3 1v32.6h-.8c-22.4-3.8-35.6 6.3-35.6 29.5v42.3h-38.2V685.5h36.4v15.6zm78.9 0h.6c8.3-12.9 18.2-17.8 31.3-17.8 3 0 5.1.4 6.3 1v32.6h-.8c-22.4-3.8-35.6 6.3-35.6 29.5v42.3h-38.2V685.5H963v15.6zm39.5 36.2c0-31.3 22.2-54.8 56.6-54.8 34.4 0 56.2 23.5 56.2 54.8s-21.8 54.6-56.2 54.6c-34.4-.1-56.6-23.3-56.6-54.6zm74 0c0-17.4-6.1-29.1-17.8-29.1-11.7 0-17.4 11.7-17.4 29.1 0 17.4 5.7 29.1 17.4 29.1s17.8-11.8 17.8-29.1zm83.1-36.2h.6c8.3-12.9 18.2-17.8 31.3-17.8 3 0 5.1.4 6.3 1v32.6h-.8c-22.4-3.8-35.6 6.3-35.6 29.5v42.3h-38.2V685.5h36.4v15.6z"/><path fill="none" d="M718.9 807.7h645v285.4h-645z"/><text fill="#3a6d8b" style="text-align:center;position:absolute;left:100%;font-size:47px;font-family:system-ui,-apple-system,BlinkMacSystemFont,'.SFNSText-Regular',sans-serif" x="50%" y="848.017" text-anchor="middle">${this._errorMessage}</text></svg></div>`:o}</div>${this.controls?this.renderControls():o}</figure>`}constructor(...e){super(...e),this.background="transparent",this.controls=!1,this.currentState=PlayerState.Loading,this.direction=1,this.hover=!1,this.intermission=0,this.loop=!1,this.mode=PlayMode.Normal,this.objectfit="contain",this.renderer="svg",this.simple=!1,this.speed=1,this.subframe=!0,this._isSettingsOpen=!1,this._seeker=0,this._currentAnimation=0,this._lottieInstance=null,this._identifier=this.id||useId("dotlottie"),this._errorMessage="Something went wrong",this._isDotLottie=!1,this._playerState={prev:PlayerState.Loading,count:0,loaded:!1}}};_ts_decorate([i({type:Boolean,reflect:!0})],DotLottiePlayer.prototype,"autoplay",void 0),_ts_decorate([i({type:String})],DotLottiePlayer.prototype,"background",void 0),_ts_decorate([i({type:Boolean,reflect:!0})],DotLottiePlayer.prototype,"controls",void 0),_ts_decorate([i({type:Number})],DotLottiePlayer.prototype,"count",void 0),_ts_decorate([i({type:String})],DotLottiePlayer.prototype,"currentState",void 0),_ts_decorate([i({type:String})],DotLottiePlayer.prototype,"description",void 0),_ts_decorate([i({type:Number})],DotLottiePlayer.prototype,"direction",void 0),_ts_decorate([i({type:Boolean})],DotLottiePlayer.prototype,"hover",void 0),_ts_decorate([i({type:Number})],DotLottiePlayer.prototype,"intermission",void 0),_ts_decorate([i({type:Boolean,reflect:!0})],DotLottiePlayer.prototype,"loop",void 0),_ts_decorate([i({type:String})],DotLottiePlayer.prototype,"mode",void 0),_ts_decorate([i({type:String})],DotLottiePlayer.prototype,"objectfit",void 0),_ts_decorate([i({type:String})],DotLottiePlayer.prototype,"preserveAspectRatio",void 0),_ts_decorate([i({type:String})],DotLottiePlayer.prototype,"renderer",void 0),_ts_decorate([i({type:Array})],DotLottiePlayer.prototype,"segment",void 0),_ts_decorate([i({type:Boolean})],DotLottiePlayer.prototype,"simple",void 0),_ts_decorate([i({type:Number})],DotLottiePlayer.prototype,"speed",void 0),_ts_decorate([i({type:String})],DotLottiePlayer.prototype,"src",void 0),_ts_decorate([i({type:Boolean})],DotLottiePlayer.prototype,"subframe",void 0),_ts_decorate([n(".animation")],DotLottiePlayer.prototype,"container",void 0),_ts_decorate([a()],DotLottiePlayer.prototype,"_isSettingsOpen",void 0),_ts_decorate([a()],DotLottiePlayer.prototype,"_seeker",void 0),_ts_decorate([a()],DotLottiePlayer.prototype,"_currentAnimation",void 0),_ts_decorate([a()],DotLottiePlayer.prototype,"_animations",void 0),DotLottiePlayer=_ts_decorate([s("dotlottie-player")],DotLottiePlayer),globalThis.dotLottiePlayer=()=>new DotLottiePlayer;export{DotLottiePlayer,PlayMode,PlayerEvents,PlayerState};
|
|
1
|
+
import { css, LitElement, html, nothing } from 'lit';
|
|
2
|
+
import { property, query, state, customElement } from 'lit/decorators.js';
|
|
3
|
+
import Lottie from 'lottie-web';
|
|
4
|
+
import { zip, strFromU8, unzip as unzip$1 } from 'fflate';
|
|
5
|
+
|
|
6
|
+
var PlayerState;
|
|
7
|
+
(function(PlayerState) {
|
|
8
|
+
PlayerState["Completed"] = "completed";
|
|
9
|
+
PlayerState["Destroyed"] = "destroyed";
|
|
10
|
+
PlayerState["Error"] = "error";
|
|
11
|
+
PlayerState["Frozen"] = "frozen";
|
|
12
|
+
PlayerState["Loading"] = "loading";
|
|
13
|
+
PlayerState["Paused"] = "paused";
|
|
14
|
+
PlayerState["Playing"] = "playing";
|
|
15
|
+
PlayerState["Stopped"] = "stopped";
|
|
16
|
+
})(PlayerState || (PlayerState = {}));
|
|
17
|
+
var PlayMode;
|
|
18
|
+
(function(PlayMode) {
|
|
19
|
+
PlayMode["Bounce"] = "bounce";
|
|
20
|
+
PlayMode["Normal"] = "normal";
|
|
21
|
+
})(PlayMode || (PlayMode = {}));
|
|
22
|
+
var PlayerEvents;
|
|
23
|
+
(function(PlayerEvents) {
|
|
24
|
+
PlayerEvents["Complete"] = "complete";
|
|
25
|
+
PlayerEvents["Destroyed"] = "destroyed";
|
|
26
|
+
PlayerEvents["Error"] = "error";
|
|
27
|
+
PlayerEvents["Frame"] = "frame";
|
|
28
|
+
PlayerEvents["Freeze"] = "freeze";
|
|
29
|
+
PlayerEvents["Load"] = "load";
|
|
30
|
+
PlayerEvents["Loop"] = "loop";
|
|
31
|
+
PlayerEvents["Pause"] = "pause";
|
|
32
|
+
PlayerEvents["Play"] = "play";
|
|
33
|
+
PlayerEvents["Ready"] = "ready";
|
|
34
|
+
PlayerEvents["Rendered"] = "rendered";
|
|
35
|
+
PlayerEvents["Stop"] = "stop";
|
|
36
|
+
})(PlayerEvents || (PlayerEvents = {}));
|
|
37
|
+
class CustomError extends Error {
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const addExt = (ext, str)=>{
|
|
41
|
+
if (!str) return;
|
|
42
|
+
if (getExt(str)) {
|
|
43
|
+
if (getExt(str) === ext) return str;
|
|
44
|
+
return `${getFilename(str)}.${ext}`;
|
|
45
|
+
}
|
|
46
|
+
return `${str}.${ext}`;
|
|
47
|
+
}, aspectRatio = (objectFit)=>{
|
|
48
|
+
switch(objectFit){
|
|
49
|
+
case 'contain':
|
|
50
|
+
case 'scale-down':
|
|
51
|
+
return 'xMidYMid meet';
|
|
52
|
+
case 'cover':
|
|
53
|
+
return 'xMidYMid slice';
|
|
54
|
+
case 'fill':
|
|
55
|
+
return 'none';
|
|
56
|
+
case 'none':
|
|
57
|
+
return 'xMinYMin slice';
|
|
58
|
+
default:
|
|
59
|
+
return 'xMidYMid meet';
|
|
60
|
+
}
|
|
61
|
+
}, /**
|
|
62
|
+
* Convert Base64 encoded string to Uint8Array
|
|
63
|
+
* @param { string } str Base64 encoded string
|
|
64
|
+
* @returns { Uint8Array} UTF-8/Latin-1 binary
|
|
65
|
+
*/ base64ToU8 = (str)=>{
|
|
66
|
+
const parsedStr = str.substring(str.indexOf(',') + 1);
|
|
67
|
+
return strToU8(isServer() ? Buffer.from(parsedStr, 'base64').toString('binary') : atob(parsedStr));
|
|
68
|
+
}, /**
|
|
69
|
+
* Convert a JSON Lottie to dotLottie or combine several animations and download new dotLottie file in your browser.
|
|
70
|
+
* @param { LottieJSON[] } animations The animations to combine.
|
|
71
|
+
* @param { LottieManifest } manifest Manifest of meta information.
|
|
72
|
+
* @param { string } filename Name of file to download. If not specified a random string will be generated.
|
|
73
|
+
* @param { boolean } triggerDownload Whether to trigger a download in the browser. Defaults to true.
|
|
74
|
+
*/ createDotLottie = async (animations, manifest, filename, triggerDownload = true)=>{
|
|
75
|
+
try {
|
|
76
|
+
if (!animations?.length || !manifest) {
|
|
77
|
+
throw new Error(`Missing or malformed required parameter(s):\n ${!animations?.length ? '- animations\n' : ''} ${!manifest ? '- manifest \n' : ''}`);
|
|
78
|
+
}
|
|
79
|
+
const name = addExt('lottie', filename) || `${useId()}.lottie`, dotlottie = {
|
|
80
|
+
'manifest.json': [
|
|
81
|
+
strToU8(JSON.stringify(manifest)),
|
|
82
|
+
{
|
|
83
|
+
level: 0
|
|
84
|
+
}
|
|
85
|
+
]
|
|
86
|
+
};
|
|
87
|
+
for (const [i, animation] of animations.entries()){
|
|
88
|
+
if (animation.assets?.length) {
|
|
89
|
+
for (const asset of animation.assets){
|
|
90
|
+
const { id, p } = asset;
|
|
91
|
+
if (id && p) {
|
|
92
|
+
const ext = getExtFromB64(p);
|
|
93
|
+
asset.p = `${id}.${ext}`;
|
|
94
|
+
asset.e = 0;
|
|
95
|
+
dotlottie[`images/${id}.${ext}`] = [
|
|
96
|
+
base64ToU8(p),
|
|
97
|
+
{
|
|
98
|
+
level: 9
|
|
99
|
+
}
|
|
100
|
+
];
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
dotlottie[`animations/${manifest.animations[i].id}.json`] = [
|
|
105
|
+
strToU8(JSON.stringify(animation)),
|
|
106
|
+
{
|
|
107
|
+
level: 9
|
|
108
|
+
}
|
|
109
|
+
];
|
|
110
|
+
}
|
|
111
|
+
const buffer = await getArrayBuffer(dotlottie);
|
|
112
|
+
return triggerDownload ? download(buffer, {
|
|
113
|
+
name,
|
|
114
|
+
mimeType: 'application/zip'
|
|
115
|
+
}) : buffer;
|
|
116
|
+
} catch (err) {
|
|
117
|
+
console.error(`❌ ${handleErrors(err).message}`);
|
|
118
|
+
}
|
|
119
|
+
}, /**
|
|
120
|
+
* Download file, either SVG or dotLottie.
|
|
121
|
+
* @param { string } data The data to be downloaded
|
|
122
|
+
* @param { string } name Don't include file extension in the filename
|
|
123
|
+
*/ download = (data, options)=>{
|
|
124
|
+
const blob = new Blob([
|
|
125
|
+
data
|
|
126
|
+
], {
|
|
127
|
+
type: options?.mimeType
|
|
128
|
+
}), fileName = options?.name || useId(), dataURL = URL.createObjectURL(blob), link = document.createElement('a');
|
|
129
|
+
link.href = dataURL;
|
|
130
|
+
link.download = fileName;
|
|
131
|
+
link.hidden = true;
|
|
132
|
+
document.body.appendChild(link);
|
|
133
|
+
link.click();
|
|
134
|
+
setTimeout(()=>{
|
|
135
|
+
link.remove();
|
|
136
|
+
URL.revokeObjectURL(dataURL);
|
|
137
|
+
}, 1000);
|
|
138
|
+
}, handleErrors = (err)=>{
|
|
139
|
+
const res = {
|
|
140
|
+
message: 'Unknown error',
|
|
141
|
+
status: isServer() ? 500 : 400
|
|
142
|
+
};
|
|
143
|
+
if (err && typeof err === 'object') {
|
|
144
|
+
if ('message' in err && typeof err.message === 'string') {
|
|
145
|
+
res.message = err.message;
|
|
146
|
+
}
|
|
147
|
+
if ('status' in err) {
|
|
148
|
+
res.status = Number(err.status);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return res;
|
|
152
|
+
}, frameOutput = (frame)=>{
|
|
153
|
+
return ((frame ?? 0) + 1).toString().padStart(3, '0');
|
|
154
|
+
}, getAnimationData = async (input)=>{
|
|
155
|
+
try {
|
|
156
|
+
if (!input || typeof input !== 'string' && typeof input !== 'object') {
|
|
157
|
+
throw new Error('Broken file or invalid file format');
|
|
158
|
+
}
|
|
159
|
+
if (typeof input !== 'string') {
|
|
160
|
+
const animations = Array.isArray(input) ? input : [
|
|
161
|
+
input
|
|
162
|
+
];
|
|
163
|
+
return {
|
|
164
|
+
animations,
|
|
165
|
+
manifest: null
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
const result = await fetch(input);
|
|
169
|
+
if (!result.ok) {
|
|
170
|
+
const error = new CustomError(result.statusText);
|
|
171
|
+
error.status = result.status;
|
|
172
|
+
throw error;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Check if file is JSON, first by parsing file name for extension,
|
|
176
|
+
* then – if filename has no extension – by cloning the response
|
|
177
|
+
* and parsing it for content.
|
|
178
|
+
*/ const ext = getExt(input);
|
|
179
|
+
if (ext === 'json' || !ext) {
|
|
180
|
+
if (ext) {
|
|
181
|
+
const lottie = await result.json();
|
|
182
|
+
return {
|
|
183
|
+
animations: [
|
|
184
|
+
lottie
|
|
185
|
+
],
|
|
186
|
+
manifest: null
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
const text = await result.clone().text();
|
|
190
|
+
try {
|
|
191
|
+
const lottie = JSON.parse(text);
|
|
192
|
+
return {
|
|
193
|
+
animations: [
|
|
194
|
+
lottie
|
|
195
|
+
],
|
|
196
|
+
manifest: null
|
|
197
|
+
};
|
|
198
|
+
} catch {}
|
|
199
|
+
}
|
|
200
|
+
const { data, manifest } = await getLottieJSON(result);
|
|
201
|
+
return {
|
|
202
|
+
animations: data,
|
|
203
|
+
manifest,
|
|
204
|
+
isDotLottie: true
|
|
205
|
+
};
|
|
206
|
+
} catch (err) {
|
|
207
|
+
console.error(`❌ ${handleErrors(err).message}`);
|
|
208
|
+
return {
|
|
209
|
+
animations: null,
|
|
210
|
+
manifest: null
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
}, getArrayBuffer = async (zippable)=>{
|
|
214
|
+
const arrayBuffer = await new Promise((resolve, reject)=>{
|
|
215
|
+
zip(zippable, {
|
|
216
|
+
level: 9
|
|
217
|
+
}, (err, data)=>{
|
|
218
|
+
if (err) {
|
|
219
|
+
reject(err);
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
resolve(data.buffer);
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
return arrayBuffer;
|
|
226
|
+
}, /**
|
|
227
|
+
* Get extension from filename, URL or path
|
|
228
|
+
* @param { string } str Filename, URL or path
|
|
229
|
+
*/ getExt = (str)=>{
|
|
230
|
+
if (!str || !hasExt(str)) return;
|
|
231
|
+
return str.split('.').pop()?.toLowerCase();
|
|
232
|
+
}, getExtFromB64 = (str)=>{
|
|
233
|
+
const mime = str.split(':')[1].split(';')[0];
|
|
234
|
+
return mime.split('/')[1].split('+')[0];
|
|
235
|
+
}, /**
|
|
236
|
+
* Parse URL to get filename
|
|
237
|
+
* @param { string } src The url string
|
|
238
|
+
* @param { boolean } keepExt Whether to include file extension
|
|
239
|
+
* @returns { string } Filename, in lowercase
|
|
240
|
+
*/ getFilename = (src, keepExt)=>{
|
|
241
|
+
// Because the regex strips all special characters, we need to extract the file extension, so we can add it later if we need it
|
|
242
|
+
const ext = getExt(src);
|
|
243
|
+
return `${src.replace(/\.[^.]*$/, '').replace(/\W+/g, '')}${keepExt && ext ? `.${ext}` : ''}`.toLowerCase();
|
|
244
|
+
}, getLottieJSON = async (resp)=>{
|
|
245
|
+
const unzipped = await unzip(resp), manifest = getManifest(unzipped), data = [];
|
|
246
|
+
for (const { id } of manifest.animations){
|
|
247
|
+
const str = strFromU8(unzipped[`animations/${id}.json`]), lottie = JSON.parse(str);
|
|
248
|
+
await resolveAssets(unzipped, lottie.assets);
|
|
249
|
+
data.push(lottie);
|
|
250
|
+
}
|
|
251
|
+
return {
|
|
252
|
+
data,
|
|
253
|
+
manifest
|
|
254
|
+
};
|
|
255
|
+
}, getManifest = (unzipped)=>{
|
|
256
|
+
const file = strFromU8(unzipped['manifest.json'], false), manifest = JSON.parse(file);
|
|
257
|
+
if (!('animations' in manifest)) throw new Error('Manifest not found');
|
|
258
|
+
if (!manifest.animations.length) throw new Error('No animations listed in manifest');
|
|
259
|
+
return manifest;
|
|
260
|
+
}, getMimeFromExt = (ext)=>{
|
|
261
|
+
switch(ext){
|
|
262
|
+
case 'svg':
|
|
263
|
+
case 'svg+xml':
|
|
264
|
+
return 'image/svg+xml';
|
|
265
|
+
case 'jpg':
|
|
266
|
+
case 'jpeg':
|
|
267
|
+
return 'image/jpeg';
|
|
268
|
+
case 'png':
|
|
269
|
+
case 'gif':
|
|
270
|
+
case 'webp':
|
|
271
|
+
return `image/${ext}`;
|
|
272
|
+
case 'mp3':
|
|
273
|
+
case 'mpeg':
|
|
274
|
+
case 'wav':
|
|
275
|
+
return `audio/${ext}`;
|
|
276
|
+
default:
|
|
277
|
+
return '';
|
|
278
|
+
}
|
|
279
|
+
}, hasExt = (path)=>{
|
|
280
|
+
const lastDotIndex = path?.split('/').pop()?.lastIndexOf('.');
|
|
281
|
+
return (lastDotIndex ?? 0) > 1 && path && path.length - 1 > (lastDotIndex ?? 0);
|
|
282
|
+
}, isAudio = (asset)=>{
|
|
283
|
+
return !('h' in asset) && !('w' in asset) && 'p' in asset && 'e' in asset && 'u' in asset && 'id' in asset;
|
|
284
|
+
}, isImage = (asset)=>{
|
|
285
|
+
return 'w' in asset && 'h' in asset && !('xt' in asset) && 'p' in asset;
|
|
286
|
+
}, isServer = ()=>{
|
|
287
|
+
return !(typeof window !== 'undefined' && window.document);
|
|
288
|
+
}, /**
|
|
289
|
+
* Convert string to Uint8Array
|
|
290
|
+
* @param { string } str Base64 encoded string
|
|
291
|
+
* @returns { Uint8Array} UTF-8/Latin-1 binary
|
|
292
|
+
*/ strToU8 = (str)=>{
|
|
293
|
+
const u8 = new Uint8Array(str.length);
|
|
294
|
+
for(let i = 0; i < str.length; i++){
|
|
295
|
+
u8[i] = str.charCodeAt(i);
|
|
296
|
+
}
|
|
297
|
+
return u8;
|
|
298
|
+
}, resolveAssets = async (unzipped, assets)=>{
|
|
299
|
+
if (!Array.isArray(assets)) return;
|
|
300
|
+
const toResolve = [];
|
|
301
|
+
for (const asset of assets){
|
|
302
|
+
if (!isAudio(asset) && !isImage(asset)) continue;
|
|
303
|
+
const type = isImage(asset) ? 'images' : 'audio', u8 = unzipped?.[`${type}/${asset.p}`];
|
|
304
|
+
if (!u8) continue;
|
|
305
|
+
toResolve.push(new Promise((resolveAsset)=>{
|
|
306
|
+
const assetB64 = isServer() ? Buffer.from(u8).toString('base64') : btoa(u8.reduce((dat, byte)=>dat + String.fromCharCode(byte), ''));
|
|
307
|
+
asset.p = `data:${getMimeFromExt(getExt(asset.p))};base64,${assetB64}`;
|
|
308
|
+
asset.e = 1;
|
|
309
|
+
asset.u = '';
|
|
310
|
+
resolveAsset();
|
|
311
|
+
}));
|
|
312
|
+
}
|
|
313
|
+
await Promise.all(toResolve);
|
|
314
|
+
}, unzip = async (resp)=>{
|
|
315
|
+
const u8 = new Uint8Array(await resp.arrayBuffer()), unzipped = await new Promise((resolve, reject)=>{
|
|
316
|
+
unzip$1(u8, /*{ filter },*/ (err, file)=>{
|
|
317
|
+
if (err) {
|
|
318
|
+
reject(err);
|
|
319
|
+
}
|
|
320
|
+
resolve(file);
|
|
321
|
+
});
|
|
322
|
+
});
|
|
323
|
+
return unzipped;
|
|
324
|
+
}, useId = (prefix)=>{
|
|
325
|
+
const s4 = ()=>{
|
|
326
|
+
return ((1 + Math.random()) * 0x10000 | 0).toString(16).substring(1);
|
|
327
|
+
};
|
|
328
|
+
return `${prefix ?? `:${s4()}`}-${s4()}`;
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
var name = "@aarsteinmedia/dotlottie-player";
|
|
332
|
+
var version = "2.1.7";
|
|
333
|
+
var description = "Web Component for playing Lottie animations in your web app. Previously @johanaarstein/dotlottie-player";
|
|
334
|
+
var exports = {
|
|
335
|
+
".": {
|
|
336
|
+
"import": "./dist/esm/index.js",
|
|
337
|
+
node: "./dist/esm/index.js",
|
|
338
|
+
require: "./dist/cjs/index.js",
|
|
339
|
+
types: "./dist/index.d.ts"
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
var main = "./dist/esm/index.js";
|
|
343
|
+
var unpkg = "./dist/index.js";
|
|
344
|
+
var module = "./dist/esm/index.js";
|
|
345
|
+
var types = "./dist/index.d.ts";
|
|
346
|
+
var type = "module";
|
|
347
|
+
var homepage = "https://www.aarstein.media/en/dotlottie-player";
|
|
348
|
+
var repository = {
|
|
349
|
+
url: "https://github.com/aarsteinmedia/dotlottie-player.git",
|
|
350
|
+
type: "git"
|
|
351
|
+
};
|
|
352
|
+
var bugs = "https://github.com/aarsteinmedia/dotlottie-player/issues";
|
|
353
|
+
var author = {
|
|
354
|
+
name: "Johan Martin Aarstein",
|
|
355
|
+
email: "johan@aarstein.media",
|
|
356
|
+
url: "https://www.aarstein.media",
|
|
357
|
+
organization: "Aarstein Media"
|
|
358
|
+
};
|
|
359
|
+
var license = "GPL-2.0-or-later";
|
|
360
|
+
var scripts = {
|
|
361
|
+
build: "pnpm cleanup && rollup -c",
|
|
362
|
+
"build:types": "pnpm cleanup:types && tsc",
|
|
363
|
+
"build:cem": "npx cem analyze --config 'cem.config.mjs'",
|
|
364
|
+
prod: "pnpm build:types && pnpm build && pnpm build:cem",
|
|
365
|
+
dev: "pnpm cleanup && rollup -c --watch",
|
|
366
|
+
lint: "tsc && eslint . --ext .ts,.js",
|
|
367
|
+
"lint:fix": "eslint . --ext .ts,.js --fix",
|
|
368
|
+
"cleanup:types": "shx rm -rf types && shx mkdir types",
|
|
369
|
+
cleanup: "shx rm -rf dist && shx mkdir dist"
|
|
370
|
+
};
|
|
371
|
+
var dependencies = {
|
|
372
|
+
fflate: "^0.8.1",
|
|
373
|
+
lit: "^2.8.0",
|
|
374
|
+
"lottie-web": "^5.12.2"
|
|
375
|
+
};
|
|
376
|
+
var peerDependencies = {
|
|
377
|
+
"@types/react": ">= 16.0.0"
|
|
378
|
+
};
|
|
379
|
+
var devDependencies = {
|
|
380
|
+
"@custom-elements-manifest/analyzer": "^0.6.9",
|
|
381
|
+
"@rollup/plugin-commonjs": "^25.0.7",
|
|
382
|
+
"@rollup/plugin-json": "^6.0.1",
|
|
383
|
+
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
384
|
+
"@rollup/plugin-replace": "^5.0.5",
|
|
385
|
+
"@swc/core": "^1.3.95",
|
|
386
|
+
"@types/node": "^20.8.10",
|
|
387
|
+
"@typescript-eslint/eslint-plugin": "^5.62.0",
|
|
388
|
+
"@typescript-eslint/parser": "^5.62.0",
|
|
389
|
+
eslint: "^8.52.0",
|
|
390
|
+
"eslint-plugin-lit": "^1.10.1",
|
|
391
|
+
rollup: "^3.29.4",
|
|
392
|
+
"rollup-plugin-dts": "^6.1.0",
|
|
393
|
+
"rollup-plugin-html-literals": "^1.1.8",
|
|
394
|
+
"rollup-plugin-summary": "^2.0.0",
|
|
395
|
+
"rollup-plugin-swc3": "^0.9.1",
|
|
396
|
+
shx: "^0.3.4",
|
|
397
|
+
"ts-lit-plugin": "^1.2.1",
|
|
398
|
+
typescript: "^5.2.2"
|
|
399
|
+
};
|
|
400
|
+
var customElements = "dist/custom-elements.json";
|
|
401
|
+
var files = [
|
|
402
|
+
"dist",
|
|
403
|
+
"README.md"
|
|
404
|
+
];
|
|
405
|
+
var keywords = [
|
|
406
|
+
"lottie",
|
|
407
|
+
"dotlottie",
|
|
408
|
+
"animation",
|
|
409
|
+
"web component",
|
|
410
|
+
"component",
|
|
411
|
+
"lit-element",
|
|
412
|
+
"svg",
|
|
413
|
+
"vector",
|
|
414
|
+
"player"
|
|
415
|
+
];
|
|
416
|
+
var publishConfig = {
|
|
417
|
+
access: "public"
|
|
418
|
+
};
|
|
419
|
+
var engines = {
|
|
420
|
+
node: ">= 7.6.0"
|
|
421
|
+
};
|
|
422
|
+
var funding = {
|
|
423
|
+
type: "paypal",
|
|
424
|
+
url: "https://www.paypal.com/donate/?hosted_button_id=E7C7DMN8KSQ6A"
|
|
425
|
+
};
|
|
426
|
+
var pkg = {
|
|
427
|
+
name: name,
|
|
428
|
+
version: version,
|
|
429
|
+
description: description,
|
|
430
|
+
exports: exports,
|
|
431
|
+
main: main,
|
|
432
|
+
unpkg: unpkg,
|
|
433
|
+
module: module,
|
|
434
|
+
types: types,
|
|
435
|
+
type: type,
|
|
436
|
+
homepage: homepage,
|
|
437
|
+
repository: repository,
|
|
438
|
+
bugs: bugs,
|
|
439
|
+
author: author,
|
|
440
|
+
license: license,
|
|
441
|
+
scripts: scripts,
|
|
442
|
+
dependencies: dependencies,
|
|
443
|
+
peerDependencies: peerDependencies,
|
|
444
|
+
devDependencies: devDependencies,
|
|
445
|
+
customElements: customElements,
|
|
446
|
+
files: files,
|
|
447
|
+
keywords: keywords,
|
|
448
|
+
publishConfig: publishConfig,
|
|
449
|
+
engines: engines,
|
|
450
|
+
funding: funding
|
|
451
|
+
};
|
|
452
|
+
|
|
453
|
+
var styles = css`*{box-sizing:border-box}:host{--lottie-player-toolbar-height:35px;--lottie-player-toolbar-background-color:#FFF;--lottie-player-toolbar-icon-color:#000;--lottie-player-toolbar-icon-hover-color:#000;--lottie-player-toolbar-icon-active-color:#4285f4;--lottie-player-seeker-track-color:rgba(0, 0, 0, 0.2);--lottie-player-seeker-thumb-color:#4285f4;--lottie-player-seeker-display:block;display:block;width:100%;height:100%}@media (prefers-color-scheme:dark){:host{--lottie-player-toolbar-background-color:#000;--lottie-player-toolbar-icon-color:#FFF;--lottie-player-toolbar-icon-hover-color:#FFF;--lottie-player-seeker-track-color:rgba(255, 255, 255, 0.6)}}.main{display:flex;flex-direction:column;height:100%;width:100%;margin:0}.animation{width:100%;height:100%;display:flex}[data-controls=true] .animation{height:calc(100% - 35px)}.animation-container{position:relative}.popover{position:absolute;right:5px;bottom:40px;background-color:var(--lottie-player-toolbar-background-color);border-radius:5px;padding:10px 15px;border:solid 2px var(--lottie-player-toolbar-icon-color);animation:fadeIn .2s ease-in-out}.popover::before{content:"";right:10px;border:7px solid transparent;border-top-color:transparent;margin-right:-7px;height:0;width:0;position:absolute;pointer-events:none;top:100%;border-top-color:var(--lottie-player-toolbar-icon-color)}.toolbar{display:flex;align-items:center;justify-items:center;background:var(--lottie-player-toolbar-background-color);margin:0;height:35px;padding:5px;border-radius:5px;gap:5px}.toolbar.has-error{pointer-events:none;opacity:.5}.toolbar button{cursor:pointer;fill:var(--lottie-player-toolbar-icon-color);color:var(--lottie-player-toolbar-icon-color);display:flex;background:0 0;border:0;padding:0;outline:0;height:100%;margin:0;align-items:center;gap:5px;opacity:.9}.toolbar button:hover{opacity:1}.toolbar button[data-active=true]{opacity:1;fill:var(--lottie-player-toolbar-icon-active-color)}.toolbar button:disabled{opacity:.5}.toolbar button:focus{outline:0}.toolbar button svg{pointer-events:none}.toolbar button svg>*{fill:inherit}.toolbar button.disabled svg{display:none}.progress-container{position:relative;width:100%}.progress-container.simple{margin-right:12px}.seeker,.seeker::-webkit-slider-runnable-track,.seeker::-webkit-slider-thumb,progress{-webkit-appearance:none;appearance:none;outline:0}.seeker{width:100%;height:20px;border-radius:3px;border:0;cursor:pointer;background-color:transparent;display:var(--lottie-player-seeker-display);color:var(--lottie-player-seeker-thumb-color);margin:0;padding:7.5px 0;position:relative;z-index:1}progress{position:absolute;width:100%;height:5px;border-radius:3px;border:0;top:0;left:0;margin:7.5px 0;background-color:var(--lottie-player-seeker-track-color);pointer-events:none}::-moz-progress-bar{background-color:var(--lottie-player-seeker-thumb-color)}::-webkit-progress-inner-element{border-radius:3px;overflow:hidden}::-webkit-slider-runnable-track{background-color:transparent}::-webkit-progress-value{background-color:var(--lottie-player-seeker-thumb-color)}.seeker::-webkit-slider-thumb{height:15px;width:15px;border-radius:50%;border:0;background-color:var(--lottie-player-seeker-thumb-color);cursor:pointer;transition:transform .2s ease-in-out;transform:scale(0)}.seeker:focus::-webkit-slider-thumb,.seeker:hover::-webkit-slider-thumb{transform:scale(1)}.seeker::-moz-range-progress{background-color:var(--lottie-player-seeker-thumb-color);height:5px;border-radius:3px}.seeker::-moz-range-thumb{height:15px;width:15px;border-radius:50%;background-color:var(--lottie-player-seeker-thumb-color);border:0;cursor:pointer;transition:transform .2s ease-in-out;transform:scale(0)}.seeker:focus::-moz-range-thumb,.seeker:hover::-moz-range-thumb{transform:scale(1)}.seeker::-ms-track{width:100%;height:5px;cursor:pointer;background:0 0;border-color:transparent;color:transparent}.seeker::-ms-fill-upper{background:var(--lottie-player-seeker-track-color);border-radius:3px}.seeker::-ms-fill-lower{background-color:var(--lottie-player-seeker-thumb-color);border-radius:3px}.seeker::-ms-thumb{border:0;height:15px;width:15px;border-radius:50%;background:var(--lottie-player-seeker-thumb-color);cursor:pointer;transition:transform .2s ease-in-out;transform:scale(0)}.seeker:focus::-ms-thumb,.seeker:hover::-ms-thumb{transform:scale(1)}.seeker:focus::-ms-fill-lower{background:var(--lottie-player-seeker-track-color)}.seeker:focus::-ms-fill-upper{background:var(--lottie-player-seeker-track-color)}.error{display:flex;margin:auto;justify-content:center;height:100%;align-items:center}.error svg{width:100%;height:auto}@keyframes fadeIn{0%{opacity:0}100%{opacity:1}}`;
|
|
454
|
+
|
|
455
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
456
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
457
|
+
if (typeof Reflect === "object" && typeof undefined === "function") r = undefined(decorators, target, key, desc);
|
|
458
|
+
else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
459
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
460
|
+
}
|
|
461
|
+
let DotLottiePlayer = class DotLottiePlayer extends LitElement {
|
|
462
|
+
/**
|
|
463
|
+
* Get options from props
|
|
464
|
+
* @returns { AnimationConfig }
|
|
465
|
+
*/ _getOptions() {
|
|
466
|
+
const preserveAspectRatio = this.preserveAspectRatio ?? (this.objectfit && aspectRatio(this.objectfit)), initialSegment = !this.segment || this.segment.some((val)=>val < 0) ? undefined : this.segment.every((val)=>val > 0) ? [
|
|
467
|
+
this.segment[0] - 1,
|
|
468
|
+
this.segment[1] - 1
|
|
469
|
+
] : this.segment, options = {
|
|
470
|
+
container: this.container,
|
|
471
|
+
loop: !!this.loop,
|
|
472
|
+
autoplay: !!this.autoplay,
|
|
473
|
+
renderer: this.renderer,
|
|
474
|
+
initialSegment,
|
|
475
|
+
rendererSettings: {
|
|
476
|
+
imagePreserveAspectRatio: preserveAspectRatio
|
|
477
|
+
}
|
|
478
|
+
};
|
|
479
|
+
switch(this.renderer){
|
|
480
|
+
case 'svg':
|
|
481
|
+
options.rendererSettings = {
|
|
482
|
+
...options.rendererSettings,
|
|
483
|
+
hideOnTransparent: true,
|
|
484
|
+
preserveAspectRatio,
|
|
485
|
+
progressiveLoad: true
|
|
486
|
+
};
|
|
487
|
+
break;
|
|
488
|
+
case 'canvas':
|
|
489
|
+
options.rendererSettings = {
|
|
490
|
+
...options.rendererSettings,
|
|
491
|
+
clearCanvas: true,
|
|
492
|
+
preserveAspectRatio,
|
|
493
|
+
progressiveLoad: true
|
|
494
|
+
};
|
|
495
|
+
break;
|
|
496
|
+
case 'html':
|
|
497
|
+
options.rendererSettings = {
|
|
498
|
+
...options.rendererSettings,
|
|
499
|
+
hideOnTransparent: true
|
|
500
|
+
};
|
|
501
|
+
}
|
|
502
|
+
return options;
|
|
503
|
+
}
|
|
504
|
+
/**
|
|
505
|
+
* Initialize Lottie Web player
|
|
506
|
+
* @param { string | LottieJSON } src URL to lottie animation, or raw JSON data
|
|
507
|
+
*/ async load(src) {
|
|
508
|
+
if (!this.shadowRoot) return;
|
|
509
|
+
// Load the resource
|
|
510
|
+
try {
|
|
511
|
+
const { animations, manifest, isDotLottie } = await getAnimationData(src);
|
|
512
|
+
if (!animations || animations.some((animation)=>!this._isLottie(animation))) {
|
|
513
|
+
throw new Error('Broken or corrupted file');
|
|
514
|
+
}
|
|
515
|
+
this._isDotLottie = !!isDotLottie;
|
|
516
|
+
this._animations = animations;
|
|
517
|
+
this._manifest = manifest ?? {
|
|
518
|
+
animations: [
|
|
519
|
+
{
|
|
520
|
+
id: useId(),
|
|
521
|
+
autoplay: this.autoplay,
|
|
522
|
+
loop: this.loop,
|
|
523
|
+
direction: this.direction,
|
|
524
|
+
mode: this.mode,
|
|
525
|
+
speed: this.speed
|
|
526
|
+
}
|
|
527
|
+
]
|
|
528
|
+
};
|
|
529
|
+
// Clear previous animation, if any
|
|
530
|
+
if (this._lottieInstance) this._lottieInstance.destroy();
|
|
531
|
+
// Initialize lottie player and load animation
|
|
532
|
+
this._lottieInstance = Lottie.loadAnimation({
|
|
533
|
+
...this._getOptions(),
|
|
534
|
+
animationData: animations[this._currentAnimation]
|
|
535
|
+
});
|
|
536
|
+
} catch (err) {
|
|
537
|
+
this._errorMessage = handleErrors(err).message;
|
|
538
|
+
this.currentState = PlayerState.Error;
|
|
539
|
+
this.dispatchEvent(new CustomEvent(PlayerEvents.Error));
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
this._addEventListeners();
|
|
543
|
+
// Set initial playback speed and direction
|
|
544
|
+
this.setSpeed(this.speed);
|
|
545
|
+
this.setDirection(this.direction ?? 1);
|
|
546
|
+
this.setSubframe(!!this.subframe);
|
|
547
|
+
// Start playing if autoplay is enabled
|
|
548
|
+
if (this.autoplay) {
|
|
549
|
+
if (this.direction === -1) this.seek('99%');
|
|
550
|
+
this.play();
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* Add event listeners
|
|
555
|
+
*/ _addEventListeners() {
|
|
556
|
+
if (!this._lottieInstance) return;
|
|
557
|
+
// Calculate and save the current progress of the animation
|
|
558
|
+
this._lottieInstance.addEventListener('enterFrame', ()=>{
|
|
559
|
+
if (this._lottieInstance) {
|
|
560
|
+
const { currentFrame, totalFrames } = this._lottieInstance;
|
|
561
|
+
this._seeker = Math.floor(currentFrame / totalFrames * 100);
|
|
562
|
+
this.dispatchEvent(new CustomEvent(PlayerEvents.Frame, {
|
|
563
|
+
detail: {
|
|
564
|
+
frame: currentFrame,
|
|
565
|
+
seeker: this._seeker
|
|
566
|
+
}
|
|
567
|
+
}));
|
|
568
|
+
}
|
|
569
|
+
});
|
|
570
|
+
// Handle animation play complete
|
|
571
|
+
this._lottieInstance.addEventListener('complete', ()=>{
|
|
572
|
+
this.currentState = PlayerState.Completed;
|
|
573
|
+
this.dispatchEvent(new CustomEvent(PlayerEvents.Complete));
|
|
574
|
+
if (this._animations?.length > 1 && this.autoplay && this._currentAnimation < this._animations?.length - 1) {
|
|
575
|
+
this.next();
|
|
576
|
+
}
|
|
577
|
+
});
|
|
578
|
+
//Handle complete loop
|
|
579
|
+
const loopComplete = ()=>{
|
|
580
|
+
if (!this._lottieInstance) {
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
583
|
+
const { firstFrame, totalFrames, playDirection } = this._lottieInstance;
|
|
584
|
+
if (this.count) {
|
|
585
|
+
this.mode === PlayMode.Bounce ? this._playerState.count += 1 : this._playerState.count += 0.5;
|
|
586
|
+
if (this._playerState.count >= this.count) {
|
|
587
|
+
this.setLooping(false);
|
|
588
|
+
this.currentState = PlayerState.Completed;
|
|
589
|
+
this.dispatchEvent(new CustomEvent(PlayerEvents.Complete));
|
|
590
|
+
return;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
this.dispatchEvent(new CustomEvent(PlayerEvents.Loop));
|
|
594
|
+
if (this.mode === PlayMode.Bounce) {
|
|
595
|
+
this._lottieInstance?.goToAndStop(playDirection === -1 ? firstFrame : totalFrames * 0.99, true);
|
|
596
|
+
this._lottieInstance?.setDirection(playDirection * -1);
|
|
597
|
+
return setTimeout(()=>{
|
|
598
|
+
this._lottieInstance?.play();
|
|
599
|
+
}, this.intermission);
|
|
600
|
+
}
|
|
601
|
+
this._lottieInstance?.goToAndStop(playDirection === -1 ? totalFrames * 0.99 : firstFrame, true);
|
|
602
|
+
return setTimeout(()=>{
|
|
603
|
+
this._lottieInstance?.play();
|
|
604
|
+
}, this.intermission);
|
|
605
|
+
};
|
|
606
|
+
this._lottieInstance.addEventListener('loopComplete', loopComplete);
|
|
607
|
+
// Handle lottie-web ready event
|
|
608
|
+
this._lottieInstance.addEventListener('DOMLoaded', ()=>{
|
|
609
|
+
this._playerState.loaded = true;
|
|
610
|
+
this.dispatchEvent(new CustomEvent(PlayerEvents.Ready));
|
|
611
|
+
});
|
|
612
|
+
// Handle animation data load complete
|
|
613
|
+
this._lottieInstance.addEventListener('data_ready', ()=>{
|
|
614
|
+
this.dispatchEvent(new CustomEvent(PlayerEvents.Load));
|
|
615
|
+
});
|
|
616
|
+
// Set error state when animation load fail event triggers
|
|
617
|
+
this._lottieInstance.addEventListener('data_failed', ()=>{
|
|
618
|
+
this.currentState = PlayerState.Error;
|
|
619
|
+
this.dispatchEvent(new CustomEvent(PlayerEvents.Error));
|
|
620
|
+
});
|
|
621
|
+
if (this.container) {
|
|
622
|
+
// Set handlers to auto play animation on hover if enabled
|
|
623
|
+
this.container.addEventListener('mouseenter', ()=>{
|
|
624
|
+
if (this.hover && this.currentState !== PlayerState.Playing) {
|
|
625
|
+
this.play();
|
|
626
|
+
}
|
|
627
|
+
});
|
|
628
|
+
this.container.addEventListener('mouseleave', ()=>{
|
|
629
|
+
if (this.hover && this.currentState === PlayerState.Playing) {
|
|
630
|
+
this.stop();
|
|
631
|
+
}
|
|
632
|
+
});
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
/**
|
|
636
|
+
* Handle visibility change events
|
|
637
|
+
*/ _onVisibilityChange() {
|
|
638
|
+
if (document.hidden && this.currentState === PlayerState.Playing) {
|
|
639
|
+
this._freeze();
|
|
640
|
+
} else if (this.currentState === PlayerState.Frozen) {
|
|
641
|
+
this.play();
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
/**
|
|
645
|
+
* Handles click and drag actions on the progress track
|
|
646
|
+
* @param { Event & { HTMLInputElement } } event
|
|
647
|
+
*/ _handleSeekChange({ target }) {
|
|
648
|
+
if (!(target instanceof HTMLInputElement) || !this._lottieInstance || isNaN(Number(target.value))) return;
|
|
649
|
+
this.seek(Math.floor(Number(target.value) / 100 * this._lottieInstance.totalFrames));
|
|
650
|
+
setTimeout(()=>{
|
|
651
|
+
if (target.parentElement instanceof HTMLFormElement) {
|
|
652
|
+
target.parentElement.reset();
|
|
653
|
+
}
|
|
654
|
+
}, 100);
|
|
655
|
+
}
|
|
656
|
+
_isLottie(json) {
|
|
657
|
+
const mandatory = [
|
|
658
|
+
'v',
|
|
659
|
+
'ip',
|
|
660
|
+
'op',
|
|
661
|
+
'layers',
|
|
662
|
+
'fr',
|
|
663
|
+
'w',
|
|
664
|
+
'h'
|
|
665
|
+
];
|
|
666
|
+
return mandatory.every((field)=>Object.prototype.hasOwnProperty.call(json, field));
|
|
667
|
+
}
|
|
668
|
+
/**
|
|
669
|
+
* Creates a new dotLottie file, by combinig several animations
|
|
670
|
+
* @param { Config } configs
|
|
671
|
+
* @param { string } fileName
|
|
672
|
+
* @param { boolean } triggerDownload Whether to trigger a download in the browser.
|
|
673
|
+
* If set to false the function returns an ArrayBuffer. Defaults to true.
|
|
674
|
+
*/ async addAnimation(configs, fileName, triggerDownload = true) {
|
|
675
|
+
try {
|
|
676
|
+
const oldManifest = this._manifest || {
|
|
677
|
+
animations: []
|
|
678
|
+
};
|
|
679
|
+
let manifest = {
|
|
680
|
+
...oldManifest,
|
|
681
|
+
generator: pkg.name
|
|
682
|
+
}, animations = this._animations || [];
|
|
683
|
+
for (const config of configs){
|
|
684
|
+
const { url } = config, { animations: animationsToAdd } = await getAnimationData(url);
|
|
685
|
+
if (!animationsToAdd) {
|
|
686
|
+
throw new Error('No animation loaded');
|
|
687
|
+
}
|
|
688
|
+
if (manifest.animations.some(({ id })=>id === config.id)) {
|
|
689
|
+
throw new Error('Duplicate id for animation');
|
|
690
|
+
}
|
|
691
|
+
manifest = {
|
|
692
|
+
...manifest,
|
|
693
|
+
animations: [
|
|
694
|
+
...manifest.animations,
|
|
695
|
+
config
|
|
696
|
+
]
|
|
697
|
+
};
|
|
698
|
+
animations = [
|
|
699
|
+
...animations,
|
|
700
|
+
...animationsToAdd
|
|
701
|
+
];
|
|
702
|
+
}
|
|
703
|
+
return createDotLottie(animations, manifest, fileName, triggerDownload);
|
|
704
|
+
} catch (err) {
|
|
705
|
+
console.error(handleErrors(err).message);
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
/**
|
|
709
|
+
* Returns the lottie-web instance used in the component
|
|
710
|
+
*/ getLottie() {
|
|
711
|
+
return this._lottieInstance;
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* Play
|
|
715
|
+
*/ play() {
|
|
716
|
+
if (!this._lottieInstance) return;
|
|
717
|
+
if (this.currentState) {
|
|
718
|
+
this._playerState.prev = this.currentState;
|
|
719
|
+
}
|
|
720
|
+
this._lottieInstance.play();
|
|
721
|
+
setTimeout(()=>{
|
|
722
|
+
this.currentState = PlayerState.Playing;
|
|
723
|
+
}, 0);
|
|
724
|
+
this.dispatchEvent(new CustomEvent(PlayerEvents.Play));
|
|
725
|
+
}
|
|
726
|
+
/**
|
|
727
|
+
* Pause
|
|
728
|
+
*/ pause() {
|
|
729
|
+
if (!this._lottieInstance) return;
|
|
730
|
+
if (this.currentState) {
|
|
731
|
+
this._playerState.prev = this.currentState;
|
|
732
|
+
}
|
|
733
|
+
this._lottieInstance.pause();
|
|
734
|
+
setTimeout(()=>{
|
|
735
|
+
this.currentState = PlayerState.Paused;
|
|
736
|
+
}, 0);
|
|
737
|
+
this.dispatchEvent(new CustomEvent(PlayerEvents.Pause));
|
|
738
|
+
}
|
|
739
|
+
/**
|
|
740
|
+
* Stop
|
|
741
|
+
*/ stop() {
|
|
742
|
+
if (!this._lottieInstance) return;
|
|
743
|
+
if (this.currentState) {
|
|
744
|
+
this._playerState.prev = this.currentState;
|
|
745
|
+
}
|
|
746
|
+
this._playerState.count = 0;
|
|
747
|
+
this._lottieInstance.stop();
|
|
748
|
+
setTimeout(()=>{
|
|
749
|
+
this.currentState = PlayerState.Stopped;
|
|
750
|
+
}, 0);
|
|
751
|
+
this.dispatchEvent(new CustomEvent(PlayerEvents.Stop));
|
|
752
|
+
}
|
|
753
|
+
/**
|
|
754
|
+
* Destroy animation and element
|
|
755
|
+
*/ destroy() {
|
|
756
|
+
if (!this._lottieInstance) return;
|
|
757
|
+
this.currentState = PlayerState.Destroyed;
|
|
758
|
+
this._lottieInstance.destroy();
|
|
759
|
+
this._lottieInstance = null;
|
|
760
|
+
this.dispatchEvent(new CustomEvent(PlayerEvents.Destroyed));
|
|
761
|
+
this.remove();
|
|
762
|
+
}
|
|
763
|
+
/**
|
|
764
|
+
* Seek to a given frame
|
|
765
|
+
* @param { number | string } value Frame to seek to
|
|
766
|
+
*/ seek(value) {
|
|
767
|
+
if (!this._lottieInstance) return;
|
|
768
|
+
// Extract frame number from either number or percentage value
|
|
769
|
+
const matches = value.toString().match(/^([0-9]+)(%?)$/);
|
|
770
|
+
if (!matches) {
|
|
771
|
+
return;
|
|
772
|
+
}
|
|
773
|
+
// Calculate and set the frame number
|
|
774
|
+
const frame = Math.floor(matches[2] === '%' ? this._lottieInstance.totalFrames * Number(matches[1]) / 100 : Number(matches[1]));
|
|
775
|
+
// Set seeker to new frame number
|
|
776
|
+
this._seeker = frame;
|
|
777
|
+
// Send lottie player to the new frame
|
|
778
|
+
if (this.currentState === PlayerState.Playing || this.currentState === PlayerState.Frozen && this._playerState.prev === PlayerState.Playing) {
|
|
779
|
+
this._lottieInstance.goToAndPlay(frame, true);
|
|
780
|
+
this.currentState = PlayerState.Playing;
|
|
781
|
+
} else {
|
|
782
|
+
this._lottieInstance.goToAndStop(frame, true);
|
|
783
|
+
this._lottieInstance.pause();
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
/**
|
|
787
|
+
* Snapshot and download the current frame as SVG
|
|
788
|
+
*/ snapshot() {
|
|
789
|
+
if (!this.shadowRoot) return;
|
|
790
|
+
// Get SVG element and serialize markup
|
|
791
|
+
const svgElement = this.shadowRoot.querySelector('.animation svg'), data = svgElement instanceof Node ? new XMLSerializer().serializeToString(svgElement) : null;
|
|
792
|
+
if (!data) {
|
|
793
|
+
console.error('Could not serialize data');
|
|
794
|
+
return;
|
|
795
|
+
}
|
|
796
|
+
download(data, {
|
|
797
|
+
name: `${getFilename(this.src)}-${frameOutput(this._seeker)}.svg`,
|
|
798
|
+
mimeType: 'image/svg+xml'
|
|
799
|
+
});
|
|
800
|
+
return data;
|
|
801
|
+
}
|
|
802
|
+
/**
|
|
803
|
+
* Toggles subframe, for more smooth animations
|
|
804
|
+
* @param { boolean } value Whether animation uses subframe
|
|
805
|
+
*/ setSubframe(value) {
|
|
806
|
+
if (!this._lottieInstance) return;
|
|
807
|
+
this.subframe = value;
|
|
808
|
+
this._lottieInstance.setSubframe(value);
|
|
809
|
+
}
|
|
810
|
+
/**
|
|
811
|
+
* Freeze animation.
|
|
812
|
+
* This internal state pauses animation and is used to differentiate between
|
|
813
|
+
* user requested pauses and component instigated pauses.
|
|
814
|
+
*/ _freeze() {
|
|
815
|
+
if (!this._lottieInstance) return;
|
|
816
|
+
if (this.currentState) {
|
|
817
|
+
this._playerState.prev = this.currentState;
|
|
818
|
+
}
|
|
819
|
+
this._lottieInstance.pause();
|
|
820
|
+
setTimeout(()=>{
|
|
821
|
+
this.currentState = PlayerState.Frozen;
|
|
822
|
+
}, 0);
|
|
823
|
+
this.dispatchEvent(new CustomEvent(PlayerEvents.Freeze));
|
|
824
|
+
}
|
|
825
|
+
/**
|
|
826
|
+
* Reload animation
|
|
827
|
+
*/ async reload() {
|
|
828
|
+
if (!this._lottieInstance) return;
|
|
829
|
+
this._lottieInstance.destroy();
|
|
830
|
+
if (this.src) {
|
|
831
|
+
await this.load(this.src);
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
/**
|
|
835
|
+
* Set animation playback speed
|
|
836
|
+
* @param { number } value Playback speed
|
|
837
|
+
*/ setSpeed(value = 1) {
|
|
838
|
+
if (!this._lottieInstance) return;
|
|
839
|
+
this.speed = value;
|
|
840
|
+
this._lottieInstance.setSpeed(value);
|
|
841
|
+
}
|
|
842
|
+
/**
|
|
843
|
+
* Animation play direction
|
|
844
|
+
* @param { AnimationDirection } value Animation direction
|
|
845
|
+
*/ setDirection(value) {
|
|
846
|
+
if (!this._lottieInstance) return;
|
|
847
|
+
this.direction = value;
|
|
848
|
+
this._lottieInstance.setDirection(value);
|
|
849
|
+
}
|
|
850
|
+
/**
|
|
851
|
+
* Set loop
|
|
852
|
+
* @param { boolean } value
|
|
853
|
+
*/ setLooping(value) {
|
|
854
|
+
if (this._lottieInstance) {
|
|
855
|
+
this.loop = value;
|
|
856
|
+
this._lottieInstance.setLoop(value);
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
/**
|
|
860
|
+
* Toggle playing state
|
|
861
|
+
*/ togglePlay() {
|
|
862
|
+
if (!this._lottieInstance) return;
|
|
863
|
+
const { currentFrame, playDirection, totalFrames } = this._lottieInstance;
|
|
864
|
+
if (this.currentState === PlayerState.Playing) {
|
|
865
|
+
return this.pause();
|
|
866
|
+
}
|
|
867
|
+
if (this.currentState === PlayerState.Completed) {
|
|
868
|
+
this.currentState = PlayerState.Playing;
|
|
869
|
+
if (this.mode === PlayMode.Bounce) {
|
|
870
|
+
this.setDirection(playDirection * -1);
|
|
871
|
+
return this._lottieInstance.goToAndPlay(currentFrame, true);
|
|
872
|
+
}
|
|
873
|
+
if (playDirection === -1) {
|
|
874
|
+
return this._lottieInstance.goToAndPlay(totalFrames, true);
|
|
875
|
+
}
|
|
876
|
+
return this._lottieInstance.goToAndPlay(0, true);
|
|
877
|
+
}
|
|
878
|
+
return this.play();
|
|
879
|
+
}
|
|
880
|
+
/**
|
|
881
|
+
* Toggle loop
|
|
882
|
+
*/ toggleLooping() {
|
|
883
|
+
this.setLooping(!this.loop);
|
|
884
|
+
}
|
|
885
|
+
/**
|
|
886
|
+
* Toggle Boomerang
|
|
887
|
+
*/ toggleBoomerang() {
|
|
888
|
+
if (this.mode === PlayMode.Normal) {
|
|
889
|
+
this.mode = PlayMode.Bounce;
|
|
890
|
+
} else {
|
|
891
|
+
this.mode = PlayMode.Normal;
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
/**
|
|
895
|
+
* Toggle show Settings
|
|
896
|
+
*/ _toggleSettings(flag) {
|
|
897
|
+
if (flag === undefined) {
|
|
898
|
+
this._isSettingsOpen = !this._isSettingsOpen;
|
|
899
|
+
} else {
|
|
900
|
+
this._isSettingsOpen = flag;
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
/**
|
|
904
|
+
* Handle blur
|
|
905
|
+
*/ _handleBlur() {
|
|
906
|
+
setTimeout(()=>this._toggleSettings(false), 200);
|
|
907
|
+
}
|
|
908
|
+
_switchInstance() {
|
|
909
|
+
// Clear previous animation
|
|
910
|
+
if (this._lottieInstance) this._lottieInstance.destroy();
|
|
911
|
+
// Re-initialize lottie player
|
|
912
|
+
this._lottieInstance = Lottie.loadAnimation({
|
|
913
|
+
...this._getOptions(),
|
|
914
|
+
animationData: this._animations[this._currentAnimation]
|
|
915
|
+
});
|
|
916
|
+
// Add event listeners to new Lottie instance
|
|
917
|
+
this._addEventListeners();
|
|
918
|
+
if (this.autoplay) {
|
|
919
|
+
this._lottieInstance?.goToAndPlay(0, true);
|
|
920
|
+
this.currentState = PlayerState.Playing;
|
|
921
|
+
} else {
|
|
922
|
+
this._lottieInstance?.goToAndStop(0, true);
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
/**
|
|
926
|
+
* Skip to next animation
|
|
927
|
+
*/ next() {
|
|
928
|
+
this._currentAnimation++;
|
|
929
|
+
this._switchInstance();
|
|
930
|
+
}
|
|
931
|
+
/**
|
|
932
|
+
* Skip to previous animation
|
|
933
|
+
*/ prev() {
|
|
934
|
+
this._currentAnimation--;
|
|
935
|
+
this._switchInstance();
|
|
936
|
+
}
|
|
937
|
+
/**
|
|
938
|
+
* Convert JSON Lottie to dotLottie
|
|
939
|
+
* @param { boolean | undefined } typeCheck External type safety
|
|
940
|
+
* @param { LottieManifest | undefined } manifest Externally added manifest
|
|
941
|
+
* @param { LottieJSON[] | undefined } animations Externally added animations
|
|
942
|
+
* @param { boolean } download Whether to trigger a download in the browser
|
|
943
|
+
*/ convert(typeCheck, manifest, animations, fileName, download = true) {
|
|
944
|
+
if (typeCheck || this._isDotLottie) return;
|
|
945
|
+
const oldManifest = manifest || this._manifest, newManifest = {
|
|
946
|
+
...oldManifest,
|
|
947
|
+
generator: pkg.name
|
|
948
|
+
};
|
|
949
|
+
return createDotLottie(animations || this._animations, newManifest, `${getFilename(fileName || this.src)}.lottie`, download);
|
|
950
|
+
}
|
|
951
|
+
/**
|
|
952
|
+
* Return the styles for the component
|
|
953
|
+
* @returns { CSSResult }
|
|
954
|
+
*/ static get styles() {
|
|
955
|
+
return styles;
|
|
956
|
+
}
|
|
957
|
+
/**
|
|
958
|
+
* Initialize everything on component first render
|
|
959
|
+
*/ connectedCallback() {
|
|
960
|
+
super.connectedCallback();
|
|
961
|
+
// Add listener for Visibility API's change event.
|
|
962
|
+
if (typeof document.hidden !== 'undefined') {
|
|
963
|
+
document.addEventListener('visibilitychange', this._onVisibilityChange);
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
async firstUpdated() {
|
|
967
|
+
// Add intersection observer for detecting component being out-of-view.
|
|
968
|
+
if ('IntersectionObserver' in window) {
|
|
969
|
+
this._intersectionObserver = new IntersectionObserver((entries)=>{
|
|
970
|
+
if (entries[0].isIntersecting) {
|
|
971
|
+
if (!document.hidden && this.currentState === PlayerState.Frozen) {
|
|
972
|
+
this.play();
|
|
973
|
+
}
|
|
974
|
+
} else if (this.currentState === PlayerState.Playing) {
|
|
975
|
+
this._freeze();
|
|
976
|
+
}
|
|
977
|
+
});
|
|
978
|
+
this._intersectionObserver.observe(this.container);
|
|
979
|
+
}
|
|
980
|
+
// Setup lottie player
|
|
981
|
+
if (this.src) {
|
|
982
|
+
await this.load(this.src);
|
|
983
|
+
}
|
|
984
|
+
this.dispatchEvent(new CustomEvent(PlayerEvents.Rendered));
|
|
985
|
+
}
|
|
986
|
+
/**
|
|
987
|
+
* Cleanup on component destroy
|
|
988
|
+
*/ disconnectedCallback() {
|
|
989
|
+
super.disconnectedCallback();
|
|
990
|
+
// Remove intersection observer for detecting component being out-of-view
|
|
991
|
+
if (this._intersectionObserver) {
|
|
992
|
+
this._intersectionObserver.disconnect();
|
|
993
|
+
this._intersectionObserver = undefined;
|
|
994
|
+
}
|
|
995
|
+
// Destroy the animation instance
|
|
996
|
+
if (this._lottieInstance) this._lottieInstance.destroy();
|
|
997
|
+
// Remove the attached Visibility API's change event listener
|
|
998
|
+
document.removeEventListener('visibilitychange', this._onVisibilityChange);
|
|
999
|
+
}
|
|
1000
|
+
renderControls() {
|
|
1001
|
+
const isPlaying = this.currentState === PlayerState.Playing, isPaused = this.currentState === PlayerState.Paused, isStopped = this.currentState === PlayerState.Stopped, isError = this.currentState === PlayerState.Error;
|
|
1002
|
+
return html`<div class="${`lottie-controls toolbar ${isError ? 'has-error' : ''}`}" aria-label="Lottie Animation controls"><button @click="${this.togglePlay}" data-active="${isPlaying || isPaused}" tabindex="0" aria-label="Toggle Play/Pause">${isPlaying ? html`<svg width="24" height="24" aria-hidden="true" focusable="false"><path d="M14.016 5.016H18v13.969h-3.984V5.016zM6 18.984V5.015h3.984v13.969H6z"/></svg>` : html`<svg width="24" height="24" aria-hidden="true" focusable="false"><path d="M8.016 5.016L18.985 12 8.016 18.984V5.015z"/></svg>`}</button> <button @click="${this.stop}" data-active="${isStopped}" tabindex="0" aria-label="Stop"><svg width="24" height="24" aria-hidden="true" focusable="false"><path d="M6 6h12v12H6V6z"/></svg></button> ${this._animations?.length > 1 ? html`${this._currentAnimation > 0 ? html`<button @click="${this.prev}" tabindex="0" aria-label="Previous animation"><svg width="24" height="24" aria-hidden="true" focusable="false"><path d="M17.9 18.2 8.1 12l9.8-6.2v12.4zm-10.3 0H6.1V5.8h1.5v12.4z"/></svg></button>` : nothing} ${this._currentAnimation + 1 < this._animations?.length ? html`<button @click="${this.next}" tabindex="0" aria-label="Next animation"><svg width="24" height="24" aria-hidden="true" focusable="false"><path d="m6.1 5.8 9.8 6.2-9.8 6.2V5.8zM16.4 5.8h1.5v12.4h-1.5z"/></svg></button>` : nothing}` : nothing}<form class="progress-container${this.simple ? ' simple' : ''}"><input class="seeker" type="range" min="0" max="100" step="1" value="${this._seeker}" @change="${this._handleSeekChange}" @mousedown="${this._freeze}" aria-valuemin="0" aria-valuemax="100" role="slider" aria-valuenow="${this._seeker}" tabindex="0" aria-label="Slider for search"><progress max="100" value="${this._seeker}"></progress></form>${this.simple ? nothing : html`<button @click="${this.toggleLooping}" data-active="${this.loop ?? nothing}" tabindex="0" aria-label="Toggle looping"><svg width="24" height="24" aria-hidden="true" focusable="false"><path d="M17.016 17.016v-4.031h1.969v6h-12v3l-3.984-3.984 3.984-3.984v3h10.031zM6.984 6.984v4.031H5.015v-6h12v-3l3.984 3.984-3.984 3.984v-3H6.984z"/></svg></button> <button @click="${this.toggleBoomerang}" data-active="${this.mode === PlayMode.Bounce}" aria-label="Toggle boomerang" tabindex="0"><svg width="24" height="24" aria-hidden="true" focusable="false"><path d="m11.8 13.2-.3.3c-.5.5-1.1 1.1-1.7 1.5-.5.4-1 .6-1.5.8-.5.2-1.1.3-1.6.3s-1-.1-1.5-.3c-.6-.2-1-.5-1.4-1-.5-.6-.8-1.2-.9-1.9-.2-.9-.1-1.8.3-2.6.3-.7.8-1.2 1.3-1.6.3-.2.6-.4 1-.5.2-.2.5-.2.8-.3.3 0 .7-.1 1 0 .3 0 .6.1.9.2.9.3 1.7.9 2.4 1.5.4.4.8.7 1.1 1.1l.1.1.4-.4c.6-.6 1.2-1.2 1.9-1.6.5-.3 1-.6 1.5-.7.4-.1.7-.2 1-.2h.9c1 .1 1.9.5 2.6 1.4.4.5.7 1.1.8 1.8.2.9.1 1.7-.2 2.5-.4.9-1 1.5-1.8 2-.4.2-.7.4-1.1.4-.4.1-.8.1-1.2.1-.5 0-.9-.1-1.3-.3-.8-.3-1.5-.9-2.1-1.5-.4-.4-.8-.7-1.1-1.1h-.3zm-1.1-1.1c-.1-.1-.1-.1 0 0-.3-.3-.6-.6-.8-.9-.5-.5-1-.9-1.6-1.2-.4-.3-.8-.4-1.3-.4-.4 0-.8 0-1.1.2-.5.2-.9.6-1.1 1-.2.3-.3.7-.3 1.1 0 .3 0 .6.1.9.1.5.4.9.8 1.2.5.4 1.1.5 1.7.5.5 0 1-.2 1.5-.5.6-.4 1.1-.8 1.6-1.3.1-.3.3-.5.5-.6zM13 12c.5.5 1 1 1.5 1.4.5.5 1.1.9 1.9 1 .4.1.8 0 1.2-.1.3-.1.6-.3.9-.5.4-.4.7-.9.8-1.4.1-.5 0-.9-.1-1.4-.3-.8-.8-1.2-1.7-1.4-.4-.1-.8-.1-1.2 0-.5.1-1 .4-1.4.7-.5.4-1 .8-1.4 1.2-.2.2-.4.3-.5.5z"/></svg></button> <button @click="${this._handleSettingsClick}" @blur="${this._handleBlur}" aria-label="Settings" aria-haspopup="true" aria-expanded="${!!this._isSettingsOpen}" aria-controls="${`${this._identifier}-settings`}"><svg width="24" height="24" aria-hidden="true" focusable="false"><circle cx="12" cy="5.4" r="2.5"/><circle cx="12" cy="12" r="2.5"/><circle cx="12" cy="18.6" r="2.5"/></svg></button><div id="${`${this._identifier}-settings`}" class="popover" style="display:${this._isSettingsOpen ? 'block' : 'none'}">${this._isDotLottie ? nothing : html`<button @click="${this.convert}" aria-label="Convert JSON animation to dotLottie format" tabindex="0"><svg width="24" height="24" aria-hidden="true" focusable="false"><path d="M17.016 17.016v-4.031h1.969v6h-12v3l-3.984-3.984 3.984-3.984v3h10.031zM6.984 6.984v4.031H5.015v-6h12v-3l3.984 3.984-3.984 3.984v-3H6.984z"/></svg> Convert to dotLottie</button>`} <button @click="${this.snapshot}" aria-label="Download still image" tabindex="0"><svg width="24" height="24" aria-hidden="true" focusable="false"><path d="M16.8 10.8 12 15.6l-4.8-4.8h3V3.6h3.6v7.2h3zM12 15.6H3v4.8h18v-4.8h-9zm7.8 2.4h-2.4v-1.2h2.4V18z"/></svg> Download still image</button></div>`}</div>`;
|
|
1003
|
+
}
|
|
1004
|
+
render() {
|
|
1005
|
+
return html`<figure class="${'animation-container main'}" data-controls="${this.controls ?? nothing}" lang="${this.description ? document?.documentElement?.lang : 'en'}" role="img" aria-label="${this.description ?? 'Lottie animation'}" data-loaded="${this._playerState.loaded}"><div class="animation" style="background:${this.background}">${this.currentState === PlayerState.Error ? html`<div class="error"><svg preserveAspectRatio="xMidYMid slice" xmlns="http://www.w3.org/2000/svg" xml:space="preserve" width="1920" height="1080" viewBox="0 0 1920 1080"><path fill="#fff" d="M0 0h1920v1080H0z"/><path fill="#3a6d8b" d="M1190.2 531 1007 212.4c-22-38.2-77.2-38-98.8.5L729.5 531.3c-21.3 37.9 6.1 84.6 49.5 84.6l361.9.3c43.7 0 71.1-47.3 49.3-85.2zM937.3 288.7c.2-7.5 3.3-23.9 23.2-23.9 16.3 0 23 16.1 23 23.5 0 55.3-10.7 197.2-12.2 214.5-.1 1-.9 1.7-1.9 1.7h-18.3c-1 0-1.8-.7-1.9-1.7-1.4-17.5-13.4-162.9-11.9-214.1zm24.2 283.8c-13.1 0-23.7-10.6-23.7-23.7s10.6-23.7 23.7-23.7 23.7 10.6 23.7 23.7-10.6 23.7-23.7 23.7zM722.1 644h112.6v34.4h-70.4V698h58.8v31.7h-58.8v22.6h72.4v36.2H722.1V644zm162 57.1h.6c8.3-12.9 18.2-17.8 31.3-17.8 3 0 5.1.4 6.3 1v32.6h-.8c-22.4-3.8-35.6 6.3-35.6 29.5v42.3h-38.2V685.5h36.4v15.6zm78.9 0h.6c8.3-12.9 18.2-17.8 31.3-17.8 3 0 5.1.4 6.3 1v32.6h-.8c-22.4-3.8-35.6 6.3-35.6 29.5v42.3h-38.2V685.5H963v15.6zm39.5 36.2c0-31.3 22.2-54.8 56.6-54.8 34.4 0 56.2 23.5 56.2 54.8s-21.8 54.6-56.2 54.6c-34.4-.1-56.6-23.3-56.6-54.6zm74 0c0-17.4-6.1-29.1-17.8-29.1-11.7 0-17.4 11.7-17.4 29.1 0 17.4 5.7 29.1 17.4 29.1s17.8-11.8 17.8-29.1zm83.1-36.2h.6c8.3-12.9 18.2-17.8 31.3-17.8 3 0 5.1.4 6.3 1v32.6h-.8c-22.4-3.8-35.6 6.3-35.6 29.5v42.3h-38.2V685.5h36.4v15.6z"/><path fill="none" d="M718.9 807.7h645v285.4h-645z"/><text fill="#3a6d8b" style="text-align:center;position:absolute;left:100%;font-size:47px;font-family:system-ui,-apple-system,BlinkMacSystemFont,'.SFNSText-Regular',sans-serif" x="50%" y="848.017" text-anchor="middle">${this._errorMessage}</text></svg></div>` : nothing}</div>${this.controls ? this.renderControls() : nothing}</figure>`;
|
|
1006
|
+
}
|
|
1007
|
+
constructor(...args){
|
|
1008
|
+
super(...args);
|
|
1009
|
+
/**
|
|
1010
|
+
* Background color
|
|
1011
|
+
*/ this.background = 'transparent';
|
|
1012
|
+
/**
|
|
1013
|
+
* Display controls
|
|
1014
|
+
*/ this.controls = false;
|
|
1015
|
+
/**
|
|
1016
|
+
* Player state
|
|
1017
|
+
*/ this.currentState = PlayerState.Loading;
|
|
1018
|
+
/**
|
|
1019
|
+
* Direction of animation
|
|
1020
|
+
*/ this.direction = 1;
|
|
1021
|
+
/**
|
|
1022
|
+
* Whether to play on mouseover
|
|
1023
|
+
*/ this.hover = false;
|
|
1024
|
+
/**
|
|
1025
|
+
* Intermission
|
|
1026
|
+
*/ this.intermission = 0;
|
|
1027
|
+
/**
|
|
1028
|
+
* Whether to loop
|
|
1029
|
+
*/ this.loop = false;
|
|
1030
|
+
/**
|
|
1031
|
+
* Play mode
|
|
1032
|
+
*/ this.mode = PlayMode.Normal;
|
|
1033
|
+
/**
|
|
1034
|
+
* Resizing to container
|
|
1035
|
+
*/ this.objectfit = 'contain';
|
|
1036
|
+
/**
|
|
1037
|
+
* Renderer to use (svg, canvas or html)
|
|
1038
|
+
*/ this.renderer = 'svg';
|
|
1039
|
+
/**
|
|
1040
|
+
* Hide advanced controls
|
|
1041
|
+
*/ this.simple = false;
|
|
1042
|
+
/**
|
|
1043
|
+
* Speed
|
|
1044
|
+
*/ this.speed = 1;
|
|
1045
|
+
/**
|
|
1046
|
+
* Subframe
|
|
1047
|
+
*/ this.subframe = true;
|
|
1048
|
+
/**
|
|
1049
|
+
* Whether settings toolbar is open
|
|
1050
|
+
*/ this._isSettingsOpen = false;
|
|
1051
|
+
/**
|
|
1052
|
+
* Seeker
|
|
1053
|
+
*/ this._seeker = 0;
|
|
1054
|
+
/**
|
|
1055
|
+
* Which animation to show, if several
|
|
1056
|
+
*/ this._currentAnimation = 0;
|
|
1057
|
+
this._lottieInstance = null;
|
|
1058
|
+
this._identifier = this.id || useId('dotlottie');
|
|
1059
|
+
this._errorMessage = 'Something went wrong';
|
|
1060
|
+
this._isDotLottie = false;
|
|
1061
|
+
this._playerState = {
|
|
1062
|
+
prev: PlayerState.Loading,
|
|
1063
|
+
count: 0,
|
|
1064
|
+
loaded: false
|
|
1065
|
+
};
|
|
1066
|
+
/**
|
|
1067
|
+
* Handle settings click event
|
|
1068
|
+
*/ this._handleSettingsClick = ({ target })=>{
|
|
1069
|
+
this._toggleSettings();
|
|
1070
|
+
// Because Safari does not add focus on click, we need to add it manually, so the onblur event will fire
|
|
1071
|
+
if (target instanceof HTMLElement) {
|
|
1072
|
+
target.focus();
|
|
1073
|
+
}
|
|
1074
|
+
};
|
|
1075
|
+
}
|
|
1076
|
+
};
|
|
1077
|
+
_ts_decorate([
|
|
1078
|
+
property({
|
|
1079
|
+
type: Boolean,
|
|
1080
|
+
reflect: true
|
|
1081
|
+
})
|
|
1082
|
+
], DotLottiePlayer.prototype, "autoplay", void 0);
|
|
1083
|
+
_ts_decorate([
|
|
1084
|
+
property({
|
|
1085
|
+
type: String
|
|
1086
|
+
})
|
|
1087
|
+
], DotLottiePlayer.prototype, "background", void 0);
|
|
1088
|
+
_ts_decorate([
|
|
1089
|
+
property({
|
|
1090
|
+
type: Boolean,
|
|
1091
|
+
reflect: true
|
|
1092
|
+
})
|
|
1093
|
+
], DotLottiePlayer.prototype, "controls", void 0);
|
|
1094
|
+
_ts_decorate([
|
|
1095
|
+
property({
|
|
1096
|
+
type: Number
|
|
1097
|
+
})
|
|
1098
|
+
], DotLottiePlayer.prototype, "count", void 0);
|
|
1099
|
+
_ts_decorate([
|
|
1100
|
+
property({
|
|
1101
|
+
type: String
|
|
1102
|
+
})
|
|
1103
|
+
], DotLottiePlayer.prototype, "currentState", void 0);
|
|
1104
|
+
_ts_decorate([
|
|
1105
|
+
property({
|
|
1106
|
+
type: String
|
|
1107
|
+
})
|
|
1108
|
+
], DotLottiePlayer.prototype, "description", void 0);
|
|
1109
|
+
_ts_decorate([
|
|
1110
|
+
property({
|
|
1111
|
+
type: Number
|
|
1112
|
+
})
|
|
1113
|
+
], DotLottiePlayer.prototype, "direction", void 0);
|
|
1114
|
+
_ts_decorate([
|
|
1115
|
+
property({
|
|
1116
|
+
type: Boolean
|
|
1117
|
+
})
|
|
1118
|
+
], DotLottiePlayer.prototype, "hover", void 0);
|
|
1119
|
+
_ts_decorate([
|
|
1120
|
+
property({
|
|
1121
|
+
type: Number
|
|
1122
|
+
})
|
|
1123
|
+
], DotLottiePlayer.prototype, "intermission", void 0);
|
|
1124
|
+
_ts_decorate([
|
|
1125
|
+
property({
|
|
1126
|
+
type: Boolean,
|
|
1127
|
+
reflect: true
|
|
1128
|
+
})
|
|
1129
|
+
], DotLottiePlayer.prototype, "loop", void 0);
|
|
1130
|
+
_ts_decorate([
|
|
1131
|
+
property({
|
|
1132
|
+
type: String
|
|
1133
|
+
})
|
|
1134
|
+
], DotLottiePlayer.prototype, "mode", void 0);
|
|
1135
|
+
_ts_decorate([
|
|
1136
|
+
property({
|
|
1137
|
+
type: String
|
|
1138
|
+
})
|
|
1139
|
+
], DotLottiePlayer.prototype, "objectfit", void 0);
|
|
1140
|
+
_ts_decorate([
|
|
1141
|
+
property({
|
|
1142
|
+
type: String
|
|
1143
|
+
})
|
|
1144
|
+
], DotLottiePlayer.prototype, "preserveAspectRatio", void 0);
|
|
1145
|
+
_ts_decorate([
|
|
1146
|
+
property({
|
|
1147
|
+
type: String
|
|
1148
|
+
})
|
|
1149
|
+
], DotLottiePlayer.prototype, "renderer", void 0);
|
|
1150
|
+
_ts_decorate([
|
|
1151
|
+
property({
|
|
1152
|
+
type: Array
|
|
1153
|
+
})
|
|
1154
|
+
], DotLottiePlayer.prototype, "segment", void 0);
|
|
1155
|
+
_ts_decorate([
|
|
1156
|
+
property({
|
|
1157
|
+
type: Boolean
|
|
1158
|
+
})
|
|
1159
|
+
], DotLottiePlayer.prototype, "simple", void 0);
|
|
1160
|
+
_ts_decorate([
|
|
1161
|
+
property({
|
|
1162
|
+
type: Number
|
|
1163
|
+
})
|
|
1164
|
+
], DotLottiePlayer.prototype, "speed", void 0);
|
|
1165
|
+
_ts_decorate([
|
|
1166
|
+
property({
|
|
1167
|
+
type: String
|
|
1168
|
+
})
|
|
1169
|
+
], DotLottiePlayer.prototype, "src", void 0);
|
|
1170
|
+
_ts_decorate([
|
|
1171
|
+
property({
|
|
1172
|
+
type: Boolean
|
|
1173
|
+
})
|
|
1174
|
+
], DotLottiePlayer.prototype, "subframe", void 0);
|
|
1175
|
+
_ts_decorate([
|
|
1176
|
+
query('.animation')
|
|
1177
|
+
], DotLottiePlayer.prototype, "container", void 0);
|
|
1178
|
+
_ts_decorate([
|
|
1179
|
+
state()
|
|
1180
|
+
], DotLottiePlayer.prototype, "_isSettingsOpen", void 0);
|
|
1181
|
+
_ts_decorate([
|
|
1182
|
+
state()
|
|
1183
|
+
], DotLottiePlayer.prototype, "_seeker", void 0);
|
|
1184
|
+
_ts_decorate([
|
|
1185
|
+
state()
|
|
1186
|
+
], DotLottiePlayer.prototype, "_currentAnimation", void 0);
|
|
1187
|
+
_ts_decorate([
|
|
1188
|
+
state()
|
|
1189
|
+
], DotLottiePlayer.prototype, "_animations", void 0);
|
|
1190
|
+
DotLottiePlayer = _ts_decorate([
|
|
1191
|
+
customElement('dotlottie-player')
|
|
1192
|
+
], DotLottiePlayer);
|
|
1193
|
+
|
|
1194
|
+
/**
|
|
1195
|
+
* Expose DotLottiePlayer class as global variable
|
|
1196
|
+
* @returns { DotLottiePlayer }
|
|
1197
|
+
*/ globalThis.dotLottiePlayer = ()=>new DotLottiePlayer();
|
|
1198
|
+
|
|
1199
|
+
export { DotLottiePlayer, PlayMode, PlayerEvents, PlayerState };
|