|
3 | 3 | module.exports = FrameHistory; |
4 | 4 |
|
5 | 5 | function FrameHistory() { |
6 | | - this.frameHistory = []; |
7 | | -} |
| 6 | + this.changeTimes = new Float64Array(256); |
| 7 | + this.changeOpacities = new Uint8Array(256); |
| 8 | + this.opacities = new Uint8ClampedArray(256); |
| 9 | + this.array = new Uint8Array(this.opacities.buffer); |
8 | 10 |
|
9 | | -FrameHistory.prototype.getFadeProperties = function(duration) { |
10 | | - if (duration === undefined) duration = 300; |
11 | | - var currentTime = (new Date()).getTime(); |
| 11 | + this.fadeDuration = 300; |
| 12 | + this.previousZoom = 0; |
| 13 | + this.firstFrame = true; |
| 14 | +} |
12 | 15 |
|
13 | | - // Remove frames until only one is outside the duration, or until there are only three |
14 | | - while (this.frameHistory.length > 3 && this.frameHistory[1].time + duration < currentTime) { |
15 | | - this.frameHistory.shift(); |
16 | | - } |
| 16 | +FrameHistory.prototype.record = function(zoom) { |
| 17 | + var now = Date.now(); |
17 | 18 |
|
18 | | - if (this.frameHistory[1].time + duration < currentTime) { |
19 | | - this.frameHistory[0].z = this.frameHistory[1].z; |
| 19 | + if (this.firstFrame) { |
| 20 | + now = 0; |
| 21 | + this.firstFrame = false; |
20 | 22 | } |
21 | 23 |
|
22 | | - var frameLen = this.frameHistory.length; |
23 | | - if (frameLen < 3) console.warn('there should never be less than three frames in the history'); |
24 | | - |
25 | | - // Find the range of zoom levels we want to fade between |
26 | | - var startingZ = this.frameHistory[0].z, |
27 | | - lastFrame = this.frameHistory[frameLen - 1], |
28 | | - endingZ = lastFrame.z, |
29 | | - lowZ = Math.min(startingZ, endingZ), |
30 | | - highZ = Math.max(startingZ, endingZ); |
31 | | - |
32 | | - // Calculate the speed of zooming, and how far it would zoom in terms of zoom levels in one duration |
33 | | - var zoomDiff = lastFrame.z - this.frameHistory[1].z, |
34 | | - timeDiff = lastFrame.time - this.frameHistory[1].time; |
35 | | - var fadedist = zoomDiff / (timeDiff / duration); |
| 24 | + zoom = Math.floor(zoom * 10); |
36 | 25 |
|
37 | | - if (isNaN(fadedist)) console.warn('fadedist should never be NaN'); |
| 26 | + var z; |
| 27 | + if (zoom < this.previousZoom) { |
| 28 | + for (z = zoom + 1; z <= this.previousZoom; z++) { |
| 29 | + this.changeTimes[z] = now; |
| 30 | + this.changeOpacities[z] = this.opacities[z]; |
| 31 | + } |
| 32 | + } else { |
| 33 | + for (z = zoom; z > this.previousZoom; z--) { |
| 34 | + this.changeTimes[z] = now; |
| 35 | + this.changeOpacities[z] = this.opacities[z]; |
| 36 | + } |
| 37 | + } |
38 | 38 |
|
39 | | - // At end of a zoom when the zoom stops changing continue pretending to zoom at that speed |
40 | | - // bump is how much farther it would have been if it had continued zooming at the same rate |
41 | | - var bump = (currentTime - lastFrame.time) / duration * fadedist; |
| 39 | + for (z = 0; z < 256; z++) { |
| 40 | + var timeSince = now - this.changeTimes[z]; |
| 41 | + var opacityChange = timeSince / this.fadeDuration * 255; |
| 42 | + if (z <= zoom) { |
| 43 | + this.opacities[z] = this.changeOpacities[z] + opacityChange; |
| 44 | + } else { |
| 45 | + this.opacities[z] = this.changeOpacities[z] - opacityChange; |
| 46 | + } |
| 47 | + } |
42 | 48 |
|
43 | | - return { |
44 | | - fadedist: fadedist, |
45 | | - minfadezoom: lowZ, |
46 | | - maxfadezoom: highZ, |
47 | | - bump: bump |
48 | | - }; |
| 49 | + this.changed = true; |
| 50 | + this.previousZoom = zoom; |
49 | 51 | }; |
50 | 52 |
|
51 | | -// Record frame history that will be used to calculate fading params |
52 | | -FrameHistory.prototype.record = function(zoom) { |
53 | | - var currentTime = (new Date()).getTime(); |
54 | | - |
55 | | - // first frame ever |
56 | | - if (!this.frameHistory.length) { |
57 | | - this.frameHistory.push({time: 0, z: zoom }, {time: 0, z: zoom }); |
58 | | - } |
| 53 | +FrameHistory.prototype.bind = function(gl) { |
| 54 | + if (!this.texture) { |
| 55 | + this.texture = gl.createTexture(); |
| 56 | + gl.bindTexture(gl.TEXTURE_2D, this.texture); |
| 57 | + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); |
| 58 | + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); |
| 59 | + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); |
| 60 | + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); |
| 61 | + gl.texImage2D(gl.TEXTURE_2D, 0, gl.ALPHA, 256, 1, 0, gl.ALPHA, gl.UNSIGNED_BYTE, this.array); |
59 | 62 |
|
60 | | - if (this.frameHistory.length === 2 || this.frameHistory[this.frameHistory.length - 1].z !== zoom) { |
61 | | - this.frameHistory.push({ |
62 | | - time: currentTime, |
63 | | - z: zoom |
64 | | - }); |
| 63 | + } else { |
| 64 | + gl.bindTexture(gl.TEXTURE_2D, this.texture); |
| 65 | + if (this.changed) { |
| 66 | + gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 256, 1, gl.ALPHA, gl.UNSIGNED_BYTE, this.array); |
| 67 | + this.changed = false; |
| 68 | + } |
65 | 69 | } |
66 | 70 | }; |
0 commit comments