forked from Reactive-Extensions/RxJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrop.js
More file actions
179 lines (145 loc) · 4.94 KB
/
Copy pathcrop.js
File metadata and controls
179 lines (145 loc) · 4.94 KB
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
(function (window) {
var // a model for the region of the image about to be cropped
boundingBox,
// an array of draggable elements that modify the crop region
handles = [],
// `overlay` is a canvas element that allows us to darken the portion of the image that will be removed.
overlay,
// `ctx` will be the drawing context for `overlay`
ctx;
function loadImage () {
var // `buffer` is a canvas element that displays the actual image to crop
buffer = document.getElementById('buffer'),
// `img` is an img element we use to load the img, though we never add it to the DOM
img = document.createElement('img');
img.src = 'images/leaf twirl.jpg';
// Returns an observable which fires when the image is loaded
return Rx.Observable.fromEvent(img, 'load').map(function () {
overlay.width = img.width;
overlay.height = img.height;
buffer.width = img.width;
buffer.height = img.height;
buffer.getContext('2d').drawImage(img, 0, 0);
return {
width: img.width,
height: img.height
};
});
}
function initBoundingBox(size) {
boundingBox = {
x: 0,
y: 0,
x2: size.width,
y2: size.height
};
}
function createHandles () {
var container = document.getElementById('container');
function createHandle (id, render, updateModel) {
var handle = document.createElement('div');
handle.className += ' handle';
handle.setAttribute('id', id);
container.appendChild(handle);
// `render` allows us to visually update the handle after it has been dragged
handle['render'] = render;
// `updateModel` allows us to modify the correct part of the crop region model
handle['updateModel'] = updateModel;
handles.push(handle);
}
// top left
createHandle('tl', function () {
this.style.top = boundingBox.y + 'px';
this.style.left = boundingBox.x + 'px';
}, function (x, y) {
boundingBox.x = x;
boundingBox.y = y;
});
//top right
createHandle('tr', function () {
this.style.top = boundingBox.y + 'px';
this.style.left = boundingBox.x2 + 'px';
}, function (x, y) {
boundingBox.y = y;
boundingBox.x2 = x;
});
// bottom left
createHandle('bl', function (s) {
this.style.top = boundingBox.y2 + 'px';
this.style.left = boundingBox.x + 'px';
}, function (x, y) {
boundingBox.x = x;
boundingBox.y2 = y;
});
// bottom right
createHandle('br', function (s) {
this.style.top = boundingBox.y2 + 'px';
this.style.left = boundingBox.x2 + 'px';
}, function (x, y) {
boundingBox.y2 = y;
boundingBox.x2 = x;
});
// render the handles in their initial positiions
handles.forEach(function (element) { element['render'](); });
}
function respondToGestures() {
// Check for pointer events
var moves = 'mousemove', downs = 'mousedown', ups = 'mouseup';
if (window.navigator.pointerEnabled) {
moves = 'pointermove';
downs = 'pointerdown';
ups = 'pointerup';
}
var fromEvent = Rx.Observable.fromEvent,
move = fromEvent(overlay, moves),
up = fromEvent(document, ups),
down = fromEvent(handles, downs);
// When the mouse is down on a handle, return the handle element
return down.flatMap(function (handle) {
handle.preventDefault();
return move
// We combine the handle element with the position data from the move event
.map(function (pos) {
return {
element: handle.target,
offsetX: pos.offsetX,
offsetY: pos.offsetY
};
})
// However, when the mouse is up (anywhere on the document) then stop the stream
.takeUntil(up);
});
}
function drawOverlay() {
var x = boundingBox.x,
y = boundingBox.y,
w = boundingBox.x2 - boundingBox.x,
h = boundingBox.y2 - boundingBox.y;
ctx.globalCompositeOperation = 'source-over';
ctx.clearRect(0, 0, overlay.width, overlay.height);
ctx.fillStyle = 'rgba(0,0,0,0.7)';
ctx.fillRect(0, 0, overlay.width, overlay.height);
ctx.globalCompositeOperation = 'destination-out';
ctx.fillStyle = 'rgba(0,0,0,1)';
ctx.fillRect(x, y, w, h);
ctx.fill();
handles.forEach(function (tool) { tool['render'](); });
}
function main () {
overlay = document.getElementById('overlay');
ctx = overlay.getContext('2d');
var subscription = loadImage()
.flatMap(function (size) {
// Initialize after load
initBoundingBox(size);
createHandles();
return respondToGestures();
})
.subscribe(function (data) {
// Update model and redraw via an async operation
data.element.updateModel(data.offsetX, data.offsetY);
Rx.Scheduler.requestAnimationFrame.schedule(drawOverlay);
});
}
main();
}(window));