-
Notifications
You must be signed in to change notification settings - Fork 105
/
DrawingThread.js
181 lines (155 loc) · 5.32 KB
/
DrawingThread.js
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import rbush from 'rbush';
import AnnotationThread from '../AnnotationThread';
import { STATES_DRAW, DRAW_RENDER_THRESHOLD } from '../annotationConstants';
const RTREE_WIDTH = 5; // Lower number - faster search, higher - faster insert
const BASE_LINE_WIDTH = 3;
class DrawingThread extends AnnotationThread {
/** @property {number} - Drawing state */
drawingFlag = STATES_DRAW.idle;
/** @property {rbush} - Rtree path container */
/* eslint-disable new-cap */
pathContainer = new rbush(RTREE_WIDTH);
/* eslint-enable new-cap */
/** @property {CanvasContext} - A canvas for drawing new strokes */
memoryCanvas;
/** @property {DrawingPath} - The path being drawn but not yet finalized */
pendingPath;
/** @property {CanvasContext} - The context to be drawn on */
drawingContext;
/** @property {number} - Timestamp of the last render */
lastRenderTimestamp;
/** @property {number} - The the last animation frame request id */
lastAnimationRequestId;
/**
* [constructor]
*
* @inheritdoc
* @param {AnnotationThreadData} data - Data for constructing thread
* @return {DrawingThread} Drawing annotation thread instance
*/
constructor(data) {
super(data);
this.render = this.render.bind(this);
this.handleStart = this.handleStart.bind(this);
this.handleMove = this.handleMove.bind(this);
this.handleStop = this.handleStop.bind(this);
}
/**
* Soft destructor for a drawingthread object
*
* [destructor]
* @inheritdoc
* @return {void}
*/
destroy() {
if (this.lastAnimationRequestId) {
window.cancelAnimationFrame(this.lastAnimationRequestId);
}
this.removeAllListeners();
this.reset();
super.destroy();
}
/**
* Get all of the DrawingPaths in the current thread.
*
* @return {void}
*/
getDrawings() {
return this.pathContainer.all();
}
/* eslint-disable no-unused-vars */
/**
* Handle a pointer movement
*
* @param {Object} location - The location information of the pointer
* @return {void}
*/
handleMove(location) {}
/**
* Start a drawing stroke *
*
* @param {Object} location - The location information of the pointer
* @return {void}
*/
handleStart(location) {}
/**
* End a drawing stroke
*
* @param {Object} location - The location information of the pointer
* @return {void}
*/
handleStop(location) {}
/* eslint-disable no-unused-vars */
//--------------------------------------------------------------------------
// Protected
//--------------------------------------------------------------------------
/**
* Set the drawing styles
*
* @protected
* @param {Object} config - The configuration Object
* @param {number} config.scale - The document scale
* @param {string} config.color - The brush color
* @return {void}
*/
setContextStyles(config) {
if (!this.drawingContext) {
return;
}
const { scale, color } = config;
this.drawingContext.lineCap = 'round';
this.drawingContext.lineJoin = 'round';
this.drawingContext.strokeStyle = color || 'black';
this.drawingContext.lineWidth = BASE_LINE_WIDTH * (scale || 1);
}
/**
* Draw the pending path onto the DrawingThread CanvasContext. Should be used
* in conjunction with requestAnimationFrame.
*
* @protected
* @param {number} timestamp - The time when the function was called;
* @return {void}
*/
render(timestamp) {
const elapsed = timestamp - (this.lastRenderTimestamp || 0);
if (elapsed < DRAW_RENDER_THRESHOLD || !this.drawingContext) {
return;
}
this.lastRenderTimestamp = timestamp;
const canvas = this.drawingContext.canvas;
const drawings = this.getDrawings();
/* OPTIMIZE (@minhnguyen): Render only what has been obstructed by the new drawing
* rather than every single line in the thread. If we do end
* up splitting saves into multiple requests, we can buffer
* the amount of re-renders onto a temporary memory canvas.
*/
this.drawingContext.clearRect(0, 0, canvas.width, canvas.height);
drawings.forEach((drawing) => drawing.drawPath(this.drawingContext));
if (this.pendingPath) {
this.pendingPath.drawPath(this.drawingContext);
}
}
//--------------------------------------------------------------------------
// Protected
//--------------------------------------------------------------------------
/**
* Create an annotation data object to pass to annotation service.
*
* @inheritdoc
* @private
* @param {string} type - Type of annotation
* @param {string} text - Annotation text
* @return {Object} Annotation data
*/
createAnnotationData(type, text) {
return {
type,
drawingPaths: this.getDrawings(),
fileVersionId: this.fileVersionId,
user: this.annotationService.user,
threadID: this.threadID,
threadNumber: this.threadNumber
};
}
}
export default DrawingThread;