getCursorNDC method

dynamic getCursorNDC(
  1. dynamic cursorX,
  2. dynamic cursorY,
  3. dynamic canvas
)
  • Calculate the cursor position in NDC
    • @param {number} x Cursor horizontal coordinate within the canvas
      • @param {number} y Cursor vertical coordinate within the canvas
      • @param {HTMLElement} canvas The canvas where the renderer draws its output
      • @returns {Vector2} Cursor normalized position inside the canvas

Implementation

getCursorNDC(cursorX, cursorY, canvas) {
  // var canvasRect = canvas.getBoundingClientRect();

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

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