The code is not wrong. There are several reasons that you might get coordinates outside of the pixels of the screen:
* You are looking outside of the screen – look on the screen
* The user calibration is incorrect – perform a new user calibration
* the screen setup is incorrect – perform a new screen setup
The eye tracking signal isn’t perfect. typically the error is larger near the edges of the screen too, and there will always be occasional samples with significantly incorrect coordinates (due to some edge case during the image processing or computation in the eye tracker).
If you plot the computed coordinate on the screen, does it typically match where you are looking? Is there a constant offset? is there a scale issue? Either of these indicate that your personal calibration or screen setup is off. Very noisy signals could indicate problems with eye tracking for that particular person. Glasses and contact lenses, heavy make up, long eye lashes and partially closed eyes all lead to a degradation in signal quality.