Skip to content

Instantly share code, notes, and snippets.

@jacksenechal
Last active August 22, 2020 16:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacksenechal/67315945593d452407ff to your computer and use it in GitHub Desktop.
Save jacksenechal/67315945593d452407ff to your computer and use it in GitHub Desktop.
General-purpose browser orientation locking
lockOrientation = (orientation) ->
new Promise (resolve, reject) ->
locked = false
try
locked = screen.lockOrientation?(orientation) or
screen.mozLockOrientation?(orientation) or
screen.msLockOrientation?(orientation)
if locked then resolve()
catch e
reject e
if not locked
if screen.orientation?.lock? # Chrome/ium
screen.orientation.lock orientation
.then (result) -> resolve result
.catch (error) -> reject error
else reject()
lockOrientation 'portrait'
.then -> console.log 'screen orientation locked'
.catch -> console.log 'screen orientation lock failed'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment