creepyface
Advanced tools
Comparing version 7.1.0-beta.1 to 7.1.0
@@ -9,3 +9,3 @@ import getOptions from './util/options'; | ||
const options = getOptions(img, userOptions); | ||
return (img.__creepyfaceCancel = preload(img, options, unload => { | ||
return (img.__creepyfaceCancel = preload(img, options, () => { | ||
const update = (src, point, angle) => { | ||
@@ -32,3 +32,2 @@ img.src = src; | ||
delete img.__creepyfaceCancel; | ||
unload(); | ||
options.onDetach(); | ||
@@ -35,0 +34,0 @@ }; |
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).creepyface=e()}(this,function(){"use strict";var i=function(o){return 1===o.length?o[0]:function(e,n){var t=o.map(function(t){return t(e,n)});return function(){return t.forEach(function(t){return t()})}}},t=function(n){var o,r=[];return function(t,e){return r.push(t),1===r.length&&(o=n(function(e){return r.forEach(function(t){return t(e)})},e)),function(){r.splice(r.indexOf(t),1),0===r.length&&o&&o()}}},e=t(function(e){var t=function(t){return e([t.clientX,t.clientY])};return window.addEventListener("mousemove",t,!0),function(){return window.removeEventListener("mousemove",t,!0)}}),s=function(t,e){return[t[0]+e[0],t[1]+e[1]]},d=function(t){return t*Math.PI/180},h=function(t){return e=Math.atan2(t[1],t[0]),n=2*Math.PI,180*((n+e%n)%n)/Math.PI;var e,n},n=t(function(r){var t=function(t){for(var e=[0,0],n=0;n<t.touches.length;n++){var o=t.touches[n];e=s(e,[o.clientX,o.clientY])}r(e)};return window.addEventListener("touchmove",t,!0),function(){return window.removeEventListener("touchmove",t,!0)}}),o={mouse:e,finger:n,pointer:i([e,n])},c=function(t){return o[t]?o[t]:(console.error("No point provider registered as '".concat(t,"', defaulting to pointer.")),o.pointer)},r=function(t){var e=t?parseFloat(t):NaN;return isNaN(e)?void 0:e},u=function(t){return{hover:t.getAttribute("data-src-hover")||void 0,looks:function(t){for(var e=/data-src-look-(\d+)/i,n=[],o=0;o<t.attributes.length;o++){var r=t.attributes[o],i=e.exec(r.name);i&&n.push({angle:parseFloat(i[1]),src:r.value})}return n.length?n:void 0}(t),points:t.getAttribute("data-points")||void 0,timeToDefault:r(t.getAttribute("data-timetodefault")),throttle:r(t.getAttribute("data-throttle"))}},a=function(){};function f(t){var e,n=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},o=function(r){for(var t=1;t<arguments.length;t++){var i=null!=arguments[t]?arguments[t]:{},e=Object.keys(i);"function"==typeof Object.getOwnPropertySymbols&&(e=e.concat(Object.getOwnPropertySymbols(i).filter(function(t){return Object.getOwnPropertyDescriptor(i,t).enumerable}))),e.forEach(function(t){var e,n,o;e=r,o=i[n=t],n in e?Object.defineProperty(e,n,{value:o,enumerable:!0,configurable:!0,writable:!0}):e[n]=o})}return r}({},u(t),n),r=t.getAttribute("src");if(!r)throw new Error("A default URL must be specified");return{src:r,hover:o.hover||"",pointProvider:(e=o,"function"==typeof e.points?e.points:i((e.points||"pointer").split(",").map(c))),looks:o.looks||[],timeToDefault:o.timeToDefault||1e3,throttle:o.throttle||100,onDebug:o.onDebug||a,onAttach:o.onAttach||a,onDetach:o.onDetach||a}}var l=function(r,i){var c=[];r.forEach(function(t){var e,n,o;e=t,n=function(t){c.push(t),c.length===r.length&&i(c)},(o=new Image).src=e,o.onload=o.onerror=function(){o.naturalWidth||console.error("Creepyface was unable to load ".concat(e)),delete o.onload,delete o.onerror,n(o)}})};function v(r,i,c,u){var a,f=!1,l=0;function s(){a&&clearTimeout(a)}function t(){var t=this,e=Date.now()-l,n=arguments;function o(){l=Date.now(),c.apply(t,n)}f||(u&&!a&&o(),s(),void 0===u&&r<e?o():!0!==i&&(a=setTimeout(u?function(){a=void 0}:o,void 0===u?r-e:r)))}return"boolean"!=typeof i&&(u=c,c=i,i=void 0),t.cancel=function(){s(),f=!0},t}var p=function(t,e){return h((r=s([window.scrollX,window.scrollY],e),c=t.getBoundingClientRect(),u=c.left,a=c.top,f=c.width,l=c.height,i=[u+window.pageXOffset+f/2,a+window.pageYOffset+l/2],n=[r[0]-i[0],r[1]-i[1]],o=90,[n[0]*Math.cos(d(o))-n[1]*Math.sin(d(o)),n[0]*Math.sin(d(o))+n[1]*Math.cos(d(o))]));var n,o,r,i,c,u,a,f,l},g=function(t){return 180<Math.abs(t)?t-360*(t<0?-1:1):t},m=function(t,e){return e.slice(0).sort((n=t,function(t,e){return Math.abs(g(t.angle-n))-Math.abs(g(e.angle-n))}))[0];var n},b=function(t,e,n){return e<=t&&t<=n},w=function(t,e){return document.elementFromPoint?document.elementFromPoint(e[0],e[1])===t:(n=t.getBoundingClientRect(),o=[e[0],e[1]],r=n.left,i=n.top,c=n.right,u=n.bottom,b(o[0],r,c)&&b(o[1],i,u));var n,o,r,i,c,u},y=function(s,t){var e,n,o,r,i,c,d=f(s,t);return s.__creepyfaceCancel=(e=s,i=!(n=function(t){var e,n,o,f=function(t,e,n){s.src=t,d.onDebug({src:t,point:e,angle:n,options:d})},l=(e=d.timeToDefault,n=function(){return f(d.src)},void 0===o?v(e,n,!1):v(e,o,!1!==n)),r=v(d.throttle,function(t){if(!t)return f(d.src);var e,n,o,r,i,c,u=p(s,t),a=(e=s,n=t,o=u,i=(r=d).looks,(c=r.hover)&&w(e,n)?c:m(o,i).src);f(a,t,u),l()}),i=d.pointProvider(r,s);return d.onAttach(),function(){l.cancel(),r.cancel(),i(),s.src=d.src,delete s.__creepyfaceCancel,t(),d.onDetach()}}),c=function(){i=!0},l(((r=(o=d).looks.map(function(t){return t.src})).push(o.src),o.hover&&r.push(o.hover),r),function(t){e.__creepyfaceReachableImages=t,c=n(function(){delete e.__creepyfaceReachableImages}),(i||t.some(function(t){return!t.naturalWidth}))&&c()}),function(){return c()})};return y.cancel=function(t){var e=t.__creepyfaceCancel;e&&e()},y.registerPointProvider=function(t,e){o[t]=e},document.addEventListener("DOMContentLoaded",function(){for(var t=document.querySelectorAll("img[data-creepy],img[data-creepyface]"),e=0;e<t.length;e++)y(t[e])}),y}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).creepyface=e()}(this,function(){"use strict";var i=function(o){return 1===o.length?o[0]:function(e,n){var t=o.map(function(t){return t(e,n)});return function(){return t.forEach(function(t){return t()})}}},t=function(n){var o,r=[];return function(t,e){return r.push(t),1===r.length&&(o=n(function(e){return r.forEach(function(t){return t(e)})},e)),function(){r.splice(r.indexOf(t),1),0===r.length&&o&&o()}}},e=t(function(e){var t=function(t){return e([t.clientX,t.clientY])};return window.addEventListener("mousemove",t,!0),function(){return window.removeEventListener("mousemove",t,!0)}}),s=function(t,e){return[t[0]+e[0],t[1]+e[1]]},d=function(t){return t*Math.PI/180},h=function(t){return e=Math.atan2(t[1],t[0]),n=2*Math.PI,180*((n+e%n)%n)/Math.PI;var e,n},n=t(function(r){var t=function(t){for(var e=[0,0],n=0;n<t.touches.length;n++){var o=t.touches[n];e=s(e,[o.clientX,o.clientY])}r(e)};return window.addEventListener("touchmove",t,!0),function(){return window.removeEventListener("touchmove",t,!0)}}),o={mouse:e,finger:n,pointer:i([e,n])},c=function(t){return o[t]?o[t]:(console.error("No point provider registered as '".concat(t,"', defaulting to pointer.")),o.pointer)},r=function(t){var e=t?parseFloat(t):NaN;return isNaN(e)?void 0:e},u=function(t){return{hover:t.getAttribute("data-src-hover")||void 0,looks:function(t){for(var e=/data-src-look-(\d+)/i,n=[],o=0;o<t.attributes.length;o++){var r=t.attributes[o],i=e.exec(r.name);i&&n.push({angle:parseFloat(i[1]),src:r.value})}return n.length?n:void 0}(t),points:t.getAttribute("data-points")||void 0,timeToDefault:r(t.getAttribute("data-timetodefault")),throttle:r(t.getAttribute("data-throttle"))}},a=function(){};function f(t){var e,n=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},o=function(r){for(var t=1;t<arguments.length;t++){var i=null!=arguments[t]?arguments[t]:{},e=Object.keys(i);"function"==typeof Object.getOwnPropertySymbols&&(e=e.concat(Object.getOwnPropertySymbols(i).filter(function(t){return Object.getOwnPropertyDescriptor(i,t).enumerable}))),e.forEach(function(t){var e,n,o;e=r,o=i[n=t],n in e?Object.defineProperty(e,n,{value:o,enumerable:!0,configurable:!0,writable:!0}):e[n]=o})}return r}({},u(t),n),r=t.getAttribute("src");if(!r)throw new Error("A default URL must be specified");return{src:r,hover:o.hover||"",pointProvider:(e=o,"function"==typeof e.points?e.points:i((e.points||"pointer").split(",").map(c))),looks:o.looks||[],timeToDefault:o.timeToDefault||1e3,throttle:o.throttle||100,onDebug:o.onDebug||a,onAttach:o.onAttach||a,onDetach:o.onDetach||a}}var l=function(r,i){var c=[];r.forEach(function(t){var e,n,o;e=t,n=function(t){c.push(t),c.length===r.length&&i(c)},(o=new Image).src=e,o.onload=o.onerror=function(){o.naturalWidth||console.error("Creepyface was unable to load ".concat(e)),delete o.onload,delete o.onerror,n(o)}})};function v(r,i,c,u){var a,f=!1,l=0;function s(){a&&clearTimeout(a)}function t(){var t=this,e=Date.now()-l,n=arguments;function o(){l=Date.now(),c.apply(t,n)}f||(u&&!a&&o(),s(),void 0===u&&r<e?o():!0!==i&&(a=setTimeout(u?function(){a=void 0}:o,void 0===u?r-e:r)))}return"boolean"!=typeof i&&(u=c,c=i,i=void 0),t.cancel=function(){s(),f=!0},t}var p=function(t,e){return h((r=s([window.scrollX,window.scrollY],e),c=t.getBoundingClientRect(),u=c.left,a=c.top,f=c.width,l=c.height,i=[u+window.pageXOffset+f/2,a+window.pageYOffset+l/2],n=[r[0]-i[0],r[1]-i[1]],o=90,[n[0]*Math.cos(d(o))-n[1]*Math.sin(d(o)),n[0]*Math.sin(d(o))+n[1]*Math.cos(d(o))]));var n,o,r,i,c,u,a,f,l},g=function(t){return 180<Math.abs(t)?t-360*(t<0?-1:1):t},m=function(t,e){return e.slice(0).sort((n=t,function(t,e){return Math.abs(g(t.angle-n))-Math.abs(g(e.angle-n))}))[0];var n},b=function(t,e,n){return e<=t&&t<=n},w=function(t,e){return document.elementFromPoint?document.elementFromPoint(e[0],e[1])===t:(n=t.getBoundingClientRect(),o=[e[0],e[1]],r=n.left,i=n.top,c=n.right,u=n.bottom,b(o[0],r,c)&&b(o[1],i,u));var n,o,r,i,c,u},y=function(s,t){var n,o,e,r,i,c,d=f(s,t);return s.__creepyfaceCancel=(n=s,i=!(o=function(){var t,e,n,f=function(t,e,n){s.src=t,d.onDebug({src:t,point:e,angle:n,options:d})},l=(t=d.timeToDefault,e=function(){return f(d.src)},void 0===n?v(t,e,!1):v(t,n,!1!==e)),o=v(d.throttle,function(t){if(!t)return f(d.src);var e,n,o,r,i,c,u=p(s,t),a=(e=s,n=t,o=u,i=(r=d).looks,(c=r.hover)&&w(e,n)?c:m(o,i).src);f(a,t,u),l()}),r=d.pointProvider(o,s);return d.onAttach(),function(){l.cancel(),o.cancel(),r(),s.src=d.src,delete s.__creepyfaceCancel,d.onDetach()}}),c=function(){i=!0},l(((r=(e=d).looks.map(function(t){return t.src})).push(e.src),e.hover&&r.push(e.hover),r),function(t){n.__creepyfaceReachableImages=t;var e=o();c=function(){e(),delete n.__creepyfaceReachableImages},(i||t.some(function(t){return!t.naturalWidth}))&&c()}),function(){return c()})};return y.cancel=function(t){var e=t.__creepyfaceCancel;e&&e()},y.registerPointProvider=function(t,e){o[t]=e},document.addEventListener("DOMContentLoaded",function(){for(var t=document.querySelectorAll("img[data-creepy],img[data-creepyface]"),e=0;e<t.length;e++)y(t[e])}),y}); |
import { Options } from './options'; | ||
import { Cancel } from '../types'; | ||
export default function preload(img: HTMLImageElement, options: Options, callback: (unload: Cancel) => Cancel): Cancel; | ||
export default function preload(img: HTMLImageElement, options: Options, callback: () => Cancel): Cancel; |
@@ -37,5 +37,7 @@ const getSrcs = (options) => { | ||
img.__creepyfaceReachableImages = imgs; | ||
cancel = callback(() => { | ||
const cancelCallback = callback(); | ||
cancel = () => { | ||
cancelCallback(); | ||
delete img.__creepyfaceReachableImages; | ||
}); | ||
}; | ||
if (cancelled || imgs.some(img => !img.naturalWidth)) { | ||
@@ -42,0 +44,0 @@ cancel(); |
{ | ||
"name": "creepyface", | ||
"version": "7.1.0-beta.1", | ||
"version": "7.1.0", | ||
"keywords": [ | ||
@@ -5,0 +5,0 @@ "face", |
@@ -55,2 +55,13 @@ # [Creepyface](https://creepyface.io) · [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/4lejandrito/creepyface/blob/master/LICENSE) [![npm version](https://img.shields.io/npm/v/creepyface.svg?style=flat)](https://www.npmjs.com/package/creepyface) [![Build Status](https://api.travis-ci.org/4lejandrito/creepyface.svg?branch=master)](https://travis-ci.org/4lejandrito/creepyface) [![Coverage Status](https://coveralls.io/repos/github/4lejandrito/creepyface/badge.svg?branch=master)](https://coveralls.io/github/4lejandrito/creepyface?branch=master) | ||
### Full list of data attributes | ||
| Name | Description | | ||
| ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| `data-creepyface` | Add this to automatically attach creepyface to your image when the page loads. | | ||
| `data-src-hover` | The URL of the image to use when the pointer is over your image. | | ||
| `data-src-look-<angle>` | The URL of the image to use when the pointer forms the specified angle (in degrees) with the center of your image. Add as many as you want. | | ||
| `data-timetodefault` | The amount of time (in milliseconds) after which the default src is restored if no pointer events are received. 1 second by default. | | ||
| `data-throttle` | The amount of time (in milliseconds) to wait between src changes. 100 by default. | | ||
| `data-points` | Optionally, a comma-separated list of point provider names to make your face look at things other than the pointer. See [Super advanced usage](#super-advanced-usage) for more information. | | ||
## Advanced usage | ||
@@ -91,3 +102,3 @@ | ||
Creepyface will look at the pointer by default, however custom point providers can be defined (see [firefly](https://github.com/4lejandrito/creepyface-firefly) for a real world implementation). | ||
Creepyface will look at the pointer by default, however custom point providers can be defined. | ||
@@ -114,3 +125,3 @@ For example, to make your face look at a random point every half a second (see [codepen](https://codepen.io/4lejandrito/pen/ZEYJLrN)) you need to register a [point provider](https://github.com/4lejandrito/creepyface/blob/master/src/types.ts#L4-L7): | ||
and consume it from the data-attribute API using the `data-points` attribute: | ||
and consume it using the `data-points` attribute: | ||
@@ -170,2 +181,4 @@ ```html | ||
**Note:** several point providers can work at the same time by using a comma-separated string like `"random,pointer"`. | ||
The following point providers are available out of the box: | ||
@@ -177,2 +190,6 @@ | ||
The are also external point providers: | ||
- [firefly](https://github.com/4lejandrito/creepyface-firefly) to follow a moving firefly on the screen. | ||
## Developing | ||
@@ -179,0 +196,0 @@ |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
29046
357
1
209