Rapha?l 鼠標拖動,滾輪縮放js代碼

n6xb 10年前發布 | 1K 次閱讀 JavaScript

$(window).load(function() {
    var p = Raphael('canvas');
    p.cx = 0, p.cy = 0, p.cs = 1;
    p.canvas.style.backgroundColor = '#008080';
    $(window).resize(function() {
        p.setSize($('#canvas').width(), $('#canvas').height());
    });
    $(p.canvas).on('mousedown', function(e) {
        if (e.target == p.canvas) {
            this.ox = e.offsetX;
            this.oy = e.offsetY;
        }
    });
    $(p.canvas).on('mousemove', function(e) {
        if (e.target == p.canvas && this.ox) {
            var size = p.getSize();
            p.cx = p.cx - (e.offsetX - this.ox) / p.cs;
            p.cy = p.cy - (e.offsetY - this.oy) / p.cs;
            p.setViewBox(p.cx, p.cy, size.width / p.cs, size.height / p.cs);
            this.ox = e.offsetX;
            this.oy = e.offsetY;
        }
    });
    $(p.canvas).on('mouseup', function(e) {
        if (e.target == p.canvas && this.ox) {
            delete this.ox;
            delete this.oy;
        }
    });
    $(p.canvas).on('mousewheel', function(e) {
        if (e.target == p.canvas) {
            var size = p.getSize();
            p.cs2 = e.originalEvent.wheelDelta > 0 ? p.cs * 1.125 : p.cs / 1.125;
            p.cx = p.cx + (e.offsetX / p.cs - e.offsetX / p.cs2);
            p.cy = p.cy + (e.offsetY / p.cs - e.offsetY / p.cs2);
            p.cs = p.cs2;
            p.setViewBox(p.cx, p.cy, size.width / p.cs, size.height / p.cs);
        }
        return false;
    });
    for (var i = 0; i < 200; i++) {
        var r = Math.random;
        p.rect(700 * r(), 500 * r(), 100 * r(), 100 * r()).attr('fill', '#00ffff');
    }
});

 本文由用戶 n6xb 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!