/
PageCanvas.jsx
161 lines (127 loc) · 3.95 KB
/
PageCanvas.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import mergeRefs from 'merge-refs';
import warning from 'tiny-warning';
import * as pdfjs from 'pdfjs-dist/legacy/build/pdf';
import PageContext from '../PageContext';
import { getPixelRatio, isCancelException, makePageCallback } from '../shared/utils';
import { isPage, isRef, isRotate } from '../shared/propTypes';
const ANNOTATION_MODE = pdfjs.AnnotationMode;
export class PageCanvasInternal extends PureComponent {
componentDidMount() {
this.drawPageOnCanvas();
}
componentDidUpdate(prevProps) {
const { canvasBackground, page, renderForms } = this.props;
if (canvasBackground !== prevProps.canvasBackground || renderForms !== prevProps.renderForms) {
// Ensures the canvas will be re-rendered from scratch. Otherwise all form data will stay.
page.cleanup();
this.drawPageOnCanvas();
}
}
componentWillUnmount() {
this.cancelRenderingTask();
/**
* Zeroing the width and height cause most browsers to release graphics
* resources immediately, which can greatly reduce memory consumption.
*/
if (this.canvasLayer) {
this.canvasLayer.width = 0;
this.canvasLayer.height = 0;
this.canvasLayer = null;
}
}
cancelRenderingTask() {
if (this.renderer) {
this.renderer.cancel();
this.renderer = null;
}
}
/**
* Called when a page is rendered successfully.
*/
onRenderSuccess = () => {
this.renderer = null;
const { onRenderSuccess, page, scale } = this.props;
if (onRenderSuccess) onRenderSuccess(makePageCallback(page, scale));
};
/**
* Called when a page fails to render.
*/
onRenderError = (error) => {
if (isCancelException(error)) {
return;
}
warning(error);
const { onRenderError } = this.props;
if (onRenderError) onRenderError(error);
};
get renderViewport() {
const { page, rotate, scale } = this.props;
const pixelRatio = getPixelRatio();
return page.getViewport({ scale: scale * pixelRatio, rotation: rotate });
}
get viewport() {
const { page, rotate, scale } = this.props;
return page.getViewport({ scale, rotation: rotate });
}
drawPageOnCanvas = () => {
const { canvasLayer: canvas } = this;
if (!canvas) {
return null;
}
const { renderViewport, viewport } = this;
const { canvasBackground, page, renderForms } = this.props;
canvas.width = renderViewport.width;
canvas.height = renderViewport.height;
canvas.style.width = `${Math.floor(viewport.width)}px`;
canvas.style.height = `${Math.floor(viewport.height)}px`;
const renderContext = {
annotationMode: renderForms ? ANNOTATION_MODE.ENABLE_FORMS : ANNOTATION_MODE.ENABLE,
get canvasContext() {
return canvas.getContext('2d');
},
viewport: renderViewport,
};
if (canvasBackground) {
renderContext.background = canvasBackground;
}
// If another render is in progress, let's cancel it
this.cancelRenderingTask();
this.renderer = page.render(renderContext);
return this.renderer.promise.then(this.onRenderSuccess).catch(this.onRenderError);
};
render() {
const { canvasRef } = this.props;
return (
<canvas
className="react-pdf__Page__canvas"
dir="ltr"
ref={mergeRefs(canvasRef, (ref) => {
this.canvasLayer = ref;
})}
style={{
display: 'block',
userSelect: 'none',
}}
/>
);
}
}
PageCanvasInternal.propTypes = {
canvasBackground: PropTypes.string,
canvasRef: isRef,
onRenderError: PropTypes.func,
onRenderSuccess: PropTypes.func,
page: isPage.isRequired,
renderForms: PropTypes.bool,
rotate: isRotate,
scale: PropTypes.number.isRequired,
};
export default function PageCanvas(props) {
return (
<PageContext.Consumer>
{(context) => <PageCanvasInternal {...context} {...props} />}
</PageContext.Consumer>
);
}