Browse Source

Adding zoom feature through the mouse wheel

Patrick Augusto 4 năm trước cách đây
mục cha
commit
6dd364026b
3 tập tin đã thay đổi với 27 bổ sung19 xóa
  1. 3 2
      Pixel.css
  2. 0 3
      Pixel.html
  3. 24 14
      Pixel.js

+ 3 - 2
Pixel.css

@@ -1,6 +1,7 @@
 html, body {
-    /* width: 100%; */
-    /* height: 100%; */
+    margin: 0;
+    width: 100vw;
+    height: 100vh;
     background-color: darkgreen;
 }
 

+ 0 - 3
Pixel.html

@@ -7,9 +7,6 @@
     </head>
     <body>
         <canvas id="canvas" tabindex="0"></canvas>
-        <button id="closer">Zoom +</button>
-        <button id="farther">Zoom -</button>
-        <span id="level">0</span>
         <script>main();</script>
     </body>
 </html>

+ 24 - 14
Pixel.js

@@ -33,7 +33,7 @@ const canvas = {
     width: 0,
     height: 0,
     images: [],
-    level: 4
+    level: 0
 };
 
 const mouse = {
@@ -138,20 +138,31 @@ const draw_ = function(ctx, isize, url, level, images, id, coords) {
    }
 };
 
+const init = function(c) {
+    const image = new Image();
+    image.src = canvas.url + "0".repeat((canvas.level + 1) * 2)
+
+    image.onload = () => {
+        canvas.width = image.width;
+        canvas.height = image.height;
+        canvas.images = draw(c.getContext("2d"), canvas.boundary, canvas,
+            canvas.url, canvas.level, canvas.images, {x: 0, y: 0});
+    }
+}
+
 const main = function() {
     const c = document.getElementById("canvas");
-    c.setAttribute("width",  1000); // window.getComputedStyle(document.body).getPropertyValue("width"));
-    c.setAttribute("height", 1000); // window.getComputedStyle(document.body).getPropertyValue("height"));
+    c.setAttribute("width",  window.getComputedStyle(document.body).getPropertyValue("width"));
+    c.setAttribute("height", window.getComputedStyle(document.body).getPropertyValue("height"));
 
     document.addEventListener("mousedown", (e) => {
-        console.log(e);
-        if (c === document.activeElement && e.buttons === 1) {
+        if (c === document.activeElement && e.button === 0) {
             mouse.prevX = e.clientX;
             mouse.prevY = e.clientY;
         }
     });
     document.addEventListener("mouseup", (e) => {
-        if (c === document.activeElement) {
+        if (c === document.activeElement && e.button === 0) {
             mouse.x = e.clientX;
             mouse.y = e.clientY;
 
@@ -160,13 +171,12 @@ const main = function() {
         }
     });
 
-    const image = new Image();
-    image.src = canvas.url + "0".repeat((canvas.level + 1) * 2)
+    document.addEventListener("wheel", (e) => {
+        if (c === document.activeElement) {
+            canvas.level += e.deltaY > 0 ? 1 : -1;
+            init(c);
+        }
+    });
 
-    image.onload = () => {
-        canvas.width = image.width;
-        canvas.height = image.height;
-        canvas.images = draw(c.getContext("2d"), canvas.boundary, canvas,
-            canvas.url, canvas.level, canvas.images, {x: 0, y: 0});
-    }
+    init(c);
 };