forked from Flipboard/react-canvas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrawingUtils.js
418 lines (366 loc) · 11.5 KB
/
DrawingUtils.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
'use strict';
var ImageCache = require('./ImageCache');
var FontUtils = require('./FontUtils');
var FontFace = require('./FontFace');
var FrameUtils = require('./FrameUtils');
var CanvasUtils = require('./CanvasUtils');
var Canvas = require('./Canvas');
// Global backing store <canvas> cache
var _backingStores = [];
/**
* Maintain a cache of backing <canvas> for RenderLayer's which are accessible
* through the RenderLayer's `backingStoreId` property.
*
* @param {String} id The unique `backingStoreId` for a RenderLayer
* @return {HTMLCanvasElement}
*/
function getBackingStore (id) {
for (var i=0, len=_backingStores.length; i < len; i++) {
if (_backingStores[i].id === id) {
return _backingStores[i].canvas;
}
}
return null;
}
/**
* Purge a layer's backing store from the cache.
*
* @param {String} id The layer's backingStoreId
*/
function invalidateBackingStore (id) {
for (var i=0, len=_backingStores.length; i < len; i++) {
if (_backingStores[i].id === id) {
_backingStores.splice(i, 1);
break;
}
}
}
/**
* Purge the entire backing store cache.
*/
function invalidateAllBackingStores () {
_backingStores = [];
}
/**
* Find the nearest backing store ancestor for a given layer.
*
* @param {RenderLayer} layer
*/
function getBackingStoreAncestor (layer) {
while (layer) {
if (layer.backingStoreId) {
return layer;
}
layer = layer.parentLayer;
}
return null;
}
/**
* Check if a layer is using a given image URL.
*
* @param {RenderLayer} layer
* @param {String} imageUrl
* @return {Boolean}
*/
function layerContainsImage (layer, imageUrl) {
// Check the layer itself.
if (layer.type === 'image' && layer.imageUrl === imageUrl) {
return layer;
}
// Check the layer's children.
if (layer.children) {
for (var i=0, len=layer.children.length; i < len; i++) {
if (layerContainsImage(layer.children[i], imageUrl)) {
return layer.children[i];
}
}
}
return false;
}
/**
* Check if a layer is using a given FontFace.
*
* @param {RenderLayer} layer
* @param {FontFace} fontFace
* @return {Boolean}
*/
function layerContainsFontFace (layer, fontFace) {
// Check the layer itself.
if (layer.type === 'text' && layer.fontFace && layer.fontFace.id === fontFace.id) {
return layer;
}
// Check the layer's children.
if (layer.children) {
for (var i=0, len=layer.children.length; i < len; i++) {
if (layerContainsFontFace(layer.children[i], fontFace)) {
return layer.children[i];
}
}
}
return false;
}
/**
* Invalidates the backing stores for layers which contain an image layer
* associated with the given imageUrl.
*
* @param {String} imageUrl
*/
function handleImageLoad (imageUrl) {
_backingStores.forEach(function (backingStore) {
if (layerContainsImage(backingStore.layer, imageUrl)) {
invalidateBackingStore(backingStore.id);
}
});
}
/**
* Invalidates the backing stores for layers which contain a text layer
* associated with the given font face.
*
* @param {FontFace} fontFace
*/
function handleFontLoad (fontFace) {
_backingStores.forEach(function (backingStore) {
if (layerContainsFontFace(backingStore.layer, fontFace)) {
invalidateBackingStore(backingStore.id);
}
});
}
/**
* Draw a RenderLayer instance to a <canvas> context.
*
* @param {CanvasRenderingContext2d} ctx
* @param {RenderLayer} layer
*/
function drawRenderLayer (ctx, layer) {
var customDrawFunc;
// Performance: avoid drawing hidden layers.
if (typeof layer.alpha === 'number' && layer.alpha <= 0) {
return;
}
switch (layer.type) {
case 'image':
customDrawFunc = drawImageRenderLayer;
break;
case 'text':
customDrawFunc = drawTextRenderLayer;
break;
case 'gradient':
customDrawFunc = drawGradientRenderLayer;
break;
}
// Establish drawing context for certain properties:
// - alpha
// - translate
var saveContext = (layer.alpha !== null && layer.alpha < 1) ||
(layer.translateX || layer.translateY);
if (saveContext) {
ctx.save();
// Alpha:
if (layer.alpha !== null && layer.alpha < 1) {
ctx.globalAlpha = layer.alpha;
}
// Translation:
if (layer.translateX || layer.translateY) {
ctx.translate(layer.translateX || 0, layer.translateY || 0);
}
}
// If the layer is bitmap-cacheable, draw in a pooled off-screen canvas.
// We disable backing stores on pad since we flip there.
if (layer.backingStoreId) {
drawCacheableRenderLayer(ctx, layer, customDrawFunc);
} else {
// Draw default properties, such as background color.
ctx.save();
drawBaseRenderLayer(ctx, layer);
// Draw custom properties if needed.
customDrawFunc && customDrawFunc(ctx, layer);
ctx.restore();
// Draw child layers, sorted by their z-index.
if (layer.children) {
layer.children.slice().sort(sortByZIndexAscending).forEach(function (childLayer) {
drawRenderLayer(ctx, childLayer);
});
}
}
// Pop the context state if we established a new drawing context.
if (saveContext) {
ctx.restore();
}
}
/**
* Draw base layer properties into a rendering context.
* NOTE: The caller is responsible for calling save() and restore() as needed.
*
* @param {CanvasRenderingContext2d} ctx
* @param {RenderLayer} layer
*/
function drawBaseRenderLayer (ctx, layer) {
var frame = layer.frame;
// Border radius:
if (layer.borderRadius) {
ctx.beginPath();
ctx.moveTo(frame.x + layer.borderRadius, frame.y);
ctx.arcTo(frame.x + frame.width, frame.y, frame.x + frame.width, frame.y + frame.height, layer.borderRadius);
ctx.arcTo(frame.x + frame.width, frame.y + frame.height, frame.x, frame.y + frame.height, layer.borderRadius);
ctx.arcTo(frame.x, frame.y + frame.height, frame.x, frame.y, layer.borderRadius);
ctx.arcTo(frame.x, frame.y, frame.x + frame.width, frame.y, layer.borderRadius);
ctx.closePath();
// Create a clipping path when drawing an image or using border radius.
if (layer.type === 'image') {
ctx.clip();
}
// Border with border radius:
if (layer.borderColor) {
ctx.lineWidth = layer.borderWidth || 1;
ctx.strokeStyle = layer.borderColor;
ctx.stroke();
}
}
// Border color (no border radius):
if (layer.borderColor && !layer.borderRadius) {
ctx.lineWidth = layer.borderWidth || 1;
ctx.strokeStyle = layer.borderColor;
ctx.strokeRect(frame.x, frame.y, frame.width, frame.height);
}
// Background color:
if (layer.backgroundColor) {
ctx.fillStyle = layer.backgroundColor;
if (layer.borderRadius) {
// Fill the current path when there is a borderRadius set.
ctx.fill();
} else {
ctx.fillRect(frame.x, frame.y, frame.width, frame.height);
}
}
}
/**
* Draw a bitmap-cacheable layer into a pooled <canvas>. The result will be
* drawn into the given context. This will populate the layer backing store
* cache with the result.
*
* @param {CanvasRenderingContext2d} ctx
* @param {RenderLayer} layer
* @param {Function} customDrawFunc
* @private
*/
function drawCacheableRenderLayer (ctx, layer, customDrawFunc) {
// See if there is a pre-drawn canvas in the pool.
var backingStore = getBackingStore(layer.backingStoreId);
var backingStoreScale = layer.scale || window.devicePixelRatio;
var frameOffsetY = layer.frame.y;
var frameOffsetX = layer.frame.x;
var backingContext;
if (!backingStore) {
if (_backingStores.length >= Canvas.poolSize) {
// Re-use the oldest backing store once we reach the pooling limit.
backingStore = _backingStores[0].canvas;
Canvas.call(backingStore, layer.frame.width, layer.frame.height, backingStoreScale);
// Move the re-use canvas to the front of the queue.
_backingStores[0].id = layer.backingStoreId;
_backingStores[0].canvas = backingStore;
_backingStores.push(_backingStores.shift());
} else {
// Create a new backing store, we haven't yet reached the pooling limit
backingStore = new Canvas(layer.frame.width, layer.frame.height, backingStoreScale);
_backingStores.push({
id: layer.backingStoreId,
layer: layer,
canvas: backingStore
});
}
// Draw into the backing <canvas> at (0, 0) - we will later use the
// <canvas> to draw the layer as an image at the proper coordinates.
backingContext = backingStore.getContext('2d');
layer.translate(-frameOffsetX, -frameOffsetY);
// Draw default properties, such as background color.
backingContext.save();
drawBaseRenderLayer(backingContext, layer);
// Custom drawing operations
customDrawFunc && customDrawFunc(backingContext, layer);
backingContext.restore();
// Draw child layers, sorted by their z-index.
if (layer.children) {
layer.children.slice().sort(sortByZIndexAscending).forEach(function (childLayer) {
drawRenderLayer(backingContext, childLayer);
});
}
// Restore layer's original frame.
layer.translate(frameOffsetX, frameOffsetY);
}
// We have the pre-rendered canvas ready, draw it into the destination canvas.
if (layer.clipRect) {
// Fill the clipping rect in the destination canvas.
var sx = (layer.clipRect.x - layer.frame.x) * backingStoreScale;
var sy = (layer.clipRect.y - layer.frame.y) * backingStoreScale;
var sw = layer.clipRect.width * backingStoreScale;
var sh = layer.clipRect.height * backingStoreScale;
var dx = layer.clipRect.x;
var dy = layer.clipRect.y;
var dw = layer.clipRect.width;
var dh = layer.clipRect.height;
// No-op for zero size rects. iOS / Safari will throw an exception.
if (sw > 0 && sh > 0) {
ctx.drawImage(backingStore.getRawCanvas(), sx, sy, sw, sh, dx, dy, dw, dh);
}
} else {
// Fill the entire canvas
ctx.drawImage(backingStore.getRawCanvas(), layer.frame.x, layer.frame.y, layer.frame.width, layer.frame.height);
}
}
/**
* @private
*/
function sortByZIndexAscending (layerA, layerB) {
return (layerA.zIndex || 0) - (layerB.zIndex || 0);
}
/**
* @private
*/
function drawImageRenderLayer (ctx, layer) {
if (!layer.imageUrl) {
return;
}
// Don't draw until loaded
var image = ImageCache.get(layer.imageUrl);
if (!image.isLoaded()) {
return;
}
CanvasUtils.drawImage(ctx, image, layer.frame.x, layer.frame.y, layer.frame.width, layer.frame.height);
}
/**
* @private
*/
function drawTextRenderLayer (ctx, layer) {
// Fallback to standard font.
var fontFace = layer.fontFace || FontFace.Default();
// Don't draw text until loaded
if (!FontUtils.isFontLoaded(fontFace)) {
return;
}
CanvasUtils.drawText(ctx, layer.text, layer.frame.x, layer.frame.y, layer.frame.width, layer.frame.height, fontFace, {
fontSize: layer.fontSize,
lineHeight: layer.lineHeight,
textAlign: layer.textAlign,
color: layer.color
});
}
/**
* @private
*/
function drawGradientRenderLayer (ctx, layer) {
// Default to linear gradient from top to bottom.
var x1 = layer.x1 || layer.frame.x;
var y1 = layer.y1 || layer.frame.y;
var x2 = layer.x2 || layer.frame.x;
var y2 = layer.y2 || layer.frame.y + layer.frame.height;
CanvasUtils.drawGradient(ctx, x1, y1, x2, y2, layer.colorStops, layer.frame.x, layer.frame.y, layer.frame.width, layer.frame.height);
}
module.exports = {
drawRenderLayer: drawRenderLayer,
invalidateBackingStore: invalidateBackingStore,
invalidateAllBackingStores: invalidateAllBackingStores,
handleImageLoad: handleImageLoad,
handleFontLoad: handleFontLoad,
layerContainsImage: layerContainsImage,
layerContainsFontFace: layerContainsFontFace
};