Skip to content

Commit

Permalink
🎉 Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ezekielaquino committed Mar 17, 2017
0 parents commit 06ed238
Show file tree
Hide file tree
Showing 5 changed files with 287 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

*.json
21 changes: 21 additions & 0 deletions LICENSE.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Ezekiel

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.
96 changes: 96 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# PointerGeometry3000
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. 🙋🏼

[🍒 VISIT THE PROJECT (MINI DEMO) WEBSITE](http://ezekielaquino.com/PointerGeometry3000)

# Why?
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.

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!

[💅🏾 THE PROJECT SITE IS A MINI DEMO](http://ezekielaquino.com/PointerGeometry3000)

*More demos coming soon (mocking 3d, AppleTV rotating cards, etc)*


# How?
Simply call PointerGeometry3000. This will create a global variable `PointerGeometry` which contains `points`. Values are updated onMouseMove by default (see available settings below).

```js
// Initialize
PointerGeometry();
```

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(PointerGeometry('pointA')` after initialisation, you will find that a point object contains the following data:

```js
// Sample data
pointA: {
cx: 640, // (centerX, reference)
cy: 333, // (centerY, reference)
deg: 43.42633594589745, // (degrees),
hyp: 426.8480408763756 // (distance from reference)
rad: 0.7579325443330766, // (radians)
x: 187 // (positionX),
y: 177 // (positionY)
}
```

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:

```js
// We initialize with multiple points
PointerGeometry({
points: {
aNicelyNamedPoint: {
cx: 200, // coordinate in pixels relative to viewport
cy: 200
},
anotherPoint: {
cx: 'center', // you can just pass 'center' for center of viewport
cy: 'center' // you can just pass 'center' for center of viewport
},
goCrazy: {
cx: 450
cy: 620
}
},
initialPos: { pageX: 200, pageY: 200 }, // (optional) registered 'mouse position' immediately on init. defaults to center
listener: 'mousemove', // (optional, default: mousemove) event to listen to when updating values
console: true // (optional) set to true if you want a console with values to be shown
});
```

So how do you get a specific point's particular value? You just do: `PointerGeometry(pointName).propertyKey`

```js
// Get a specific point's value
var distanceFromReference = PointerGeometry('aNicelyNamedPoint').hyp;
var angleInDegrees = PointerGeometry('aNicelyNamedPoint').deg;
// ... so on
```

You can also access `all` points via `PointerGeometry.points`.


# Notes

- If manipulating DOM elements, it is still best to use transforms as they are way more performant than manipulating e.g. width and height.
- 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.


# Plans

- More values e.g. adjacent, opposite and corresponding angles
- point relative to a parent element
- any suggestions?

# Say Hi!

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 [email protected] or via @the_ezekiel on Twitter!

P.S. Follow me on twitter!

# License

This project is licensed under the MIT License - see the LICENSE.md file for details
167 changes: 167 additions & 0 deletions pointergeometry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/**
* POINTERGEOMETRY3000
* http://github.com/ezekielaquino/pointergeometry3000
* JS Utility to get all sorts of pointer properties
* MIT License
*/

'use strict';

(function() {
let keys, propsKeys, points, isConsole, propElems, initialPos, listener;

const defaults = {
points: {
pointA: {
cx: window.innerWidth / 2,
cy: window.innerHeight / 2
}
}
};



window.PointerGeometry = function(arg) {
// If passed a string, we return the
// object containing the named point
if (typeof arg === 'string' || Number.isInteger(arg)) {
return window.PointerGeometry.points[arg];
} else {
// If passed an object, then we initialise
// the plugin and register the points
arg = arg || defaults;
keys = Object.keys(arg.points);
points = arg.points || {};
isConsole = arg.console;
initialPos = arg.initialPos || { pageX: window.innerWidth / 2, pageY: window.innerHeight / 2 };
listener = arg.listener || 'mousemove';

// initial register, before initial mousemove
// so we populate the values for the console
if (isConsole) {
register();
initializeConsole(keys);
}

// register with initial position
register(initialPos);

// Listen to mousemove within the viewport
window.addEventListener(listener, function(event) {
register(event);
});
}
};



function register(event) {
event = event || {};

for (let i = 0; i < keys.length; i++) {
const key = keys[i];
let point;

let cx = (points[key].cx == 'center' || !points[key].cx) ? defaults.points['pointA'].cx : points[key].cx;
let cy = (points[key].cy == 'center' || !points[key].cy) ? defaults.points['pointA'].cy : points[key].cy;
let x = (event.pageX || 0) - cx;
let y = (event.pageY || 0) - cy;
let rad = Math.atan2(y, x);
let deg = rad * (180 / Math.PI);
let hyp = Math.hypot(x, y);

point = {
cx: cx,
cy: cy,
x: x,
y: y,
rad: rad,
deg: deg,
hyp: hyp
};

propsKeys = Object.keys(point);
points[key] = point;

updateConsole(point, i);
}

window.PointerGeometry.points = points;
}



function initializeConsole(keys) {
if (isConsole) {
const container = document.createElement('div');

// Create the parent container
if (!document.querySelector('.js-pgProps')) {
container.classList.add('js-pgProps');
container.style.position = 'fixed';
container.style.background = 'rgba(0, 0, 0, 0.7)';
container.style.color = 'white';
container.style.fontSize = '9px';
container.style.fontFamily = 'Arial';
container.style.width = 140 + 'px';
container.style.top = 0;
container.style.right = 0;

document.body.appendChild(container);
}

// Create each point's container
if (document.querySelectorAll('.js-pgPoint').length < keys.length) {
for (var i = 0; i < keys.length; i++) {
// Initialize console
const key = keys[i];
const propKeys = Object.keys(window.PointerGeometry.points[key]);
const pointInfo = document.createElement('div');
const pointLabel = document.createElement('label');

pointInfo.classList.add('js-pgPoint');

pointLabel.innerHTML = key;
pointLabel.style.padding = '2px 5px';

pointInfo.appendChild(pointLabel);

for (let n = 0; n < propKeys.length; n++) {
const prop = document.createElement('div');
const propKey = propKeys[n];

prop.classList.add('js-pgProp');
prop.style.padding = '2px 5px 2px 15px';

prop.innerHTML = propKey + ':' + window.PointerGeometry.points[propKey];
pointInfo.appendChild(prop);
}

container.appendChild(pointInfo);
}
}

propElems = document.querySelectorAll('.js-pgProp');
}
}



function updateConsole(point, index) {
if (propElems) {
for (var j = 0; j < propsKeys.length; j++) {
const i = (index * propsKeys.length) + j;
const elem = propElems[i];
const key = propsKeys[j];
const value = point[key];

elem.innerHTML = key + ':' + value;
}
}
}





})();
1 change: 1 addition & 0 deletions pointergeometry.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 06ed238

Please sign in to comment.