├── .gitignore ├── LICENSE.MD ├── README.md ├── pointer3k.js └── pointer3k.min.js /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | *.json 3 | package.json -------------------------------------------------------------------------------- /LICENSE.MD: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Ezekiel 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Pointer3000 2 | A simple little utility that makes it easy to work with values calculated from the pointer's location relative to one or multiple defined points. 🙋🏼 3 | 4 | [🍒 VISIT THE PROJECT (MINI DEMO) WEBSITE](https://ezekielaquino.github.io/Pointer3000/) 5 | 6 | # Why? 7 | I experiment a lot with interactive graphics and interaction in general, and have found that I often have to calculate `this` and `that`, particularly the distance and angle from (a) specific point(s). I decided to make this little utility so I can just plop it in and start trying things out. This is meant to be used in compositions with just regular DOM elements– the constraint of what can be done with just a 'regular' page I find to be a very welcome creative challenge. 8 | 9 | While the output is very 'basic', I found that what can be done with them when used to manipulate `things` are endless. I hope you find this stirring your creativity and imagination. Lots of possibilities! 10 | 11 | [💅🏾 THE PROJECT SITE IS A MINI DEMO](https://ezekielaquino.github.io/Pointer3000/) 12 | 13 | *More demos coming soon (mocking 3d, AppleTV rotating cards, etc)* 14 | 15 | 16 | # How? 17 | Simply call Pointer3000. This will create a global variable `Pointer3k` which contains `points`. Values are updated onMouseMove by default (see available settings below). Download the script or `npm install pointer3000`. 18 | 19 | ```js 20 | // Initialize 21 | Pointer3k(); 22 | ``` 23 | 24 | While the utility registers events on `mousemove` by default, you can use values via `requestAnimationFrame` so you don't have to register another `mousemove` listener to update the view. 25 | 26 | If initialised without arguments it will simply register the center of the viewport as its one and only reference point. The default name of this auto-created point is `pointA`. If you do a `console.log(Pointer3k('pointA')` after initialisation, you will find that a point object contains the following data: 27 | 28 | ```js 29 | // Sample data 30 | pointA: { 31 | cx: 640, // (centerX, reference) 32 | cy: 333, // (centerY, reference) 33 | deg: 43.42633594589745, // (degrees), 34 | dist: 426.8480408763756 // (distance from reference) 35 | rad: 0.7579325443330766, // (radians) 36 | x: 187 // (positionX), 37 | y: 177 // (positionY) 38 | } 39 | ``` 40 | 41 | [In Action](https://www.instagram.com/p/BRq40BYAiht/) 42 | 43 | But you probably want to define specific points of reference, have multiple and even name them for convenience, so you'd want to initialize it like so: 44 | 45 | ```js 46 | // We initialize with multiple points 47 | Pointer3k({ 48 | points: { 49 | aNicelyNamedPoint: { 50 | cx: 200, // coordinate in pixels relative to viewport 51 | cy: 200 52 | }, 53 | anotherPoint: { 54 | cx: 'center', // you can just pass 'center' for center of viewport 55 | cy: 'center' // you can just pass 'center' for center of viewport 56 | }, 57 | goCrazy: { 58 | cx: 450 59 | cy: 620 60 | } 61 | }, 62 | initialPos: { pageX: 200, pageY: 200 }, // (optional) registered 'mouse position' immediately on init. defaults to center 63 | listener: 'mousemove', // (optional, default: mousemove) event to listen to when updating values 64 | console: true // (optional) set to true if you want a console with values to be shown 65 | }); 66 | ``` 67 | 68 | So how do you get a specific point's particular value? You just do: `Pointer3k(pointName).propertyKey` 69 | 70 | ```js 71 | // Get a specific point's value 72 | var distanceFromReference = Pointer3k('aNicelyNamedPoint').dist; 73 | var angleInDegrees = Pointer3k('aNicelyNamedPoint').deg; 74 | // ... so on 75 | ``` 76 | 77 | If you want to pause updating values (basically just having an empty mousemove listener, performance gains or what have you) e.g. you only want values to be updated if a button has been clicked, then you use: 78 | 79 | ```js 80 | // passing true or false as argument will enable/disable the listener 81 | Pointer3k(boolean); 82 | 83 | // Sample usage, toggle pointer update 84 | var updatePointer = false; 85 | 86 | button.addEventListener(function() { 87 | updatePointer = !updatePointer; 88 | 89 | Pointer3k(updatePointer); 90 | }); 91 | ``` 92 | 93 | You can also access `all` points via `Pointer3k.points`. 94 | 95 | 96 | # Notes 97 | 98 | - If manipulating DOM elements, it is still best to use transforms as they are way more performant than manipulating e.g. width and height. 99 | - If you are going to be doing something relatively complex graphic wise then I think you should be looking at canvas or other drawing libraries. Those would be more performant, and have more math methods at your disposal. e.g. paperJS, raphael, svg.js 100 | - Having `console: true` or having inspector open (as you already know) will reduce performance as it will be updating values, recording timelines, highlighting updated DOM elements what have you. 101 | 102 | 103 | # Plans 104 | 105 | - More values e.g. adjacent, opposite and corresponding angles 106 | - point relative to a parent element 107 | - any suggestions? 108 | 109 | # Say Hi! 110 | 111 | Please do. If you have any comments or suggestions, i'd love to hear them. This utility is completely free but if you have used it for something cool please do let me know, let me see what you've made! Drop me a line at ezekielaquino@gmail.com or via [@the_ezekiel](http://twitter.com/the_ezekiel) or [Share this](https://twitter.com/home?status=%E2%86%98%E2%86%98%20Supercharged%20pointer%20interactions%20%E2%80%93%20POINTER3000%20%E2%86%99%E2%86%99%20http%3A//ezekielaquino.com/Pointer3000) on Twitter! 112 | 113 | P.S. Follow me on twitter! 114 | 115 | # License 116 | 117 | This project is licensed under the MIT License - see the LICENSE.md file for details 118 | -------------------------------------------------------------------------------- /pointer3k.js: -------------------------------------------------------------------------------- 1 | /** 2 | * POINTER3000 3 | * http://github.com/ezekielaquino/Pointer3000 4 | * JS Utility to get all sorts of pointer properties 5 | * MIT License 6 | */ 7 | 8 | 'use strict'; 9 | 10 | (function() { 11 | let keys, propsKeys, points, propElems; 12 | let isConsole, initialPos, listener, update; 13 | 14 | const defaults = { 15 | points: { 16 | pointA: { 17 | cx: window.innerWidth / 2, 18 | cy: window.innerHeight / 2 19 | } 20 | } 21 | }; 22 | 23 | 24 | 25 | window.Pointer3k = function(arg) { 26 | // If passed a string, we return the 27 | // object containing the named point 28 | if (typeof arg === 'string' || Number.isInteger(arg)) { 29 | return window.Pointer3k.points[arg]; 30 | } else if (typeof arg == 'boolean') { 31 | // we 'pause' value updating 32 | update = arg; 33 | } else { 34 | // If passed an object, then we initialise 35 | // the plugin and register the points 36 | arg = arg || defaults; 37 | keys = Object.keys(arg.points); 38 | points = arg.points || {}; 39 | isConsole = arg.console; 40 | initialPos = arg.initialPos || { pageX: window.innerWidth / 2, pageY: window.innerHeight / 2 }; 41 | listener = arg.listener || 'mousemove'; 42 | update = true; 43 | 44 | // initial register, before initial mousemove 45 | // so we populate the values for the console 46 | if (isConsole) { 47 | register(); 48 | initializeConsole(keys); 49 | } 50 | 51 | // register with initial position 52 | register(initialPos); 53 | 54 | // Listen to mousemove within the viewport 55 | window.addEventListener(listener, function(event) { 56 | if (update) { 57 | register(event); 58 | } 59 | }); 60 | } 61 | }; 62 | 63 | 64 | 65 | function register(event) { 66 | event = event || {}; 67 | 68 | for (let i = 0; i < keys.length; i++) { 69 | const key = keys[i]; 70 | let point; 71 | 72 | let cx = points[key].cx == 'center' ? defaults.points['pointA'].cx : points[key].cx; 73 | let cy = points[key].cy == 'center' ? defaults.points['pointA'].cy : points[key].cy; 74 | let x = (event.pageX || 0) - cx; 75 | let y = (event.pageY || 0) - cy; 76 | let rad = Math.atan2(y, x); 77 | let deg = (rad * (180 / Math.PI) + 360) % 360; 78 | let dist = Math.hypot(x, y); 79 | 80 | point = { 81 | cx: cx, 82 | cy: cy, 83 | x: x, 84 | y: y, 85 | rad: rad, 86 | deg: deg, 87 | dist: dist 88 | }; 89 | 90 | propsKeys = Object.keys(point); 91 | points[key] = point; 92 | 93 | updateConsole(point, i); 94 | } 95 | 96 | window.Pointer3k.points = points; 97 | } 98 | 99 | 100 | 101 | function initializeConsole(keys) { 102 | if (isConsole) { 103 | const container = document.createElement('div'); 104 | 105 | // Create the parent container 106 | if (!document.querySelector('.js-pgProps')) { 107 | container.classList.add('js-pgProps'); 108 | container.style.position = 'fixed'; 109 | container.style.background = 'rgba(0, 0, 0, 0.7)'; 110 | container.style.color = 'white'; 111 | container.style.fontSize = '9px'; 112 | container.style.fontFamily = 'Arial'; 113 | container.style.width = 140 + 'px'; 114 | container.style.top = 0; 115 | container.style.right = 0; 116 | 117 | document.body.appendChild(container); 118 | } 119 | 120 | // Create each point's container 121 | if (document.querySelectorAll('.js-pgPoint').length < keys.length) { 122 | for (var i = 0; i < keys.length; i++) { 123 | // Initialize console 124 | const key = keys[i]; 125 | const propKeys = Object.keys(window.Pointer3k.points[key]); 126 | const pointInfo = document.createElement('div'); 127 | const pointLabel = document.createElement('label'); 128 | 129 | pointInfo.classList.add('js-pgPoint'); 130 | 131 | pointLabel.innerHTML = key; 132 | pointLabel.style.padding = '2px 5px'; 133 | 134 | pointInfo.appendChild(pointLabel); 135 | 136 | for (let n = 0; n < propKeys.length; n++) { 137 | const prop = document.createElement('div'); 138 | const propKey = propKeys[n]; 139 | 140 | prop.classList.add('js-pgProp'); 141 | prop.style.padding = '2px 5px 2px 15px'; 142 | 143 | prop.innerHTML = propKey + ':' + window.Pointer3k.points[propKey]; 144 | pointInfo.appendChild(prop); 145 | } 146 | 147 | container.appendChild(pointInfo); 148 | } 149 | } 150 | 151 | propElems = document.querySelectorAll('.js-pgProp'); 152 | } 153 | } 154 | 155 | 156 | 157 | function updateConsole(point, index) { 158 | if (propElems) { 159 | for (var j = 0; j < propsKeys.length; j++) { 160 | const i = (index * propsKeys.length) + j; 161 | const elem = propElems[i]; 162 | const key = propsKeys[j]; 163 | const value = point[key]; 164 | 165 | elem.innerHTML = key + ':' + value; 166 | } 167 | } 168 | } 169 | 170 | 171 | 172 | 173 | 174 | })(); -------------------------------------------------------------------------------- /pointer3k.min.js: -------------------------------------------------------------------------------- 1 | "use strict";(function(){let t,n,e,o;let i,s,c,d;const p={points:{pointA:{cx:window.innerWidth/2,cy:window.innerHeight/2}}};window.Pointer3k=function(n){if(typeof n==="string"||Number.isInteger(n)){return window.Pointer3k.points[n]}else if(typeof n=="boolean"){d=n}else{n=n||p;t=Object.keys(n.points);e=n.points||{};i=n.console;s=n.initialPos||{pageX:window.innerWidth/2,pageY:window.innerHeight/2};c=n.listener||"mousemove";d=true;if(i){r();l(t)}r(s);window.addEventListener(c,function(t){if(d){r(t)}})}};function r(o){o=o||{};for(let i=0;i