javascript - JQuery 代码,适用于版本 1.5.2 但不适用于版本 1.9.1

标签 javascript jquery

我有从 this tutorial 中获取的 JavaScript/JQuery 代码

当我使用 1.5.2 版本的 JQuery 时,这段代码工作得非常好。但是当我使用最新版本的 JQuery (1.9.1) 时,这段代码不起作用。要使此代码在最新的 JQuery 中工作,需要更改哪些内容。

Here is a version with JQuery 1.5.2

Here is a version with JQuery 1.9.1

如您所见,Jquery 1.5.2 有效但 1.9.1 无效。

var canvas, ctx;
var circles = [];
var selectedCircle;
var hoveredCircle;

// -------------------------------------------------------------

// objects :

function Circle(x, y, radius){
    this.x = x;
    this.y = y;
    this.radius = radius;
}

// -------------------------------------------------------------

// draw functions :

function clear() { // clear canvas function
    ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
}

function drawCircle(ctx, x, y, radius) { // draw circle function
    ctx.fillStyle = 'rgba(255, 35, 55, 1.0)';
    ctx.beginPath();
    ctx.arc(x, y, radius, 0, Math.PI*2, true);
    ctx.closePath();
    ctx.fill();
}

function drawScene() { // main drawScene function
    clear(); // clear canvas

    ctx.beginPath(); // custom shape begin
    ctx.fillStyle = 'rgba(255, 110, 110, 0.5)';
    ctx.moveTo(circles[0].x, circles[0].y);
    for (var i=0; i<circles.length; i++) {
        ctx.lineTo(circles[i].x, circles[i].y);
    }
    ctx.closePath(); // custom shape end
    ctx.fill(); // fill custom shape

    ctx.lineWidth = 5;
    ctx.strokeStyle = 'rgba(0, 0, 255, 0.5)';
    ctx.stroke(); // draw border

    for (var i=0; i<circles.length; i++) { // display all our circles
        drawCircle(ctx, circles[i].x, circles[i].y, (hoveredCircle == i) ? 25 : 15);
    }
}

// -------------------------------------------------------------

// initialization

$(function(){
    canvas = document.getElementById('scene');
    ctx = canvas.getContext('2d');

    var circleRadius = 15;
    var width = canvas.width;
    var height = canvas.height;

    var circlesCount = 7; // we will draw 7 circles randomly
    for (var i=0; i<circlesCount; i++) {
        var x = Math.random()*width;
        var y = Math.random()*height;
        circles.push(new Circle(x,y,circleRadius));
    }

    // binding mousedown event (for dragging)
    $('#scene').mousedown(function(e) {
        var canvasPosition = $(this).offset();
        var mouseX = e.layerX || 0;
        var mouseY = e.layerY || 0;
        for (var i=0; i<circles.length; i++) { // checking through all circles - are mouse down inside circle or not
            var circleX = circles[i].x;
            var circleY = circles[i].y;
            var radius = circles[i].radius;
            if (Math.pow(mouseX-circleX,2) + Math.pow(mouseY-circleY,2) < Math.pow(radius,2)) {
                selectedCircle = i;
                break;
            }
        }
    });

    $('#scene').mousemove(function(e) { // binding mousemove event for dragging selected circle
            var mouseX = e.layerX || 0;
            var mouseY = e.layerY || 0;
        if (selectedCircle != undefined) {
            var canvasPosition = $(this).offset();

            var radius = circles[selectedCircle].radius;
            circles[selectedCircle] = new Circle(mouseX, mouseY,radius); // changing position of selected circle
        }

        hoveredCircle = undefined;
        for (var i=0; i<circles.length; i++) { // checking through all circles - are mouse down inside circle or not
            var circleX = circles[i].x;
            var circleY = circles[i].y;
            var radius = circles[i].radius;
            if (Math.pow(mouseX-circleX,2) + Math.pow(mouseY-circleY,2) < Math.pow(radius,2)) {
                hoveredCircle = i;
                break;
            }
        }
    });

    $('#scene').mouseup(function(e) { // on mouseup - cleaning selectedCircle
        selectedCircle = undefined;
    });

    setInterval(drawScene, 30); // loop drawScene
});

最佳答案

jQuery 不再盲目地将 Event 对象的所有 属性从真实事件对象复制到 jQuery 提供的对象,因此这些行:

var mouseX = e.layerX || 0;
var mouseY = e.layerY || 0;

...在 mousedownmousemove 处理程序,因为 the jQuery event object没有 layerXlayerY

不过,jQuery 使原始事件对象作为 originalEvent 可用,因此修复了它:

var mouseX = e.originalEvent.layerX || 0;
var mouseY = e.originalEvent.layerY || 0;

Updated JSBin | Source

或者为了兼容多个版本:

var mouseX = e.layerX || e.originalEvent.layerX || 0;
var mouseY = e.layerY || e.originalEvent.layerY || 0;

Updated JSBin | Source

关于javascript - JQuery 代码,适用于版本 1.5.2 但不适用于版本 1.9.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15309600/

相关文章:

javascript - 如何使用typescript读取json数据?

javascript - Skeleton Meteor 应用程序未跨选项卡更新

javascript - React setState 渲染行为

jquery - iPad,位置 :fixed, right:0;

javascript - 如何更改单击的特定按钮的样式?

javascript - 解析角色ACL解释

javascript - 无法理解为什么这个 javascript 不起作用

javascript - 简单的步骤形式不起作用

javascript - 图像只占用盒子内的小空间

javascript - 日期选择器 Bootstrap 中的 "TypeError: date.match is not a function"