Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

creepyface

Package Overview
Dependencies
Maintainers
1
Versions
83
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

creepyface - npm Package Compare versions

Comparing version 6.0.1 to 7.0.0-beta.1

dist/providers/combined.d.ts

8

dist/creepyface.d.ts
import { UserOptions } from './util/options';
import { Cancel, Observable } from './util/types';
import { Point } from './util/algebra';
import { Cancel } from './types';
declare const creepyface: {
(img: HTMLImageElement, options?: UserOptions | undefined): Cancel;
(img: HTMLImageElement, userOptions?: UserOptions | undefined): Cancel;
cancel(img: HTMLImageElement): void;
registerObservable(name: string, observable: Observable<Point>): void;
registerPointProvider: (name: string, provider: import("./types").PointProvider) => void;
};
export default creepyface;
export { PointProvider, Consumer, Point } from './types';

@@ -1,25 +0,43 @@

import attach from './util/attach';
import watchElement from './util/watch-element';
import noop from './util/noop';
import * as observableStore from './observables/util/store';
const creepyface = (img, options) => {
creepyface.cancel(img);
let detach = noop;
const stopWatching = watchElement(img, () => {
detach = attach(img, options);
}, () => creepyface.cancel(img));
return (img.creepyfaceCancel = () => {
stopWatching();
detach();
delete img.creepyfaceCancel;
import getOptions from './util/options';
import { register as registerPointProvider } from './providers/store';
import preload from './util/preload';
import { throttle, debounce } from 'throttle-debounce';
import getAngle from './util/get-angle';
import getSrc from './util/get-src';
const creepyface = (img, userOptions) => {
const options = getOptions(img, userOptions);
const cancel = preload(img, options, unload => {
const update = (src, point, angle) => {
img.src = src;
options.onDebug({ src, point, angle, options });
};
const backToDefault = debounce(options.timeToDefault, () => update(options.src));
const pointConsumer = throttle(options.throttle, point => {
const angle = getAngle(img, point);
const src = getSrc(img, point, angle, options);
update(src, point, angle);
backToDefault();
});
const stopPointProvider = options.pointProvider(pointConsumer, img);
options.onAttach();
return () => {
backToDefault.cancel();
pointConsumer.cancel();
stopPointProvider();
img.src = options.src;
unload();
options.onDetach();
};
});
return (img.__creepyfaceCancel = () => {
cancel();
delete img.__creepyfaceCancel;
});
};
creepyface.cancel = (img) => {
const cancel = img.creepyfaceCancel;
const cancel = img.__creepyfaceCancel;
if (cancel)
cancel();
};
creepyface.registerObservable = (name, observable) => {
observableStore.register(name, observable);
};
creepyface.registerPointProvider = registerPointProvider;
document.addEventListener('DOMContentLoaded', () => {

@@ -26,0 +44,0 @@ const elements = document.querySelectorAll('img[data-creepy],img[data-creepyface]');

@@ -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 l=function(o,i){var c=[];o.forEach(function(t){var e,n,r;e=t,n=function(t){c.push(t),c.length===o.length&&i(c)},(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,n(r)}})};var c=function(){},n=[];document.addEventListener("mousemove",function(e){return n.forEach(function(t){return t([e.clientX,e.clientY])})},!0);var u=function(t,e){return[t[0]+e[0],t[1]+e[1]]},a=function(t){return t*Math.PI/180},f=function(t){return e=Math.atan2(t[1],t[0]),n=2*Math.PI,180*((n+e%n)%n)/Math.PI;var e,n},e=[];document.addEventListener("touchmove",function(o){return e.forEach(function(t){for(var e=[0,0],n=0;n<o.touches.length;n++){var r=o.touches[n];e=u(e,[r.clientX,r.clientY])}t(e)})},!0);var r,o={pointer:(r=[function(t){return n.push(t),function(){n.splice(n.indexOf(t),1)}},function(t){return e.push(t),function(){e.splice(e.indexOf(t),1)}}],function(e){var t=r.map(function(t){return t(e)});return function(){return t.forEach(function(t){return t()})}})},i=function(t){var e=t?parseFloat(t):NaN;return isNaN(e)?void 0:e},s=function(t){return{hover:t.getAttribute("data-src-hover")||void 0,looks:function(t){for(var e=/data-src-look-(\d+)/i,n=[],r=0;r<t.attributes.length;r++){var o=t.attributes[r],i=e.exec(o.name);i&&n.push({angle:parseFloat(i[1]),src:o.value})}return n.length?n:void 0}(t),points:t.getAttribute("data-points")||void 0,timeToDefault:i(t.getAttribute("data-timetodefault")),throttle:i(t.getAttribute("data-throttle")),fieldOfVision:i(t.getAttribute("data-fieldofvision"))}},d=function(t){return"function"==typeof t.points?t.points:(e=t.points||"pointer",o[e]?o[e]:(console.error("No observable registered as '".concat(e,"', defaulting to pointer.")),o.pointer));var e};function v(t){var e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},n=function(o){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,r;e=o,r=i[n=t],n in e?Object.defineProperty(e,n,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[n]=r})}return o}({},s(t),e),r=t.getAttribute("src");if(!r)throw new Error("A default URL must be specified");return{fieldOfVision:n.fieldOfVision||150,src:r,hover:n.hover||"",points:d(n),looks:n.looks||[],timeToDefault:n.timeToDefault||1e3,throttle:n.throttle||100,onDebug:n.onDebug||c,onAttach:n.onAttach||c,onDetach:n.onDetach||c}}var h=function(t){var e=t.getBoundingClientRect(),n=e.top+window.pageYOffset+e.height/2,r=e.left+window.pageXOffset+e.width/2;return{y:n,x:r}};var m=function(t,e){return f((o=u([window.scrollX,window.scrollY],e),c=h(t),i=[c.x,c.y],n=[o[0]-i[0],o[1]-i[1]],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,i,c},b=function(t){return 180<Math.abs(t)?t-360*((e=t)?e<0?-1:1:0):t;var e},g=function(t,e){return e.slice(0).sort((n=t,function(t,e){return Math.abs(b(t.angle-n))-Math.abs(b(e.angle-n))}))[0];var n},p=function(t,e,n){return e<=t&&t<=n},y=function(t,e){return document.elementFromPoint?document.elementFromPoint(e[0],e[1])===t:(n=t.getBoundingClientRect(),r=[e[0],e[1]],o=n.left,i=n.top,c=n.right,u=n.bottom,p(r[0],o,c)&&p(r[1],i,u));var n,r,o,i,c,u},O=function(h,p){return function(r){var t,e,n,o,i,c,u,a,f,l,s,d=(t=function(){return r({src:p.src,options:p})},e=p.timeToDefault,(o=function(){n&&clearTimeout(n),n=setTimeout(function(){n=null,t()},e)}).clear=function(){n&&(clearTimeout(n),n=null)},o),v=p.points((i=function(t){var e=m(h,t),n=function(t,e,n,r){var o=r.looks,i=r.hover,c=r.fieldOfVision,u=r.src;if(i&&y(t,e))u=i;else{var a=g(n,o);Math.abs(b(a.angle-n))<c/2&&(u=a.src)}return u}(h,t,e,p);r({point:t,angle:e,src:n,options:p}),d()},c=p.throttle,f=0,l=function(){f=Date.now(),u=null,i(a)},(s=function(t){var e=Date.now(),n=c-(e-f);a=t,n<=0||c<n?(u&&(clearTimeout(u),u=null),f=e,i(t)):u||(u=setTimeout(l,n))}).clear=function(){u&&clearTimeout(u),f=0,u=null},s));return function(){d.clear(),v()}}},w=function(n,t){var e,r,o,i,c=v(n,t),u=function(t){n.src=t},a=!1,f=function(){a=!0};return e=n,r=function(t){if(!a){c.onAttach();var e=O(n,c)(function(t){u(t.src),c.onDebug(t)});f=function(){e(),u(c.src),c.onDetach(),t()}}},l((i=(o=c).looks.map(function(t){return t.src}),o.src&&i.push(o.src),o.hover&&i.push(o.hover),i),function(t){e.creepyfaceReachableImages=t,r(function(){delete e.creepyfaceReachableImages})}),function(){return f()}},M=function t(e,n){t.cancel(e);var r=c,o=function(n,e,r){var o=function(t){return t?e():r()};if(MutationObserver){var i=document.body.contains(n);i&&o(!0);var t=new MutationObserver(function(t){var e=document.body.contains(n);!e&&i?(o(!1),i=!1):e&&!i&&(o(!0),i=!0)});return t.observe(document,{childList:!0,subtree:!0}),function(){return t.disconnect()}}return document.body.contains(n)&&o(!0),n.addEventListener("DOMNodeInserted",function(){return o(!0)},!1),n.addEventListener("DOMNodeRemoved",function(){return o(!1)},!1),c}(e,function(){r=w(e,n)},function(){return t.cancel(e)});return e.creepyfaceCancel=function(){o(),r(),delete e.creepyfaceCancel}};return M.cancel=function(t){var e=t.creepyfaceCancel;e&&e()},M.registerObservable=function(t,e){var n;n=e,o[t]=n},document.addEventListener("DOMContentLoaded",function(){for(var t=document.querySelectorAll("img[data-creepy],img[data-creepyface]"),e=0;e<t.length;e++){var n=t[e];n instanceof HTMLImageElement&&M(n)}}),M});
!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 o,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},v=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)}}),r={mouse:e,finger:n,pointer:(o=[e,n],function(e,n){var t=o.map(function(t){return t(e,n)});return function(){return t.forEach(function(t){return t()})}})},i=function(t){var e=t?parseFloat(t):NaN;return isNaN(e)?void 0:e},c=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:i(t.getAttribute("data-timetodefault")),throttle:i(t.getAttribute("data-throttle"))}},u=function(t){return"function"==typeof t.points?t.points:(e=t.points||"pointer",r[e]?r[e]:(console.error("No point provider registered as '".concat(e,"', defaulting to pointer.")),r.pointer));var e},a=function(){};function f(t){var e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},n=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}({},c(t),e),o=t.getAttribute("src");if(!o)throw new Error("A default URL must be specified");return{src:o,hover:n.hover||"",pointProvider:u(n),looks:n.looks||[],timeToDefault:n.timeToDefault||1e3,throttle:n.throttle||100,onDebug:n.onDebug||a,onAttach:n.onAttach||a,onDetach:n.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 h(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 v((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*((e=t)?e<0?-1:1:0):t;var e},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),u=(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?h(e,n,!1):h(e,o,!1!==n)),r=h(d.throttle,function(t){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,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){i||(e.__creepyfaceReachableImages=t,c=n(function(){delete e.__creepyfaceReachableImages}))}),function(){return c()});return s.__creepyfaceCancel=function(){u(),delete s.__creepyfaceCancel}};return y.cancel=function(t){var e=t.__creepyfaceCancel;e&&e()},y.registerPointProvider=function(t,e){r[t]=e},document.addEventListener("DOMContentLoaded",function(){for(var t=document.querySelectorAll("img[data-creepy],img[data-creepyface]"),e=0;e<t.length;e++){var n=t[e];n instanceof HTMLImageElement&&y(n)}}),y});

@@ -1,2 +0,2 @@

export declare type Point = [number, number];
import { Point } from '../types';
export declare type Degrees = number;

@@ -3,0 +3,0 @@ export declare type Radians = number;

@@ -1,3 +0,3 @@

import { Observable } from './types';
import { Angle, Point } from './algebra';
import { PointProvider, Point, Consumer } from '../types';
import { Angle } from './algebra';
export declare type Millis = number;

@@ -16,27 +16,23 @@ export declare type Time = Millis;

};
export declare type Debug = (data: CreepyData) => void;
export declare type EventListener = () => void;
export declare type Options = {
fieldOfVision: Angle;
src: ImageURL;
hover?: ImageURL;
looks: Array<Look>;
points: Observable<Point>;
pointProvider: PointProvider;
timeToDefault: Time;
throttle: Time;
onDebug: Debug;
onAttach: EventListener;
onDetach: EventListener;
onDebug: Consumer<CreepyData>;
onAttach: Consumer<void>;
onDetach: Consumer<void>;
};
export declare type UserOptions = {
fieldOfVision?: Angle;
hover?: ImageURL;
looks?: Array<Look>;
points?: Observable<Point> | string;
points?: PointProvider | string;
timeToDefault?: Time;
throttle?: Time;
onDebug?: Debug;
onAttach?: EventListener;
onDetach?: EventListener;
onDebug?: Consumer<CreepyData>;
onAttach?: Consumer<void>;
onDetach?: Consumer<void>;
};
export default function getOptions(img: HTMLImageElement, options?: UserOptions): Options;

@@ -1,3 +0,2 @@

import noop from './noop';
import * as observableStore from '../observables/util/store';
import { retrieve as retrievePointProvider } from '../providers/store';
const getLooks = (img) => {

@@ -24,4 +23,3 @@ const regex = /data-src-look-(\d+)/i;

timeToDefault: getFloat(img.getAttribute('data-timetodefault')),
throttle: getFloat(img.getAttribute('data-throttle')),
fieldOfVision: getFloat(img.getAttribute('data-fieldofvision'))
throttle: getFloat(img.getAttribute('data-throttle'))
});

@@ -32,4 +30,5 @@ const getPoints = (userOptions) => {

}
return observableStore.retrieve(userOptions.points || 'pointer');
return retrievePointProvider(userOptions.points || 'pointer');
};
const noop = () => undefined;
export default function getOptions(img, options = {}) {

@@ -41,6 +40,5 @@ const userOptions = { ...fromImage(img), ...options };

return {
fieldOfVision: userOptions.fieldOfVision || 150,
src,
hover: userOptions.hover || '',
points: getPoints(userOptions),
pointProvider: getPoints(userOptions),
looks: userOptions.looks || [],

@@ -47,0 +45,0 @@ timeToDefault: userOptions.timeToDefault || 1000,

import { Options } from './options';
import { CreepyImage, Cancel } from './types';
export default function preload(img: CreepyImage, options: Options, callback: (cancel: Cancel) => void): void;
import { Cancel } from '../types';
export default function preload(img: HTMLImageElement, options: Options, callback: (unload: Cancel) => Cancel): Cancel;
const getSrcs = (options) => {
const srcs = options.looks.map(({ src }) => src);
if (options.src)
srcs.push(options.src);
srcs.push(options.src);
if (options.hover)

@@ -9,3 +8,3 @@ srcs.push(options.hover);

};
const loadImage = (src, callback) => {
const loadImage = (src, consumer) => {
const img = new Image();

@@ -19,6 +18,6 @@ img.src = src;

delete img.onerror;
callback(img);
consumer(img);
};
};
const loadImages = (srcs, callback) => {
const loadImages = (srcs, consumer) => {
const imgs = [];

@@ -29,3 +28,3 @@ srcs.forEach(src => {

if (imgs.length === srcs.length)
callback(imgs);
consumer(imgs);
});

@@ -35,8 +34,15 @@ });

export default function preload(img, options, callback) {
let cancelled = false;
let cancel = () => {
cancelled = true;
};
loadImages(getSrcs(options), imgs => {
img.creepyfaceReachableImages = imgs;
callback(() => {
delete img.creepyfaceReachableImages;
if (cancelled)
return;
img.__creepyfaceReachableImages = imgs;
cancel = callback(() => {
delete img.__creepyfaceReachableImages;
});
});
return () => cancel();
}
{
"name": "creepyface",
"version": "6.0.1",
"version": "7.0.0-beta.1",
"keywords": [

@@ -28,2 +28,3 @@ "face",

"@types/jest": "^23.3.11",
"@types/throttle-debounce": "^2.1.0",
"babel-core": "7.0.0-bridge.0",

@@ -50,2 +51,3 @@ "babel-jest": "^24.9.0",

"standard": "^12.0.1",
"throttle-debounce": "^2.1.0",
"ts-jest": "^24.2.0",

@@ -57,5 +59,2 @@ "tslint": "^5.12.0",

},
"dependencies": {
"get-element-center": "^1.1.0"
},
"lint-staged": {

@@ -62,0 +61,0 @@ "./**/*": [

@@ -90,5 +90,5 @@ # [Creepyface](https://creepyface.io) &middot; [![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)

Creepyface will look at the pointer by default, however custom point sources 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 (see [firefly](https://github.com/4lejandrito/creepyface-firefly) for a real world implementation).
For example, to make your face look at a random point every half a second you need to register an observable:
For example, to make your face look at a random point every half a second you need to register a point provider:

@@ -98,6 +98,6 @@ ```js

creepyface.registerObservable('random', observer => {
creepyface.registerPointProvider('random', (consumer, img) => {
const interval = setInterval(
() =>
observer([
consumer([
Math.random() * window.innerWidth,

@@ -139,7 +139,7 @@ Math.random() * window.innerHeight

creepyface(img, {
// An observable that provides the points to look at
points: observer => {
// An provider that provides the points to look at
points: (consumer, img) => {
const interval = setInterval(
() =>
observer([
consumer([
Math.random() * window.innerWidth,

@@ -170,2 +170,8 @@ Math.random() * window.innerHeight

The following point providers are available out of the box:
- `pointer` for both mouse and touch events. This is the default.
- `mouse` just for mouse events.
- `finger` just for touch events.
## Developing

@@ -179,3 +185,3 @@

Please feel free to create issues and / or submit pull requests. For the latter, [test cases](src/__test__) are very welcome.
Please feel free to create issues and / or submit pull requests. For the latter, [test cases](test/) are very welcome.

@@ -182,0 +188,0 @@ ## License

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc