From e9e8d572ee16e60bf67fa1c531d9f08d4883039c Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Sun, 26 Aug 2018 19:49:29 +0200 Subject: [PATCH] widgets: gyroscope widget 1st draft --- package.json | 1 + resources/docs/widgets/inputs.md | 54 ++++++++ src/browser/js/app/widgets/index.js | 3 +- .../js/app/widgets/inputs/gyroscope.js | 116 ++++++++++++++++++ src/browser/scss/widgets/_inputs.scss | 40 ++++++ 5 files changed, 213 insertions(+), 1 deletion(-) create mode 100644 src/browser/js/app/widgets/inputs/gyroscope.js diff --git a/package.json b/package.json index 8e0d8905..e5304323 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/resources/docs/widgets/inputs.md b/resources/docs/widgets/inputs.md index 99565bfd..ea69f38c 100644 --- a/resources/docs/widgets/inputs.md +++ b/resources/docs/widgets/inputs.md @@ -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 diff --git a/src/browser/js/app/widgets/index.js b/src/browser/js/app/widgets/index.js index d71d59b8..38023248 100644 --- a/src/browser/js/app/widgets/index.js +++ b/src/browser/js/app/widgets/index.js @@ -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'] } diff --git a/src/browser/js/app/widgets/inputs/gyroscope.js b/src/browser/js/app/widgets/inputs/gyroscope.js new file mode 100644 index 00000000..edd90029 --- /dev/null +++ b/src/browser/js/app/widgets/inputs/gyroscope.js @@ -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 = ` +
+ ${iconify('^arrows-alt')} +
+ ` + + 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 diff --git a/src/browser/scss/widgets/_inputs.scss b/src/browser/scss/widgets/_inputs.scss index 1d268ae5..41b25eca 100644 --- a/src/browser/scss/widgets/_inputs.scss +++ b/src/browser/scss/widgets/_inputs.scss @@ -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); + } +}