简体   繁体   English

优化移动设备的easyljs线条图

[英]Optimizing easeljs line drawing for mobile

I'm trying to make a simple application where the user can draw lines by click-dragging the mouse or through gestures on their touch device screen. 我正在尝试制作一个简单的应用程序,使用户可以通过单击拖动鼠标或通过其触摸设备屏幕上的手势来绘制线条。

It's perfectly fine on my desktop machine, but on my phone its very slow and jerky. 在我的台式机上完全可以,但是在我的手机上却非常缓慢而生涩。 It's not that performance degrades over time, it's immediately noticable. 并不是随着时间的流逝性能会下降,而是立即值得注意的。

I'm using easeljs and I extended shape. 我正在使用easyljs,并且扩展了形状。 On mouse movements it records the points and on tick it draws them. 在鼠标移动时,它将记录这些点,在刻度线上,它将绘制它们。 The stage's autoClear is set to false and the graphics object clears before it draws so it doesn't redraw anything from the previous tick. 舞台的autoClear设置为false,并且图形对象在绘制之前清除,因此它不会从上一个刻度中重画任何内容。

(function (window) {
    function LineDrawer() {
        this.initialize();
    }
    //Inheritance from Container
    LineDrawer.prototype = new createjs.Shape();
    LineDrawer.prototype.Shape_initialize = LineDrawer.prototype.initialize;
    LineDrawer.prototype.Shape_tick = LineDrawer.prototype._tick;

    LineDrawer.prototype.initialize = function () {
        //call to initialize() method from parent class 
        this.Shape_initialize();

        this.points = [];
        this.mouseMoveEventListener = $.proxy(this.onMouseMove, this);
    }
    LineDrawer.prototype._tick = function (e) {
        //call to _tick method from parent class 
        this.Shape_tick();

        var points = this.points;
        if (points.length > 0) {
            var graphics = this.graphics.clear()
                                .setStrokeStyle(3, 'round', 'round')
                                .beginStroke("#000000")
                                .moveTo(points[0].x, points[0].y)

            var pt;                
            for (var i = 1; i < points.length; i = i + 1) {
                pt = points[i];
                graphics.lineTo(pt.x, pt.y);
            }

            points.length = 0;
            if (typeof pt !== 'undefined') {
                points.push(new createjs.Point(pt.x, pt.y));
            }
        }
    }

    LineDrawer.prototype.onMouseDown = function (e) {
        this.points.push(new createjs.Point(e.stageX, e.stageY));
        this.parent.addEventListener("stagemousemove", this.mouseMoveEventListener);
    }

    LineDrawer.prototype.onMouseMove = function (e) {
        this.points.push(new createjs.Point(e.stageX, e.stageY));
    }

    LineDrawer.prototype.onMouseUp = function (e) {
        this.points.push(new createjs.Point(e.stageX, e.stageY));
        this.parent.removeEventListener("stagemousemove", this.mouseMoveEventListener);
    }

    window.LineDrawer = LineDrawer;
}(window));

Here's the code for setting up the stage: 这是设置阶段的代码:

var stage,
    lineDrawer;

$(document).ready(function () {
    lineDrawer = new LineDrawer();

    var $canvas = $('#canvasMain');

    stage = new createjs.Stage($canvas[0]);
    createjs.Touch.enable(stage);
    stage.addChild(lineDrawer);
    stage.autoClear = false;

    stage.addEventListener("stagemousedown", $.proxy(lineDrawer.onMouseDown, lineDrawer));
    stage.addEventListener("stagemouseup", $.proxy(lineDrawer.onMouseUp, lineDrawer));

    createjs.Ticker.timingMode = createjs.Ticker.RAF;
    createjs.Ticker.addEventListener("tick", stage);
})

Here's a fiddle for everything. 这是一切的小提琴 Other information: I'm using jquery-1.8.3 and a RAF polyfill, these are my phone specs . 其他信息:我正在使用jquery-1.8.3和RAF polyfill,这些是我的手机规格 I also got somebody to try with a much better Samsung phone with the same results. 我也有人尝试使用性能更好的三星手机。

Although admittedly my phone is on the low end of the spectrum, its not a dinosaur of a phone. 尽管我的手机虽然处于低端,但它不是手机的恐龙。 It is android 4.0+ and what I'm doing really isn't that complicated as far as I can tell. 据我所知,它是android 4.0以上版本,而我正在做的事情实际上并不那么复杂。 Am I doing anything wrong and/or is there anything I can do to improve this? 我做错什么了吗?和/或有什么我可以做些改善的事情? I wonder if maybe its a problem with the touch events and not the drawing speed as well. 我想知道它是否是触摸事件的问题,而不是绘制速度的问题。

EDIT: the other phone with laggy drawing was a Samsung S3 编辑:另一款拖延画图的手机是三星S3

Answering my own question. 回答我自己的问题。

The problem was a combination of me not using the graphic's endStroke method after all the lines causing the drawing to be one tick behind, and a logic error where I would only draw lines if there was one or more points, but really it was supposed to be 2 or more points. 问题的综合原因是我在所有线都导致绘图落后一格之后不使用图形的endStroke方法,以及逻辑错误,我只在有一个或多个点的情况下才绘制线,但实际上应该这样做是2分或以上。

The second part was causing the most lag. 第二部分造成了最大的滞后。 Interestingly there was no error since the array index is never out of bounds because of the for loop condition, so I guess just having an impossible for condition was really slow on the Chrome for android browsers, without crashing. 有趣的是,由于for循环条件,数组索引永远不会超出范围,因此没有错误,因此我猜想在Android的Chrome浏览器上,条件设置的实现确实很慢,而且不会崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM