fTelnet icon indicating copy to clipboard operation
fTelnet copied to clipboard

Fix problems selection characters with mouse when scaling Canvas

Open opicron opened this issue 8 months ago • 9 comments

When changing the .fTelnetCrtCanvas with CSS, as such:

.fTelnetCrtCanvas {
height:100vh !important;
width:98vw !important;
}

The selection of text is no longer correct due to the adjusted size. For example url please check: https://bbs.theforze.eu.

Edit: after diving deeper, I found the code in MouseDown/MouseMove/MouseUp had to be adjusted instead. Below code wasnt even called in my situation.

https://github.com/rickparrish/fTelnet/blob/47aa268d2a4e52b58c6561c00d584aa1db9481e7/source/common/Offset.ts#L16

Example code which should be working:

function getOffsetRect(elem: HTMLElement): Point {
        const rect = elem.getBoundingClientRect();

        const scaleX = elem.width / rect.width;
        const scaleY = elem.height / rect.height;
    
        return {
            x: Math.round(rect.left * scaleX),
            y: Math.round(rect.top * scaleY)
        };    
}

For canvas mouse input, we need coordinates relative to the canvas, not the document.

opicron avatar May 01 '25 12:05 opicron

Very cool layout, I've never thought to try using vh or vw like that!

So if I understand correctly, you're saying that should be a general purpose replacement that works in your 100vh and 98vw setup as well as a "standard" setup?

rickparrish avatar May 04 '25 01:05 rickparrish

Yeah, full screen fTelnet without F11 is really pleasant to the eye.

After examining the issue, googling for answers and some chatgpt (kuch) help this should indeed be a general purpose replacement. I havent been able to test it.

opicron avatar May 04 '25 07:05 opicron

Mhm I just tested the code and it isnt working-- let me fix this myself and I will report back.

opicron avatar May 04 '25 07:05 opicron

Alright, I fixed the intial selected character by adjusthing the following line. The starting point of selection is perfect.

https://github.com/rickparrish/fTelnet/blob/9dbbd71c3dfb12edf07bf8bea29a5b7eed4f6fd9/source/crt/Crt.ts#L1050

To:

            //this._MouseDownPoint = this.MousePositionToScreenPosition(me.offsetX, me.offsetY);
            const rect = this._Canvas.getBoundingClientRect();
            const scaleX = this._Canvas.width / rect.width;
            const scaleY = this._Canvas.height / rect.height;

            this._MouseDownPoint = this.MousePositionToScreenPosition(
                me.offsetX * scaleX,
                me.offsetY * scaleY
            );

opicron avatar May 04 '25 07:05 opicron

Ah, I had to update this line too (OnMouseMove):

https://github.com/rickparrish/fTelnet/blob/9dbbd71c3dfb12edf07bf8bea29a5b7eed4f6fd9/source/crt/Crt.ts#L1083

            //NewMovePoint = this.MousePositionToScreenPosition(me.offsetX, me.offsetY);
            const rect = this._Canvas.getBoundingClientRect();
            const scaleX = this._Canvas.width / rect.width;
            const scaleY = this._Canvas.height / rect.height;

            NewMovePoint = this.MousePositionToScreenPosition(
                me.offsetX * scaleX,
                me.offsetY * scaleY
            );

opicron avatar May 04 '25 07:05 opicron

Oh, if this code gets added, it also fixes getting incorrect coordinate ANSI codes. Big win for my small next projects!

opicron avatar May 04 '25 11:05 opicron

Thanks for the code samples, I've just pushed a commit that incorporates the scale calculation.

rickparrish avatar May 05 '25 21:05 rickparrish

I notice I also added the code to OnMouseUp event. Just to be sure the right coordinate gets send. I did not list that part of the code above.

opicron avatar May 07 '25 14:05 opicron

Yeah I put the scaling adjustment into MousePositionToScreenPosition so it's accounted for in both of the if branches in all three mouse events.

(I wasn't able to test the if branch where me.offsetX is undefined, so I'm not 100% sure that the adjustment will be needed there, or will work as expected if it is needed, but I'm guessing that second branch was for IE support and in that case not likely anybody will hit it going forward anyway!)

rickparrish avatar May 07 '25 15:05 rickparrish