title: RXJS draw line on html5 canvas
body: <p>I'm trying to achieve the same effect I'm posting here using Reactive Extensions for Javascript (RX-JS).
  I'm a bit puzzled on how to do it.
  Here is the page:</p>
  
  <pre><code>      &lt;!DOCTYPE html&gt;
        &lt;html&gt;
        &lt;head&gt;
          &lt;title&gt;drag and drop&lt;/title&gt;
        &lt;/head&gt;
        &lt;style type="text/css"&gt;
  
        canvas {
          border:1px solid steelblue;
          background-color: whitesmoke;
        }
  
        &lt;/style&gt;
        &lt;body&gt;
          &lt;canvas id="canvas" width=300 height=300&gt;&lt;/canvas&gt;
          &lt;script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"&gt;&lt;/script&gt;
          &lt;script type="text/javascript"&gt;
          $(function() {
              var canvas = document.getElementById("canvas");
              var ctx = canvas.getContext("2d");
  
              var canvasOffset = $("#canvas").offset();
              var offsetX = canvasOffset.left;
              var offsetY = canvasOffset.top;
  
              var drawing = false;
              var mouseX = 0;
              var mouseY = 0;
  
  
              function handleMouseDown(e) {
                  mouseX = parseInt(e.clientX - offsetX);
                  mouseY = parseInt(e.clientY - offsetY);         
                  drawing= true;                
          }
  
          function handleMouseUp(e) {             
              drawing = false;    
          }
          function handleMouseMove(e) {
              if(drawing){
                mouseeX = parseInt(e.clientX - offsetX);
                mouseeY = parseInt(e.clientY - offsetY);
                $("#movelog").html("Move: " + mouseX + " / " + mouseY);
  
                  var ctx =  canvas.getContext("2d");
                  // some cleanup code
                  ctx.save();
                  ctx.setTransform(1, 0, 0, 1, 0, 0);
                  ctx.clearRect(0, 0, canvas.width, canvas.height);
                  ctx.restore();          
                  ctx.beginPath();
                  ctx.moveTo(mouseX,mouseY);
                  ctx.lineTo(mouseeX,mouseeY);
                  ctx.stroke();
               }
          }
  
          $("#canvas").mousedown(function(e) {
              handleMouseDown(e);
          });
          $("#canvas").mousemove(function(e) {
              handleMouseMove(e);
          });
          $("#canvas").mouseup(function(e) {
              handleMouseUp(e);
          });
  
        });
        &lt;/script&gt; 
        &lt;/body&gt;
        &lt;/html&gt;
  </code></pre>
  
  <p>I think I should create observables for the mouseDown, mouseMove and mouseUp events.</p>
  
  <pre><code>    var mouseDown = Rx.Observable.fromEvent(canvas, 'mousedown');
      var mouseMove = Rx.Observable.fromEvent(canvas, 'mousemove');
      var mouseUp = Rx.Observable.fromEvent(canvas, 'mouseup');
  </code></pre>
  
  <p>but I don't know how to combine them. I think should start observing the mousedown, then collect all the moves until the mouseup is raised, and in the whil redraw the line from the starting point to the current point where the mouse is during the mousemove. 
  Do you have any ideas? Thanks a lot. </p>
  
  <p>°°°°°°°°°°°°°°°°°°°°°°EDIT°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°</p>
  
  <p>Here is my code after the answer by Brandon:</p>
  
  <pre><code>              $(function() {
  
                    var canvas = document.getElementById('canvas');
                    var ctx = canvas.getContext("2d");
                    var canvasOffset = $("#canvas").offset();
                    var offsetX = canvasOffset.left;
                    var offsetY = canvasOffset.top;
  
                    var mouseDown = Rx.Observable.fromEvent($("#canvas"), 'mousedown');
                    var mouseMove = Rx.Observable.fromEvent($("#canvas"), 'mousemove');
                    var mouseUp = Rx.Observable.fromEvent($("#canvas"), 'mouseup');
  
                    // keep a reference to the pisition when the mouse down was fired
                    // then flatten the stream with concatAll
  
  
                    var traceLineStream = mouseDown.map(function(md) {
                        var movesFromMouseDown = mouseMove.takeUntil(mouseUp);
                        var movesFromMouseDownAndMouseDown = movesFromMouseDown.map(function(mm) {
                            return {
                                mouseDownPoint: md,
                                mouseMovePoint: mm
                            }
                        });
                        return movesFromMouseDownAndMouseDown;
                    }).concatAll();
  
  
                    var subscription = traceLineStream.subscribe(
                        function(y) {
  
                            var mouseDown = y.mouseDownPoint;
                            var mouseMove = y.mouseMovePoint;
  
                            var mouseDownX = parseInt(mouseDown.clientX - offsetX);
                            var mouseDownY = parseInt(mouseDown.clientY - offsetY);               
  
                            var mouseMoveX = parseInt(mouseMove.clientX - offsetX);
                            var mouseMoveY = parseInt(mouseMove.clientY - offsetY);
  
                            ctx.save();
                            ctx.setTransform(1, 0, 0, 1, 0, 0);
                            ctx.clearRect(0, 0, canvas.width, canvas.height);
                            ctx.restore();
                            ctx.beginPath();
                            ctx.moveTo(mouseDownX, mouseDownY);
                            ctx.lineTo(mouseMoveX, mouseMoveY);
                            ctx.stroke();
  
                        },
                        function(e) {
                            console.log('onError: ' + e.message);
                        },
                        function() {
                            console.log('onCompleted');
                        });
                });
  </code></pre>