Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sceneCoordsToViewportCoords, jumping text when there is an offset (#4413) #4630

Merged
merged 221 commits into from
Jan 29, 2022

Conversation

zsviczian
Copy link
Collaborator

This resolves #4413 and fixes the incorrect sceneCoordsToViewportCoords function.

Before

old.mp4

After

new.mp4

@vercel
Copy link

vercel bot commented Jan 22, 2022

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/excalidraw/excalidraw/6waJcPj6ZB2JBKkSx23s8kSY2EMG
✅ Preview: https://excalidraw-git-fork-zsviczian-fix-jumpingtext-excalidraw.vercel.app

…ext"

This reverts commit ef7e1e4, reversing
changes made to 2aea28f.
@zsviczian
Copy link
Collaborator Author

@ad1992, this simplifies and resolves the errors (and heuristic "magic") with text positioning in textwysiwyg. This change greatly improves the user experience for Excalidraw when it is rendered embedded in an app with offsetLeft & offsetTop !=0.

Please take a look and let's push this important improvement to production.

Comment on lines -180 to -184
Number(
getComputedStyle(
excalidrawContainer?.parentNode as Element,
).marginRight.slice(0, -2),
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note sure about what this solved before, but shouldn't w keep this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this deliberately and after extensive testing. I think it is based on the misunderstanding that appState.width is the entire screen width, while in reality, it is the canvasDOMWidth. Subtracting the right-hand side margin is unnecessary.

const {
width: canvasDOMWidth,
height: canvasDOMHeight,
viewModeEnabled,
} = this.state;
const canvasWidth = canvasDOMWidth * canvasScale;
const canvasHeight = canvasDOMHeight * canvasScale;
if (viewModeEnabled) {
return (
<canvas
className="excalidraw__canvas"
style={{
width: canvasDOMWidth,
height: canvasDOMHeight,
cursor: CURSOR_TYPE.GRAB,
}}

@ad1992
Copy link
Member

ad1992 commented Jan 23, 2022

This is a great improvement @zsviczian ❤️. Could you also write specs for this ? Will do one round of testing and then should be good to merge.

@zsviczian
Copy link
Collaborator Author

This is a great improvement @zsviczian ❤️. Could you also write specs for this ? Will do one round of testing and then should be good to merge.

Great! A second (and 3rd, 4th...) round of testing is always essential. What do you mean by specs?

@ad1992
Copy link
Member

ad1992 commented Jan 23, 2022

This is a great improvement @zsviczian ❤️. Could you also write specs for this ? Will do one round of testing and then should be good to merge.

Great! A second (and 3rd, 4th...) round of testing is always essential. What do you mean by specs?

I meant tests for the change just to make sure we don't break in future as this part has been broken often in the past.

@zsviczian
Copy link
Collaborator Author

This is a great improvement @zsviczian ❤️. Could you also write specs for this ? Will do one round of testing and then should be good to merge.

Great! A second (and 3rd, 4th...) round of testing is always essential. What do you mean by specs?

I meant tests for the change just to make sure we don't break in future as this part has been broken often in the past.

I think this broke frequently in the future because there was a formula error. sceneCoordsToViewportCoords was wrong, and we were trying to resolve its impact by applying complex calculations in textwysiwyg. That should be resolved for good.

Regarding testing... I don't really know how to test for zoom and offset situations... I haven't yet looked at excalidraw tests, I'll take a look to see if this sort of testing can be automated. My recommendation would be to deploy a second app with every change, the second should have offsets and righthand side margin.

@ad1992
Copy link
Member

ad1992 commented Jan 23, 2022

This is a great improvement @zsviczian ❤️. Could you also write specs for this ? Will do one round of testing and then should be good to merge.

Great! A second (and 3rd, 4th...) round of testing is always essential. What do you mean by specs?

I meant tests for the change just to make sure we don't break in future as this part has been broken often in the past.

I think this broke frequently in the future because there was a formula error. sceneCoordsToViewportCoords was wrong, and we were trying to resolve its impact by applying complex calculations in textwysiwyg. That should be resolved for good.

Regarding testing... I don't really know how to test for zoom and offset situations... I haven't yet looked at excalidraw tests, I'll take a look to see if this sort of testing can be automated.

Sure 👍🏻 . The tests would probably be tricky for this but lets give it a try and decide accordingly :).

My recommendation would be to deploy a second app with every change, the second should have offsets and righthand side margin.

What do you mean by deploying an app with offsets and right handed margin ? We have the same code sandbox example running with package example in port 3001 which can be used for testing

@zsviczian
Copy link
Collaborator Author

What do you mean by deploying an app with offsets and right handed margin ? We have the same code sandbox example running with package example in port 3001 which can be used for testing

To make the question concrete. If I would want to test a PR with screen offsets, how can I do that. For sake of example, the current preview release for the hyperlinks PR is https://excalidraw-f13roaq8h-excalidraw.vercel.app/ I tried https://excalidraw-f13roaq8h-excalidraw.vercel.app:3001/ but that did not bring up anything...

@ad1992
Copy link
Member

ad1992 commented Jan 24, 2022

What do you mean by deploying an app with offsets and right handed margin ? We have the same code sandbox example running with package example in port 3001 which can be used for testing

To make the question concrete. If I would want to test a PR with screen offsets, how can I do that. For sake of example, the current preview release for the hyperlinks PR is https://excalidraw-f13roaq8h-excalidraw.vercel.app/ I tried https://excalidraw-f13roaq8h-excalidraw.vercel.app:3001/ but that did not bring up anything...

Ohh ok I meant it will work in localhost:3001, we can also deploy it as well so its available in preview too

@zsviczian
Copy link
Collaborator Author

zsviczian commented Jan 28, 2022

Ohh ok I meant it will work in localhost:3001, we can also deploy it as well so its available in preview too

That would be great! I was looking around the repository but didn't yet have the patience to figure it out. If you give me some pointers I am also happy to propose a PR with port 3001 being available in preview too.

BTW Have you had time to test this fix? I've deployed this to the Obsidian Excalidraw build a week ago and have not experienced any issues since. I am pretty confident that this is working as expected. Would be good to get this deployed before further functions build on the current erroneous sceneCoordsToViewportCoords() function.

Copy link
Member

@ad1992 ad1992 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 from my side, tested it out and it works great, thanks @zsviczian for fixing this 💯 , lets wait for @dwelle's +1 as well and then we are good to 🚀

@dwelle
Copy link
Member

dwelle commented Jan 29, 2022

For sure, let's merge! Thanks!

@dwelle dwelle merged commit e4edda4 into excalidraw:master Jan 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

getViewportCoords and viewportCoordsToSceneCoords don't transform into each other
4 participants