getCursorNDC method

Vector2 getCursorNDC(
  1. double cursorX,
  2. double cursorY
)
  • Calculate the cursor position in NDC
  • cursorX Cursor horizontal coordinate within the canvas
  • cursorY Cursor vertical coordinate within the canvas
  • @ Vector2 Cursor normalized position inside the canvas

Implementation

Vector2 getCursorNDC(double cursorX, double cursorY) {
    // final canvasRect = canvas.getBoundingClientRect();

    final box = listenableKey.currentContext!.findRenderObject() as RenderBox;
    final canvasRect = box.size;
    final local = box.globalToLocal(const Offset(0, 0));

    _v2_1.setX(((cursorX - local.dx) / canvasRect.width) * 2 - 1);
    _v2_1.setY((((local.dy + canvasRect.height) - cursorY) / canvasRect.height) * 2 - 1);
    return _v2_1.clone();
  }