FPS counter

Toggleable FPS counter using Ctrl+Alt+F, to move the counter hover over it and drag using Ctrl+Middle-click

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

Advertisement:

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

Advertisement:

// ==UserScript==
// @name         FPS counter
// @namespace    https://fps-bookmarkletuserscript.gabrielzv1233.repl.co
// @version      1.0
// @description  Toggleable FPS counter using Ctrl+Alt+F, to move the counter hover over it and drag using Ctrl+Middle-click
// @match        *://*/*
// @grant        none
// @license      gpl-3.0
// @language     en-US
// ==/UserScript==

(function() {
    let fpsElement = document.createElement('div');
    fpsElement.style.position = 'fixed';
    fpsElement.style.top = '0';
    fpsElement.style.padding = '4px';
    fpsElement.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
    fpsElement.style.color = '#fff';
    fpsElement.style.fontFamily = 'Arial, sans-serif';
    fpsElement.style.fontSize = '12px';
    fpsElement.style.zIndex = '9999';

    let isShowingFPS = false;
    let frameCount = 0;
    let lastTime = performance.now();
    let isDraggingFPS = false;
    let offsetX = 0;
    let offsetY = 0;
    let previousCursorStyle = '';

    document.addEventListener('keydown', function(event) {
        if (event.ctrlKey && event.altKey && event.key === 'f' && !isDraggingFPS) {
            toggleFPS();
        }
    });

    fpsElement.addEventListener('mousedown', function(event) {
        if (event.ctrlKey && event.button === 1) {
            startDragFPS(event);
        }
    });

    document.addEventListener('mouseup', function(event) {
        if (event.button === 1) {
            stopDragFPS();
        }
    });

    function toggleFPS() {
        if (isShowingFPS) {
            document.body.removeChild(fpsElement);
            isShowingFPS = false;
        } else {
            document.body.appendChild(fpsElement);
            isShowingFPS = true;
            requestAnimationFrame(updateFPS);
        }
    }

    function startDragFPS(event) {
        isDraggingFPS = true;
        offsetX = event.clientX - fpsElement.getBoundingClientRect().left;
        offsetY = event.clientY - fpsElement.getBoundingClientRect().top;
        fpsElement.style.pointerEvents = 'none';
        previousCursorStyle = document.body.style.cursor;
        document.body.style.cursor = 'move';
        document.addEventListener('mousemove', dragFPS);
    }

    function stopDragFPS() {
        isDraggingFPS = false;
        fpsElement.style.pointerEvents = 'auto';
        document.body.style.cursor = previousCursorStyle;
        document.removeEventListener('mousemove', dragFPS);
    }

    function dragFPS(event) {
        let left = event.clientX - offsetX;
        let maxLeft = window.innerWidth - fpsElement.offsetWidth;
        fpsElement.style.left = Math.max(0, Math.min(left, maxLeft)) + 'px';

        let top = event.clientY - offsetY;
        let maxTop = window.innerHeight - fpsElement.offsetHeight;
        fpsElement.style.top = Math.max(0, Math.min(top, maxTop)) + 'px';
    }

    function updateFPS() {
        frameCount++;
        let currentTime = performance.now();
        let deltaTime = currentTime - lastTime;

        if (deltaTime >= 1000) {
            let fps = Math.round((frameCount * 1000) / deltaTime);
            fpsElement.textContent = 'FPS: ' + fps;

            frameCount = 0;
            lastTime = currentTime;
        }

        if (isShowingFPS) {
            requestAnimationFrame(updateFPS);
        }
    }
})();