bebraw
12/16/2011 - 4:30 PM

Simple dragging algo borrowed from my drawing app

Simple dragging algo borrowed from my drawing app

define(function() {
    return {
        meta: {
            tooltip: 'Drags the canvas around',
            category: 'canvas'
        },
        selected: function() {
            var $elem = $$('.paintCursor')[0];

            this._oldPaintCursor = $elem.getStyle('cursor');
            $elem.setStyle('cursor', 'move');
        },
        deselected: function() {
            var $elem = $$('.paintCursor')[0];

            $elem.setStyle('cursor', this._oldPaintCursor);
        },
        down: function() {
            this._i = 0;
        },
        drag: function(opts) {
            if(!(this._i % 2)) {
                var canvasPanel = CD.panels.get('canvas');
                var canvasLoc = canvasPanel.getLocation();
                var offset = opts.ctx._toAbsolute(opts.points.cur.sub(opts.points.prev));

                var newPoint = {
                    x: canvasLoc.x + offset.x,
                    y: canvasLoc.y + offset.y
                };

                canvasPanel.setLocation(newPoint);
            }

            this._i++;
        }
    };
});