0

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. i.e 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.

4

1 に答える 1