-
Notifications
You must be signed in to change notification settings - Fork 923
/
Surface.js
executable file
·233 lines (198 loc) · 6.17 KB
/
Surface.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
'use strict';
var React = require('react');
var ReactUpdates = require('react-dom/lib/ReactUpdates');
var invariant = require('fbjs/lib/invariant');
var ContainerMixin = require('./ContainerMixin');
var RenderLayer = require('./RenderLayer');
var FrameUtils = require('./FrameUtils');
var DrawingUtils = require('./DrawingUtils');
var hitTest = require('./hitTest');
var layoutNode = require('./layoutNode');
/**
* Surface is a standard React component and acts as the main drawing canvas.
* ReactCanvas components cannot be rendered outside a Surface.
*/
var Surface = React.createClass({
mixins: [ContainerMixin],
propTypes: {
className: React.PropTypes.string,
id: React.PropTypes.string,
top: React.PropTypes.number.isRequired,
left: React.PropTypes.number.isRequired,
width: React.PropTypes.number.isRequired,
height: React.PropTypes.number.isRequired,
scale: React.PropTypes.number.isRequired,
enableCSSLayout: React.PropTypes.bool
},
getDefaultProps: function () {
return {
scale: window.devicePixelRatio || 1
};
},
componentDidMount: function () {
// Prepare the <canvas> for drawing.
this.scale();
// ContainerMixin expects `this.node` to be set prior to mounting children.
// `this.node` is injected into child components and represents the current
// render tree.
this.node = new RenderLayer();
this.node.frame = FrameUtils.make(this.props.left, this.props.top, this.props.width, this.props.height);
this.node.draw = this.batchedTick;
// This is the integration point between custom canvas components and React
var transaction = ReactUpdates.ReactReconcileTransaction.getPooled();
transaction.perform(
this.mountAndInjectChildrenAtRoot,
this,
this.props.children,
transaction
);
ReactUpdates.ReactReconcileTransaction.release(transaction);
// Execute initial draw on mount.
this.node.draw();
},
componentWillUnmount: function () {
// Implemented in ReactMultiChild.Mixin
this.unmountChildren();
},
componentDidUpdate: function (prevProps, prevState) {
// We have to manually apply child reconciliation since child are not
// declared in render().
var transaction = ReactUpdates.ReactReconcileTransaction.getPooled();
transaction.perform(
this.updateChildrenAtRoot,
this,
this.props.children,
transaction
);
ReactUpdates.ReactReconcileTransaction.release(transaction);
// Re-scale the <canvas> when changing size.
if (prevProps.width !== this.props.width || prevProps.height !== this.props.height) {
this.scale();
}
// Redraw updated render tree to <canvas>.
if (this.node) {
this.node.draw();
}
},
render: function () {
// Scale the drawing area to match DPI.
var width = this.props.width * this.props.scale;
var height = this.props.height * this.props.scale;
var style = {
width: this.props.width,
height: this.props.height
};
return (
React.createElement('canvas', {
ref: 'canvas',
className: this.props.className,
id: this.props.id,
width: width,
height: height,
style: style,
onTouchStart: this.handleTouchStart,
onTouchMove: this.handleTouchMove,
onTouchEnd: this.handleTouchEnd,
onTouchCancel: this.handleTouchEnd,
onClick: this.handleClick,
onContextMenu: this.handleContextMenu,
onDoubleClick: this.handleDoubleClick})
);
},
// Drawing
// =======
getContext: function () {
('production' !== process.env.NODE_ENV ? invariant(
this.isMounted(),
'Tried to access drawing context on an unmounted Surface.'
) : invariant(this.isMounted()));
return this.refs.canvas.getContext('2d');
},
scale: function () {
this.getContext().scale(this.props.scale, this.props.scale);
},
batchedTick: function () {
if (this._frameReady === false) {
this._pendingTick = true;
return;
}
this.tick();
},
tick: function () {
// Block updates until next animation frame.
this._frameReady = false;
this.clear();
this.draw();
requestAnimationFrame(this.afterTick);
},
afterTick: function () {
// Execute pending draw that may have been scheduled during previous frame
this._frameReady = true;
if (this._pendingTick) {
this._pendingTick = false;
this.batchedTick();
}
},
clear: function () {
this.getContext().clearRect(0, 0, this.props.width, this.props.height);
},
draw: function () {
var layout;
if (this.node) {
if (this.props.enableCSSLayout) {
layout = layoutNode(this.node);
}
DrawingUtils.drawRenderLayer(this.getContext(), this.node);
}
},
// Events
// ======
hitTest: function (e) {
var hitTarget = hitTest(e, this.node, this.refs.canvas);
if (hitTarget) {
hitTarget[hitTest.getHitHandle(e.type)](e);
}
},
handleTouchStart: function (e) {
var hitTarget = hitTest(e, this.node, this.refs.canvas);
var touch;
if (hitTarget) {
// On touchstart: capture the current hit target for the given touch.
this._touches = this._touches || {};
for (var i=0, len=e.touches.length; i < len; i++) {
touch = e.touches[i];
this._touches[touch.identifier] = hitTarget;
}
hitTarget[hitTest.getHitHandle(e.type)](e);
}
},
handleTouchMove: function (e) {
this.hitTest(e);
},
handleTouchEnd: function (e) {
// touchend events do not generate a pageX/pageY so we rely
// on the currently captured touch targets.
if (!this._touches) {
return;
}
var hitTarget;
var hitHandle = hitTest.getHitHandle(e.type);
for (var i=0, len=e.changedTouches.length; i < len; i++) {
hitTarget = this._touches[e.changedTouches[i].identifier];
if (hitTarget && hitTarget[hitHandle]) {
hitTarget[hitHandle](e);
}
delete this._touches[e.changedTouches[i].identifier];
}
},
handleClick: function (e) {
this.hitTest(e);
},
handleContextMenu: function (e) {
this.hitTest(e);
},
handleDoubleClick: function (e) {
this.hitTest(e);
},
});
module.exports = Surface;