Hello!
I am currently working on a canvas / codemirror editor combination where I need to synchronize the canvas’ viewport with codemirror’s viewport (you can find it here ).
My current approach is to scale the editor vertically and horizontally, but only translate the editor horizontally and use the scrollTop of the editor to sync the viewports’ vertical positions.
function updateTransforms() {
editorView.requestMeasure({
read: () => {},
write: () => {
const mat3d = Mat3dUtils.mul(
Mat3dUtils.scale(scale, scale),
Mat3dUtils.translate(-translateX, 0)
);
// vertical translation
if (editorView.scrollDOM) {
editorView.scrollDOM.scrollTop = translateY;
}
// horizontal translation and vertical/horizontal scale
editorView.dom.style.transform = `matrix(${Mat3dUtils.elems(mat3d).join(',')})`;
// Update canvas viewport
updateCanvas()
}
});
}
However, when I scroll down far enough (e.g., 100 lines down), and try to zoom, the editor’s scroll starts jittering and its viewports get out of sync. This only happens when I zoom the editor, not when I pan the editor.
Here is an example where:
- Canvas viewport: surrounded by a blue border
- Editor: surrounded by a green border
- Red squares: shapes within the canvas that should stay on the same position relative to the editor’s text
- Use shift+scroll to zoom the viewport, and scroll/drag to move the viewport around
Video of issue: https://imgur.com/a/7VZ2rP8
Demo: codemirror demo
Furthermore, I noticed that this only happens after a requestMeasure happened, and when, for example, the height of the editor is changed. I am guessing the internal content height somehow changes after a requestMeasure, which causes the new scrollTop to be slightly off.
Any ideas on how to solve this problem? If I need to provide anything else, please let me know!
Kind regards