|
| 1 | +# mr-Taps |
| 2 | + |
| 3 | +Taps provide source agnostic sync access to input. Either it comes from mouse and/or touch, it is the same API. It assumes multiple instances of taps making your code multi-touch by design. |
| 4 | + |
| 5 | +Providing sync access instead of event based, for best usage in real-time applications. |
| 6 | + |
| 7 | +[](LICENSE) |
| 8 | + |
| 9 | + |
| 10 | +## :rocket: Install |
| 11 | + |
| 12 | + |
| 13 | +```html |
| 14 | +<script type='module' src='mr-tap.module.min.js'></script> |
| 15 | +<script nomodule src='mr-tap.min.js'></script> |
| 16 | +``` |
| 17 | +Use built files from `dist` directory for browser. It will load ES8+ version if it is supported ([~94%](https://caniuse.com/?search=ES8)). There are two versions: `nomodule` (global scope) and `module` (for `import`). |
| 18 | + |
| 19 | +```js |
| 20 | +// create taps interface |
| 21 | +const taps = new Taps(); |
| 22 | + |
| 23 | +function update() { |
| 24 | + requestAnimationFrame(update); |
| 25 | + |
| 26 | + // update taps on each frame |
| 27 | + taps.update(); |
| 28 | + |
| 29 | + // access all taps |
| 30 | + for(const tap of taps) { |
| 31 | + // iterate through all taps |
| 32 | + } |
| 33 | +} |
| 34 | +requestAnimationFrame(update); |
| 35 | +``` |
| 36 | + |
| 37 | +## :scroll: [API Documentation](API.md) |
| 38 | + |
| 39 | +## Usage |
| 40 | + |
| 41 | + |
| 42 | +#### Creating: |
| 43 | + |
| 44 | +```js |
| 45 | +const taps = new Taps(); |
| 46 | +``` |
| 47 | + |
| 48 | +#### Updating: |
| 49 | +```js |
| 50 | +function update() { |
| 51 | + requestAnimationFrame(update); |
| 52 | + |
| 53 | + // update taps on each frame |
| 54 | + taps.update(); |
| 55 | + |
| 56 | + // application logic |
| 57 | +} |
| 58 | +requestAnimationFrame(update); |
| 59 | +``` |
| 60 | + |
| 61 | + |
| 62 | +#### Accessing: |
| 63 | + |
| 64 | +All taps: |
| 65 | +```js |
| 66 | +// Taps - is an iterator |
| 67 | +for(const tap in taps) { |
| 68 | + // all taps |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +Specific state taps, e.g. clicks: |
| 73 | + |
| 74 | +```js |
| 75 | +for(const tap in taps.click) { |
| 76 | + // taps that are only clicks |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | + |
| 81 | +## Examples |
| 82 | + |
| 83 | +Examples are multi-touch by design. |
| 84 | + |
| 85 | +Clicking on objects: |
| 86 | +```js |
| 87 | +for(const tap of taps.click) { |
| 88 | + const object = pickObject(tap.x, tap.y); |
| 89 | + if (! object) continue; |
| 90 | + object.interact(); |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +Rendering taps: |
| 95 | +```js |
| 96 | +// drawCircle(x, y, radius) |
| 97 | + |
| 98 | +for(const tap of taps) { |
| 99 | + drawCircle(tap.x, tap.y, 32); |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +Selecting multiple objects using rect (RTS style): |
| 104 | +```js |
| 105 | +// drawRect(left, top, right, bottom) |
| 106 | + |
| 107 | +// draw selection rect |
| 108 | +for(const tap of taps.drag) { |
| 109 | + drawRect(tap.sx, tap.sy, tap.x, tap.y); |
| 110 | +} |
| 111 | + |
| 112 | +// select objects on dragend |
| 113 | +for(const tap of taps.dragend) { |
| 114 | + const objects = objectsInRect(tap.sx, tap.sy, tap.x, tap.y); |
| 115 | + // selected some objects |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +Dragging objects: |
| 120 | +```js |
| 121 | +// pick an object from tap start position |
| 122 | +// and remember against tap object |
| 123 | +for(const tap of taps.dragstart) { |
| 124 | + const object = pickObject(tap.sx, tap.sy); |
| 125 | + if (! object) continue; |
| 126 | + |
| 127 | + tap.object = object; |
| 128 | + // remember difference of position between tap and object center |
| 129 | + tap.offsetX = object.x - tap.sx; |
| 130 | + tap.offsetY = object.y - tap.sy; |
| 131 | +} |
| 132 | + |
| 133 | +// position objects |
| 134 | +for(const tap of taps.drag) { |
| 135 | + // tap might have no object associated |
| 136 | + if (! tap.object) continue; |
| 137 | + // position with offset to prevent object snapping |
| 138 | + tap.object.setPosition(tap.x + tap.offsetX, tap.y + tap.offsetY); |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +Throwing objects: |
| 143 | +```js |
| 144 | +// pick objects |
| 145 | +// position objects |
| 146 | + |
| 147 | +// throw an object |
| 148 | +for(const tap of taps.dragend) { |
| 149 | + // tap might have no object associated |
| 150 | + if (! tap.object) continue; |
| 151 | + // set linear velocity |
| 152 | + // multiply speed by deltaTime |
| 153 | + tap.object.setLinearVelocity(tap.dx * dt, tap.dy * dt); |
| 154 | +} |
| 155 | +``` |
| 156 | + |
| 157 | +## Tap |
| 158 | + |
| 159 | +Each tap is instantiated by the Taps interface, and provided through iterators. Tap is agnostic to the input source: mouse or touch. And behaves identical. Also, due to iterator pattern, it provides sync access instead of event driven, and is multi-touch by design. |
| 160 | + |
| 161 | +Tap has states with two branching scenarios: |
| 162 | + |
| 163 | +Click: `start` > `click + up` |
| 164 | +Drag: `start` > `dragstart + drag` > `drag` > `dragend + drag + up` |
| 165 | + |
| 166 | + |
| 167 | +#### States: |
| 168 | + |
| 169 | +`start` - every tap starts with a start state. |
| 170 | + |
| 171 | +`click` - some taps if not moved from initial position and have not been held for too long, on `up` state, will be considered `click`. |
| 172 | + |
| 173 | +`dragstart` - some taps if moved from initial position or have been held for some time, will enter `drag` state, and once be in `dragstart` state. |
| 174 | +`drag` - once tap is in `dragstart` state, it will be in `drag` state till the end of a tap. |
| 175 | +`dragend` - if tap is in `drag` state, on `up` state instead of `click` it will be in `dragend` state. |
| 176 | + |
| 177 | +`up` - every tap ends with `up` state. |
| 178 | + |
| 179 | + |
| 180 | +#### Drag State |
| 181 | + |
| 182 | +Tap enters `drag` state if moved far from start position, or held for long enough. It is possible to change default settings of 20 pixels and 200 millisecsonds: |
| 183 | + |
| 184 | +```js |
| 185 | +taps.dragDistance = 20; // pixels |
| 186 | +taps.dragDelay = 200; // milliseconds |
| 187 | +``` |
| 188 | + |
| 189 | + |
| 190 | +## Building |
| 191 | + |
| 192 | +Builds library into two versions (normal and module) using Rollup, Babel and Terser. |
| 193 | +Source file: `src/index.js` |
| 194 | +Built versions normal (`dist/mr-tap.min.js`) and module (`dist/mr-tap.module.min.js`): |
| 195 | + |
| 196 | +```bash |
| 197 | +npm install |
| 198 | +npm run build |
| 199 | +``` |
0 commit comments