Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="root"></div>
              
            
!

CSS

              
                body {
  height: 100vh;
  margin: 0;
  display: grid;
  place-items: center;
}
.box {
  width: auto;
}

.bg {
  margin: 0 auto;
  width: 85.06666667vw;
  height: 55vw;
  background-image: url('https://i.imgur.com/FZNcwts.png');
  background-size: contain;
  background-position: center;
  background-repeat: no-repeat;
  position: relative;
  .hiddenText {
    font-size: 3.7vw;
    transform: translateZ(0);
    position: absolute;
    box-sizing: border-box;
    padding-top: 12.26666667vw;
    left: 0;
    width: 100%;
    height: 100%;
    text-align: center;
    mask-size: 34.13333333vw 34.13333333vw;
    mask-image: url('https://i.imgur.com/nWRUuqv.png');
    mask-repeat: no-repeat;
    transition: all ease 0.2s;
    mask-position: 25.565617vw 6.13333333vw;
  }
  .lens {
    position: absolute;
    top: 6.13333333vw;
    left: 25.33333333vw;
    background-image: url('https://i.imgur.com/FOUMIQ6.png');
    background-size: 100% 100%;
    background-repeat: no-repeat;
    width: 34.13333333vw;
    height:  34.13333333vw;
    transition: transform ease 0.2s;
    transform: translateX(74.9%);
  }
}

              
            
!

JS

              
                import React, {
  useCallback,
  useEffect,
  useState,
  ReactElement,
  useRef
} from "https://esm.sh/react@18";
import ReactDOM from "https://esm.sh/react-dom@18";
import throttle from "https://cdn.skypack.dev/lodash@4.17.21/throttle";

type DeviceOrientation = {
  alpha: number | null;
  beta: number | null;
  gamma: number | null;
};

type UseDeviceOrientationData = {
  orientation: DeviceOrientation | null;
  error: Error | null;
  requestAccess: () => Promise<boolean>;
  revokeAccess: () => Promise<void>;
};

const useDeviceOrientation = (): UseDeviceOrientationData => {
  const [error, setError] = useState<Error | null>(null);
  const [orientation, setOrientation] = useState<DeviceOrientation | null>(
    null
  );

  const onDeviceOrientation = throttle(
    (event: DeviceOrientationEvent): void => {
      setOrientation({
        alpha: event.alpha,
        beta: event.beta,
        gamma: event.gamma
      });
    },
    100
  );

  const revokeAccessAsync = async (): Promise<void> => {
    window.removeEventListener("deviceorientation", onDeviceOrientation);
    setOrientation(null);
  };

  const requestAccessAsync = async (): Promise<boolean> => {
    if (!DeviceOrientationEvent) {
      setError(
        new Error("Device orientation event is not supported by your browser")
      );
      return false;
    }

    if (
      DeviceOrientationEvent.requestPermission &&
      typeof DeviceMotionEvent.requestPermission === "function"
    ) {
      let permission: PermissionState;
      try {
        permission = await DeviceOrientationEvent.requestPermission();
      } catch (err) {
        setError(err);
        return false;
      }
      if (permission !== "granted") {
        setError(
          new Error("Request to access the device orientation was rejected")
        );
        return false;
      }
    }

    window.addEventListener("deviceorientation", onDeviceOrientation);

    return true;
  };

  const requestAccess = useCallback(requestAccessAsync, []);
  const revokeAccess = useCallback(revokeAccessAsync, []);

  useEffect(() => {
    return (): void => {
      revokeAccess();
    };
  }, [revokeAccess]);

  return {
    orientation,
    error,
    requestAccess,
    revokeAccess
  };
};

interface IProps {
  orientation: any;
}

const getTransformDegree = (
  value?: number,
  threshold = 30,
  max = 74.9
): number => {
  if (value) {
    const sy = value > 0 ? "" : "-";
    const degree = `${Math.min((Math.abs(value) / threshold) * 100, max)}`;
    return Number(`${sy}${degree}`);
  }
  return 0;
};

const Demo = ({ orientation }: IProps): ReactElement => {
  const degree = getTransformDegree(orientation?.gamma);
  const yDegree = getTransformDegree(orientation?.beta, 20, 40);
  const transform = `translate(${degree}%, ${yDegree}%)`;
  const maskPosition = `${25.565617 + (degree / 100) * 34.133}vw calc(${
    25.565617 + (yDegree / 100) * 34.133
  }vw - 18.4vw)`;

  return (
    <div className="bg">
      <div
        className="hiddenText"
        style={{
          maskPosition,
          "-webkit-mask-position": maskPosition
        }}
      >
        <div>Hidden Text</div>
        <div>Hidden Text</div>
        <div>Hidden Text</div>
        <div>Hidden Text</div>
      </div>
      <div
        className="lens"
        style={{
          transform
        }}
      ></div>
    </div>
  );
};

const App = () => {
  const {
    orientation,
    requestAccess,
    revokeAccess,
    error
  } = useDeviceOrientation();
  const errorElement = error ? (
    <div className="error">{error.message}</div>
  ) : null;
  return (
    <div className="box">
      <button onClick={requestAccess}>Authorize Gyro Access</button>
      {errorElement}
      <Demo orientation={orientation} />
    </div>
  );
};

ReactDOM.render(<App />, document.getElementById("root"));

              
            
!
999px

Console