Skip to content
This repository was archived by the owner on Apr 20, 2018. It is now read-only.

Commit 6809e2f

Browse files
Updating examples
1 parent 7f8c6ab commit 6809e2f

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

examples/canvaspaint.html

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<!DOCTYPE html>
2+
<head>
3+
<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
4+
<script src="rx.min.js" type="text/javascript"></script>
5+
<script src="rx.jquery.js" type="text/javascript"></script>
6+
<script type="text/javascript">
7+
8+
function getOffset(event) {
9+
return {
10+
offsetX: event.offsetX === undefined ? event.layerX : event.offsetX,
11+
offsetY: event.offsetY === undefined ? event.layerY : event.offsetY
12+
};
13+
}
14+
15+
$(function () {
16+
var canvas = document.getElementById('tutorial');
17+
if (canvas.getContext) {
18+
var ctx = canvas.getContext('2d');
19+
ctx.beginPath();
20+
21+
var cv = $('#tutorial');
22+
23+
// Calculate mouse deltas
24+
var mouseMoves = cv.onAsObservable('mousemove')
25+
, mouseDiffs = mouseMoves.bufferWithCount(2, 1).select(function (x) {
26+
return { first: getOffset(x[0]), second: getOffset(x[1]) };
27+
})
28+
29+
// Merge mouse down and mouse up
30+
, mouseButton = cv.onAsObservable('mousedown').select(function (x) { return true; })
31+
.merge(cv.onAsObservable('mouseup').select(function (x) { return false; }))
32+
33+
// Determine whether to paint or lift
34+
, paint = mouseButton.select(function (down) { return down ? mouseDiffs : mouseDiffs.take(0) }).switchLatest();
35+
36+
// Paint the results
37+
paint.subscribe(function (x) {
38+
ctx.moveTo(x.first.offsetX, x.first.offsetY);
39+
ctx.lineTo(x.second.offsetX, x.second.offsetY);
40+
ctx.stroke();
41+
});
42+
}
43+
});
44+
</script>
45+
</head>
46+
<body>
47+
<canvas id="tutorial" width="640" height="480"></canvas>
48+
</body>
49+
</html>

examples/dragndrop.html

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<!DOCTYPE html>
2+
<head>
3+
<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
4+
<script src="rx.min.js" type="text/javascript"></script>
5+
<script src="rx.jquery.js" type="text/javascript"></script>
6+
<script type="text/javascript">
7+
8+
$(function () {
9+
10+
var dragTarget = $('#dragTarget')
11+
12+
// Get the three major events
13+
, mouseup = dragTarget.onAsObservable('mouseup')
14+
, mousemove = dragTarget.onAsObservable('mousemove')
15+
, mousedown = dragTarget.onAsObservable('mousedown').select(function (event) {
16+
// calculate offsets when mouse down
17+
event.preventDefault();
18+
return { left: event.clientX - dragTarget.offset().left, top: event.clientY - dragTarget.offset().top };
19+
})
20+
21+
// Combine mouse down with mouse move until mouse up
22+
, mousedrag = mousedown.selectMany(function(imageOffset) {
23+
return mousemove.select(function (pos) {
24+
// calculate offsets from mouse down to mouse moves
25+
return {
26+
left: pos.clientX - imageOffset.left, top: pos.clientY - imageOffset.top
27+
};
28+
}).takeUntil(mouseup);
29+
});
30+
31+
mousedrag.subscribe (function (pos) {
32+
// Update position
33+
$('#dragTarget').css({top: pos.top, left: pos.left});
34+
});
35+
});
36+
</script>
37+
</head>
38+
<body>
39+
<div id="dragTarget"
40+
style="background-color: #000000; border: 1px solid #666666;
41+
color: #ffffff; padding: 10px; position: absolute;
42+
font-family: sans-serif; cursor: move">
43+
Drag Me!
44+
</div>
45+
</body>
46+
</html>

0 commit comments

Comments
 (0)