illusory
Version:
Seamlessly morph one element into another.
38 lines (31 loc) • 14.8 kB
JavaScript
!function(t){"use strict";
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */var h=function(){return(h=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var i in e=arguments[n])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t}).apply(this,arguments)};function c(t,a,s,l){return new(s=s||Promise)(function(n,e){function r(t){try{o(l.next(t))}catch(t){e(t)}}function i(t){try{o(l.throw(t))}catch(t){e(t)}}function o(t){var e;t.done?n(t.value):((e=t.value)instanceof s?e:new s(function(t){t(e)})).then(r,i)}o((l=l.apply(t,a||[])).next())})}function d(n,r){var i,o,a,t,s={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]};return t={next:e(0),throw:e(1),return:e(2)},"function"==typeof Symbol&&(t[Symbol.iterator]=function(){return this}),t;function e(e){return function(t){return function(e){if(i)throw new TypeError("Generator is already executing.");for(;s;)try{if(i=1,o&&(a=2&e[0]?o.return:e[0]?o.throw||((a=o.return)&&a.call(o),0):o.next)&&!(a=a.call(o,e[1])).done)return a;switch(o=0,a&&(e=[2&e[0],a.value]),e[0]){case 0:case 1:a=e;break;case 4:return s.label++,{value:e[1],done:!1};case 5:s.label++,o=e[1],e=[0];continue;case 7:e=s.ops.pop(),s.trys.pop();continue;default:if(!(a=0<(a=s.trys).length&&a[a.length-1])&&(6===e[0]||2===e[0])){s=0;continue}if(3===e[0]&&(!a||e[1]>a[0]&&e[1]<a[3])){s.label=e[1];break}if(6===e[0]&&s.label<a[1]){s.label=a[1],a=e;break}if(a&&s.label<a[2]){s.label=a[2],s.ops.push(e);break}a[2]&&s.ops.pop(),s.trys.pop();continue}e=r.call(n,s)}catch(t){e=[6,t],o=0}finally{i=a=0}if(5&e[0])throw e[1];return{value:e[0]?e[1]:void 0,done:!0}}([e,t])}}}var f=/^(\d+(?:\.\d+)?)([^\d\s.]+)(?:\s+(\d+(?:\.\d+)?)([^\d\s.]+))?$/;function o(t,e){var n,r,i,o,a,s,l,u,c=(n=e,l=f.exec(n),{x:s={value:+(null!==(r=null==l?void 0:l[1])&&void 0!==r?r:0),unit:null!==(i=null==l?void 0:l[2])&&void 0!==i?i:"px"},y:null!=l&&l[3]?{value:+(null!==(o=null==l?void 0:l[3])&&void 0!==o?o:0),unit:null!==(a=null==l?void 0:l[4])&&void 0!==a?a:"px"}:h({},s)});return"%"!==c.x.unit&&(c.x.value=c.x.value*t.inverseScaleX),"%"!==c.y.unit&&(c.y.value=c.y.value*t.inverseScaleY),""+(u=c).x.value+u.x.unit+" "+u.y.value+u.y.unit}function e(t,e,n){return n}function i(t,e){var n,r,i=(n=t.getStyle("transformOrigin"),{x:(r=n.split(" ").map(parseFloat))[0],y:r[1]}),o=e.rect.width/t.rect.width,a=e.rect.height/t.rect.height,s=t.rect.width/e.rect.width,l=t.rect.height/e.rect.height,u=i.x/t.rect.width*(e.rect.width*(1-s)),c=i.y/t.rect.height*(e.rect.height*(1-l));return{x:e.rect.left-t.rect.left+u,y:e.rect.top-t.rect.top+c,scaleX:o,scaleY:a,inverseScaleX:s,inverseScaleY:l}}
/*! @license Rematrix v0.7.0
Copyright 2020 Julian Lloyd.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/function p(t){if(t&&t.constructor===Array){var e=t.filter(function(t){return"number"==typeof t}).filter(function(t){return!isNaN(t)});if(6===t.length&&6===e.length){var n=y();return n[0]=e[0],n[1]=e[1],n[4]=e[2],n[5]=e[3],n[12]=e[4],n[13]=e[5],n}if(16===t.length&&16===e.length)return t}throw new TypeError("Expected a `number[]` with length 6 or 16.")}function y(){for(var t=[],e=0;e<16;e++)e%5==0?t.push(1):t.push(0);return t}function v(t,e){for(var n=p(t),r=p(e),i=[],o=0;o<4;o++)for(var a=[n[o],n[o+4],n[o+8],n[o+12]],s=0;s<4;s++){var l=4*s,u=[r[l],r[1+l],r[2+l],r[3+l]],c=a[0]*u[0]+a[1]*u[1]+a[2]*u[2]+a[3]*u[3];i[o+l]=c}return i}var g=y();function a(t,e){var n;n=e.startsWith("matrix")?function(t){if("string"==typeof t){var e=t.match(/matrix(3d)?\(([^)]+)\)/);if(e)return p(e[2].split(", ").map(parseFloat))}throw new TypeError("Expected a string containing `matrix()` or `matrix3d()")}(e):g;var r,i,o,a,s,l,u,c=[(a=t.x,s=t.y,(l=y())[12]=a,s&&(l[13]=s),l),n,(r=t.scaleX,i=t.scaleY,(o=y())[0]=r,o[5]="number"==typeof i?i:r,o)];try{u="matrix3d("+p(c.reduce(v)).join(", ")+")"}catch(t){return process&&"production"!==process.env.NODE_ENV&&"test"!==process.env.NODE_ENV&&console.error("[illusory] Failed to construct transform matrix. Is the element in the DOM?"),"none"}return u}function m(t){return t.match(new RegExp(".{"+(e=t,Math.floor((e.length-1)/3))+"}","g"));var e}function b(t){return parseInt(t.repeat(2/t.length),16)}var u={element:{includeChildren:!0,ignoreTransparency:["img"]},zIndex:1,compositeOnly:!1,duration:"300ms",easing:"ease",relativeTo:[document]},x=/^rgba\((\d+),\s*(\d+),\s*(\d+),\s*([01](?:\.\d+)?)\)$/i;function n(t){var e,n,r,i,o,a,s,l,u,c,h,d=x.exec(t);if(d)r=(e=d.slice(1,5).map(parseFloat))[0],i=e[1],o=e[2],a=e[3];else{if(!/^#([a-f0-9]{4}){1,2}$/i.test(t))return!1;var f=m(t.slice(1));if(!f)return!1;s=f.map(b),l=s[0],u=s[1],c=s[2],h=s[3],r=(n=[l,u,c,+(h/256).toFixed(2)])[0],i=n[1],o=n[2],a=n[3]}return{r:r,g:i,b:o,a:a}}var s=function(t){return t instanceof HTMLElement||t instanceof SVGElement};function l(t,n,r){void 0===r&&(r=0);var i,o,a=t.cloneNode(!1);if(s(t)&&(function(t,e){var n=window.getComputedStyle(t);if(""!==n.cssText)e.style.cssText=n.cssText;else{for(var r="",i=0;i<n.length;i++)r+=n[i]+": "+n.getPropertyValue(n[i])+"; ";e.style.cssText=r}e.style.transformOrigin=n.transformOrigin}(t,a),!0!==n.preserveDataAttributes&&(i=a,o=n.preserveDataAttributes,Object.keys(i.dataset).forEach(function(t){var e="data-"+t.split(/(?=[A-Z])/).join("-").toLowerCase();0!==e.indexOf("data-illusory")&&(o&&o(e)||i.removeAttribute(e))}))),"function"==typeof n.processClone){var e=n.processClone(a,r);if(!e)return null;a=e}return n.includeChildren&&t.childNodes.forEach(function(t){var e=l(t,n,r+1);e&&a.appendChild(e)}),a}function w(t,e){t.clone.clientWidth,null!=e&&e.clone.clientWidth}var S=(r.prototype._makeCompositeOnly=function(){this.deltaHandlers={transform:a,borderTopLeftRadius:e,borderTopRightRadius:e,borderBottomLeftRadius:e,borderBottomRightRadius:e}},Object.defineProperty(r.prototype,"_ignoreTransparency",{get:function(){return!0===this._shouldIgnoreTransparency||!(!Array.isArray(this._shouldIgnoreTransparency)||-1===this._shouldIgnoreTransparency.indexOf(this.clone.tagName.toLowerCase()))},enumerable:!1,configurable:!0}),r.prototype._hasTransparentBackground=function(){if(this._ignoreTransparency)return!1;var t=n(this.getStyle("backgroundColor"));return!!t&&t.a<1},r.prototype._to=function(e){var n=this,r=i(this,e);Object.keys(this.deltaHandlers).forEach(function(t){n.setStyle(t,n.deltaHandlers[t](r,e.getStyle(t),n.getStyle(t)))})},r.prototype._enableTransitions=function(t){var e;this.setStyle("transition",(e=t).compositeOnly?"transform "+e.duration+" "+e.easing+" 0s, opacity "+e.duration+" "+e.easing+" 0s":"all "+e.duration+" "+e.easing+" 0s")},r.prototype._disableTransitions=function(){this.setStyle("transition","none")},r.prototype._setParent=function(t){this.isAttached&&this.detach(),this.hideNatural(),t.appendChild(this.clone),this.isAttached=!0},r.prototype._resetNaturalStyleAttribute=function(){this.initialStyleAttributeValue?this.natural.setAttribute("style",this.initialStyleAttributeValue):this.natural.removeAttribute("style")},r.prototype.getStyle=function(t){var e;return null!==(e=this.originalStyle[t])&&void 0!==e?e:this.clone.style[t]},r.prototype.setStyle=function(t,e){this.originalStyle[t]=this.getStyle(t),this.clone.style[t]=e},r.prototype.waitFor=function(i){var t=this;return new Promise(function(n){var r=function(e){return c(t,void 0,void 0,function(){return d(this,function(t){switch(t.label){case 0:return"any"!==i&&e.propertyName!==i?[2]:"any"!==i?[3,2]:[4,new Promise(function(t){return requestAnimationFrame(t)})];case 1:t.sent(),t.label=2;case 2:return this.clone.removeEventListener("transitionend",r),n(),[2]}})})};t.clone.addEventListener("transitionend",r)})},r.prototype.hide=function(){this.setStyle("opacity","0")},r.prototype.show=function(){this.setStyle("opacity","1")},r.prototype.hideNatural=function(){this.natural.style.opacity="0"},r.prototype.showNatural=function(){this.natural.style.opacity="1"},r.prototype.flushCSS=function(){w(this)},r.prototype.attach=function(){this._setParent(document.body)},r.prototype.detach=function(){this.isAttached&&(this.showNatural(),this.flushCSS(),this._resetNaturalStyleAttribute(),this.clone.remove(),this.isAttached=!1)},r);function r(t,e){var n,r;this.originalStyle={},this.deltaHandlers={transform:a,borderTopLeftRadius:o,borderTopRightRadius:o,borderBottomRightRadius:o,borderBottomLeftRadius:o},this.isAttached=!1,this.natural=t,this.initialStyleAttributeValue=this.natural.getAttribute("style"),this._shouldIgnoreTransparency=null!==(n=null==e?void 0:e.ignoreTransparency)&&void 0!==n?n:u.element.ignoreTransparency,this.natural.style.transition="none",this.natural.style.animation="none";var i=this.natural.style.transform;this.natural.style.transform="none",this.rect=this.natural.getBoundingClientRect(),this.natural.style.transform=i,this.clone=l(this.natural,{includeChildren:null!==(r=null==e?void 0:e.includeChildren)&&void 0!==r?r:u.element.includeChildren,preserveDataAttributes:null==e?void 0:e.preserveDataAttributes,processClone:null==e?void 0:e.processClone}),this.setStyle("left","auto"),this.setStyle("right","auto"),this.setStyle("top","auto"),this.setStyle("bottom","auto"),this.setStyle("margin","0 0 0 0"),this.setStyle("transition","none"),this.setStyle("animation","none"),this.setStyle("pointerEvents","none"),this.setStyle("position","fixed"),this.setStyle("left",this.rect.left+"px"),this.setStyle("top",this.rect.top+"px"),null!=e&&e.attachImmediately&&this.attach()}var T=new Map,E=(A.getCumulativeDelta=function(o){return o.dependencies.reduce(function(t,e){var n=t.x,r=t.y,i=T.get(e).getDelta(o);return{x:n+i.x,y:r+i.y}},{x:0,y:0})},Object.defineProperty(A.prototype,"currentPosition",{get:function(){return{x:this.target instanceof Document?window.scrollX:this.target.scrollLeft,y:this.target instanceof Document?window.scrollY:this.target.scrollTop}},enumerable:!1,configurable:!0}),A.prototype.getDelta=function(t){var e=this.initialPositions.get(t),n=this.currentPosition;return{x:e.x-n.x,y:e.y-n.y}},Object.defineProperty(A.prototype,"listenerCount",{get:function(){return this.listeners.length},enumerable:!1,configurable:!0}),A.prototype.add=function(t){this.listeners.push(t),this.initialPositions.set(t,this.currentPosition)},A.prototype.remove=function(t){this.listeners.splice(this.listeners.indexOf(t),1),this.initialPositions.delete(t)},A.prototype.dispatch=function(){this.listeners.forEach(function(t){var e=A.getCumulativeDelta(t);t.handler(e)})},A);function A(t,e){this.listeners=[],this.initialPositions=new Map,this.target=t,this.add(e)}function C(e){O||(O=!0,requestAnimationFrame(function(){O=!1;var t=e.target;T.has(t)&&T.get(t).dispatch()}))}var O=!1,_={add:function(e){e.dependencies.forEach(function(t){T.size||document.addEventListener("scroll",C,!0),T.has(t)?T.get(t).add(e):T.set(t,new E(t,e))})},remove:function(n){n.dependencies.forEach(function(t){var e=T.get(t);e.remove(n),e.listenerCount||T.delete(t)}),T.size||document.removeEventListener("scroll",C,!0)}};t.IllusoryElement=S,t.illusory=function(t,e,n){function r(t){return t instanceof S?t:new S(t,i.element)}var i=h(h({},u),n),o=r(t),a=r(e);o.setStyle("zIndex",1),a.setStyle("zIndex",2);var s=function(i){var o=document.createElement("div");o.style.position="fixed",o.style.top="0",o.style.left="0",o.style.zIndex=i.zIndex.toString(),o.style.transition="opacity "+i.duration+" "+i.easing+" 0s",document.body.appendChild(o);var a={dependencies:i.relativeTo,handler:function(t){var e=t.x,n=t.y;o.style.transform="translate("+e+"px, "+n+"px)"}};return _.add(a),{setOpacity:function(t){o.style.opacity=t},add:function(t,e){[t,e].forEach(function(t){i.compositeOnly&&t._makeCompositeOnly(),t._setParent(o),t.setStyle("opacity",1)})},destroy:function(e,n,r){return c(this,void 0,void 0,function(){return d(this,function(t){switch(t.label){case 0:return"function"!=typeof i.beforeDetach?[3,2]:[4,i.beforeDetach(e,n,r)];case 1:t.sent(),t.label=2;case 2:return _.remove(a),e.detach(),n.detach(),o.remove(),[2]}})})}}}(i);s.add(o,a),s.setOpacity(o.getStyle("opacity"));var l={cancel:function(){throw new Error("Cancel called before assigned")}};return{finished:function(o,a,s,l,u){var t=this;return new Promise(function(i){return c(t,void 0,void 0,function(){var e,n,r=this;return d(this,function(t){switch(t.label){case 0:return e=!1,n=function(){return c(r,void 0,void 0,function(){return d(this,function(t){switch(t.label){case 0:return e=!0,[4,s.destroy(o,a,e)];case 1:return t.sent(),i(e),[2]}})})},u.cancel=n,a.hide(),a._to(o),"function"!=typeof l.beforeAnimate?[3,2]:[4,Promise.resolve(l.beforeAnimate(o,a))];case 1:t.sent(),t.label=2;case 2:return e?[2]:(o._enableTransitions(l),a._enableTransitions(l),w(o,a),o._to(a),a._to(a),a._ignoreTransparency||!a._hasTransparentBackground()&&!l.compositeOnly||o.hide(),a.show(),s.setOpacity(a.getStyle("opacity")),[4,a.waitFor("any")]);case 3:return t.sent(),e?[2]:[4,s.destroy(o,a,e)];case 4:return t.sent(),i(e),[2]}})})})}(o,a,s,i,l),cancel:function(){l.cancel()}}}}(this.window=this.window||{});