getVisibleElements static method

JSArray<JSAny?> getVisibleElements()

Implementation

static JSArray getVisibleElements() {
  try {
    var pixRatio = getDevicePixelRatio(Plotline.ctx);
    var screenHeight = (getScreenHeight(Plotline.ctx) * pixRatio).round();
    var screenWidth = (getScreenWidth(Plotline.ctx) * pixRatio).round();
    List<Map<String, dynamic>> views = Plotline.recurseKey(
        Plotline.ctx, pixRatio, screenWidth, screenHeight);
    List<JSAny> parsedViews = [];

    for (Map<String, dynamic> view in views) {
      try {
        int x = (view['position']['x'] / pixRatio).round();
        int y = (view['position']['y'] / pixRatio).round();
        int width = (view['position']['width'] / pixRatio).round();
        int height = (view['position']['height'] / pixRatio).round();
        int left = (view['position']['x'] / pixRatio).round();
        int top = (view['position']['y'] / pixRatio).round();
        int right =
            ((view['position']['x'] + view['position']['width']) / pixRatio)
                .round();
        int bottom =
            ((view['position']['y'] + view['position']['height']) / pixRatio)
                .round();

        // Create JS objects for the bounds
        final jsConstructor = globalContext['Object'] as JSFunction;
        final boundsObj = jsConstructor.callAsConstructor() as JSObject;

        // Set bounds properties
        boundsObj['x'] = x.toJS;
        boundsObj['y'] = y.toJS;
        boundsObj['width'] = width.toJS;
        boundsObj['height'] = height.toJS;
        boundsObj['left'] = left.toJS;
        boundsObj['top'] = top.toJS;
        boundsObj['right'] = right.toJS;
        boundsObj['bottom'] = bottom.toJS;

        // Create the element object
        final elementObj = jsConstructor.callAsConstructor() as JSObject;
        elementObj['id'] = view['clientElementId'];
        elementObj['bounds'] = boundsObj;

        parsedViews.add(elementObj);
      } catch (err) {
        debugPrint("Error parsing element: $err");
      }
    }
    return parsedViews.toJS;
  } catch (e) {
    debugPrint("Error in getVisibleElements: $e");
    return [].map((element) => element as JSAny).toList().toJS;
  }
}