-
Notifications
You must be signed in to change notification settings - Fork 27
/
glop.js
executable file
·215 lines (207 loc) · 5.72 KB
/
glop.js
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* @namespace The base of the interface between javascript and the canvas element. GLOP is a general javascript/canvas drawing system.
*/
var Glop = {
/**
* The number of frames that have been drawn.
* @type Number
*/
frame: 0,
/**
* The startup time in milliseconds since... 1970?
* @type Number
*/
start_time: 0,
/**
* The current time in milliseconds since... 1970?
* @type Number
*/
timestamp: 0,
/**
* The last 100 frames worth of timestamps, used to calculate fps.
* @type Number
*/
times: [],
/**
* The framerate over the last 10 seconds.
* @type Number
*/
fps: 0,
/**
* Whether the Glop environment has changed x,y size since the last frame.
* @type Boolean
*/
changed_size: 0,
/**
* A Date object updated (regenerated) every frame.
* @type Date
*/
date: new Date,
/**
* The width of the canvas
* @type Number
*/
width: 0,
/**
* The height of the canvas
*/
height: 0,
/**
* If set to true, drawing is disabled
*/
paused: false,
/**
* Binds all events to the main canvas. Use Glop.observe for all events.
*/
observe: function(a,b,c) {
$('main').observe(a,b,c)
},
/**
* Binds all events to the main canvas. Use Glop.observe for all events.
*/
fire: function(a,b,c) {
$('main').fire(a,b,c)
},
/**
* Binds all events to the main canvas. Use Glop.observe for all events.
*/
stopObserving: function(a,b,c) {
$('main').stopObserving(a,b,c)
},
/**
* Sets up powersaving.
*/
init: function() {
// seconds between redraws:
// new PeriodicalExecuter(Glop.draw_powersave, 0.1)
TimerManager.setup(Glop.draw_powersave,this)
},
/**
* Used to check if anything has changed since the last frame
*/
snapshot: '',
/**
* Draws a frame. Sets height/width, moves the map as needed, fires draw events, and draws
* the object array unless told not to (by subscriber of glop:draw.
*/
draw: function(custom_size, force_draw) {
if (Glop.paused && (force_draw != true)) {
Glop.fire('glop:predraw')
return
}
Glop.timestamp = Glop.date.getTime()
Glop.times.unshift(Glop.timestamp)
// the last 100 frames
if (Glop.times.length > 100) Glop.times.pop()
Glop.fps = parseInt(Glop.times.length/(Glop.timestamp - Glop.times.last())*1000)
// clear only if anything's changed!
var new_snapshot = Map.x +','+ Map.x_old +','+ Map.y +','+ Map.y_old +','+ Map.rotate +','+ Map.rotate_old +','+ Map.zoom +','+ Map.old_zoom
// consider thawing background:
// a) if there are remaining tasks to be completed
// b) measuring whether a canvas has been drawn on, or drawn 'completely'
if (new_snapshot != Glop.snapshot || force_draw || Glop.changed_size) {
Glop.fire('cartagen:change')
$C.thaw('background')
} else {
$C.freeze('background')
}
Glop.snapshot = new_snapshot
Glop.resize(custom_size)
// for embedded
//Glop.width = $C.element.getWidth()
//Glop.height = $C.element.getHeight()
//$C.element.width = Glop.width
//$C.element.height = Glop.height
Events.drag()
/**
* @name Glop#glop:predraw
* @event
* Fired each frame before features are drawn.
*/
Glop.fire('glop:predraw')
/**
* @name Glop#glop:draw
* @event
* Fired each frame between glop:predraw and glop:postdraw. SHould be used
* to draw features on the canvas. If the 'no_draw' property of the event
* is set to true, GLOP will not raw the objects array.
*/
draw_event = $('main').fire('glop:draw')
if (Config.vectors && !draw_event.no_draw) {
objects.each(function(object) {
object.draw()
})
}
/**
* @name Glop#glop:postdraw
* @event
* Fired at the end of each frame, after features are drawn.
*/
Glop.fire('glop:postdraw')
},
/**
* Adjusts size of canvas element to match browser window size
* @return Color in rgb(r, g, b) format
* @type String
*/
resize: function(custom_size) {
if (!custom_size) { // see Canvas.to_print_data_url()
Glop.changed_size = (Glop.width != document.viewport.getWidth() || Glop.height != document.viewport.getHeight()-Config.padding_top)
Glop.width = document.viewport.getWidth()
Glop.height = document.viewport.getHeight()-Config.padding_top
}
$C.canvases.each(function(canvas) {
if ($C.freezer.get(canvas.key) != true || Glop.changed_size) {
// we miss the initial draw before freezing
// if ($(canvas.key).width != Glop.width || $(canvas.key).height != Glop.height) {
$(canvas.key).width = Glop.width
$(canvas.key).height = Glop.height
}
})
$$('body')[0].style.width = Glop.width+"px"
},
/**
* Creates a random color
* @return Color in rgb(r, g, b) format
* @type String
*/
random_color: function() {
return "rgb("+Math.round(Math.random()*255)+","+Math.round(Math.random()*255)+","+Math.round(Math.random()*255)+")"
},
/**
* The number of frames to continue rendering, even if there's no further user interaction
*/
tail: 0,
/**
* Triggered when moused is moved on the canvas
* @param {Event} event
*/
trigger_draw: function(t) {
// t = t || 1
if (Object.isNumber(t) && !Object.isUndefined(t)) {
if (t > this.tail) this.tail = t
} else {
if (this.tail <= 0) this.tail = 1
}
},
/**
* Draws only if needed. Designed to be called periodically.
*/
draw_powersave: function() {
var delay = 20
if (this.tail > 0 || Config.powersave == false || (Importer.requested_plots && Importer.requested_plots > 0) || Cartagen.last_loaded_geohash_frame > Glop.frame-delay || Importer.parse_manager.completed < 100) {
if (this.tail > 0) this.tail -= 1
Glop.draw()
Glop.frame += 1
} else {
Glop.times = []
}
Glop.date = new Date
}
}
document.observe('cartagen:init', Glop.init.bindAsEventListener(Glop))
//= require "tasks"
//= require "timer"
//= require "events"
//= require "canvas"
//= require "canvastext"