Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
144 views
in Technique[技术] by (71.8m points)

javascript - fabric.js具有许多项目的性能(Performance of fabric.js with many items)

I'm playing around with fabric.js and wonder how to tweak performance on handling object events when having a lot of items (more than 1500...).

(我在玩fabric.js,想知道当有很多项目(超过1500个...)时,如何调整处理对象事件的性能。)

I have build a little example: https://codepen.io/skurrilewelt/pen/XWWvBoP

(我建立了一个小例子: https : //codepen.io/skurrilewelt/pen/XWWvBoP)

    $(document).ready(function () {
        var canvas = new fabric.Canvas('canvas');
        var width = 10;
        var height = 10;
        var dist = 5;

        function add(left, top) {
            canvas.add(new fabric.Rect({
                left: left,
                top: top,
                width: width,
                height: height,
                fill: 'green',
                stroke: 'black',
                strokeWidth: 1,
                padding: 10,
                draggable: false,
                hasControls: false,
                hasBorders: false,
            }));
        }

        var col = 0;
        var row = 0;
        for (row = 0; row < 50; row++) {
            for (col = 0; col < 30; col++) {
                add(row * (width + dist), col * (height + dist));
            }
        }

        canvas.renderOnAddRemove = false;
        canvas.on({
            'mouse:down': function (e) {
                if (e.target) {
                    console.log(e.target);
                    canvas.getActiveObject().set("fill", '#f00');
                    canvas.renderAll();
                }
            }
        });

        canvas.renderAll();
    });

If you click an rect you will have to wait approximatly 3 secs until the rect will change it's color.

(如果单击一个rect,则必须等待大约3秒钟,直到rect改变其颜色。)

Are there other approaches or can I enhance the performance of this?

(还有其他方法还是可以提高其性能?)

I will need up to 3500 objects... Thanks in advance

(我最多需要3500个对象...预先感谢)

  ask by Ole_S translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...