forked from mattdesl/google-panorama-equirectangular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (47 loc) · 1.42 KB
/
index.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
var getPanoramaTiles = require('./lib/getPanoramaTiles')
var loader = require('async-image-loader')
var Emitter = require('events').EventEmitter
module.exports = loadEquirectangular
function loadEquirectangular (id, opt) {
opt = opt || {}
var data = getPanoramaTiles(id, opt)
var canvas = opt.canvas || document.createElement('canvas')
var context = canvas.getContext('2d')
var canvasWidth = data.width
var canvasHeight = data.height
// failed tiles will be transparent
canvas.width = canvasWidth
canvas.height = canvasHeight
context.clearRect(0, 0, canvasWidth, canvasHeight)
var emitter = new Emitter()
var images = data.images
var zero = [0, 0]
process.nextTick(start)
return emitter
function done () {
emitter.emit('complete', canvas)
// ensure these will get collected quickly
canvas = null
context = null
}
function start () {
emitter.emit('start', data)
loader(images, { crossOrigin: opt.crossOrigin }, done)
.on('not-found', function (data) {
emitter.emit('not-found', data.url)
})
.on('progress', function (ev) {
var tile = ev.data
var position = tile.position || zero
if (ev.image) {
context.drawImage(ev.image, position[0], position[1])
}
emitter.emit('progress', {
count: ev.count,
total: ev.total,
image: ev.image,
position: position
})
})
}
}