widgets: gyroscope widget 1st draft

This commit is contained in:
jean-emmanuel 2018-08-26 19:49:29 +02:00
parent a278ddd638
commit 1ceacb23d7
5 changed files with 213 additions and 1 deletions

View file

@ -33,6 +33,7 @@
"electron-packager-plugin-non-proprietary-codecs-ffmpeg": "^1.0.1",
"exorcist": "1.0.1",
"file-saver": "1.3.8",
"gyronorm": "^2.0.6",
"jquery": "3.3.1",
"json5": "1.0.1",
"keyboardjs": "github:jean-emmanuel/KeyboardJS",

View file

@ -100,3 +100,57 @@
#### `set(id, value)`
- `id` (`string`): a widget's id
- `value`: the widget's new value
## Gyroscope
```js
{
type:'gyroscope',
// etc
}
```
The gyroscope's value is an object containing multiple values, which can be used by other widgets via the [property maths syntax](../extras/advanced-property-syntax.md) :
```
// value.do.alpha ( deviceorientation event alpha value )
// value.do.beta ( deviceorientation event beta value )
// value.do.gamma ( deviceorientation event gamma value )
// value.do.absolute ( deviceorientation event absolute value )
// value.dm.x ( devicemotion event acceleration x value )
// value.dm.y ( devicemotion event acceleration y value )
// value.dm.z ( devicemotion event acceleration z value )
// value.dm.gx ( devicemotion event accelerationIncludingGravity x value )
// value.dm.gy ( devicemotion event accelerationIncludingGravity y value )
// value.dm.gz ( devicemotion event accelerationIncludingGravity z value )
// value.dm.alpha ( devicemotion event rotationRate alpha value )
// value.dm.beta ( devicemotion event rotationRate beta value )
// value.dm.gamma ( devicemotion event rotationRate gamma value )
```
### `frequency`
- type: `number`
- default: `30`
- usage: update frequency in Hz (value updates per seconds)
### `normalize`
- type: `boolean`
- default: `true`
- usage: normalize gravity related values
### `compass`
- type: `boolean`
- default: `false`
- usage: set to `true` to return the orientation values with respect to the actual north direction of the world instead of the head direction of the device
### `screenAdjusted`
- type: `boolean`
- default: `false`
- usage: set to `true` to return screen adjusted values

View file

@ -52,6 +52,7 @@ module.exports.widgets = {
input: require('./inputs/input'),
keys: require('./inputs/keys'),
script: require('./inputs/script'),
gyroscope: require('./inputs/gyroscope'),
// maths
// deprecated / hidden
@ -67,5 +68,5 @@ module.exports.categories = {
'Plots':['plot','eq','visualizer','led', 'rgbled','meter','text', 'image', 'svg'],
'Containers':['panel','strip','modal', 'clone'],
'Switchers':['switcher','crossfader'],
'Inputs':['input', 'keys', 'script']
'Inputs':['input', 'keys', 'script', 'gyroscope']
}

View file

@ -0,0 +1,116 @@
var Widget = require('../common/widget'),
GyroNorm = require('gyronorm/dist/gyronorm.complete.js'),
{iconify} = require('../../ui/utils'),
{clip} = require('../utils')
class Gyroscope extends Widget {
static defaults() {
return {
type:'gyroscope',
id:'auto',
linkId:'',
_geometry:'geometry',
left:'auto',
top:'auto',
width:'auto',
height:'auto',
_style:'style',
label:'auto',
color:'auto',
css:'',
_gyroscope: 'gyroscope',
frequency: 30,
normalize: true,
compass: false,
screenAdjusted: false
precision: 2,
}
}
constructor(options) {
var html = `
<div class="gyroscope">
${iconify('^arrows-alt')}
</div>
`
super({...options, html: html})
this.sensor = new GyroNorm()
this.sensor.init({
frequency: 1000 / clip(this.getProp('frequency'), [0.01, 60]),
gravityNormalized: this.getProp('normalize'),
orientationBase: this.getProp('compass') ? GyroNorm.WORLD : GyroNorm.GAME,
decimalCount: this.getProp('precision'),
screenAdjusted: this.getProp('screenAdjusted'),
logger: null
}).then(()=>{
this.sensor.start((data)=>{
this.setValue(data, {sync: true})
// data.do.alpha ( deviceorientation event alpha value )
// data.do.beta ( deviceorientation event beta value )
// data.do.gamma ( deviceorientation event gamma value )
// data.do.absolute ( deviceorientation event absolute value )
// data.dm.x ( devicemotion event acceleration x value )
// data.dm.y ( devicemotion event acceleration y value )
// data.dm.z ( devicemotion event acceleration z value )
// data.dm.gx ( devicemotion event accelerationIncludingGravity x value )
// data.dm.gy ( devicemotion event accelerationIncludingGravity y value )
// data.dm.gz ( devicemotion event accelerationIncludingGravity z value )
// data.dm.alpha ( devicemotion event rotationRate alpha value )
// data.dm.beta ( devicemotion event rotationRate beta value )
// data.dm.gamma ( devicemotion event rotationRate gamma value )
})
}).catch((e)=>{
console.log(e)
})
}
setValue(v, options={} ) {
this.value = v
// if (options.send) this.sendValue()
if (options.sync) this.changed(options)
}
onRemove() {
this.sensor.end()
super.onRemove()
}
}
module.exports = Gyroscope

View file

@ -160,3 +160,43 @@
border: 1rem solid var(--color-bg);
}
}
.gyroscope-container {
width: 100rem;
height:60rem;
flex-shrink: 0;
padding:1rem;
--opacity: .75;
.gyroscope {
flex: 1 1 100%;
height:35rem;
padding: 10rem;
justify-content: center;
display: flex;
align-items: center;
background: var(--color-track);
color:var(--color-custom);
border: 1rem solid var(--color-bg);
@extend .glass-effect;
i {
opacity: var(--opacity);
}
span {
font-family: monospace;
letter-spacing: 0;
margin-left: 4rem;
}
}
}
.panel:not(.tab) > .gyroscope-container {
--color-border:transparent;
padding: 0rem;
.gyroscope {
margin: 1rem;
box-shadow: 0 0 0 1rem var(--color-light),
0 0 0 1rem var(--color-fg);
border: 1rem solid var(--color-bg);
}
}