python - 转密和放大镜代码

标签 python transcrypt magnifier.js

我尝试通过 Transcrypt 将放大镜的 Javascript 程序移植到 Python 代码。不幸的是,我无法摆脱放大镜的框架,它完全覆盖了完整的图像。原始代码位于此处:https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_image_zoom

我用它编写的 Python 代码是:

class Magnifier():

    def __init__(self):
        self.img = document.getElementById("myimage")
        self.saved_src = self.img.src
        print(self.saved_src)
        self.resultat = document.getElementById("myresult");
        # create lens:
        self.lens = document.createElement("DIV");
        self.lens.setAttribute("class", "img-zoom-lens");
        # insert lens:
        self.img.parentElement.insertBefore(self.lens, self.img);
        # calculate the ratio between result DIV and lens:
        self.cx = self.resultat.offsetWidth / self.lens.offsetWidth;
        self.cy = self.resultat.offsetHeight / self.lens.offsetHeight;
        # set background properties for the result DIV:
        self.resultat.style.backgroundImage = "url('" + self.img.src + "')";
        self.resultat.style.backgroundSize = (self.img.width * self.cx) + "px " + (self.img.height * self.cy) + "px";
        # execute a function when someone moves the cursor over the image, or the lens:
        window.addEventListener("mousemove", self.moveLens);
        self.img.addEventListener("mousemove", self.moveLens);
        # and also for touch screens:
        self.lens.addEventListener("touchmove", self.moveLens);
        self.img.addEventListener("touchmove", self.moveLens);
        console.log("run")
        self.counter = 0

    def moveLens(self, e):
        # console.log("moveLens")
        self.img.src = self.saved_src + "?" + __new__(Date().getTime())
        if (not e): e = window.event # only for IE which has window.event
        a = self.img.getBoundingClientRect();
        # e.preventDefault()
        # get the cursor's x and y positions:
        # calculate the cursor's x and y coordinates, relative to the image:
        x = e.pageX - a.left;
        y = e.pageY - a.top;
        # print (a, e.pageX, e.pageY)
        # consider any page scrolling:
        x = x - window.pageXOffset;
        y = y - window.pageYOffset;
        # print (x, y)
        # calculate the position of the lens:
        # create lens:
        self.lens = document.createElement("DIV");
        self.lens.setAttribute("class", "img-zoom-lens");
        self.lens.setAttribute("position", "absolute")
        self.img.parentElement.insertBefore(self.lens, self.img)
        x = x - (self.lens.offsetWidth / 2);
        y = y - (self.lens.offsetHeight / 2);
        # prevent the lens from being positioned outside the image:
        if (x > self.img.width - self.lens.offsetWidth): x = self.img.width - self.lens.offsetWidth
        if (x < 0): x = 0;
        if (y > self.img.height - self.lens.offsetHeight): y = self.img.height - self.lens.offsetHeight;
        if (y < 0): y = 0;
        # set the position of the lens:
        print ("xy", x, y)
        self.lens.style.left = str(x) + "px";
        self.lens.style.top = str(y) + "px";
        # display what the lens "sees":
        self.resultat.style.backgroundPosition = "-" + str(x * self.cx) + "px -" + str(y * self.cy) + "px";

magnificent = Magnifier()

这是它的 html:

<html>
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
  * {box-sizing: border-box;}
  .img-zoom-container {
    position: relative;
  }
  .img-zoom-lens {
    position: absolute;
    border: 1px solid #d4d4d4;
    /*set the size of the lens:*/
    width: 40px;
    height: 40px;
  }
  .img-zoom-result {
    border: 1px solid #d4d4d4;
    /*set the size of the result div:*/
    width: 300px;
    height: 300px;
  }
  </style>
</head>
<body>
<h1>Image Zoom</h1>
<p>Mouse over the image:</p>
<div class="img-zoom-container">
  <img id="myimage" src="https://raw.githubusercontent.com/bunkahle/Transcrypt-Examples/master/user-interface/computer-desktop.jpg" width="300" height="215">
  <div id="myresult" class="img-zoom-result"></div>
</div>
<p>The image must be placed inside a container with relative positioning.</p>
<p>The result can be put anywhere on the page, but must have the class name "img-zoom-result".</p>
<p>Make sure both the image and the result have IDs. These IDs are used when a javaScript initiates the zoom effect.</p>
<script src="__javascript__/magnifier.js" charset="UTF-8"></script>
</body>
</html>

有什么想法可能是错误的吗?如果注释掉该行:

self.img.parentElement.insertBefore(self.lens, self.img)

我根本看不到框架,但它至少工作了一半。但如果放大空间周围有边界那就太好了。

最佳答案

您在 moveLens 中一遍又一遍地创建新镜头。方法并将其中最小的分配给 self.lens ,在以下代码中覆盖对前一个镜头的引用(但不破坏前一个镜头本身):

    # create lens:
    self.lens = document.createElement("DIV");
    self.lens.setAttribute("class", "img-zoom-lens");
    self.lens.setAttribute("position", "absolute")
    self.img.parentElement.insertBefore(self.lens, self.img)

您只移动您创建的最后一个,其余部分仍然可见并保留在它们创建的位置,自 self.lens.style.left = <position> 起只移动最近创建的一个。

所以只需取出一些代码即可。以下代码将起作用。但它可能可以简化一点。

class Magnifier():

    def __init__(self):
        self.img = document.getElementById("myimage")
        self.saved_src = self.img.src
        print(self.saved_src)
        self.resultat = document.getElementById("myresult");
        # create lens:
        self.lens = document.createElement("DIV");
        self.lens.setAttribute("class", "img-zoom-lens");
        self.lens.setAttribute("position", "absolute")
        # insert lens:
        self.img.parentElement.insertBefore(self.lens, self.img);
        # calculate the ratio between result DIV and lens:
        self.cx = self.resultat.offsetWidth / self.lens.offsetWidth;
        self.cy = self.resultat.offsetHeight / self.lens.offsetHeight;
        # set background properties for the result DIV:
        self.resultat.style.backgroundImage = "url('" + self.img.src + "')";
        self.resultat.style.backgroundSize = (self.img.width * self.cx) + "px " + (self.img.height * self.cy) + "px";
        # execute a function when someone moves the cursor over the image, or the lens:
        window.addEventListener("mousemove", self.moveLens);
        self.img.addEventListener("mousemove", self.moveLens);
        # and also for touch screens:
        self.lens.addEventListener("touchmove", self.moveLens);
        self.img.addEventListener("touchmove", self.moveLens);
        console.log("run")
        self.counter = 0

    def moveLens(self, e):
        # console.log("moveLens")
        self.img.src = self.saved_src + "?" + __new__(Date().getTime())
        if (not e): e = window.event # only for IE which has window.event
        a = self.img.getBoundingClientRect();
        # e.preventDefault()
        # get the cursor's x and y positions:
        # calculate the cursor's x and y coordinates, relative to the image:
        x = e.pageX - a.left;
        y = e.pageY - a.top;
        # print (a, e.pageX, e.pageY)
        # consider any page scrolling:
        x = x - window.pageXOffset;
        y = y - window.pageYOffset;
        # self.img.parentElement.insertBefore(self.lens, self.img)
        x = x - (self.lens.offsetWidth / 2);
        y = y - (self.lens.offsetHeight / 2);
        # prevent the lens from being positioned outside the image:
        if (x > self.img.width - self.lens.offsetWidth): x = self.img.width - self.lens.offsetWidth
        if (x < 0): x = 0;
        if (y > self.img.height - self.lens.offsetHeight): y = self.img.height - self.lens.offsetHeight;
        if (y < 0): y = 0;
        # set the position of the lens:
        print ("xy", x, y)
        self.lens.style.left = str(x) + "px";
        self.lens.style.top = str(y) + "px";
        # display what the lens "sees":
        self.resultat.style.backgroundPosition = "-" + str(x * self.cx) + "px -" + str(y * self.cy) + "px";

magnificent = Magnifier()

关于python - 转密和放大镜代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49083318/

相关文章:

javascript - 如何在 Javascript 中从 id 获取图像的 url

python - Tkinter 文本小部件间距

python - 调用父类时返回子类

python - 在 Python 中定义接口(interface)

python - 如何扩展 functools.wraps 装饰器的功能?

javascript - transcrypt 中的可迭代仿真

python - 在 Transcrypt 中使用 XMLHttpRequest()

python - 在使用 Python 3.5 的 AST 模块为字符串文字生成代码时处理 à,需要使用正确的编码打开