在 RaphealJS 中使用拖放来优化捕捉点的性能

发布于 2024-10-31 21:46:37 字数 1768 浏览 3 评论 0原文

我有一个演示页面,我正在其中查看对象被拖动时应捕捉到的大约 300 个点的性能。

http://jsfiddle.net/digiguru/rVFje/

我通过加载每个的边界来优化它开始拖动时“捕捉”点到 4 个数组中。

var circleT = [];
var circleR = [];
var circleB = [];
var circleL = [];

var start = function (event) {
    this.ox = this.attr("cx");
    this.oy = this.attr("cy");
    var threshold = 15;

    for (var myCircle in circles) {
        circleT[myCircle] = circles[myCircle].attr("cy") - threshold;
        circleR[myCircle] = circles[myCircle].attr("cx") + threshold;
        circleB[myCircle] = circles[myCircle].attr("cy") + threshold;
        circleL[myCircle] = circles[myCircle].attr("cx") - threshold;
    }
    circle.animate({ r: 20,fill: "#319F40", "stroke-width": 1 }, 200);
},

然后在移动事件中,我们使用以下内容来计算每个捕捉点拖动的对象...

move = function (mx, my) {
    var inrange = false; 
    var mouseCX = this.ox + mx;
    var mouseCY = this.oy + my;
    var lockX = 0;
    var lockY = 0;

   for (var myCircle in circles) {
        if ((circleT[myCircle] < mouseCY
             && circleB[myCircle] > mouseCY ) 
            && (circleR[myCircle] > mouseCX 
                && circleL[myCircle] < mouseCX )) {
           inrange = true; 
           lockX =  circles[myCircle].attr("cx");
           lockY =  circles[myCircle].attr("cy");
        }
    }
    if (inrange) {
         this.attr({
            cx: lockX ,
            cy: lockY 
        });
    } else {
        this.attr({
            cx: mouseCX ,
            cy: mouseCY 
        });
    }

}, 

一般来说,性能良好,但您可以注意到在稍旧的 IE 浏览器(IE8 及更低版本)上出现帧丢失。有没有一种忍者方法可以提高性能?也许我可以改进 4 个 if 语句?使用另一个 JavaScript 库(例如处理 JS)会产生更好的结果吗?

I have a demo page where I am looking at the performance of around 300 points that an object should snap to when being dragged.

http://jsfiddle.net/digiguru/rVFje/

I have optimized it by loading the bounds of each "snap" point into 4 arrays when the drag is started.

var circleT = [];
var circleR = [];
var circleB = [];
var circleL = [];

var start = function (event) {
    this.ox = this.attr("cx");
    this.oy = this.attr("cy");
    var threshold = 15;

    for (var myCircle in circles) {
        circleT[myCircle] = circles[myCircle].attr("cy") - threshold;
        circleR[myCircle] = circles[myCircle].attr("cx") + threshold;
        circleB[myCircle] = circles[myCircle].attr("cy") + threshold;
        circleL[myCircle] = circles[myCircle].attr("cx") - threshold;
    }
    circle.animate({ r: 20,fill: "#319F40", "stroke-width": 1 }, 200);
},

Then in the move event we use the following to calculate the dragged object from each of the snap points...

move = function (mx, my) {
    var inrange = false; 
    var mouseCX = this.ox + mx;
    var mouseCY = this.oy + my;
    var lockX = 0;
    var lockY = 0;

   for (var myCircle in circles) {
        if ((circleT[myCircle] < mouseCY
             && circleB[myCircle] > mouseCY ) 
            && (circleR[myCircle] > mouseCX 
                && circleL[myCircle] < mouseCX )) {
           inrange = true; 
           lockX =  circles[myCircle].attr("cx");
           lockY =  circles[myCircle].attr("cy");
        }
    }
    if (inrange) {
         this.attr({
            cx: lockX ,
            cy: lockY 
        });
    } else {
        this.attr({
            cx: mouseCX ,
            cy: mouseCY 
        });
    }

}, 

Genrally performance, is good, but you can notice frames dropping on slightly older IE browsers (IE8 and below). Is there a ninja way to improve performance at all? Perhaps I can improve the 4 if statements? Would using another javascript library like processing JS yeild better results?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听,心雨的声音 2024-11-07 21:46:38

目前,您在每次迭代时都会考虑每个圆。您可以使用与此类似的技术考虑更少的圆圈来提高性能。

http://en.wikipedia.org/wiki/Quadtree

基本上,为集合创建边界框界。如果您拖动的圆圈超出边界,则不要考虑边界框内的任何圆圈。我希望这有帮助。

Currently you consider every circle on every iteration. You could improve performance by considering fewer circles using a technique similar to this.

http://en.wikipedia.org/wiki/Quadtree

Basically, create bounding boxes for collections of circles. If the circle you are dragging is out of bounds then don't consider any circle within the bounding box. I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文