Jquery放大和缩小可以使用抖动而不是平滑

时间:2016-06-14 05:40:04

标签: javascript jquery

我有画布。我必须缩放它直到按下放大按钮并缩小它直到按下缩小按钮。我设法让它工作,但它适用于延迟。即使我已经释放按钮,它也会一直放大或缩小。这是我到目前为止所尝试的。请帮我。您可以在Here上的实际网站上查看。

<script language="javascript">
    var timeout, clicker = $('#zoomin'),
        clicker2 = $('#zoomout');

    clicker.mousedown(function() {
        timeout = setInterval(function() {
            var wd = $("#canvas").width();
            var ht = $("#canvas").height();
            wd = parseInt(wd) + 250;
            ht = parseInt(ht) + 250;
            $("#canvas").animate({
                width: wd,
                height: ht
            });
            var ctx = canvas.getContext("2d");
            ctx.drawImage(backgroundImage, 0, 0, wd - 1, ht - 1);
            ctx.drawImage(outlineImage, 0, 0, wd - 1, ht - 1);
            redraw();
        }, 500);

        return false;
    });

    clicker2.mousedown(function() {
        timeout = setInterval(function() {
            var wd = $("#canvas").width();
            var ht = $("#canvas").height();
            wd = parseInt(wd) - 250;
            ht = parseInt(ht) - 250;
            if (wd < 500) {
                return false;
            }
            $("#canvas").animate({
                width: wd,
                height: ht
            });
            var ctx = canvas.getContext("2d");
            ctx.drawImage(backgroundImage, 0, 0, wd - 1, ht - 1);
            ctx.drawImage(outlineImage, 0, 0, wd - 1, ht - 1);
            redraw();
        }, 500);

        return false;
    });

    $(document).mouseup(function() {
        clearInterval(timeout);
        return false;
    });

    $("#zoomout").mousedown(function() {
        var wd = $("#canvas").width();
        var ht = $("#canvas").height();
        wd = parseInt(wd) - 250;
        ht = parseInt(ht) - 250;
        if (wd < 500) {
            return false;
        }
        $("#canvas").animate({
            width: wd,
            height: ht
        });
        var ctx = canvas.getContext("2d");
        ctx.drawImage(backgroundImage, 0, 0, wd - 1, ht - 1);
        ctx.drawImage(outlineImage, 0, 0, wd - 1, ht - 1);
        redraw();
        console.log(wd + ":" + ht);
    });

    $("#zoomin").click(function() {
        var wd = $("#canvas").width();
        var ht = $("#canvas").height();
        wd = parseInt(wd) + 250;
        ht = parseInt(ht) + 250;
        $("#canvas").animate({
            width: wd,
            height: ht
        });
        var ctx = canvas.getContext("2d");
        ctx.drawImage(backgroundImage, 0, 0, wd - 1, ht - 1);
        ctx.drawImage(outlineImage, 0, 0, wd - 1, ht - 1);
        redraw();
        console.log(wd + ":" + ht);
    });
    $("#zoomout").click(function() {
        var wd = $("#canvas").width();
        var ht = $("#canvas").height();
        wd = parseInt(wd) - 250;
        ht = parseInt(ht) - 250;
        if (wd < 500) {
            return false;
        }
        $("#canvas").animate({
            width: wd,
            height: ht
        });
        var ctx = canvas.getContext("2d");
        ctx.drawImage(backgroundImage, 0, 0, wd - 1, ht - 1);
        ctx.drawImage(outlineImage, 0, 0, wd - 1, ht - 1);
        redraw();
        console.log(wd + ":" + ht);
    });
</script>

1 个答案:

答案 0 :(得分:1)

禁用/删除点击处理程序。

鼠标释放时会触发click事件。 这意味着,你正在做的是,当用户释放鼠标时,你调用jQuery来进一步缩放画布。

如果删除单击处理程序,则在鼠标释放后画布不应继续动画。