forked from Flipboard/react-canvas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCanvas.js
41 lines (29 loc) · 968 Bytes
/
Canvas.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
'use strict';
var assign = require('react/lib/Object.assign');
// Note that this class intentionally does not use PooledClass.
// DrawingUtils manages <canvas> pooling for more fine-grained control.
function Canvas (width, height, scale) {
// Re-purposing an existing canvas element.
if (!this._canvas) {
this._canvas = document.createElement('canvas');
}
this.width = width;
this.height = height;
this.scale = scale || window.devicePixelRatio;
this._canvas.width = this.width * this.scale;
this._canvas.height = this.height * this.scale;
this._canvas.getContext('2d').scale(this.scale, this.scale);
}
assign(Canvas.prototype, {
getRawCanvas: function () {
return this._canvas;
},
getContext: function () {
return this._canvas.getContext('2d');
}
});
// PooledClass:
// Be fairly conserative - we are potentially drawing a large number of medium
// to large size images.
Canvas.poolSize = 30;
module.exports = Canvas;