-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
78 lines (70 loc) · 1.72 KB
/
app.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
'use strict'
const domready = require('domready')
const React = require('react')
function createRect (a, b) {
return {
x: Math.min(a.x, b.x),
y: Math.min(a.y, b.y),
width: Math.abs(a.x - b.x),
height: Math.abs(a.y - b.y)
}
}
const App = React.createClass({
getInitialState: function () {
return {
x: 0,
y: 0,
cropping: false,
downPoint: {},
rect: {}
}
},
render: function () {
const self = this
return React.DOM.div({
className: 'window',
onMouseMove: function (e) {
self.setState({
x: e.clientX,
y: e.clientY
})
if (!self.state.cropping) return
self.setState({
rect: createRect(
self.state.downPoint,
{
x: e.clientX,
y: e.clientY
}
)
})
},
onMouseUp: function (e) {
console.log(JSON.stringify(self.state.rect))
self.setState({ cropping: false, rect: {} })
},
onMouseDown: function (e) {
self.setState({ downPoint: { x: e.clientX, y: e.clientY }, cropping: true })
}
}, [
React.DOM.div(
{
className: 'rect',
key: 'rect',
style: { left: self.state.rect.x, top: self.state.rect.y, width: self.state.rect.width, height: self.state.rect.height }
}
),
React.DOM.div(
{
className: 'cursor',
key: 'cursor',
style: { left: self.state.x, top: self.state.y }
},
React.DOM.div({ className: 'indicator'}, `${self.state.x}\n${self.state.y}`)
)
])
}
})
domready(function () {
React.render(React.createFactory(App)(), document.querySelector('body'))
})