简体   繁体   中英

Syncrhonous scrolling of 2 separate canvases on a single page

I have 2 canvases:-

<canvas id="myCanvas" width="915" height="650" style="border: 2px double #000000;"></canvas>
    <canvas id="pharmacy" width="915" height ="320" style ="border:2px double #000000;"></canvas>

They are separate canvases, on the same aspx page.

The idea is to have them both represent a timeline. ie they are both scrollable left to right and vice-versa.

I had created the first canvas a while ago, and recently decided to implement the 2nd canvas.

When I copy pasted the original code in the 2nd canvas to emulate the scrolling part, I figured out that in practice, when I scrolled , only the original canvas would scroll and the new canvas doesn't.

If I comment out the code for the original canvas then the new canvas scrolls.

Which led me to think, that the event based scrolling I was trying to achieve has some form of flaw in its naming convention. (since a mouse click is represented as an event and then dragging is allowed; my code was only registering dragging event of the original canvas and not the new canvas).

http://jsfiddle.net/przBL/3/

I would greatly appreciate if someone could take a look at the code and let me know where I am going wrong??

The goal:- is to click on either of the canvases, drag the mouse, and both canvases scroll at the same time.

1st canvas:-

 // when mouse is clicked on canvas
        window.onmousedown = function (e) {
            var evt = e || event;
            // dragging is set to true.
            dragging = true;
            lastX = evt.offsetX;
        }

        // when mouse is clicked again and the canvas is deselected  
        window.onmouseup = function () {
            // dragging is set to false.
            dragging = false;
        }

        // when mouse is dragging the canvas sideways
        can.onmousemove = function (e) {
            var evt = e || event;
            if (dragging) {
                var delta = evt.offsetX - lastX;
                translated += delta;
                //console.log(translated);
                ctx.restore();
                ctx.clearRect(0, 0, 930, 900);
                ctx.save();
                ctx.translate(translated, 0);
                lastX = evt.offsetX;
                timeline();
            }
        }

2nd canvas:-

   // when mouse is clicked on canvas
   window.onmousedown = function (e) {
       var evt = e || event;
       // dragging is set to true.
       dragging = true;
       lastX = evt.offsetX;
   }


   // when mouse is clicked again and the canvas is deselected  
   window.onmouseup = function () {
       // dragging is set to false.
       dragging = false;
   }



   // when mouse is dragging the canvas sideways
   can1.onmousemove = function (e) {
       var evt = e || event;
       if (dragging) {
           var delta = evt.offsetX - lastX;
           translated += delta;
           //console.log(translated);
           ctx1.restore();
           ctx1.clearRect(0, 0, 915, 600);
           ctx1.save();
           ctx1.translate(translated, 0);
           lastX = evt.offsetX;
           pharm_line();
       }
   }

now I want both can and can1 to move synchronously on any mousedown and mousemove event and stop when mouse is up.

You can use common mouse-handlers to identically scroll both canvas's.

See window.onmousemove below which keeps both canvas's in translated sync.

 // when mouse is clicked on canvas
 window.onmousedown = function (e) {
     var evt = e || event;
     // dragging is set to true.
     dragging = true;
     lastX = evt.offsetX;
 }


 // when mouse is clicked again and the canvas is deselected  
 window.onmouseup = function (e) {
     // dragging is set to false.
     dragging = false;
 }


 window.onmousemove = function(e) {
      var evt = e || event;
      if (dragging) {
          var delta = evt.offsetX - lastX;
          translated += delta;
          move(ctx,930,900);
          move(ctx1,915,600);
          lastX = evt.offsetX;
          timeline();
          pharm_line();
      }
  }

  // common code used to service either canvas
  function move(context,width,height){
      context.restore();
      context.clearRect(0, 0, width, height);
      context.save();
      context.translate(translated, 0);
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM