-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathds-Canvas-Scrollmanager.js
More file actions
97 lines (88 loc) · 2.82 KB
/
ds-Canvas-Scrollmanager.js
File metadata and controls
97 lines (88 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
(function (root, global, Backbone, _, $, undefined) {
var Canvas = root.Canvas,
SizeManager = root.SizeManager;
var ScrollManager = new (Backbone.Model)({scrollTop: 0, scrollLeft: 0});
var counter = 0;
new (Backbone.View.extend({
initialize: function () {
this._checkOffset = _.bind(this._checkOffset, this);
var canvasDimensions = {
width: 1,
height: 1
}
Canvas.on("setup", function (data) {
var scrollTop = ScrollManager.get("scrollTop") || 0,
scrollLeft = ScrollManager.get("scrollLeft") || 0,
relY = (scrollTop / canvasDimensions.height) || ((data.height - SizeManager.get("height")) / 2) / data.height,
relX = (scrollLeft / canvasDimensions.width) || ((data.width - SizeManager.get("width")) / 2) / data.width ;
canvasDimensions = data;
ScrollManager.set({
scrollTop: ~~(data.height * relY),
scrollLeft: ~~(data.width * relX)
})
}, this);
this.model.on("change", function (m) {
if (m.get("scrollTop") !== this.position.scrollTop || m.get("scrollLeft") !== this.position.scrollLeft) {
this.position = {
scrollTop: m.get("scrollTop"),
scrollLeft: m.get("scrollLeft")
};
this.$el.scrollTop(m.get("scrollTop")).scrollLeft(m.get("scrollLeft"));
}
}, this);
SizeManager
.on("change:width", function (m, width) {
var previousWidth = m.previousAttributes().width,
diff = width - previousWidth,
scaleX = root.ScaleManager ? root.ScaleManager.get("scaleX") : 1,
scrollLeft = this.position.scrollLeft;
ScrollManager.set("scrollLeft", scrollLeft - (diff/2) / scaleX);
}, this)
.on("change:height", function (m, height) {
var previousHeight = m.previousAttributes().height,
diff = height - previousHeight,
scaleY = root.ScaleManager ? root.ScaleManager.get("scaleY") : 1,
scrollTop = this.position.scrollTop;
ScrollManager.set("scrollTop", scrollTop - (diff/2) / scaleY);
}, this)
this.offset = {
x: 0,
y: 0
};
this._getScrollingPosition();
this.isScrolled = false;
this.checkOffsetIsRequested = false;
},
_getScrollingPosition: function () {
this.position = {
scrollTop: this.$el.scrollTop(),
scrollLeft: this.$el.scrollLeft()
};
this.model.set(this.position);
},
_checkOffset: function () {
this.checkOffsetIsRequested = false;
if (this.isScrolled) {
this.isScrolled = false;
this._getScrollingPosition();
}
},
scrollHandler: function (e) {
e.stopImmediatePropagation();
this.isScrolled = true;
if (!this.checkOffsetIsRequested) {
this.checkOffsetIsRequested = true;
window.requestAnimationFrame(this._checkOffset);
}
},
events: {
"scroll": "scrollHandler"
}
}))({
el: $(".ds-canvas-viewport"),
model: ScrollManager
});
root.supply({
"ScrollManager": ScrollManager
})
})(ds, this, Backbone, _, jQuery);