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 Blurry issue by forcing rebuild ObjectUrl in PageRenderContext #181

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ui/library/dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,11 @@ declare module '@allenai/pdf-components/src/context/TransformContext' {
rotation: PageRotation;
scale: number;
zoomMultiplier: number;
isScaleChanged: boolean;
setRotation: (rotation: PageRotation) => void;
setScale: (scale: number) => void;
setZoomMultiplier: (zoom: number) => void;
setIsScaleChanged: (isScaleChange: boolean) => void;
}
export const TransformContext: React.Context<ITransformContext>;
export function useTransformContextProps(): ITransformContext;
Expand Down
2 changes: 1 addition & 1 deletion ui/library/dist/pdf-components.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion ui/library/src/components/ZoomInButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const ZoomInButton: React.FunctionComponent<Props> = ({
children,
...extraProps
}: Props) => {
const { scale, setScale, zoomMultiplier } = React.useContext(TransformContext);
const { scale, setScale, zoomMultiplier, setIsScaleChanged } = React.useContext(TransformContext);
const { updateScrollPosition } = React.useContext(ScrollContext);

const handleZoomIn = React.useCallback(
Expand All @@ -23,6 +23,7 @@ export const ZoomInButton: React.FunctionComponent<Props> = ({
event.stopPropagation();
const zoomScale = Number(PercentFormatter.format(scale * zoomMultiplier).replace('%', ''));
if (zoomScale <= MAX_ZOOM_IN_SCALE) {
setIsScaleChanged(true);
updateScrollPosition(1 * zoomMultiplier);
setScale(scale * zoomMultiplier);
}
Expand Down
3 changes: 2 additions & 1 deletion ui/library/src/components/ZoomOutButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type Props = {
const MIN_ZOOM_OUT_SCALE = 25;

export const ZoomOutButton: React.FunctionComponent = ({ children, ...extraProps }: Props) => {
const { scale, setScale, zoomMultiplier } = React.useContext(TransformContext);
const { scale, setScale, zoomMultiplier, setIsScaleChanged } = React.useContext(TransformContext);
const { updateScrollPosition } = React.useContext(ScrollContext);

const handleZoomOut = React.useCallback(
Expand All @@ -20,6 +20,7 @@ export const ZoomOutButton: React.FunctionComponent = ({ children, ...extraProps
event.stopPropagation();
const zoomScale = Number(PercentFormatter.format(scale / zoomMultiplier).replace('%', ''));
if (zoomScale >= MIN_ZOOM_OUT_SCALE) {
setIsScaleChanged(true);
updateScrollPosition(1 / zoomMultiplier);
setScale(scale / zoomMultiplier);
}
Expand Down
12 changes: 8 additions & 4 deletions ui/library/src/context/PageRenderContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { PageNumber } from '../components/types/page';
import { Nullable } from '../components/types/utils';
import { logProviderWarning } from '../utils/provider';
import { PageRotation } from '../utils/rotate';
import { TransformContext } from './TransformContext';

export type RenderState = {
promise: Promise<string>;
Expand Down Expand Up @@ -57,6 +58,8 @@ export function usePageRenderContextProps({
}
);

const { isScaleChanged } = React.useContext(TransformContext);

// Because rendering a page is async, we will lose the current pageRenderStates
// This ref trick allows the latest to be accessible when the objectURL is ready
const pageRenderStatesRef = React.useRef(pageRenderStates);
Expand Down Expand Up @@ -112,7 +115,7 @@ export function usePageRenderContextProps({

// Don't need to start another task if already rendered
const existingPromise = pageRenderStates.get(pageNumber)?.promise;
if (existingPromise) {
if (existingPromise && !isScaleChanged) {
return existingPromise;
}

Expand Down Expand Up @@ -144,12 +147,12 @@ export function usePageRenderContextProps({

React.useEffect(() => {
for (const pageNumber of visiblePageRatios.keys()) {
if (pageRenderStates.has(pageNumber)) {
continue;
if (pageRenderStates.has(pageNumber) && !isScaleChanged) {
return;
}
buildObjectURLForPage({ pageNumber });
}
}, [pageRenderStates, visiblePageRatios]);
}, [pageRenderStates, visiblePageRatios, scale, rotation, devicePixelRatio]);

return {
pageRenderStates,
Expand Down Expand Up @@ -185,6 +188,7 @@ async function buildPageObjectURL({
const viewport = pageProxy.getViewport({ scale: scale * zoomMultiplier * devicePixelRatio });
canvas.height = viewport.height;
canvas.width = viewport.width;
canvas.style.width = '100%'; //de-zoom canvas with style (maybe you can directly use CSS), reaching de-zoom of higher definition PDF
Copy link
Contributor

Choose a reason for hiding this comment

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

Style is meaningless when the element isn't in the DOM.

Copy link
Contributor

Choose a reason for hiding this comment

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

You might try canvasContext.imageSmoothingEnabled = false;.

Another thing you could try is canvasContext.imageSmoothingQuality = 'high';.

const canvasContext = canvas.getContext('2d');
if (!canvasContext) {
throw new Error('canvas was unable to get a context');
Expand Down
9 changes: 9 additions & 0 deletions ui/library/src/context/TransformContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ export interface ITransformContext {
rotation: PageRotation;
scale: number;
zoomMultiplier: number;
isScaleChanged: boolean;
setRotation: (rotation: PageRotation) => void;
setScale: (scale: number) => void;
setZoomMultiplier: (zoom: number) => void;
setIsScaleChanged: (isScaleChange: boolean) => void;
}

export const TransformContext = React.createContext<ITransformContext>({
rotation: PageRotation.Rotate0,
scale: 1,
zoomMultiplier: 1.2,
isScaleChanged: false,
setRotation: rotation => {
logProviderWarning(`setRotation(${rotation})`, 'TransformContext');
},
Expand All @@ -25,19 +28,25 @@ export const TransformContext = React.createContext<ITransformContext>({
setZoomMultiplier: zoom => {
logProviderWarning(`setZoomMultiplier(${zoom})`, 'TransformContext');
},
setIsScaleChanged: isScaleChange => {
logProviderWarning(`setIsScaleChanged(${isScaleChange})`, 'TransformContext');
},
});

export function useTransformContextProps(): ITransformContext {
const [rotation, setRotation] = React.useState<PageRotation>(PageRotation.Rotate0);
const [scale, setScale] = React.useState<number>(1.0);
const [zoomMultiplier, setZoomMultiplier] = React.useState<number>(1.2);
const [isScaleChanged, setIsScaleChanged] = React.useState<boolean>(false);

return {
rotation,
scale,
isScaleChanged,
setRotation,
setScale,
setZoomMultiplier,
setIsScaleChanged,
zoomMultiplier,
};
}