-
Notifications
You must be signed in to change notification settings - Fork 0
/
pano.js
184 lines (163 loc) · 4.01 KB
/
pano.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
// Just in case
if (!window.requestAnimationFrame) {
window.requestAnimationFrame = function(func){ setTimeout(func, 16) }
}
function Pano(canvas, cfg) {
// CONFIG
cfg = cfg || {};
cfg.fovMax = cfg.fovMax || Infinity;
cfg.fovMin = cfg.fovMin || -Infinity;
// INIT
var engine = null;
try {
if (location.hash == "#no-webgl") throw new Error("OK, without WebGL.");
engine = new WebGLPano(canvas);
} catch(e) {
alert(e.message);
engine = new CanvasPano(canvas);
}
var pMatrix = mat4.create();
var mvMatrices = new function() {
var i = mat4.identity(mat4.create());
this.north = i;
this.south = mat4.rotateY(mat4.create(i), Math.PI);
this.west = mat4.rotateY(mat4.create(i), Math.PI/2);
this.east = mat4.rotateY(mat4.create(i), -Math.PI/2);
this.top = mat4.rotateX(mat4.create(i), Math.PI/2);
this.bottom =mat4.rotateX(mat4.create(i), -Math.PI/2);
}
// LOADING
var images = {};
var textures = {};
function setImagesSrc(srcs, onOk, onErr) {
var n = 0;
function oneLoaded() {
n--;
if (n == 0 && onOk) onOk();
}
for (var side in mvMatrices) {
if (!srcs[side]) continue;
var img = new Image();
img.src = srcs[side];
img.onload = (function(side){
textures[side] = engine.handleLoadedImage(this);
console.log("["+side+"] done.")
reqestRedraw();
oneLoaded();
}).bind(img, side);
img.onerror = onErr;
images[side] = img;
n++;
}
}
// DRAWIND
var fov = 75;
var xRot = 0;
var yRot = 0;
function drawScene() {
mat4.perspective(fov, canvas.width / canvas.height, 0.1, 1000.0, pMatrix);
mat4.rotateX(pMatrix, xRot);
mat4.rotateY(pMatrix, yRot);
engine.drawStart(pMatrix);
for (var side in mvMatrices) {
if (!textures[side]) continue;
engine.drawSide(pMatrix, mvMatrices[side], textures[side]);
}
engine.drawEnd(pMatrix);
}
var requested = false;
function redraw() {
drawScene();
requested = false;
smoothIfNecessary();
}
function reqestRedraw() {
if (requested) return;
requested = true;
requestAnimationFrame(redraw, canvas);
}
// TRANSFORMING
function resize() {
canvas.width = canvas.offsetWidth * devicePixelRatio;
canvas.height = canvas.offsetHeight * devicePixelRatio;
engine.resize(canvas);
}
function move(dx, dy) {
yRot += -dx/10000*fov;
xRot += -dy/10000*fov;
reqestRedraw();
}
function zoom(d) {
fov = Math.max(cfg.fovMin, Math.min(fov/d, cfg.fovMax));
reqestRedraw();
}
function distBetween(x1,y1,x2,y2) {
return Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
}
// MOVEMENT SMOOTHING
// "proxy" between CONTROLS and TRANSFORMING
var smooth_dz=1;
function smoothIfNecessary() {
if (smooth_dz>0.99 && smooth_dz<1.01) return;
var nz = 1+(smooth_dz-1)*0.5;
zoom(smooth_dz/nz);
smooth_dz = nz;
}
function smoothZoom(d) {
smooth_dz *= d;
smoothIfNecessary();
}
// CONTROLS
var p = {};
var grabbed = false;
var grab_x, grab_y;
p.singleDown = function(x, y, is_switching) {
grabbed = true;
grab_x = x; grab_y = y;
return true;
}
p.singleMove = function(x, y) {
if (!grabbed) return false;
move(x-grab_x, y-grab_y);
grab_x = x; grab_y = y;
return true;
}
p.singleUp = function(is_switching) {
var was_down = grabbed;
grabbed = false;
return was_down;
}
p.wheelRot = function(dx, dy, dz) {
smoothZoom(Math.pow(2, -dy/250));
return true;
}
var grab_len;
p.doubleDown = function(x1, y1, x2, y2) {
grabbed = true;
grab_x = (x1+x2)/2; grab_y = (y1+y2)/2;
grab_len = distBetween(x1, y1, x2, y2);
return true;
}
p.doubleMove = function(x1, y1, x2, y2) {
if (!grabbed) return false;
var cx=(x1+x2)/2, cy=(y1+y2)/2;
var len = distBetween(x1, y1, x2, y2);
move(cx-grab_x, cy-grab_y);
zoom(len/grab_len);
grab_x = cx; grab_y = cy;
grab_len = len;
return true;
}
p.doubleUp = function() {
var was_down = grabbed;
grabbed = false;
return was_down;
}
p.startElem = canvas;
p.stopElem = document;
control.add(p);
// PUBLIC
this.resize = resize;
this.setImagesSrc = setImagesSrc;
this.reqestRedraw = reqestRedraw;
}