Search code examples
javascriptfacebookaugmented-realityspark-ar-studio

Get the pixel screen size in Spark AR studio (for Facebook)


I am starting to work with Spark AR studio and I looking for to get the screen size in pixel to compare the coordinate obtained by the gesture.location on Tap.

TouchGestures.onTap().subscribe((gesture) => {
  // ! The location is always specified in the screen coordinates
  Diagnostics.log(`Screen touch in pixel = { x:${gesture.location.x}, y: ${gesture.location.y} }`);

  // ????
});

The gesture.location is in pixel (screen coordinate) and would like to compare it with the screen size to determine which side of the screen is touched.

Maybe using the Camera.focalPlane could be a good idea...

Update

I tried two new things to have the screen size:

const CameraInfo = require('CameraInfo');
Diagnostics.log(CameraInfo.previewSize.height.pinLastValue());

const focalPlane = Scene.root.find('Camera').focalPlane;
Diagnostics.log(focalPlane.height.pinLastValue());

But both return 0


Solution

  • Finally,

    Using the Device Info in the Patch Editor and passing these to the script works!

    First, add a variable "to script" in the editor:

    enter image description here

    Then, create that in patch editor:

    enter image description here

    And you can grab that with this script:

    const Patches = require('Patches');
    const screenSize = Patches.getPoint2DValue('screenSize');
    

    My mistake was to use Diagnostic.log() to check if my variable worked well.

    Instead use Diagnostic.watch():

    Diagnostic.watch('screenSize.x', screenSize.x);
    Diagnostic.watch('screenSize.y', screenSize.y);