CARVIEW |
Select Language
HTTP/2 200
server: GitHub.com
content-type: text/html; charset=utf-8
last-modified: Tue, 31 May 2022 19:05:38 GMT
access-control-allow-origin: *
etag: W/"62966702-5d9"
expires: Tue, 29 Jul 2025 18:08:05 GMT
cache-control: max-age=600
content-encoding: gzip
x-proxy-cache: MISS
x-github-request-id: E9AC:229AB5:D8B1F:FC67D:68890BAC
accept-ranges: bytes
age: 0
date: Tue, 29 Jul 2025 17:58:06 GMT
via: 1.1 varnish
x-served-by: cache-bom-vanm7210068-BOM
x-cache: MISS
x-cache-hits: 0
x-timer: S1753811886.877008,VS0,VE231
vary: Accept-Encoding
x-fastly-request-id: 95ec553248c11ffc6c8f2cee5a30a524d5779d14
content-length: 687
PyScript Gravity Sensor
- numpy
from js import addGravityListener
from pyodide import create_proxy
import numpy as np
span = document.querySelector("span")
def callback(x, y, z):
span.innerText = orientation(x, y, z)
def orientation(x, y, z):
gravity = np.array([x, y, z])
v = list(np.round(gravity / np.linalg.norm(gravity)).astype(int))
if v == [ 1, 0, 0]: return "Horizontal counterclockwise"
if v == [-1, 0, 0]: return "Horizontal clockwise"
if v == [ 0, 1, 0]: return "Vertical upright"
if v == [ 0, -1, 0]: return "Vertical upside down"
if v == [ 0, 0, 1]: return "Screen up"
if v == [ 0, 0, -1]: return "Screen down"
return "Tilted"
addGravityListener(create_proxy(callback))