簡體   English   中英

Javascript交互式卡出現故障

[英]Javascript interactive card being glitchy

我做了一張互動卡。 當用鼠標抓住物體並移動它時,它會立即飛回原來的位置。 然后它將“粘”在鼠標光標上。 單擊釋放。 我想要更流暢的點擊和拖動。 卡的其余部分都可以正常工作。 我在這里先向您的幫助表示感謝。

<!DOCTYPE html>

<title>Seasonal Greetings</title>

<style>

    canvas {
        border: #333 10px solid;
        background-image: url(images/springBackground.png);

    }
    #mydiv {
        position: absolute;
        z-index: 10;
        text-align: center;
    }
    h1 {
        color: #6433af;
        font: georgia;
    }
    p {
        color: #f442b0;
        font: arial;
    }



</style>

想念你的問候

    <p>Drag the flower into the flowerpot. Click to release.</p>
    <p>When finished, press the "END" key (or the "S" key on iMac)</p>

    <div id="mydiv">
      <img src="images/flower.png" alt="flower" style="width:150px;height:200px;">
    </div>

        <canvas id="canvas" width="630px" height="430px"></canvas>
        <a href="https://www.freepik.com/free-photos-vectors/background">Background vector created by Freepik</a>

        <script>
    //Make the DIV element draggagle:
    dragElement(document.getElementById(("mydiv")));

    function dragElement(elmnt) {
      var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
      if (document.getElementById(elmnt.id + "header")) {
        /* if present, the header is where you move the DIV from:*/
        document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
      } else {
        /* otherwise, move the DIV from anywhere inside the DIV:*/
        elmnt.onmousedown = dragMouseDown;
      }

      function dragMouseDown(e) {
        e = e || window.event;
        // get the mouse cursor position at startup:
        pos3 = e.clientX;
        pos4 = e.clientY;
        document.onmouseup = closeDragElement;
        // call a function whenever the cursor moves:
        document.onmousemove = elementDrag;
      }

      function elementDrag(e) {
        e = e || window.event;
        // calculate the new cursor position:
        pos1 = pos3 - e.clientX;
        pos2 = pos4 - e.clientY;
        pos3 = e.clientX;
        pos4 = e.clientY;
        // set the element's new position:
        elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
        elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
      }

      function closeDragElement() {
        /* stop moving when mouse button is released:*/
        document.onmouseup = null;
        document.onmousemove = null;
      }
    }

<script>

    //end key
    window.addEventListener("keydown", checkKeyPressed, false);
    function checkKeyPressed(e) {
        if(e.keyCode == "83" || e.keyCode == "35") {
            ctx.font = "bold Italic 18px Arial";
            ctx.fillStyle = "#a142f4";
            ctx.fillText("Thinking of you...", 320, 280);
            ctx.fillText("Each new blossom", 320, 320);
            ctx.fillText("brings a new day.", 320, 350);
            ctx.fillText("Enjoy your day, Sherri", 320, 390);
        }
        e.preventDefault();
    }

    var ctx = document.querySelector("canvas").getContext("2d"),
    dashLen = 220, dashOffset = dashLen, speed = 5,
    txt = "Happy Spring", x = 150, i = 0;


    ctx.font = "50px Georgia, cursive, sans-serif"; 
    ctx.lineWidth = 5; ctx.lineJoin = "round"; ctx.globalAlpha = 2/3;
    ctx.strokeStyle = ctx.fillStyle = "#ffcc00";

    (function loop() {
      ctx.clearRect(x, 0, 60, 150);
      ctx.setLineDash([dashLen - dashOffset, dashOffset - speed]); // create a long dash mask
      dashOffset -= speed;                                         // reduce dash length
      ctx.strokeText(txt[i], x, 200);                               // stroke letter

      if (dashOffset > 0) requestAnimationFrame(loop);             // animate
      else {
        ctx.fillText(txt[i], x, 200);                               // fill final letter
        dashOffset = dashLen;                                      // prep next char
        x += ctx.measureText(txt[i++]).width + ctx.lineWidth * Math.random();
        ctx.setTransform(1, 0, 0, 1, 0, 3 * Math.random());        // random y-delta
        ctx.rotate(Math.random() * 0.005);                         // random rotation
        if (i < txt.length) requestAnimationFrame(loop); checkKeyPressed();
      }
    })();

    </script>

springbackground.png flower.png

要使其工作,請將其附加到dragElement函數的底部:

elmnt.ondragstart = function() {
    return false;
};

這將通過拖放圖片來取消瀏覽器的默認行為。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM