creepyface
Advanced tools
Comparing version 4.1.6 to 4.1.7
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).creepyface=t()}(this,function(){"use strict";var f=function t(r){if(!r)return Promise.reject();if("string"==typeof r){var e=r;(r=new Image).src=e}else{if(void 0!==r.length){var n=[].map.call(r,function(e){return t(e).catch(function(e){return e})});return Promise.all(n).then(function(e){var t=e.filter(function(e){return e.naturalWidth});return t.length===e.length?t:Promise.reject({loaded:t,errored:e.filter(function(e){return!e.naturalWidth})})})}if("IMG"!==r.tagName)return Promise.reject()}var o=new Promise(function(e,t){function n(){r.naturalWidth?e(r):t(r),r.removeEventListener("load",n),r.removeEventListener("error",n)}r.naturalWidth?e(r):r.complete?t(r):(r.addEventListener("load",n),r.addEventListener("error",n))});return o.image=r,o};var u=function(t){return{subscribe:function(e){return{unsubscribe:t({next:e})}}}},n=[];document.addEventListener("mousemove",function(t){return n.forEach(function(e){return e.next([t.clientX,t.clientY])})},!0);var i=u(function(e){return n.push(e),function(){n.splice(n.indexOf(e),1)}}),c=function(e,n){return e.map(function(e,t){return e+n[t]})},a=function(e){return e*Math.PI/180},s=function(e){return t=Math.atan2(e[1],e[0]),n=2*Math.PI,180*((n+t%n)%n)/Math.PI;var t,n},t=[];document.addEventListener("touchmove",function(o){return t.forEach(function(e){for(var t=[0,0],n=0;n<o.touches.length;n++){var r=o.touches[n];t=c(t,[r.clientX,r.clientY])}e.next(t)})},!0);var l=u(function(e){return t.push(e),function(){t.splice(t.indexOf(e),1)}}),d=function(){},r=function(e){var t=e?parseFloat(e):NaN;return isNaN(t)?void 0:t},v=function(e){return{hover:e.getAttribute("data-src-hover")||void 0,looks:function(e){for(var t=/data-src-look-(\d+)/i,n=[],r=0;r<e.attributes.length;r++){var o=e.attributes[r],u=t.exec(o.name);u&&n.push({angle:parseFloat(u[1]),src:o.value})}return n.length?n:void 0}(e),timeToDefault:r(e.getAttribute("data-timetodefault")),throttle:r(e.getAttribute("data-throttle")),fieldOfVision:r(e.getAttribute("data-fieldofvision")),resetOnCancel:"true"===e.getAttribute("data-resetoncancel")}};function h(e){var n,t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},r=function(o){for(var e=1;e<arguments.length;e++){var u=null!=arguments[e]?arguments[e]:{},t=Object.keys(u);"function"==typeof Object.getOwnPropertySymbols&&(t=t.concat(Object.getOwnPropertySymbols(u).filter(function(e){return Object.getOwnPropertyDescriptor(u,e).enumerable}))),t.forEach(function(e){var t,n,r;t=o,r=u[n=e],n in t?Object.defineProperty(t,n,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[n]=r})}return o}({},v(e),t),o=e.src;if(!o)throw new Error("A default URL must be specified");return{fieldOfVision:r.fieldOfVision||150,src:o,hover:r.hover||"",points:r.points||(n=[i,l],u(function(t){var e=n.map(function(e){return e.subscribe(t.next)});return function(){return e.forEach(function(e){return e.unsubscribe()})}})),looks:r.looks||[],timeToDefault:r.timeToDefault||1e3,resetOnCancel:!(!1===r.resetOnCancel),throttle:r.throttle||100,onDebug:r.onDebug||d,onAttach:r.onAttach||d,onDetach:r.onDetach||d}}var m=function(e){var t=e.getBoundingClientRect(),n=t.top+window.pageYOffset+t.height/2,r=t.left+window.pageXOffset+t.width/2;return{y:n,x:r}};var b=function(e,t){return s((o=c([window.scrollX,window.scrollY],t),i=m(e),u=[i.x,i.y],n=o.map(function(e,t){return e-u[t]}),r=90,[n[0]*Math.cos(a(r))-n[1]*Math.sin(a(r)),n[0]*Math.sin(a(r))+n[1]*Math.cos(a(r))]));var n,r,o,u,i},p=function(e){return 180<Math.abs(e)?e-360*((t=e)?t<0?-1:1:0):e;var t},g=function(e,t){return t.slice(0).sort((n=e,function(e,t){return Math.abs(p(e.angle-n))-Math.abs(p(t.angle-n))}))[0];var n},y=function(e,t,n){return t<=e&&e<=n},O=function(e,t){return document.elementFromPoint?document.elementFromPoint(t[0],t[1])===e:(n=e.getBoundingClientRect(),r=[t[0],t[1]],o=n.left,u=n.top,i=n.right,c=n.bottom,y(r[0],o,i)&&y(r[1],u,c));var n,r,o,u,i,c},w=function(h,m){return u(function(r){var e,t,n,o,u,i,c,a,f,s,l,d=(e=function(){return r.next({src:m.src,options:m})},t=m.timeToDefault,(o=function(){n&&clearTimeout(n),n=setTimeout(function(){n=null,e()},t)}).clear=function(){n&&(clearTimeout(n),n=null)},o),v=m.points.subscribe((u=function(e){var t=b(h,e),n=function(e,t,n,r){var o=r.looks,u=r.hover,i=r.fieldOfVision,c=r.src;if(u&&O(e,t))c=u;else{var a=g(n,o);Math.abs(p(a.angle-n))<i/2&&(c=a.src)}return c}(h,e,t,m);r.next({point:e,angle:t,src:n,options:m}),d()},i=m.throttle,f=0,s=function(){f=Date.now(),c=null,u(a)},(l=function(e){var t=Date.now(),n=i-(t-f);a=e,n<=0||i<n?(c&&(clearTimeout(c),c=null),f=t,u(e)):c||(c=setTimeout(s,n))}).clear=function(){c&&clearTimeout(c),f=0,c=null},l));return function(){d.clear(),v.unsubscribe()}})},M=function(t,e){var n,r,o,u=h(t,e),i=function(e){t.src=e},c=(n=t,f((r=u,o=r.looks.map(function(e){return e.src}),r.src&&o.push(r.src),r.hover&&o.push(r.hover),o)).then(function(e){return n.creepyfaceReachableImages=e,function(){delete n.creepyfaceReachableImages}})),a=c.then(function(){return u.onAttach(),w(t,u).subscribe(function(e){i(e.src),u.onDebug(e)})});return function(){return a.then(function(e){return e.unsubscribe(),u.resetOnCancel&&i(u.src),u.onDetach(),c.then(function(e){return e()})})}},o=function e(t,n){e.cancel(t);var r=d,o=function(n,t,r){var o=function(e){return e?t():r()};if(MutationObserver){var u=document.body&&document.body.contains(n);u&&o(!0);var e=new MutationObserver(function(e){var t=document.body&&document.body.contains(n);!t&&u?(o(!1),u=!1):t&&!u&&(o(!0),u=!0)});return e.observe(document,{childList:!0,subtree:!0}),function(){return e.disconnect()}}return document.body&&document.body.contains(n)&&o(!0),n.addEventListener("DOMNodeInserted",function(){return o(!0)},!1),n.addEventListener("DOMNodeRemoved",function(){return o(!1)},!1),d}(t,function(){r=M(t,n)},function(){return e.cancel(t)});return t.creepyfaceCancel=function(){o(),r(),delete t.creepyfaceCancel}};return o.cancel=function(e){var t=e.creepyfaceCancel;t&&t()},document.addEventListener("DOMContentLoaded",function(){for(var e=document.querySelectorAll("img[data-creepy]"),t=0;t<e.length;t++){var n=e[t];n instanceof HTMLImageElement&&o(n)}}),o}); | ||
!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(n=n||self).creepyface=e()}(this,function(){"use strict";var s=function(o,c){var u=[];o.forEach(function(n){var e,t,r;e=n,t=function(n){u.push(n),u.length===o.length&&c(u)},(r=new Image).src=e,r.onload=r.onerror=function(){r.naturalWidth||console.error("Creepyface was unable to load ".concat(e)),delete r.onload,delete r.onerror,t(r)}})};var c=function(e){return{subscribe:function(n){return{unsubscribe:e({next:n})}}}},t=[];document.addEventListener("mousemove",function(e){return t.forEach(function(n){return n.next([e.clientX,e.clientY])})},!0);var u=c(function(n){return t.push(n),function(){t.splice(t.indexOf(n),1)}}),i=function(n,t){return n.map(function(n,e){return n+t[e]})},a=function(n){return n*Math.PI/180},f=function(n){return e=Math.atan2(n[1],n[0]),t=2*Math.PI,180*((t+e%t)%t)/Math.PI;var e,t},e=[];document.addEventListener("touchmove",function(o){return e.forEach(function(n){for(var e=[0,0],t=0;t<o.touches.length;t++){var r=o.touches[t];e=i(e,[r.clientX,r.clientY])}n.next(e)})},!0);var l=c(function(n){return e.push(n),function(){e.splice(e.indexOf(n),1)}}),d=function(){},r=function(n){var e=n?parseFloat(n):NaN;return isNaN(e)?void 0:e},v=function(n){return{hover:n.getAttribute("data-src-hover")||void 0,looks:function(n){for(var e=/data-src-look-(\d+)/i,t=[],r=0;r<n.attributes.length;r++){var o=n.attributes[r],c=e.exec(o.name);c&&t.push({angle:parseFloat(c[1]),src:o.value})}return t.length?t:void 0}(n),timeToDefault:r(n.getAttribute("data-timetodefault")),throttle:r(n.getAttribute("data-throttle")),fieldOfVision:r(n.getAttribute("data-fieldofvision")),resetOnCancel:"true"===n.getAttribute("data-resetoncancel")}};function h(n){var t,e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},r=function(o){for(var n=1;n<arguments.length;n++){var c=null!=arguments[n]?arguments[n]:{},e=Object.keys(c);"function"==typeof Object.getOwnPropertySymbols&&(e=e.concat(Object.getOwnPropertySymbols(c).filter(function(n){return Object.getOwnPropertyDescriptor(c,n).enumerable}))),e.forEach(function(n){var e,t,r;e=o,r=c[t=n],t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r})}return o}({},v(n),e),o=n.src;if(!o)throw new Error("A default URL must be specified");return{fieldOfVision:r.fieldOfVision||150,src:o,hover:r.hover||"",points:r.points||(t=[u,l],c(function(e){var n=t.map(function(n){return n.subscribe(e.next)});return function(){return n.forEach(function(n){return n.unsubscribe()})}})),looks:r.looks||[],timeToDefault:r.timeToDefault||1e3,resetOnCancel:!(!1===r.resetOnCancel),throttle:r.throttle||100,onDebug:r.onDebug||d,onAttach:r.onAttach||d,onDetach:r.onDetach||d}}var b=function(n){var e=n.getBoundingClientRect(),t=e.top+window.pageYOffset+e.height/2,r=e.left+window.pageXOffset+e.width/2;return{y:t,x:r}};var m=function(n,e){return f((o=i([window.scrollX,window.scrollY],e),u=b(n),c=[u.x,u.y],t=o.map(function(n,e){return n-c[e]}),r=90,[t[0]*Math.cos(a(r))-t[1]*Math.sin(a(r)),t[0]*Math.sin(a(r))+t[1]*Math.cos(a(r))]));var t,r,o,c,u},p=function(n){return 180<Math.abs(n)?n-360*((e=n)?e<0?-1:1:0):n;var e},g=function(n,e){return e.slice(0).sort((t=n,function(n,e){return Math.abs(p(n.angle-t))-Math.abs(p(e.angle-t))}))[0];var t},y=function(n,e,t){return e<=n&&n<=t},O=function(n,e){return document.elementFromPoint?document.elementFromPoint(e[0],e[1])===n:(t=n.getBoundingClientRect(),r=[e[0],e[1]],o=t.left,c=t.top,u=t.right,i=t.bottom,y(r[0],o,u)&&y(r[1],c,i));var t,r,o,c,u,i},w=function(h,b){return c(function(r){var n,e,t,o,c,u,i,a,f,s,l,d=(n=function(){return r.next({src:b.src,options:b})},e=b.timeToDefault,(o=function(){t&&clearTimeout(t),t=setTimeout(function(){t=null,n()},e)}).clear=function(){t&&(clearTimeout(t),t=null)},o),v=b.points.subscribe((c=function(n){var e=m(h,n),t=function(n,e,t,r){var o=r.looks,c=r.hover,u=r.fieldOfVision,i=r.src;if(c&&O(n,e))i=c;else{var a=g(t,o);Math.abs(p(a.angle-t))<u/2&&(i=a.src)}return i}(h,n,e,b);r.next({point:n,angle:e,src:t,options:b}),d()},u=b.throttle,f=0,s=function(){f=Date.now(),i=null,c(a)},(l=function(n){var e=Date.now(),t=u-(e-f);a=n,t<=0||u<t?(i&&(clearTimeout(i),i=null),f=e,c(n)):i||(i=setTimeout(s,t))}).clear=function(){i&&clearTimeout(i),f=0,i=null},l));return function(){d.clear(),v.unsubscribe()}})},M=function(t,n){var e,r,o,c,u=h(t,n),i=function(n){t.src=n},a=!1,f=function(){a=!0};return e=t,r=function(n){if(!a){u.onAttach();var e=w(t,u).subscribe(function(n){i(n.src),u.onDebug(n)});f=function(){e.unsubscribe(),u.resetOnCancel&&i(u.src),u.onDetach(),n()}}},s((c=(o=u).looks.map(function(n){return n.src}),o.src&&c.push(o.src),o.hover&&c.push(o.hover),c),function(n){e.creepyfaceReachableImages=n,r(function(){delete e.creepyfaceReachableImages})}),function(){return f()}},o=function n(e,t){n.cancel(e);var r=d,o=function(t,e,r){var o=function(n){return n?e():r()};if(MutationObserver){var c=document.body&&document.body.contains(t);c&&o(!0);var n=new MutationObserver(function(n){var e=document.body&&document.body.contains(t);!e&&c?(o(!1),c=!1):e&&!c&&(o(!0),c=!0)});return n.observe(document,{childList:!0,subtree:!0}),function(){return n.disconnect()}}return document.body&&document.body.contains(t)&&o(!0),t.addEventListener("DOMNodeInserted",function(){return o(!0)},!1),t.addEventListener("DOMNodeRemoved",function(){return o(!1)},!1),d}(e,function(){r=M(e,t)},function(){return n.cancel(e)});return e.creepyfaceCancel=function(){o(),r(),delete e.creepyfaceCancel}};return o.cancel=function(n){var e=n.creepyfaceCancel;e&&e()},document.addEventListener("DOMContentLoaded",function(){for(var n=document.querySelectorAll("img[data-creepy]"),e=0;e<n.length;e++){var t=n[e];t instanceof HTMLImageElement&&o(t)}}),o}); |
@@ -9,17 +9,23 @@ import preload from './preload'; | ||
}; | ||
const preloaded = preload(img, options); | ||
const subscribed = preloaded.then(() => { | ||
let cancelled = false; | ||
let cancel = () => { | ||
cancelled = true; | ||
}; | ||
preload(img, options, unload => { | ||
if (cancelled) | ||
return; | ||
options.onAttach(); | ||
return creepy(img, options).subscribe(data => { | ||
const subscription = creepy(img, options).subscribe(data => { | ||
setSrc(data.src); | ||
options.onDebug(data); | ||
}); | ||
cancel = () => { | ||
subscription.unsubscribe(); | ||
if (options.resetOnCancel) | ||
setSrc(options.src); | ||
options.onDetach(); | ||
unload(); | ||
}; | ||
}); | ||
return () => subscribed.then(subscription => { | ||
subscription.unsubscribe(); | ||
if (options.resetOnCancel) | ||
setSrc(options.src); | ||
options.onDetach(); | ||
return preloaded.then(unload => unload()); | ||
}); | ||
return () => cancel(); | ||
}; |
import { Options } from './options'; | ||
import { CreepyImage, Cancel } from './types'; | ||
export default function preload(img: CreepyImage, options: Options): Promise<Cancel>; | ||
export default function preload(img: CreepyImage, options: Options, callback: (cancel: Cancel) => void): void; |
@@ -1,2 +0,1 @@ | ||
import loadImages from 'image-promise'; | ||
const getSrcs = (options) => { | ||
@@ -10,9 +9,31 @@ const srcs = options.looks.map(({ src }) => src); | ||
}; | ||
export default function preload(img, options) { | ||
return loadImages(getSrcs(options)).then(imgs => { | ||
const loadImage = (src, callback) => { | ||
const img = new Image(); | ||
img.src = src; | ||
img.onload = img.onerror = () => { | ||
if (!img.naturalWidth) { | ||
console.error(`Creepyface was unable to load ${src}`); | ||
} | ||
delete img.onload; | ||
delete img.onerror; | ||
callback(img); | ||
}; | ||
}; | ||
const loadImages = (srcs, callback) => { | ||
const imgs = []; | ||
srcs.forEach(src => { | ||
loadImage(src, img => { | ||
imgs.push(img); | ||
if (imgs.length === srcs.length) | ||
callback(imgs); | ||
}); | ||
}); | ||
}; | ||
export default function preload(img, options, callback) { | ||
loadImages(getSrcs(options), imgs => { | ||
img.creepyfaceReachableImages = imgs; | ||
return () => { | ||
callback(() => { | ||
delete img.creepyfaceReachableImages; | ||
}; | ||
}); | ||
}); | ||
} |
{ | ||
"name": "creepyface", | ||
"version": "4.1.6", | ||
"version": "4.1.7", | ||
"keywords": [ | ||
@@ -57,4 +57,3 @@ "face", | ||
"dependencies": { | ||
"get-element-center": "^1.1.0", | ||
"image-promise": "^5.0.1" | ||
"get-element-center": "^1.1.0" | ||
}, | ||
@@ -61,0 +60,0 @@ "lint-staged": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28497
1
471
- Removedimage-promise@^5.0.1
- Removedimage-promise@5.0.1(transitive)