-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathscreen.ts
299 lines (260 loc) · 8.73 KB
/
screen.ts
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import { cloneDeep, isObject } from 'lodash'
import { Component, ComponentOptions } from '../core/component'
import { makeRenderFunction, CanvasContent, Image, AOI } from './util/render'
import { makePathFunction } from './util/path'
import {
makeTransform,
makeInverseTransform,
transform,
} from './util/transform'
import { setupCanvas } from './util/dom'
import { makeProcessEvent } from './util/event'
// Canvas-based components -----------------------------------------------------
export const canvasDefaults = {
ctxType: '2d',
insertCanvasOnRun: false,
// Move origin to canvas center
translateOrigin: true,
// Scale a viewport to the entire available space
viewport: <[number, number]>[800, 600],
viewportScale: <number | 'auto'>'auto',
viewportEdge: false,
// Use high resolution if possible
devicePixelScaling: undefined, // replaced by true if unspecified
}
const screenDefaults = {
content: <CanvasContent[]>[],
renderFunction: <
| ((
t: number,
canvas: HTMLCanvasElement,
ctx: RenderingContext,
obj: Component,
) => void)
| null
>null,
clearCanvas: true,
}
export type ScreenOptions = ComponentOptions &
typeof screenDefaults &
typeof canvasDefaults
export class Screen extends Component {
options!: ScreenOptions
constructor(options: Partial<ScreenOptions> = {}) {
super({
...cloneDeep(canvasDefaults),
...cloneDeep(screenDefaults),
...options,
})
// Provide an attribute for tracking redraw requests
this.internals.frameRequest = null
// Bind render method
this.render = this.render.bind(this)
}
onPrepare() {
// Add images to cached media
;(this.options.content || [])
.filter(
<(c: CanvasContent) => c is Image>(
(c => isObject(c) && c.type === 'image' && c.src !== undefined)
),
)
.forEach(c => this.options.media!.images.push(c.src))
// Prepare AOI event handling
// Canvas.screen components implement one additional feature
// compared to any other component, namely the support for AOI
// filtering on events. In this case, the selector has the format
// @aoiName, which means we have to rewrite the target to be the
// current screen canvas, and also add a check to determine whether
// the event coordinates fall into the AOI region.
this.internals.domConnection.processEvent = makeProcessEvent(this)
// Generate generic render function,
// unless a render function has been defined manually
if (this.options.renderFunction === null) {
this.options.renderFunction = makeRenderFunction(
// Exclude AOIs, and perform at least a rudimentary check
(this.options.content || []).filter(
(c: CanvasContent) => isObject(c) && c.type !== 'aoi',
),
this.internals.controller.global.cache,
)
}
}
enterContext(context: object) {
const c = super.enterContext(context)
if ('canvas' in context) {
// Re-use canvas from surrounding context
//@ts-ignore we check for the presence of a canvas above
this.internals.canvas = context.canvas
this.internals.canvasAdded = false
} else {
// Generate local canvas
this.internals.canvas = document.createElement('canvas')
this.internals.canvasAdded = true
}
// Generate drawing context
this.internals.ctx = this.internals.canvas.getContext(this.options.ctxType)
return c
}
leaveContext(context: object) {
// TODO: remove this.internals.canvas and this.internals.ctx
return super.leaveContext(context)
}
onRun() {
if (this.internals.canvasAdded) {
// Add canvas to the dom, if necessary
// TODO: Review performance implications of using innerHTML
this.internals.context.el.innerHTML = ''
this.internals.context.el.appendChild(this.internals.canvas)
// Setup canvas dimensions
setupCanvas(this.internals.canvas, {
devicePixelScaling: this.options.devicePixelScaling,
})
}
// Coordinate system translation and scaling -------------------------------
// Save current transformation state
this.internals.ctx.save()
this.internals.transformationMatrix = makeTransform(
[this.internals.canvas.width, this.internals.canvas.height],
this.options.viewport,
{
translateOrigin: this.options.translateOrigin,
viewportScale: this.options.viewportScale,
devicePixelScaling: this.options.devicePixelScaling,
},
)
this.internals.ctx.setTransform(...this.internals.transformationMatrix)
}
onRender({ timestamp }: { timestamp: number }) {
// Clear canvas if requested
if (this.options.clearCanvas && !this.internals.canvasAdded) {
this.clear()
}
// Draw viewport for debugging purposes
if (this.options.viewportEdge) {
this.internals.ctx.save()
this.internals.ctx.strokeStyle = 'rgb(229, 229, 229)'
this.internals.ctx.strokeRect(
this.options.translateOrigin ? -this.options.viewport[0] / 2 : 0,
this.options.translateOrigin ? -this.options.viewport[1] / 2 : 0,
this.options.viewport[0],
this.options.viewport[1],
)
this.internals.ctx.restore()
}
return this.options.renderFunction!.call(
this,
timestamp - this.internals.timestamps.render,
this.internals.canvas,
this.internals.ctx,
this,
)
}
onShow() {
this.internals.paths = makePathFunction(
this.options.content.filter(
<(c: CanvasContent) => c is AOI>(c => c.type === 'aoi'),
),
)(
// TODO: Update paths
0,
this.internals.canvas,
this.internals.ctx,
)
}
queueAnimationFrame() {
this.internals.frameRequest = window.requestAnimationFrame(timestamp => {
if (this.options.clearCanvas) {
this.clear()
}
this.options.renderFunction!.call(
this,
timestamp - this.internals.timestamps.render,
this.internals.canvas,
this.internals.ctx,
this,
)
})
}
onEnd() {
// Context is extracted in onRun, and may not be present
// (i.e. if the component is skipped)
if (this.internals.ctx) {
// Undo any previously applied tranformations
this.internals.ctx.restore()
}
}
clear() {
this.internals.ctx.save()
this.internals.ctx.setTransform(1, 0, 0, 1, 0, 0)
this.internals.ctx.clearRect(
0,
0,
this.internals.canvas.width,
this.internals.canvas.height,
)
this.internals.ctx.restore()
}
transform(coordinates: [number, number]) {
if (!this.internals.transformationMatrix) {
throw new Error('No transformation matrix set')
}
return transform(this.internals.transformationMatrix, coordinates)
}
transformInverse(coordinates: [number, number], fromOffset = false) {
// Translate from the browser coordinate system back into canvas
// coordinates. Use either the page origin (with fromOffset=false),
// or the canvas origin (with fromOffset=true).
// TODO: Rethink argument names
if (!this.internals.transformationMatrix) {
throw new Error('No transformation matrix set')
}
// TODO: The inverse tranformation matrix is currently computed
// on-the-fly, based on the argument that it's not going to be
// used as frequently, and thus might not be worth generating
// for every canvas-based screen. This might be wrong, and it's
// worth thinking about a mechanism that would at least cache
// the inverse matrix once it's generated.
const inverseTransformationMatrix = makeInverseTransform(
[this.internals.canvas.width, this.internals.canvas.height],
this.options.viewport,
{
translateOrigin: this.options.translateOrigin,
viewportScale: this.options.viewportScale,
devicePixelScaling: this.options.devicePixelScaling,
canvasClientRect: this.internals.canvas.getBoundingClientRect(),
fromOffset,
},
)
return transform(inverseTransformationMatrix, coordinates)
}
transformCanvasEvent({ offsetX, offsetY }: MouseEvent) {
// Translate local event coordinates to canvas coordinate system
return this.transformInverse([offsetX, offsetY], true)
}
}
Screen.metadata = {
module: ['canvas'],
nestedComponents: [],
parsableOptions: {
content: {
type: 'array',
content: {
type: 'object',
content: {
text: {},
fill: {},
stroke: {},
strokeWidth: { type: 'number' },
left: { type: 'number' },
top: { type: 'number' },
width: { type: 'number' },
height: { type: 'number' },
angle: { type: 'number' },
src: {},
fontSize: { type: 'number' },
},
},
},
},
}