Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gohypergiant/FocusEngine
A Framer module for simulating the grid focus behavior found on Apple TV and Roku.
https://github.com/gohypergiant/FocusEngine
Last synced: 2 months ago
JSON representation
A Framer module for simulating the grid focus behavior found on Apple TV and Roku.
- Host: GitHub
- URL: https://github.com/gohypergiant/FocusEngine
- Owner: gohypergiant
- License: mit
- Created: 2017-02-16T21:33:17.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-10-18T15:30:54.000Z (over 7 years ago)
- Last Synced: 2024-11-13T13:54:51.193Z (2 months ago)
- Language: CoffeeScript
- Homepage:
- Size: 846 KB
- Stars: 23
- Watchers: 5
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-framer - FocusEngine - Module for simulating the grid focus behavior found on Apple TV and Roku. (Modules)
README
# FocusEngine Framer Module
[![license](https://img.shields.io/github/license/bpxl-labs/RemoteLayer.svg)](https://opensource.org/licenses/MIT)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](.github/CONTRIBUTING.md)
[![Maintenance](https://img.shields.io/maintenance/yes/2017.svg)]()The FocusEngine module allows you to simulate the grid focus behavior seen on streaming media players like Apple TV and Roku. Use the keyboard, [RemoteLayer](https://github.com/bpxl-labs/RemoteLayer), or another mechanism to direct focus around your prototype’s canvas.
Once initialized, any _visible_ layer can be brought into focus, even if it’s off screen. This permits the activation of off-screen menus. Visual appearance of focused elements can be customized.
### Installation
#### NPM Installation
```
$ cd /your/framer/project
$ npm i @blackpixel/framer-focusengine
```#### Manual installation
Copy or save the `FocusEngine.coffee` file into your project's `modules` folder.
### Adding It to Your Project
In your Framer project add the following:
```coffeescript
# If you manually installed
fe = require "FocusEngine"
# else
fe = require "@blackpixel/framer-focusengine"
```### API
#### Customize focused and unfocused states
```coffeescript
fe.focusStyle.scale =
fe.focusStyle.shadowX =
fe.focusStyle.shadowY =
fe.focusStyle.shadowColor = (hex or rgba)
fe.focusStyle.shadowBlur =
fe.focusStyle.shadowSpread =fe.unfocusStyle.shadowX =
fe.unfocusStyle.shadowY =
fe.unfocusStyle.shadowColor = (hex or rgba)
fe.unfocusStyle.shadowBlur =
fe.unfocusStyle.shadowSpread =
```(Unfocused scale is always assumed to be the layer’s original scale. This need not be 1. You may get better visual results by drawing your layer slightly larger than needed and setting its initial scale to something less than 1.)
#### Customize state switch duration
```coffeescript
fe.time =
```#### Collect layers that will participate into an array
```coffeescript
myFocusableLayers = [layerA, layerB, layerC]
```#### Initialize the engine with your array
```coffeescript
fe.initialize(myFocusableLayers)
```#### Add a layer created post-initialization
```coffeescript
fe.addLayer(layerD)
```#### Optionally attach changeFocus() to keyboard events
```coffeescript
document.addEventListener "keydown", (event) ->
keyCode = event.which
switch keyCode
when 13 then fe.changeFocus("select")
when 37 then fe.changeFocus("left")
when 38 then fe.changeFocus("up")
when 39 then fe.changeFocus("right")
when 40 then fe.changeFocus("down")
else null
```#### Place initial focus
```coffeescript
fe.placeFocus(layerA)
```#### focusPrevious() is available to use in conjunction with FlowComponent's showPrevious()
```coffeescript
fe.focusPrevious()
```(Note that focus cannot be placed on a layer whose visibility, or whose ancestor’s visibility, is false. You may need to delay calling `focusPrevious()` until the FlowComponent’s transition is ended, perhaps by using `.onTransitionEnd`.)
#### Layers can trigger behavior upon receiving or losing focus, or being selected
```coffeescript
layerA.on "focus", ->
layerA.on "unfocus", ->
layerA.on "selected", ->
```Shortcuts are also available:
```coffeescript
layerA.onFocus ->
layerA.onUnfocus ->
layerA.onSelected ->
```#### Integration with [RemoteLayer](https://github.com/bpxl-labs/RemoteLayer)
```coffeescript
RemoteLayer = require "RemoteLayer"
myRemote = new RemoteLayer
clickAction: -> fe.changeFocus("select")
swipeUpAction: -> fe.changeFocus("up")
swipeDownAction: -> fe.changeFocus("down")
swipeLeftAction: -> fe.changeFocus("left")
swipeRightAction: -> fe.changeFocus("right")
```#### Check the currently focused layer
```coffeescript
print fe.focus
```#### Check whether a layer has focus
```coffeescript
print layerA.focus
```#### Overriding focus logic
Sometimes you will want to change focus in a way that doesn't make exact geometric sense. For example, when switching from a large header row to a smaller secondary row, you may prefer the first cell in the secondary row receive focus. Or, you may want to provide a way for focus to "loop around" at a row's end. These kinds of functions are possible through _overrides._Set a layer's overrides using the `overrides` object:
```coffeescript
layerA.overrides =
up:
down:
left:
right:
```It is not necessary to set all four overrides; but only for those directions which require custom behavior. Now, shifting focus from `layerA` will always land on the direction-specified target -- no matter what FocusEngine thinks should happen.
> Obviously, you can create very counterintuitive behaviors this way. Use with care!
If a layer has custom overrides, or has been initialized with FocusEngine, you may check any of its current overrides:
```coffeescript
print layerA.overrides?.up
```The `?` permits the check to fail gracefully on layers which have no overrides.
#### Enable debug mode to log focus changes
```coffeescript
fe.debug = true
```### Example project
[Download](https://minhaskamal.github.io/DownGit/#/home?url=https://github.com/bpxl-labs/FocusEngine/tree/master/example.framer) the example to try it for yourself.### Known issues
Attempting to perform a `placeFocus()` call as FocusEngine is changing its own focus will fail. (The call is discarded.) If you need to override FocusEngine's logic, use the `overrides` feature or add a slight delay to ensure the call is respected.
```coffeescript
layerA.on "unfocus", ->
Utils.delay 0.1, ->
fe.placeFocus(layerB)
```---
Website: [blackpixel.com](https://blackpixel.com) ·
GitHub: [@bpxl-labs](https://github.com/bpxl-labs/) ·
Twitter: [@blackpixel](https://twitter.com/blackpixel) ·
Medium: [@bpxl-craft](https://medium.com/bpxl-craft)