Catch pinch-zoom in browser2019 Community Moderator ElectionHow do you disable browser Autocomplete on web form field / input tag?How to align checkboxes and their labels consistently cross-browsersHow to make a div 100% height of the browser window?Manual Pinch-Zoom event handling on iOS Safari after event.preventDefault();Pinch to zoom on canvasJavascript support for touch without blocking browsers pinch supportAdding pinch zoom to a UICollectionViewChrome on surface tablet disable sweep after pinch zoomCanvas shift while performing Pinch-zoom and Panning on Zoomed CanvasHammer.js: How to maintain one finger for window scroll, two fingers for image pinch/zoom/pan, etc

It's a yearly task, alright

Sword in the Stone story where the sword was held in place by electromagnets

Excess Zinc in garden soil

what does the apostrophe mean in this notation?

Latest web browser compatible with Windows 98

Touchscreen-controlled dentist office snowman collector game

Are there situations where a child is permitted to refer to their parent by their first name?

How is the Swiss post e-voting system supposed to work, and how was it wrong?

What happens with multiple copies of Humility and Glorious Anthem on the battlefield?

Who is our nearest neighbor

Is "history" a male-biased word ("his+story")?

Examples of odd-dimensional manifolds that do not admit contact structure

Am I not good enough for you?

Humans have energy, but not water. What happens?

Running a subshell from the middle of the current command

Do I need to leave some extra space available on the disk which my database log files reside, for log backup operations to successfully occur?

Ban on all campaign finance?

Decoding assembly instructions in a Game Boy disassembler

Why does Deadpool say "You're welcome, Canada," after shooting Ryan Reynolds in the end credits?

Coworker uses her breast-pump everywhere in the office

When were linguistics departments first established

How to make readers know that my work has used a hidden constraint?

Can infringement of a trademark be pursued for using a company's name in a sentence?

redhat 7 + How to stop systemctl service permanent



Catch pinch-zoom in browser



2019 Community Moderator ElectionHow do you disable browser Autocomplete on web form field / input tag?How to align checkboxes and their labels consistently cross-browsersHow to make a div 100% height of the browser window?Manual Pinch-Zoom event handling on iOS Safari after event.preventDefault();Pinch to zoom on canvasJavascript support for touch without blocking browsers pinch supportAdding pinch zoom to a UICollectionViewChrome on surface tablet disable sweep after pinch zoomCanvas shift while performing Pinch-zoom and Panning on Zoomed CanvasHammer.js: How to maintain one finger for window scroll, two fingers for image pinch/zoom/pan, etc










0















I have a web application that is basically a canvas you can draw on. I'm handling the drawing - either by mouse or by single finger touch - and it's working well.



The canvas is larger than the screen, and I need to implement my own zooming mechanism (because I only want to zoom the canvas, not the entire screen). I've added a handler to the wheel event, and I process Control-Wheels properly. Now I need to handle pinch-zooms.



My canvas as a track-action: pinch-zoom CSS settings. This enables two finger swipes for scrolling, which is excellent. I want to let the browser handle pinch-zooms, too, but I want to handle the zooming logic myself (like I do with the mouse wheel).



Is there any way to catch the pinch-zoom event and override the zooming behavior?



I'm using Chrome on Android, but this technique should also work with Safari on iOS.










share|improve this question















This question has an open bounty worth +100
reputation from zmbq ending ending at 2019-03-17 06:30:15Z">in 3 days.


This question has not received enough attention.




















    0















    I have a web application that is basically a canvas you can draw on. I'm handling the drawing - either by mouse or by single finger touch - and it's working well.



    The canvas is larger than the screen, and I need to implement my own zooming mechanism (because I only want to zoom the canvas, not the entire screen). I've added a handler to the wheel event, and I process Control-Wheels properly. Now I need to handle pinch-zooms.



    My canvas as a track-action: pinch-zoom CSS settings. This enables two finger swipes for scrolling, which is excellent. I want to let the browser handle pinch-zooms, too, but I want to handle the zooming logic myself (like I do with the mouse wheel).



    Is there any way to catch the pinch-zoom event and override the zooming behavior?



    I'm using Chrome on Android, but this technique should also work with Safari on iOS.










    share|improve this question















    This question has an open bounty worth +100
    reputation from zmbq ending ending at 2019-03-17 06:30:15Z">in 3 days.


    This question has not received enough attention.


















      0












      0








      0








      I have a web application that is basically a canvas you can draw on. I'm handling the drawing - either by mouse or by single finger touch - and it's working well.



      The canvas is larger than the screen, and I need to implement my own zooming mechanism (because I only want to zoom the canvas, not the entire screen). I've added a handler to the wheel event, and I process Control-Wheels properly. Now I need to handle pinch-zooms.



      My canvas as a track-action: pinch-zoom CSS settings. This enables two finger swipes for scrolling, which is excellent. I want to let the browser handle pinch-zooms, too, but I want to handle the zooming logic myself (like I do with the mouse wheel).



      Is there any way to catch the pinch-zoom event and override the zooming behavior?



      I'm using Chrome on Android, but this technique should also work with Safari on iOS.










      share|improve this question














      I have a web application that is basically a canvas you can draw on. I'm handling the drawing - either by mouse or by single finger touch - and it's working well.



      The canvas is larger than the screen, and I need to implement my own zooming mechanism (because I only want to zoom the canvas, not the entire screen). I've added a handler to the wheel event, and I process Control-Wheels properly. Now I need to handle pinch-zooms.



      My canvas as a track-action: pinch-zoom CSS settings. This enables two finger swipes for scrolling, which is excellent. I want to let the browser handle pinch-zooms, too, but I want to handle the zooming logic myself (like I do with the mouse wheel).



      Is there any way to catch the pinch-zoom event and override the zooming behavior?



      I'm using Chrome on Android, but this technique should also work with Safari on iOS.







      html touch gesture pinchzoom






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 7 at 11:02









      zmbqzmbq

      29.3k962126




      29.3k962126






      This question has an open bounty worth +100
      reputation from zmbq ending ending at 2019-03-17 06:30:15Z">in 3 days.


      This question has not received enough attention.








      This question has an open bounty worth +100
      reputation from zmbq ending ending at 2019-03-17 06:30:15Z">in 3 days.


      This question has not received enough attention.
























          1 Answer
          1






          active

          oldest

          votes


















          0














          nice idea btw.
          i found something worth your time on codepen. also i stripped it down a little and inserted as snippet below. let me know if it helps you.
          additionally if you want to use library, you can try hammer.js or jgestures for pinch zooming gestures.






           var image_x = 0, image_y = 0;
          var zoom = 0.5;
          var mouse_x = 0, mouse_y = 0, finger_dist = 0;
          var source_image_obj = new Image();
          source_image_obj.addEventListener('load', function ()
          reset_settings();
          , false); // Reset (x,y,zoom) when new image loads

          function load_url()
          source_image_obj.src = document.getElementById("theurl").value; // load the image


          function update_canvas()
          var mainCanvas = document.getElementById("mainCanvas");
          var mainCanvasCTX = document.getElementById("mainCanvas").getContext("2d");
          var canvas_w = mainCanvas.width, canvas_h = mainCanvas.height; // make things easier to read below
          // Keep picture in bounds
          if (image_x - (canvas_w * zoom / 2) > source_image_obj.width) image_x = source_image_obj.width + (canvas_w * zoom / 2);
          if (image_y - (canvas_h * zoom / 2) > source_image_obj.height) image_y = source_image_obj.height + (canvas_h * zoom / 2);
          if (image_x + (canvas_w * zoom / 2) < 0) image_x = 0 - (canvas_w * zoom / 2);
          if (image_y + (canvas_h * zoom / 2) < 0) image_y = 0 - (canvas_h * zoom / 2);
          // Draw the scaled image onto the canvas
          mainCanvasCTX.clearRect(0, 0, canvas_w, canvas_h);
          mainCanvasCTX.drawImage(source_image_obj, image_x - (canvas_w * zoom / 2), image_y - (canvas_h * zoom / 2), canvas_w * zoom, canvas_h * zoom, 0, 0, canvas_w, canvas_h);


          function reset_settings()
          image_x = source_image_obj.width / 2;
          image_y = source_image_obj.height / 2;
          zoom = 1;
          update_canvas(); // Draw the image in its new position


          document.addEventListener('wheel', function (e)
          if (e.deltaY < 0)
          zoom = zoom * 1.5;
          else
          zoom = zoom / 1.5;

          update_canvas();
          , false);

          document.addEventListener('mousemove', function (e)
          if (e.buttons > 0)
          window.getSelection().empty();
          image_x = image_x + zoom * (mouse_x - e.clientX);
          image_y = image_y + zoom * (mouse_y - e.clientY);

          mouse_x = e.clientX;
          mouse_y = e.clientY; // Save for next time
          update_canvas(); // draw the image in its new position
          , false);

          function get_distance(e)
          var diffX = e.touches[0].clientX - e.touches[1].clientX;
          var diffY = e.touches[0].clientY - e.touches[1].clientY;
          return Math.sqrt(diffX * diffX + diffY * diffY); // Pythagorean theorem


          document.addEventListener('touchstart', function (e)
          if (e.touches.length > 1) // if multiple touches (pinch zooming)
          finger_dist = get_distance(e); // Save current finger distance
          // Else just moving around
          mouse_x = e.touches[0].clientX; // Save finger position
          mouse_y = e.touches[0].clientY; //
          , false);

          document.addEventListener('touchmove', function (e)
          e.preventDefault(); // Stop the window from moving
          if (e.touches.length > 1) // If pinch-zooming
          var new_finger_dist = get_distance(e); // Get current distance between fingers
          zoom = zoom * Math.abs(finger_dist / new_finger_dist); // Zoom is proportional to change
          finger_dist = new_finger_dist; // Save current distance for next time
          else // Else just moving around
          image_x = image_x + (zoom * (mouse_x - e.touches[0].clientX)); // Move the image
          image_y = image_y + (zoom * (mouse_y - e.touches[0].clientY)); //
          mouse_x = e.touches[0].clientX; // Save finger position for next time
          mouse_y = e.touches[0].clientY; //

          update_canvas(); // draw the new position
          , false);

          document.addEventListener('touchend', function (e)
          mouse_x = e.touches[0].clientX;
          mouse_y = e.touches[0].clientY; // could be down to 1 finger, back to moving image
          , false);

           * 
          margin: 0;
          padding: 0;
          box-sizing: border-box;

          canvas
          background: #e74c3c;
          border: 4px solid black;

          .box-photo
          border: 1px dashed black;

          .box-photo img
          width: 100%;

          <html>
          <head>
          <meta charset="utf-8" />
          <body onload="load_url();">
          <div class="box">
          <div class="box-photo">
          <canvas id="mainCanvas" width="500" height="600">
          <input id="theurl" value="https://mlb-s1-p.mlstatic.com/relogio-mondaine-classico-78527-original-a-prova-dagua-202101-MLB20273713546_042015-F.jpg">
          Your browser does not support the HTML5 canvas tag.
          </canvas>
          </div>
          </div>
          </body>
          </html>








          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55042253%2fcatch-pinch-zoom-in-browser%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            nice idea btw.
            i found something worth your time on codepen. also i stripped it down a little and inserted as snippet below. let me know if it helps you.
            additionally if you want to use library, you can try hammer.js or jgestures for pinch zooming gestures.






             var image_x = 0, image_y = 0;
            var zoom = 0.5;
            var mouse_x = 0, mouse_y = 0, finger_dist = 0;
            var source_image_obj = new Image();
            source_image_obj.addEventListener('load', function ()
            reset_settings();
            , false); // Reset (x,y,zoom) when new image loads

            function load_url()
            source_image_obj.src = document.getElementById("theurl").value; // load the image


            function update_canvas()
            var mainCanvas = document.getElementById("mainCanvas");
            var mainCanvasCTX = document.getElementById("mainCanvas").getContext("2d");
            var canvas_w = mainCanvas.width, canvas_h = mainCanvas.height; // make things easier to read below
            // Keep picture in bounds
            if (image_x - (canvas_w * zoom / 2) > source_image_obj.width) image_x = source_image_obj.width + (canvas_w * zoom / 2);
            if (image_y - (canvas_h * zoom / 2) > source_image_obj.height) image_y = source_image_obj.height + (canvas_h * zoom / 2);
            if (image_x + (canvas_w * zoom / 2) < 0) image_x = 0 - (canvas_w * zoom / 2);
            if (image_y + (canvas_h * zoom / 2) < 0) image_y = 0 - (canvas_h * zoom / 2);
            // Draw the scaled image onto the canvas
            mainCanvasCTX.clearRect(0, 0, canvas_w, canvas_h);
            mainCanvasCTX.drawImage(source_image_obj, image_x - (canvas_w * zoom / 2), image_y - (canvas_h * zoom / 2), canvas_w * zoom, canvas_h * zoom, 0, 0, canvas_w, canvas_h);


            function reset_settings()
            image_x = source_image_obj.width / 2;
            image_y = source_image_obj.height / 2;
            zoom = 1;
            update_canvas(); // Draw the image in its new position


            document.addEventListener('wheel', function (e)
            if (e.deltaY < 0)
            zoom = zoom * 1.5;
            else
            zoom = zoom / 1.5;

            update_canvas();
            , false);

            document.addEventListener('mousemove', function (e)
            if (e.buttons > 0)
            window.getSelection().empty();
            image_x = image_x + zoom * (mouse_x - e.clientX);
            image_y = image_y + zoom * (mouse_y - e.clientY);

            mouse_x = e.clientX;
            mouse_y = e.clientY; // Save for next time
            update_canvas(); // draw the image in its new position
            , false);

            function get_distance(e)
            var diffX = e.touches[0].clientX - e.touches[1].clientX;
            var diffY = e.touches[0].clientY - e.touches[1].clientY;
            return Math.sqrt(diffX * diffX + diffY * diffY); // Pythagorean theorem


            document.addEventListener('touchstart', function (e)
            if (e.touches.length > 1) // if multiple touches (pinch zooming)
            finger_dist = get_distance(e); // Save current finger distance
            // Else just moving around
            mouse_x = e.touches[0].clientX; // Save finger position
            mouse_y = e.touches[0].clientY; //
            , false);

            document.addEventListener('touchmove', function (e)
            e.preventDefault(); // Stop the window from moving
            if (e.touches.length > 1) // If pinch-zooming
            var new_finger_dist = get_distance(e); // Get current distance between fingers
            zoom = zoom * Math.abs(finger_dist / new_finger_dist); // Zoom is proportional to change
            finger_dist = new_finger_dist; // Save current distance for next time
            else // Else just moving around
            image_x = image_x + (zoom * (mouse_x - e.touches[0].clientX)); // Move the image
            image_y = image_y + (zoom * (mouse_y - e.touches[0].clientY)); //
            mouse_x = e.touches[0].clientX; // Save finger position for next time
            mouse_y = e.touches[0].clientY; //

            update_canvas(); // draw the new position
            , false);

            document.addEventListener('touchend', function (e)
            mouse_x = e.touches[0].clientX;
            mouse_y = e.touches[0].clientY; // could be down to 1 finger, back to moving image
            , false);

             * 
            margin: 0;
            padding: 0;
            box-sizing: border-box;

            canvas
            background: #e74c3c;
            border: 4px solid black;

            .box-photo
            border: 1px dashed black;

            .box-photo img
            width: 100%;

            <html>
            <head>
            <meta charset="utf-8" />
            <body onload="load_url();">
            <div class="box">
            <div class="box-photo">
            <canvas id="mainCanvas" width="500" height="600">
            <input id="theurl" value="https://mlb-s1-p.mlstatic.com/relogio-mondaine-classico-78527-original-a-prova-dagua-202101-MLB20273713546_042015-F.jpg">
            Your browser does not support the HTML5 canvas tag.
            </canvas>
            </div>
            </div>
            </body>
            </html>








            share|improve this answer



























              0














              nice idea btw.
              i found something worth your time on codepen. also i stripped it down a little and inserted as snippet below. let me know if it helps you.
              additionally if you want to use library, you can try hammer.js or jgestures for pinch zooming gestures.






               var image_x = 0, image_y = 0;
              var zoom = 0.5;
              var mouse_x = 0, mouse_y = 0, finger_dist = 0;
              var source_image_obj = new Image();
              source_image_obj.addEventListener('load', function ()
              reset_settings();
              , false); // Reset (x,y,zoom) when new image loads

              function load_url()
              source_image_obj.src = document.getElementById("theurl").value; // load the image


              function update_canvas()
              var mainCanvas = document.getElementById("mainCanvas");
              var mainCanvasCTX = document.getElementById("mainCanvas").getContext("2d");
              var canvas_w = mainCanvas.width, canvas_h = mainCanvas.height; // make things easier to read below
              // Keep picture in bounds
              if (image_x - (canvas_w * zoom / 2) > source_image_obj.width) image_x = source_image_obj.width + (canvas_w * zoom / 2);
              if (image_y - (canvas_h * zoom / 2) > source_image_obj.height) image_y = source_image_obj.height + (canvas_h * zoom / 2);
              if (image_x + (canvas_w * zoom / 2) < 0) image_x = 0 - (canvas_w * zoom / 2);
              if (image_y + (canvas_h * zoom / 2) < 0) image_y = 0 - (canvas_h * zoom / 2);
              // Draw the scaled image onto the canvas
              mainCanvasCTX.clearRect(0, 0, canvas_w, canvas_h);
              mainCanvasCTX.drawImage(source_image_obj, image_x - (canvas_w * zoom / 2), image_y - (canvas_h * zoom / 2), canvas_w * zoom, canvas_h * zoom, 0, 0, canvas_w, canvas_h);


              function reset_settings()
              image_x = source_image_obj.width / 2;
              image_y = source_image_obj.height / 2;
              zoom = 1;
              update_canvas(); // Draw the image in its new position


              document.addEventListener('wheel', function (e)
              if (e.deltaY < 0)
              zoom = zoom * 1.5;
              else
              zoom = zoom / 1.5;

              update_canvas();
              , false);

              document.addEventListener('mousemove', function (e)
              if (e.buttons > 0)
              window.getSelection().empty();
              image_x = image_x + zoom * (mouse_x - e.clientX);
              image_y = image_y + zoom * (mouse_y - e.clientY);

              mouse_x = e.clientX;
              mouse_y = e.clientY; // Save for next time
              update_canvas(); // draw the image in its new position
              , false);

              function get_distance(e)
              var diffX = e.touches[0].clientX - e.touches[1].clientX;
              var diffY = e.touches[0].clientY - e.touches[1].clientY;
              return Math.sqrt(diffX * diffX + diffY * diffY); // Pythagorean theorem


              document.addEventListener('touchstart', function (e)
              if (e.touches.length > 1) // if multiple touches (pinch zooming)
              finger_dist = get_distance(e); // Save current finger distance
              // Else just moving around
              mouse_x = e.touches[0].clientX; // Save finger position
              mouse_y = e.touches[0].clientY; //
              , false);

              document.addEventListener('touchmove', function (e)
              e.preventDefault(); // Stop the window from moving
              if (e.touches.length > 1) // If pinch-zooming
              var new_finger_dist = get_distance(e); // Get current distance between fingers
              zoom = zoom * Math.abs(finger_dist / new_finger_dist); // Zoom is proportional to change
              finger_dist = new_finger_dist; // Save current distance for next time
              else // Else just moving around
              image_x = image_x + (zoom * (mouse_x - e.touches[0].clientX)); // Move the image
              image_y = image_y + (zoom * (mouse_y - e.touches[0].clientY)); //
              mouse_x = e.touches[0].clientX; // Save finger position for next time
              mouse_y = e.touches[0].clientY; //

              update_canvas(); // draw the new position
              , false);

              document.addEventListener('touchend', function (e)
              mouse_x = e.touches[0].clientX;
              mouse_y = e.touches[0].clientY; // could be down to 1 finger, back to moving image
              , false);

               * 
              margin: 0;
              padding: 0;
              box-sizing: border-box;

              canvas
              background: #e74c3c;
              border: 4px solid black;

              .box-photo
              border: 1px dashed black;

              .box-photo img
              width: 100%;

              <html>
              <head>
              <meta charset="utf-8" />
              <body onload="load_url();">
              <div class="box">
              <div class="box-photo">
              <canvas id="mainCanvas" width="500" height="600">
              <input id="theurl" value="https://mlb-s1-p.mlstatic.com/relogio-mondaine-classico-78527-original-a-prova-dagua-202101-MLB20273713546_042015-F.jpg">
              Your browser does not support the HTML5 canvas tag.
              </canvas>
              </div>
              </div>
              </body>
              </html>








              share|improve this answer

























                0












                0








                0







                nice idea btw.
                i found something worth your time on codepen. also i stripped it down a little and inserted as snippet below. let me know if it helps you.
                additionally if you want to use library, you can try hammer.js or jgestures for pinch zooming gestures.






                 var image_x = 0, image_y = 0;
                var zoom = 0.5;
                var mouse_x = 0, mouse_y = 0, finger_dist = 0;
                var source_image_obj = new Image();
                source_image_obj.addEventListener('load', function ()
                reset_settings();
                , false); // Reset (x,y,zoom) when new image loads

                function load_url()
                source_image_obj.src = document.getElementById("theurl").value; // load the image


                function update_canvas()
                var mainCanvas = document.getElementById("mainCanvas");
                var mainCanvasCTX = document.getElementById("mainCanvas").getContext("2d");
                var canvas_w = mainCanvas.width, canvas_h = mainCanvas.height; // make things easier to read below
                // Keep picture in bounds
                if (image_x - (canvas_w * zoom / 2) > source_image_obj.width) image_x = source_image_obj.width + (canvas_w * zoom / 2);
                if (image_y - (canvas_h * zoom / 2) > source_image_obj.height) image_y = source_image_obj.height + (canvas_h * zoom / 2);
                if (image_x + (canvas_w * zoom / 2) < 0) image_x = 0 - (canvas_w * zoom / 2);
                if (image_y + (canvas_h * zoom / 2) < 0) image_y = 0 - (canvas_h * zoom / 2);
                // Draw the scaled image onto the canvas
                mainCanvasCTX.clearRect(0, 0, canvas_w, canvas_h);
                mainCanvasCTX.drawImage(source_image_obj, image_x - (canvas_w * zoom / 2), image_y - (canvas_h * zoom / 2), canvas_w * zoom, canvas_h * zoom, 0, 0, canvas_w, canvas_h);


                function reset_settings()
                image_x = source_image_obj.width / 2;
                image_y = source_image_obj.height / 2;
                zoom = 1;
                update_canvas(); // Draw the image in its new position


                document.addEventListener('wheel', function (e)
                if (e.deltaY < 0)
                zoom = zoom * 1.5;
                else
                zoom = zoom / 1.5;

                update_canvas();
                , false);

                document.addEventListener('mousemove', function (e)
                if (e.buttons > 0)
                window.getSelection().empty();
                image_x = image_x + zoom * (mouse_x - e.clientX);
                image_y = image_y + zoom * (mouse_y - e.clientY);

                mouse_x = e.clientX;
                mouse_y = e.clientY; // Save for next time
                update_canvas(); // draw the image in its new position
                , false);

                function get_distance(e)
                var diffX = e.touches[0].clientX - e.touches[1].clientX;
                var diffY = e.touches[0].clientY - e.touches[1].clientY;
                return Math.sqrt(diffX * diffX + diffY * diffY); // Pythagorean theorem


                document.addEventListener('touchstart', function (e)
                if (e.touches.length > 1) // if multiple touches (pinch zooming)
                finger_dist = get_distance(e); // Save current finger distance
                // Else just moving around
                mouse_x = e.touches[0].clientX; // Save finger position
                mouse_y = e.touches[0].clientY; //
                , false);

                document.addEventListener('touchmove', function (e)
                e.preventDefault(); // Stop the window from moving
                if (e.touches.length > 1) // If pinch-zooming
                var new_finger_dist = get_distance(e); // Get current distance between fingers
                zoom = zoom * Math.abs(finger_dist / new_finger_dist); // Zoom is proportional to change
                finger_dist = new_finger_dist; // Save current distance for next time
                else // Else just moving around
                image_x = image_x + (zoom * (mouse_x - e.touches[0].clientX)); // Move the image
                image_y = image_y + (zoom * (mouse_y - e.touches[0].clientY)); //
                mouse_x = e.touches[0].clientX; // Save finger position for next time
                mouse_y = e.touches[0].clientY; //

                update_canvas(); // draw the new position
                , false);

                document.addEventListener('touchend', function (e)
                mouse_x = e.touches[0].clientX;
                mouse_y = e.touches[0].clientY; // could be down to 1 finger, back to moving image
                , false);

                 * 
                margin: 0;
                padding: 0;
                box-sizing: border-box;

                canvas
                background: #e74c3c;
                border: 4px solid black;

                .box-photo
                border: 1px dashed black;

                .box-photo img
                width: 100%;

                <html>
                <head>
                <meta charset="utf-8" />
                <body onload="load_url();">
                <div class="box">
                <div class="box-photo">
                <canvas id="mainCanvas" width="500" height="600">
                <input id="theurl" value="https://mlb-s1-p.mlstatic.com/relogio-mondaine-classico-78527-original-a-prova-dagua-202101-MLB20273713546_042015-F.jpg">
                Your browser does not support the HTML5 canvas tag.
                </canvas>
                </div>
                </div>
                </body>
                </html>








                share|improve this answer













                nice idea btw.
                i found something worth your time on codepen. also i stripped it down a little and inserted as snippet below. let me know if it helps you.
                additionally if you want to use library, you can try hammer.js or jgestures for pinch zooming gestures.






                 var image_x = 0, image_y = 0;
                var zoom = 0.5;
                var mouse_x = 0, mouse_y = 0, finger_dist = 0;
                var source_image_obj = new Image();
                source_image_obj.addEventListener('load', function ()
                reset_settings();
                , false); // Reset (x,y,zoom) when new image loads

                function load_url()
                source_image_obj.src = document.getElementById("theurl").value; // load the image


                function update_canvas()
                var mainCanvas = document.getElementById("mainCanvas");
                var mainCanvasCTX = document.getElementById("mainCanvas").getContext("2d");
                var canvas_w = mainCanvas.width, canvas_h = mainCanvas.height; // make things easier to read below
                // Keep picture in bounds
                if (image_x - (canvas_w * zoom / 2) > source_image_obj.width) image_x = source_image_obj.width + (canvas_w * zoom / 2);
                if (image_y - (canvas_h * zoom / 2) > source_image_obj.height) image_y = source_image_obj.height + (canvas_h * zoom / 2);
                if (image_x + (canvas_w * zoom / 2) < 0) image_x = 0 - (canvas_w * zoom / 2);
                if (image_y + (canvas_h * zoom / 2) < 0) image_y = 0 - (canvas_h * zoom / 2);
                // Draw the scaled image onto the canvas
                mainCanvasCTX.clearRect(0, 0, canvas_w, canvas_h);
                mainCanvasCTX.drawImage(source_image_obj, image_x - (canvas_w * zoom / 2), image_y - (canvas_h * zoom / 2), canvas_w * zoom, canvas_h * zoom, 0, 0, canvas_w, canvas_h);


                function reset_settings()
                image_x = source_image_obj.width / 2;
                image_y = source_image_obj.height / 2;
                zoom = 1;
                update_canvas(); // Draw the image in its new position


                document.addEventListener('wheel', function (e)
                if (e.deltaY < 0)
                zoom = zoom * 1.5;
                else
                zoom = zoom / 1.5;

                update_canvas();
                , false);

                document.addEventListener('mousemove', function (e)
                if (e.buttons > 0)
                window.getSelection().empty();
                image_x = image_x + zoom * (mouse_x - e.clientX);
                image_y = image_y + zoom * (mouse_y - e.clientY);

                mouse_x = e.clientX;
                mouse_y = e.clientY; // Save for next time
                update_canvas(); // draw the image in its new position
                , false);

                function get_distance(e)
                var diffX = e.touches[0].clientX - e.touches[1].clientX;
                var diffY = e.touches[0].clientY - e.touches[1].clientY;
                return Math.sqrt(diffX * diffX + diffY * diffY); // Pythagorean theorem


                document.addEventListener('touchstart', function (e)
                if (e.touches.length > 1) // if multiple touches (pinch zooming)
                finger_dist = get_distance(e); // Save current finger distance
                // Else just moving around
                mouse_x = e.touches[0].clientX; // Save finger position
                mouse_y = e.touches[0].clientY; //
                , false);

                document.addEventListener('touchmove', function (e)
                e.preventDefault(); // Stop the window from moving
                if (e.touches.length > 1) // If pinch-zooming
                var new_finger_dist = get_distance(e); // Get current distance between fingers
                zoom = zoom * Math.abs(finger_dist / new_finger_dist); // Zoom is proportional to change
                finger_dist = new_finger_dist; // Save current distance for next time
                else // Else just moving around
                image_x = image_x + (zoom * (mouse_x - e.touches[0].clientX)); // Move the image
                image_y = image_y + (zoom * (mouse_y - e.touches[0].clientY)); //
                mouse_x = e.touches[0].clientX; // Save finger position for next time
                mouse_y = e.touches[0].clientY; //

                update_canvas(); // draw the new position
                , false);

                document.addEventListener('touchend', function (e)
                mouse_x = e.touches[0].clientX;
                mouse_y = e.touches[0].clientY; // could be down to 1 finger, back to moving image
                , false);

                 * 
                margin: 0;
                padding: 0;
                box-sizing: border-box;

                canvas
                background: #e74c3c;
                border: 4px solid black;

                .box-photo
                border: 1px dashed black;

                .box-photo img
                width: 100%;

                <html>
                <head>
                <meta charset="utf-8" />
                <body onload="load_url();">
                <div class="box">
                <div class="box-photo">
                <canvas id="mainCanvas" width="500" height="600">
                <input id="theurl" value="https://mlb-s1-p.mlstatic.com/relogio-mondaine-classico-78527-original-a-prova-dagua-202101-MLB20273713546_042015-F.jpg">
                Your browser does not support the HTML5 canvas tag.
                </canvas>
                </div>
                </div>
                </body>
                </html>








                 var image_x = 0, image_y = 0;
                var zoom = 0.5;
                var mouse_x = 0, mouse_y = 0, finger_dist = 0;
                var source_image_obj = new Image();
                source_image_obj.addEventListener('load', function ()
                reset_settings();
                , false); // Reset (x,y,zoom) when new image loads

                function load_url()
                source_image_obj.src = document.getElementById("theurl").value; // load the image


                function update_canvas()
                var mainCanvas = document.getElementById("mainCanvas");
                var mainCanvasCTX = document.getElementById("mainCanvas").getContext("2d");
                var canvas_w = mainCanvas.width, canvas_h = mainCanvas.height; // make things easier to read below
                // Keep picture in bounds
                if (image_x - (canvas_w * zoom / 2) > source_image_obj.width) image_x = source_image_obj.width + (canvas_w * zoom / 2);
                if (image_y - (canvas_h * zoom / 2) > source_image_obj.height) image_y = source_image_obj.height + (canvas_h * zoom / 2);
                if (image_x + (canvas_w * zoom / 2) < 0) image_x = 0 - (canvas_w * zoom / 2);
                if (image_y + (canvas_h * zoom / 2) < 0) image_y = 0 - (canvas_h * zoom / 2);
                // Draw the scaled image onto the canvas
                mainCanvasCTX.clearRect(0, 0, canvas_w, canvas_h);
                mainCanvasCTX.drawImage(source_image_obj, image_x - (canvas_w * zoom / 2), image_y - (canvas_h * zoom / 2), canvas_w * zoom, canvas_h * zoom, 0, 0, canvas_w, canvas_h);


                function reset_settings()
                image_x = source_image_obj.width / 2;
                image_y = source_image_obj.height / 2;
                zoom = 1;
                update_canvas(); // Draw the image in its new position


                document.addEventListener('wheel', function (e)
                if (e.deltaY < 0)
                zoom = zoom * 1.5;
                else
                zoom = zoom / 1.5;

                update_canvas();
                , false);

                document.addEventListener('mousemove', function (e)
                if (e.buttons > 0)
                window.getSelection().empty();
                image_x = image_x + zoom * (mouse_x - e.clientX);
                image_y = image_y + zoom * (mouse_y - e.clientY);

                mouse_x = e.clientX;
                mouse_y = e.clientY; // Save for next time
                update_canvas(); // draw the image in its new position
                , false);

                function get_distance(e)
                var diffX = e.touches[0].clientX - e.touches[1].clientX;
                var diffY = e.touches[0].clientY - e.touches[1].clientY;
                return Math.sqrt(diffX * diffX + diffY * diffY); // Pythagorean theorem


                document.addEventListener('touchstart', function (e)
                if (e.touches.length > 1) // if multiple touches (pinch zooming)
                finger_dist = get_distance(e); // Save current finger distance
                // Else just moving around
                mouse_x = e.touches[0].clientX; // Save finger position
                mouse_y = e.touches[0].clientY; //
                , false);

                document.addEventListener('touchmove', function (e)
                e.preventDefault(); // Stop the window from moving
                if (e.touches.length > 1) // If pinch-zooming
                var new_finger_dist = get_distance(e); // Get current distance between fingers
                zoom = zoom * Math.abs(finger_dist / new_finger_dist); // Zoom is proportional to change
                finger_dist = new_finger_dist; // Save current distance for next time
                else // Else just moving around
                image_x = image_x + (zoom * (mouse_x - e.touches[0].clientX)); // Move the image
                image_y = image_y + (zoom * (mouse_y - e.touches[0].clientY)); //
                mouse_x = e.touches[0].clientX; // Save finger position for next time
                mouse_y = e.touches[0].clientY; //

                update_canvas(); // draw the new position
                , false);

                document.addEventListener('touchend', function (e)
                mouse_x = e.touches[0].clientX;
                mouse_y = e.touches[0].clientY; // could be down to 1 finger, back to moving image
                , false);

                 * 
                margin: 0;
                padding: 0;
                box-sizing: border-box;

                canvas
                background: #e74c3c;
                border: 4px solid black;

                .box-photo
                border: 1px dashed black;

                .box-photo img
                width: 100%;

                <html>
                <head>
                <meta charset="utf-8" />
                <body onload="load_url();">
                <div class="box">
                <div class="box-photo">
                <canvas id="mainCanvas" width="500" height="600">
                <input id="theurl" value="https://mlb-s1-p.mlstatic.com/relogio-mondaine-classico-78527-original-a-prova-dagua-202101-MLB20273713546_042015-F.jpg">
                Your browser does not support the HTML5 canvas tag.
                </canvas>
                </div>
                </div>
                </body>
                </html>





                 var image_x = 0, image_y = 0;
                var zoom = 0.5;
                var mouse_x = 0, mouse_y = 0, finger_dist = 0;
                var source_image_obj = new Image();
                source_image_obj.addEventListener('load', function ()
                reset_settings();
                , false); // Reset (x,y,zoom) when new image loads

                function load_url()
                source_image_obj.src = document.getElementById("theurl").value; // load the image


                function update_canvas()
                var mainCanvas = document.getElementById("mainCanvas");
                var mainCanvasCTX = document.getElementById("mainCanvas").getContext("2d");
                var canvas_w = mainCanvas.width, canvas_h = mainCanvas.height; // make things easier to read below
                // Keep picture in bounds
                if (image_x - (canvas_w * zoom / 2) > source_image_obj.width) image_x = source_image_obj.width + (canvas_w * zoom / 2);
                if (image_y - (canvas_h * zoom / 2) > source_image_obj.height) image_y = source_image_obj.height + (canvas_h * zoom / 2);
                if (image_x + (canvas_w * zoom / 2) < 0) image_x = 0 - (canvas_w * zoom / 2);
                if (image_y + (canvas_h * zoom / 2) < 0) image_y = 0 - (canvas_h * zoom / 2);
                // Draw the scaled image onto the canvas
                mainCanvasCTX.clearRect(0, 0, canvas_w, canvas_h);
                mainCanvasCTX.drawImage(source_image_obj, image_x - (canvas_w * zoom / 2), image_y - (canvas_h * zoom / 2), canvas_w * zoom, canvas_h * zoom, 0, 0, canvas_w, canvas_h);


                function reset_settings()
                image_x = source_image_obj.width / 2;
                image_y = source_image_obj.height / 2;
                zoom = 1;
                update_canvas(); // Draw the image in its new position


                document.addEventListener('wheel', function (e)
                if (e.deltaY < 0)
                zoom = zoom * 1.5;
                else
                zoom = zoom / 1.5;

                update_canvas();
                , false);

                document.addEventListener('mousemove', function (e)
                if (e.buttons > 0)
                window.getSelection().empty();
                image_x = image_x + zoom * (mouse_x - e.clientX);
                image_y = image_y + zoom * (mouse_y - e.clientY);

                mouse_x = e.clientX;
                mouse_y = e.clientY; // Save for next time
                update_canvas(); // draw the image in its new position
                , false);

                function get_distance(e)
                var diffX = e.touches[0].clientX - e.touches[1].clientX;
                var diffY = e.touches[0].clientY - e.touches[1].clientY;
                return Math.sqrt(diffX * diffX + diffY * diffY); // Pythagorean theorem


                document.addEventListener('touchstart', function (e)
                if (e.touches.length > 1) // if multiple touches (pinch zooming)
                finger_dist = get_distance(e); // Save current finger distance
                // Else just moving around
                mouse_x = e.touches[0].clientX; // Save finger position
                mouse_y = e.touches[0].clientY; //
                , false);

                document.addEventListener('touchmove', function (e)
                e.preventDefault(); // Stop the window from moving
                if (e.touches.length > 1) // If pinch-zooming
                var new_finger_dist = get_distance(e); // Get current distance between fingers
                zoom = zoom * Math.abs(finger_dist / new_finger_dist); // Zoom is proportional to change
                finger_dist = new_finger_dist; // Save current distance for next time
                else // Else just moving around
                image_x = image_x + (zoom * (mouse_x - e.touches[0].clientX)); // Move the image
                image_y = image_y + (zoom * (mouse_y - e.touches[0].clientY)); //
                mouse_x = e.touches[0].clientX; // Save finger position for next time
                mouse_y = e.touches[0].clientY; //

                update_canvas(); // draw the new position
                , false);

                document.addEventListener('touchend', function (e)
                mouse_x = e.touches[0].clientX;
                mouse_y = e.touches[0].clientY; // could be down to 1 finger, back to moving image
                , false);

                 * 
                margin: 0;
                padding: 0;
                box-sizing: border-box;

                canvas
                background: #e74c3c;
                border: 4px solid black;

                .box-photo
                border: 1px dashed black;

                .box-photo img
                width: 100%;

                <html>
                <head>
                <meta charset="utf-8" />
                <body onload="load_url();">
                <div class="box">
                <div class="box-photo">
                <canvas id="mainCanvas" width="500" height="600">
                <input id="theurl" value="https://mlb-s1-p.mlstatic.com/relogio-mondaine-classico-78527-original-a-prova-dagua-202101-MLB20273713546_042015-F.jpg">
                Your browser does not support the HTML5 canvas tag.
                </canvas>
                </div>
                </div>
                </body>
                </html>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 17 hours ago









                GalzorGalzor

                616




                616





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55042253%2fcatch-pinch-zoom-in-browser%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

                    Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

                    2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived