cynx-lib 1.3.101 → 1.3.103
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/ui/components/Globe.jsx +764 -0
- package/ui/components/Input.jsx +54 -4
- package/ui/components/useRuler.jsx +94 -0
- package/ui/index.cjs +10 -0
- package/ui/index.js +6 -0
package/package.json
CHANGED
|
@@ -0,0 +1,764 @@
|
|
|
1
|
+
import { useEffect, useRef, useCallback } from 'react';
|
|
2
|
+
|
|
3
|
+
// WebGL Shaders & Constants
|
|
4
|
+
const VS_QUAD = `attribute vec2 a;void main(){gl_Position=vec4(a,0.0,1.0);}`;
|
|
5
|
+
|
|
6
|
+
const FS_GLOBE = `precision highp float;
|
|
7
|
+
uniform vec2 t,v,s;
|
|
8
|
+
uniform vec3 F,w;
|
|
9
|
+
uniform vec4 n;
|
|
10
|
+
uniform float k,x,y;
|
|
11
|
+
uniform sampler2D z;
|
|
12
|
+
float u;
|
|
13
|
+
mat3 A(float a,float b){
|
|
14
|
+
float c=cos(a),d=cos(b),e=sin(a),f=sin(b);
|
|
15
|
+
return mat3(d,f*e,-f*c,0.0,c,e,f,d*-e,d*c);
|
|
16
|
+
}
|
|
17
|
+
vec3 B(vec3 c,out float G){
|
|
18
|
+
c=c.xzy;
|
|
19
|
+
float q=max(2.0,floor(log2(2.236068*k*3.141593*(1.0-c.z*c.z))*0.72021));
|
|
20
|
+
vec2 g=floor(pow(1.618034,q)/2.236068*vec2(1.0,1.618034)+0.5);
|
|
21
|
+
vec2 d=fract((g+1.0)*0.618034)*6.283185-3.883222;
|
|
22
|
+
vec2 e=-2.0*g;
|
|
23
|
+
vec2 f=vec2(atan(c.y,c.x),c.z-1.0);
|
|
24
|
+
vec2 r=floor(vec2(e.y*f.x-d.y*(f.y*k+1.0),-e.x*f.x+d.x*(f.y*k+1.0))/(d.x*e.y-e.x*d.y));
|
|
25
|
+
float o=3.141593;
|
|
26
|
+
vec3 C;
|
|
27
|
+
for(float h=0.0;h<4.0;h+=1.0){
|
|
28
|
+
vec2 D=vec2(mod(h,2.0),floor(h*0.5));
|
|
29
|
+
float j=dot(g,r+D);
|
|
30
|
+
if(j>k) continue;
|
|
31
|
+
float a=j,b=0.0;
|
|
32
|
+
if(a>=16384.0){a-=16384.0;b+=0.868872;}
|
|
33
|
+
if(a>=8192.0){a-=8192.0;b+=0.934436;}
|
|
34
|
+
if(a>=4096.0){a-=4096.0;b+=0.467218;}
|
|
35
|
+
if(a>=2048.0){a-=2048.0;b+=0.733609;}
|
|
36
|
+
if(a>=1024.0){a-=1024.0;b+=0.866804;}
|
|
37
|
+
if(a>=512.0){a-=512.0;b+=0.433402;}
|
|
38
|
+
if(a>=256.0){a-=256.0;b+=0.216701;}
|
|
39
|
+
if(a>=128.0){a-=128.0;b+=0.108351;}
|
|
40
|
+
if(a>=64.0){a-=64.0;b+=0.554175;}
|
|
41
|
+
if(a>=32.0){a-=32.0;b+=0.777088;}
|
|
42
|
+
if(a>=16.0){a-=16.0;b+=0.888544;}
|
|
43
|
+
if(a>=8.0){a-=8.0;b+=0.944272;}
|
|
44
|
+
if(a>=4.0){a-=4.0;b+=0.472136;}
|
|
45
|
+
if(a>=2.0){a-=2.0;b+=0.236068;}
|
|
46
|
+
if(a>=1.0){a-=1.0;b+=0.618034;}
|
|
47
|
+
float l=fract(b)*6.283185;
|
|
48
|
+
float i=1.0-2.0*j*u;
|
|
49
|
+
float m=sqrt(1.0-i*i);
|
|
50
|
+
vec3 p=vec3(cos(l)*m,sin(l)*m,i);
|
|
51
|
+
float E=length(c-p);
|
|
52
|
+
if(E<o){o=E;C=p;}
|
|
53
|
+
}
|
|
54
|
+
G=o;
|
|
55
|
+
return C.xzy;
|
|
56
|
+
}
|
|
57
|
+
void main(){
|
|
58
|
+
u=1.0/k;
|
|
59
|
+
vec2 c=1.0/t;
|
|
60
|
+
vec2 b=(gl_FragCoord.xy*c*2.0-1.0)/x-v*vec2(1.0,-1.0)*c;
|
|
61
|
+
b.x*=t.x*c.y;
|
|
62
|
+
float a=dot(b,b);
|
|
63
|
+
float f=0.0;
|
|
64
|
+
vec4 l=vec4(0.0);
|
|
65
|
+
if(a<=0.64){
|
|
66
|
+
float g;
|
|
67
|
+
vec4 m=vec4(0.0);
|
|
68
|
+
vec3 h=normalize(vec3(b,sqrt(0.64-a)));
|
|
69
|
+
mat3 o=A(s.y,s.x);
|
|
70
|
+
float i=h.z;
|
|
71
|
+
vec3 d=B(h*o,g);
|
|
72
|
+
float j=asin(d.y);
|
|
73
|
+
float e=acos(-d.x/cos(j));
|
|
74
|
+
e=d.z<0.0?-e:e;
|
|
75
|
+
float p=max(texture2D(z,vec2(e*0.5/3.141593,-(j/3.141593+0.5))).x,y);
|
|
76
|
+
float q=p*smoothstep(0.008,0.0,g)*pow(i,n.y)*n.x;
|
|
77
|
+
m+=vec4(F*(mix((1.0-q)*pow(i,0.4),q,n.z)+0.1)+pow(1.0-i,4.0)*w,1.0);
|
|
78
|
+
l+=m*(1.0+n.w)*0.5;
|
|
79
|
+
f=(1.0-a)*(1.0-a)*smoothstep(0.0,1.0,0.2/(a-0.64));
|
|
80
|
+
}else{
|
|
81
|
+
float r=sqrt(0.2/(a-0.64));
|
|
82
|
+
f=smoothstep(0.5,1.0,r/(r+1.0));
|
|
83
|
+
}
|
|
84
|
+
gl_FragColor=l+vec4(f*w,f);
|
|
85
|
+
}`;
|
|
86
|
+
|
|
87
|
+
const VS_MARKER = `varying vec2 m;
|
|
88
|
+
varying vec3 g;
|
|
89
|
+
varying float h;
|
|
90
|
+
attribute vec2 n;
|
|
91
|
+
attribute vec3 p,w;
|
|
92
|
+
attribute float q,x;
|
|
93
|
+
uniform vec2 b,r;
|
|
94
|
+
uniform float i,j,k,s;
|
|
95
|
+
void main(){
|
|
96
|
+
float c=cos(j),d=sin(j),e=cos(i),f=sin(i);
|
|
97
|
+
vec3 a=p*(0.8+s);
|
|
98
|
+
vec3 l=vec3(e*a.x+f*a.z,f*d*a.x+c*a.y-e*d*a.z,-f*c*a.x+d*a.y+e*c*a.z);
|
|
99
|
+
if(l.z<0.0&&length(l.xy)<0.8){
|
|
100
|
+
gl_Position=vec4(2.0,2.0,0.0,1.0);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
float t=b.y/b.x;
|
|
104
|
+
vec2 y=(l.xy+n*q*2.0)*vec2(t,1.0)*k+r*vec2(1.0,-1.0)*k/b;
|
|
105
|
+
gl_Position=vec4(y,0.0,1.0);
|
|
106
|
+
m=n;
|
|
107
|
+
g=w;
|
|
108
|
+
h=x;
|
|
109
|
+
}`;
|
|
110
|
+
|
|
111
|
+
const FS_MARKER = `precision highp float;
|
|
112
|
+
varying vec2 m;
|
|
113
|
+
varying vec3 g;
|
|
114
|
+
varying float h;
|
|
115
|
+
uniform vec3 v;
|
|
116
|
+
void main(){
|
|
117
|
+
if(length(m)>0.25) discard;
|
|
118
|
+
vec3 a=h>0.5?g:v;
|
|
119
|
+
gl_FragColor=vec4(a,1.0);
|
|
120
|
+
}`;
|
|
121
|
+
|
|
122
|
+
const VS_ARC = `varying vec3 i;
|
|
123
|
+
varying float j,s,t;
|
|
124
|
+
attribute vec2 k;
|
|
125
|
+
attribute vec3 l,m,N;
|
|
126
|
+
attribute float v,w,O;
|
|
127
|
+
uniform vec2 g,x;
|
|
128
|
+
uniform float y,z,h,A;
|
|
129
|
+
mat3 B(float a,float b){
|
|
130
|
+
float c=cos(a),d=cos(b),e=sin(a),f=sin(b);
|
|
131
|
+
return mat3(d,f*e,-f*c,0.0,c,e,f,d*-e,d*c);
|
|
132
|
+
}
|
|
133
|
+
vec3 C(vec3 c,vec3 d,vec3 e,float a){
|
|
134
|
+
float b=1.0-a;
|
|
135
|
+
return b*b*c+2.0*b*a*d+a*a*e;
|
|
136
|
+
}
|
|
137
|
+
vec3 D(vec3 c,vec3 b,vec3 d,float a){
|
|
138
|
+
float e=1.0-a;
|
|
139
|
+
return 2.0*e*(b-c)+2.0*a*(d-b);
|
|
140
|
+
}
|
|
141
|
+
void main(){
|
|
142
|
+
mat3 b=B(z,y);
|
|
143
|
+
float c=0.8+A;
|
|
144
|
+
vec3 d=l*c,e=m*c,f=l+m;
|
|
145
|
+
float n=length(f);
|
|
146
|
+
vec3 E=n>0.001?f/n:vec3(0.0,1.0,0.0);
|
|
147
|
+
vec3 o=E*(0.8+v);
|
|
148
|
+
float p=k.x;
|
|
149
|
+
vec3 F=C(d,o,e,p);
|
|
150
|
+
vec3 q=b*F;
|
|
151
|
+
vec3 G=D(d,o,e,p);
|
|
152
|
+
vec3 H=b*G;
|
|
153
|
+
vec2 a=H.xy;
|
|
154
|
+
float r=length(a);
|
|
155
|
+
vec2 I=r>0.001?vec2(-a.y,a.x)/r:vec2(1.0,0.0);
|
|
156
|
+
float J=g.x/g.y;
|
|
157
|
+
vec2 K=q.xy*vec2(1.0/J,1.0)*h+x*vec2(1.0,-1.0)*h/g;
|
|
158
|
+
vec2 P=K+I*w*k.y*h;
|
|
159
|
+
gl_Position=vec4(P,0.0,1.0);
|
|
160
|
+
i=N;
|
|
161
|
+
j=O;
|
|
162
|
+
s=q.z;
|
|
163
|
+
t=length(q.xy);
|
|
164
|
+
}`;
|
|
165
|
+
|
|
166
|
+
const FS_ARC = `precision highp float;
|
|
167
|
+
varying vec3 i;
|
|
168
|
+
varying float j,s,t;
|
|
169
|
+
uniform vec3 M;
|
|
170
|
+
void main(){
|
|
171
|
+
if(s<0.0&&t<0.8) discard;
|
|
172
|
+
vec3 a=j>0.5?i:M;
|
|
173
|
+
gl_FragColor=vec4(a,1.0);
|
|
174
|
+
}`;
|
|
175
|
+
|
|
176
|
+
const SPHERE_BASE_RADIUS = 0.8;
|
|
177
|
+
const LANDMASS_IMAGE =
|
|
178
|
+
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAACAAQAAAADMzoqnAAAECklEQVR42u3VsW4jRRzH8d94gzfF4Q0VQaC4vBLTRTp0mze4ggfAPAE5XQEFsGNAVIjwBrmW7h7gJE+giKjyABTZE4g06LKJETdRJvtD65kdz6yduKABiW+TVfzRf2bXYxtcE/59YJCz6YdbgQF6ACSRrwYKYImmh5PbwOewlV3wlQNbAN6SEExjUOO+BU0aCSnxReHABUlK4YFQeJeUT3da8IIkZ6NGoSnFY5KsMoVzMKfECUnqxgPYRArarmUCndHwzIEaQEpg5xVdBXROl8mpAQx5dUgPiHoYAAkg5w3JABR06byGAVgcRGAz5bznj6phBQNRFwyqgdxebH6gshJAesWoFhgYpApAFoG8BIZ/fEhSox5jDjQXmV0Ar5XJfAIrALi3URVs09gHIL4XJCkLC5LH9JWiArABFCSrQjdgkBzRJ0WJeUOSNyQAfJJwUSWUBRlJQ8oGHATACGlBynnzy2kEYLNjrxouigD8BZcgOeVPqh12RtufaCN5wCPVDpvQ9lsIrqndsJtDcWqBCpf4hWN7OdWHBw58FwIaNOU/n1TpMW2DFaD48cmr4185T8NHkpUFX749pQPVdgRKC/DGoQPVeAEKv+WHvY8OOWNTPRp5kHuwSf8wzXtVBKR7YwEH9H3lQUaypUfSATOALyVNu5vZJW31Bnx98nkLfDUWJaz6ixvm+RIQRdl3kmRxxiaDoGnZW4CpPfkaQadlcPim1xOSvETQo7Lv75enVAXJ3xGUlony4KQBBWUM1NiDc6qhyS8RgQs18OCMMtPDaAUIyg0PZkRWDqs+wnKJBTDI1Js6BolegOsKmUxNDBAAKqQyMQmidhegBlLZ+wwKYdv5M/8x1khkb1cgKqP2H+MKyV5vS+whrE8DQDgAlUAoRBX056EElJCjJVACeJBZgNfVp+iCCm4RBWCgKsRxASSA9KgDhDtCiTuMyfHsKXzhC6wNAIjjWb8LKAOA2ctk3FmCOlgKFy8f1N0JJtgsxinYnVAHt4t3gPzZXSCTyCWCQmBT91QE3B5yarSN40dNHYPka4TlDhTUI8zLvl0JSL3vZn6DsCFZOeB2yROEpR68sECQQA++xIGCR2X7DwlEoLRgUrZrqlUg50S1uy43YqDcN6UFBVkhAjWiCV2Q0jgQPdplMKxvBXodcOfAwJYvgdL+1etA1YJJfBcZlQV7sO1i2gHoNiyxtQ5sBsCgWyoxCHiFFd2L5nUTCqMAqGUgsQ9f5kCcCiZgRYkMgMTd5WsB1rTzj0Em14BE4r+QxN1lCEsVur2PoF5Wbg8RJXR4djgvBgauhLywoEZQrt1KKRdVS4CdlJ8qafyP+9KIj/nE/d7kKwH9jgS72e9DV+kvfTWgct4ZyP8Byb8BPG7MaaIIkAQAAAAASUVORK5CYII=';
|
|
179
|
+
|
|
180
|
+
function createShader(gl, type, source) {
|
|
181
|
+
const shader = gl.createShader(type);
|
|
182
|
+
gl.shaderSource(shader, source);
|
|
183
|
+
gl.compileShader(shader);
|
|
184
|
+
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
185
|
+
gl.deleteShader(shader);
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
return shader;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function createProgram(gl, vsSource, fsSource) {
|
|
192
|
+
const vs = createShader(gl, gl.VERTEX_SHADER, vsSource);
|
|
193
|
+
const fs = createShader(gl, gl.FRAGMENT_SHADER, fsSource);
|
|
194
|
+
if (!vs || !fs) return null;
|
|
195
|
+
const program = gl.createProgram();
|
|
196
|
+
gl.attachShader(program, vs);
|
|
197
|
+
gl.attachShader(program, fs);
|
|
198
|
+
gl.linkProgram(program);
|
|
199
|
+
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
|
|
200
|
+
gl.deleteShader(vs);
|
|
201
|
+
gl.deleteShader(fs);
|
|
202
|
+
gl.deleteProgram(program);
|
|
203
|
+
return null;
|
|
204
|
+
}
|
|
205
|
+
return program;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function getUniforms(gl, program, names) {
|
|
209
|
+
const map = {};
|
|
210
|
+
for (const name of names) {
|
|
211
|
+
map[name] = gl.getUniformLocation(program, name);
|
|
212
|
+
}
|
|
213
|
+
return map;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function getAttributes(gl, program, names) {
|
|
217
|
+
const map = {};
|
|
218
|
+
for (const name of names) {
|
|
219
|
+
map[name] = gl.getAttribLocation(program, name);
|
|
220
|
+
}
|
|
221
|
+
return map;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function locationToSphere([lat, lon]) {
|
|
225
|
+
const phi = (lat * Math.PI) / 180;
|
|
226
|
+
const theta = (lon * Math.PI) / 180 - Math.PI;
|
|
227
|
+
const cosPhi = Math.cos(phi);
|
|
228
|
+
return [-cosPhi * Math.cos(theta), Math.sin(phi), cosPhi * Math.sin(theta)];
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function setupVertexAttrib(gl, location, size, stride, offset, divisor, isWebGL2, ext) {
|
|
232
|
+
if (location < 0) return;
|
|
233
|
+
gl.enableVertexAttribArray(location);
|
|
234
|
+
gl.vertexAttribPointer(location, size, gl.FLOAT, false, stride, offset);
|
|
235
|
+
if (isWebGL2) {
|
|
236
|
+
gl.vertexAttribDivisor(location, divisor);
|
|
237
|
+
} else if (ext) {
|
|
238
|
+
ext.vertexAttribDivisorANGLE(location, divisor);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Creates and renders a standalone WebGL 3D globe.
|
|
244
|
+
*
|
|
245
|
+
* @param {HTMLCanvasElement} canvas
|
|
246
|
+
* @param {Object} opts
|
|
247
|
+
* @returns {{ update: (opts: Object) => void, destroy: () => void }}
|
|
248
|
+
*/
|
|
249
|
+
export function createGlobe(canvas, opts) {
|
|
250
|
+
const contextAttrs = {
|
|
251
|
+
alpha: true,
|
|
252
|
+
stencil: false,
|
|
253
|
+
antialias: true,
|
|
254
|
+
depth: false,
|
|
255
|
+
preserveDrawingBuffer: false,
|
|
256
|
+
...opts.context,
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
let gl = canvas.getContext('webgl2', contextAttrs);
|
|
260
|
+
const isWebGL2 = !!gl;
|
|
261
|
+
if (!gl) {
|
|
262
|
+
gl = canvas.getContext('webgl', contextAttrs);
|
|
263
|
+
}
|
|
264
|
+
if (!gl) {
|
|
265
|
+
return { destroy: () => {}, update: () => {} };
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const ext = isWebGL2 ? null : gl.getExtension('ANGLE_instanced_arrays');
|
|
269
|
+
const dpr = opts.devicePixelRatio || (typeof window !== 'undefined' ? window.devicePixelRatio || 1 : 1);
|
|
270
|
+
|
|
271
|
+
canvas.width = (opts.width || 500) * dpr;
|
|
272
|
+
canvas.height = (opts.height || 500) * dpr;
|
|
273
|
+
|
|
274
|
+
let phi = opts.phi || 0;
|
|
275
|
+
let theta = opts.theta || 0;
|
|
276
|
+
let markers = opts.markers || [];
|
|
277
|
+
let arcs = opts.arcs || [];
|
|
278
|
+
let mapSamples = opts.mapSamples || 16000;
|
|
279
|
+
let mapBrightness = opts.mapBrightness ?? 6;
|
|
280
|
+
let mapBaseBrightness = opts.mapBaseBrightness ?? 0;
|
|
281
|
+
let baseColor = opts.baseColor || [0.8, 0.85, 0.9];
|
|
282
|
+
let markerColor = opts.markerColor || [0.15, 0.5, 1.0];
|
|
283
|
+
let glowColor = opts.glowColor || [1, 1, 1];
|
|
284
|
+
let arcColor = opts.arcColor || [0.3, 0.6, 1.0];
|
|
285
|
+
let arcWidth = opts.arcWidth ?? 1;
|
|
286
|
+
let arcHeight = opts.arcHeight ?? 0.2;
|
|
287
|
+
let diffuse = opts.diffuse ?? 1.2;
|
|
288
|
+
let dark = opts.dark ?? 0;
|
|
289
|
+
let opacity = opts.opacity ?? 1;
|
|
290
|
+
let offset = opts.offset || [0, 0];
|
|
291
|
+
let scale = opts.scale ?? 1;
|
|
292
|
+
let markerElevation = opts.markerElevation ?? 0.05;
|
|
293
|
+
|
|
294
|
+
const globeProgram = createProgram(gl, VS_QUAD, FS_GLOBE);
|
|
295
|
+
const markerProgram = createProgram(gl, VS_MARKER, FS_MARKER);
|
|
296
|
+
const arcProgram = createProgram(gl, VS_ARC, FS_ARC);
|
|
297
|
+
|
|
298
|
+
if (!globeProgram) {
|
|
299
|
+
return { destroy: () => {}, update: () => {} };
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// Buffers
|
|
303
|
+
const quadBuffer = gl.createBuffer();
|
|
304
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, quadBuffer);
|
|
305
|
+
gl.bufferData(
|
|
306
|
+
gl.ARRAY_BUFFER,
|
|
307
|
+
new Float32Array([-1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1]),
|
|
308
|
+
gl.STATIC_DRAW
|
|
309
|
+
);
|
|
310
|
+
|
|
311
|
+
const arcStripBuffer = gl.createBuffer();
|
|
312
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, arcStripBuffer);
|
|
313
|
+
const stripPoints = [];
|
|
314
|
+
for (let i = 0; i <= 32; i++) {
|
|
315
|
+
const t = i / 32;
|
|
316
|
+
stripPoints.push(t, -1, t, 1);
|
|
317
|
+
}
|
|
318
|
+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(stripPoints), gl.STATIC_DRAW);
|
|
319
|
+
|
|
320
|
+
const markerBuffer = gl.createBuffer();
|
|
321
|
+
const arcBuffer = gl.createBuffer();
|
|
322
|
+
|
|
323
|
+
const globeUniforms = getUniforms(gl, globeProgram, ['t', 's', 'k', 'x', 'v', 'F', 'w', 'n', 'y', 'z']);
|
|
324
|
+
const markerUniforms = markerProgram ? getUniforms(gl, markerProgram, ['i', 'j', 'b', 'k', 'r', 'v', 's']) : {};
|
|
325
|
+
const markerAttribs = markerProgram ? getAttributes(gl, markerProgram, ['n', 'p', 'q', 'w', 'x']) : {};
|
|
326
|
+
const arcUniforms = arcProgram ? getUniforms(gl, arcProgram, ['y', 'z', 'g', 'h', 'x', 'M', 'A']) : {};
|
|
327
|
+
const arcAttribs = arcProgram ? getAttributes(gl, arcProgram, ['k', 'l', 'm', 'v', 'w', 'N', 'O']) : {};
|
|
328
|
+
const globePosAttrib = gl.getAttribLocation(globeProgram, 'a');
|
|
329
|
+
|
|
330
|
+
// Landmass Texture
|
|
331
|
+
const texture = gl.createTexture();
|
|
332
|
+
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
333
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, 1, 1, 0, gl.RGB, gl.UNSIGNED_BYTE, new Uint8Array([0, 0, 0]));
|
|
334
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
|
|
335
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
|
|
336
|
+
|
|
337
|
+
const img = new Image();
|
|
338
|
+
img.onload = () => {
|
|
339
|
+
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
340
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, img);
|
|
341
|
+
gl.generateMipmap(gl.TEXTURE_2D);
|
|
342
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
|
|
343
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
|
|
344
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
345
|
+
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
346
|
+
};
|
|
347
|
+
img.src = LANDMASS_IMAGE;
|
|
348
|
+
|
|
349
|
+
let arcCount = 0;
|
|
350
|
+
|
|
351
|
+
function updateMarkers(list) {
|
|
352
|
+
markers = list;
|
|
353
|
+
const data = new Float32Array(8 * markers.length);
|
|
354
|
+
markers.forEach((m, idx) => {
|
|
355
|
+
data.set(
|
|
356
|
+
[
|
|
357
|
+
...locationToSphere(m.location),
|
|
358
|
+
m.size,
|
|
359
|
+
...(m.color || [0, 0, 0]),
|
|
360
|
+
m.color ? 1 : 0,
|
|
361
|
+
],
|
|
362
|
+
8 * idx
|
|
363
|
+
);
|
|
364
|
+
});
|
|
365
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, markerBuffer);
|
|
366
|
+
gl.bufferData(gl.ARRAY_BUFFER, data, gl.DYNAMIC_DRAW);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function updateArcs(list) {
|
|
370
|
+
arcs = list;
|
|
371
|
+
arcCount = arcs.length;
|
|
372
|
+
const data = new Float32Array(12 * arcs.length);
|
|
373
|
+
arcs.forEach((a, idx) => {
|
|
374
|
+
data.set(
|
|
375
|
+
[
|
|
376
|
+
...locationToSphere(a.from),
|
|
377
|
+
...locationToSphere(a.to),
|
|
378
|
+
arcHeight + markerElevation,
|
|
379
|
+
0.005 * arcWidth,
|
|
380
|
+
...(a.color || [0, 0, 0]),
|
|
381
|
+
a.color ? 1 : 0,
|
|
382
|
+
],
|
|
383
|
+
12 * idx
|
|
384
|
+
);
|
|
385
|
+
});
|
|
386
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, arcBuffer);
|
|
387
|
+
gl.bufferData(gl.ARRAY_BUFFER, data, gl.DYNAMIC_DRAW);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
function render(nextOpts = {}) {
|
|
391
|
+
if (nextOpts.phi !== undefined) phi = nextOpts.phi;
|
|
392
|
+
if (nextOpts.theta !== undefined) theta = nextOpts.theta;
|
|
393
|
+
if (nextOpts.markers !== undefined) updateMarkers(nextOpts.markers);
|
|
394
|
+
if (nextOpts.arcs !== undefined) updateArcs(nextOpts.arcs);
|
|
395
|
+
|
|
396
|
+
if (nextOpts.width && nextOpts.height) {
|
|
397
|
+
canvas.width = nextOpts.width * dpr;
|
|
398
|
+
canvas.height = nextOpts.height * dpr;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
if (nextOpts.mapSamples !== undefined) mapSamples = nextOpts.mapSamples;
|
|
402
|
+
if (nextOpts.mapBrightness !== undefined) mapBrightness = nextOpts.mapBrightness;
|
|
403
|
+
if (nextOpts.mapBaseBrightness !== undefined) mapBaseBrightness = nextOpts.mapBaseBrightness;
|
|
404
|
+
if (nextOpts.baseColor !== undefined) baseColor = nextOpts.baseColor;
|
|
405
|
+
if (nextOpts.markerColor !== undefined) markerColor = nextOpts.markerColor;
|
|
406
|
+
if (nextOpts.glowColor !== undefined) glowColor = nextOpts.glowColor;
|
|
407
|
+
if (nextOpts.arcColor !== undefined) arcColor = nextOpts.arcColor;
|
|
408
|
+
if (nextOpts.arcWidth !== undefined) arcWidth = nextOpts.arcWidth;
|
|
409
|
+
if (nextOpts.arcHeight !== undefined) arcHeight = nextOpts.arcHeight;
|
|
410
|
+
if (nextOpts.diffuse !== undefined) diffuse = nextOpts.diffuse;
|
|
411
|
+
if (nextOpts.dark !== undefined) dark = nextOpts.dark;
|
|
412
|
+
if (nextOpts.opacity !== undefined) opacity = nextOpts.opacity;
|
|
413
|
+
if (nextOpts.offset !== undefined) offset = nextOpts.offset;
|
|
414
|
+
if (nextOpts.scale !== undefined) scale = nextOpts.scale;
|
|
415
|
+
if (nextOpts.markerElevation !== undefined) markerElevation = nextOpts.markerElevation;
|
|
416
|
+
|
|
417
|
+
gl.viewport(0, 0, canvas.width, canvas.height);
|
|
418
|
+
gl.clearColor(0, 0, 0, 0);
|
|
419
|
+
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
420
|
+
gl.enable(gl.BLEND);
|
|
421
|
+
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
|
|
422
|
+
|
|
423
|
+
// Render Base Globe
|
|
424
|
+
gl.useProgram(globeProgram);
|
|
425
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, quadBuffer);
|
|
426
|
+
gl.enableVertexAttribArray(globePosAttrib);
|
|
427
|
+
gl.vertexAttribPointer(globePosAttrib, 2, gl.FLOAT, false, 0, 0);
|
|
428
|
+
if (isWebGL2) {
|
|
429
|
+
gl.vertexAttribDivisor(globePosAttrib, 0);
|
|
430
|
+
} else if (ext) {
|
|
431
|
+
ext.vertexAttribDivisorANGLE(globePosAttrib, 0);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
gl.uniform2f(globeUniforms.t, canvas.width, canvas.height);
|
|
435
|
+
gl.uniform2f(globeUniforms.s, phi, theta);
|
|
436
|
+
gl.uniform1f(globeUniforms.k, mapSamples);
|
|
437
|
+
gl.uniform1f(globeUniforms.x, scale);
|
|
438
|
+
gl.uniform2f(globeUniforms.v, offset[0] * dpr, offset[1] * dpr);
|
|
439
|
+
gl.uniform3fv(globeUniforms.F, baseColor);
|
|
440
|
+
gl.uniform3fv(globeUniforms.w, glowColor);
|
|
441
|
+
gl.uniform4f(globeUniforms.n, mapBrightness, diffuse, dark, opacity);
|
|
442
|
+
gl.uniform1f(globeUniforms.y, mapBaseBrightness);
|
|
443
|
+
gl.uniform1i(globeUniforms.z, 0);
|
|
444
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
445
|
+
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
446
|
+
gl.drawArrays(gl.TRIANGLES, 0, 6);
|
|
447
|
+
|
|
448
|
+
// Render Arcs
|
|
449
|
+
if (arcProgram && arcCount > 0) {
|
|
450
|
+
gl.useProgram(arcProgram);
|
|
451
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, arcStripBuffer);
|
|
452
|
+
if (arcAttribs.k >= 0) {
|
|
453
|
+
gl.enableVertexAttribArray(arcAttribs.k);
|
|
454
|
+
gl.vertexAttribPointer(arcAttribs.k, 2, gl.FLOAT, false, 0, 0);
|
|
455
|
+
if (isWebGL2) gl.vertexAttribDivisor(arcAttribs.k, 0);
|
|
456
|
+
else if (ext) ext.vertexAttribDivisorANGLE(arcAttribs.k, 0);
|
|
457
|
+
}
|
|
458
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, arcBuffer);
|
|
459
|
+
const arcStride = 48;
|
|
460
|
+
setupVertexAttrib(gl, arcAttribs.l, 3, arcStride, 0, 1, isWebGL2, ext);
|
|
461
|
+
setupVertexAttrib(gl, arcAttribs.m, 3, arcStride, 12, 1, isWebGL2, ext);
|
|
462
|
+
setupVertexAttrib(gl, arcAttribs.v, 1, arcStride, 24, 1, isWebGL2, ext);
|
|
463
|
+
setupVertexAttrib(gl, arcAttribs.w, 1, arcStride, 28, 1, isWebGL2, ext);
|
|
464
|
+
setupVertexAttrib(gl, arcAttribs.N, 3, arcStride, 32, 1, isWebGL2, ext);
|
|
465
|
+
setupVertexAttrib(gl, arcAttribs.O, 1, arcStride, 44, 1, isWebGL2, ext);
|
|
466
|
+
|
|
467
|
+
gl.uniform1f(arcUniforms.y, phi);
|
|
468
|
+
gl.uniform1f(arcUniforms.z, theta);
|
|
469
|
+
gl.uniform2f(arcUniforms.g, canvas.width, canvas.height);
|
|
470
|
+
gl.uniform1f(arcUniforms.h, scale);
|
|
471
|
+
gl.uniform2f(arcUniforms.x, offset[0] * dpr, offset[1] * dpr);
|
|
472
|
+
gl.uniform3fv(arcUniforms.M, arcColor);
|
|
473
|
+
gl.uniform1f(arcUniforms.A, markerElevation);
|
|
474
|
+
|
|
475
|
+
if (isWebGL2) {
|
|
476
|
+
gl.drawArraysInstanced(gl.TRIANGLE_STRIP, 0, 66, arcCount);
|
|
477
|
+
} else if (ext) {
|
|
478
|
+
ext.drawArraysInstancedANGLE(gl.TRIANGLE_STRIP, 0, 66, arcCount);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
// Render Markers
|
|
483
|
+
if (markerProgram && markers.length > 0) {
|
|
484
|
+
gl.useProgram(markerProgram);
|
|
485
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, quadBuffer);
|
|
486
|
+
if (markerAttribs.n >= 0) {
|
|
487
|
+
gl.enableVertexAttribArray(markerAttribs.n);
|
|
488
|
+
gl.vertexAttribPointer(markerAttribs.n, 2, gl.FLOAT, false, 0, 0);
|
|
489
|
+
if (isWebGL2) gl.vertexAttribDivisor(markerAttribs.n, 0);
|
|
490
|
+
else if (ext) ext.vertexAttribDivisorANGLE(markerAttribs.n, 0);
|
|
491
|
+
}
|
|
492
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, markerBuffer);
|
|
493
|
+
const markerStride = 32;
|
|
494
|
+
setupVertexAttrib(gl, markerAttribs.p, 3, markerStride, 0, 1, isWebGL2, ext);
|
|
495
|
+
setupVertexAttrib(gl, markerAttribs.q, 1, markerStride, 12, 1, isWebGL2, ext);
|
|
496
|
+
setupVertexAttrib(gl, markerAttribs.w, 3, markerStride, 16, 1, isWebGL2, ext);
|
|
497
|
+
setupVertexAttrib(gl, markerAttribs.x, 1, markerStride, 28, 1, isWebGL2, ext);
|
|
498
|
+
|
|
499
|
+
gl.uniform1f(markerUniforms.i, phi);
|
|
500
|
+
gl.uniform1f(markerUniforms.j, theta);
|
|
501
|
+
gl.uniform2f(markerUniforms.b, canvas.width, canvas.height);
|
|
502
|
+
gl.uniform1f(markerUniforms.k, scale);
|
|
503
|
+
gl.uniform2f(markerUniforms.r, offset[0] * dpr, offset[1] * dpr);
|
|
504
|
+
gl.uniform3fv(markerUniforms.v, markerColor);
|
|
505
|
+
gl.uniform1f(markerUniforms.s, markerElevation);
|
|
506
|
+
|
|
507
|
+
if (isWebGL2) {
|
|
508
|
+
gl.drawArraysInstanced(gl.TRIANGLES, 0, 6, markers.length);
|
|
509
|
+
} else if (ext) {
|
|
510
|
+
ext.drawArraysInstancedANGLE(gl.TRIANGLES, 0, 6, markers.length);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
// Initial draw
|
|
516
|
+
render({ markers, arcs });
|
|
517
|
+
|
|
518
|
+
return {
|
|
519
|
+
update: render,
|
|
520
|
+
destroy: () => {
|
|
521
|
+
gl.deleteBuffer(quadBuffer);
|
|
522
|
+
gl.deleteBuffer(arcStripBuffer);
|
|
523
|
+
gl.deleteBuffer(markerBuffer);
|
|
524
|
+
gl.deleteBuffer(arcBuffer);
|
|
525
|
+
gl.deleteTexture(texture);
|
|
526
|
+
gl.deleteProgram(globeProgram);
|
|
527
|
+
if (markerProgram) gl.deleteProgram(markerProgram);
|
|
528
|
+
if (arcProgram) gl.deleteProgram(arcProgram);
|
|
529
|
+
},
|
|
530
|
+
};
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/**
|
|
534
|
+
* cynx-lib Globe Component
|
|
535
|
+
*/
|
|
536
|
+
export default function Globe({
|
|
537
|
+
width,
|
|
538
|
+
height,
|
|
539
|
+
phi = 0,
|
|
540
|
+
theta = 0.3,
|
|
541
|
+
autoRotate = true,
|
|
542
|
+
autoRotateSpeed = 0.003,
|
|
543
|
+
draggable = true,
|
|
544
|
+
dark = 0,
|
|
545
|
+
diffuse = 1.2,
|
|
546
|
+
mapSamples = 16000,
|
|
547
|
+
mapBrightness = 6,
|
|
548
|
+
mapBaseBrightness = 0,
|
|
549
|
+
baseColor,
|
|
550
|
+
markerColor = [0.15, 0.5, 1.0],
|
|
551
|
+
glowColor = [1, 1, 1],
|
|
552
|
+
arcColor = [0.2, 0.55, 1.0],
|
|
553
|
+
arcWidth = 1,
|
|
554
|
+
arcHeight = 0.2,
|
|
555
|
+
markers = [],
|
|
556
|
+
arcs = [],
|
|
557
|
+
scale = 1,
|
|
558
|
+
offset = [0, 0],
|
|
559
|
+
markerElevation = 0.05,
|
|
560
|
+
onRender,
|
|
561
|
+
className = '',
|
|
562
|
+
style = {},
|
|
563
|
+
}) {
|
|
564
|
+
const containerRef = useRef(null);
|
|
565
|
+
const canvasRef = useRef(null);
|
|
566
|
+
const globeInstanceRef = useRef(null);
|
|
567
|
+
const animFrameIdRef = useRef(null);
|
|
568
|
+
|
|
569
|
+
const rotationRef = useRef({ phi, theta });
|
|
570
|
+
const isDraggingRef = useRef(false);
|
|
571
|
+
const pointerStartRef = useRef({ x: 0, y: 0, phi: 0, theta: 0 });
|
|
572
|
+
|
|
573
|
+
// Default theme-aware base colors
|
|
574
|
+
const activeBaseColor = baseColor || (dark === 1 ? [0.28, 0.3, 0.38] : [0.8, 0.85, 0.92]);
|
|
575
|
+
|
|
576
|
+
// Pointer drag events for smooth rotation
|
|
577
|
+
const handlePointerDown = useCallback(
|
|
578
|
+
(e) => {
|
|
579
|
+
if (!draggable) return;
|
|
580
|
+
isDraggingRef.current = true;
|
|
581
|
+
pointerStartRef.current = {
|
|
582
|
+
x: e.clientX,
|
|
583
|
+
y: e.clientY,
|
|
584
|
+
phi: rotationRef.current.phi,
|
|
585
|
+
theta: rotationRef.current.theta,
|
|
586
|
+
};
|
|
587
|
+
if (e.currentTarget?.setPointerCapture) {
|
|
588
|
+
try {
|
|
589
|
+
e.currentTarget.setPointerCapture(e.pointerId);
|
|
590
|
+
} catch (_) {}
|
|
591
|
+
}
|
|
592
|
+
},
|
|
593
|
+
[draggable]
|
|
594
|
+
);
|
|
595
|
+
|
|
596
|
+
const handlePointerMove = useCallback(
|
|
597
|
+
(e) => {
|
|
598
|
+
if (!isDraggingRef.current || !draggable) return;
|
|
599
|
+
const dx = e.clientX - pointerStartRef.current.x;
|
|
600
|
+
const dy = e.clientY - pointerStartRef.current.y;
|
|
601
|
+
rotationRef.current.phi = pointerStartRef.current.phi + dx * 0.005;
|
|
602
|
+
const nextTheta = pointerStartRef.current.theta - dy * 0.005;
|
|
603
|
+
rotationRef.current.theta = Math.max(-Math.PI / 2.2, Math.min(Math.PI / 2.2, nextTheta));
|
|
604
|
+
},
|
|
605
|
+
[draggable]
|
|
606
|
+
);
|
|
607
|
+
|
|
608
|
+
const handlePointerUp = useCallback(
|
|
609
|
+
(e) => {
|
|
610
|
+
if (!isDraggingRef.current) return;
|
|
611
|
+
isDraggingRef.current = false;
|
|
612
|
+
if (e.currentTarget?.releasePointerCapture) {
|
|
613
|
+
try {
|
|
614
|
+
e.currentTarget.releasePointerCapture(e.pointerId);
|
|
615
|
+
} catch (_) {}
|
|
616
|
+
}
|
|
617
|
+
},
|
|
618
|
+
[]
|
|
619
|
+
);
|
|
620
|
+
|
|
621
|
+
useEffect(() => {
|
|
622
|
+
const canvas = canvasRef.current;
|
|
623
|
+
const container = containerRef.current;
|
|
624
|
+
if (!canvas || !container) return;
|
|
625
|
+
|
|
626
|
+
const initialWidth = width || container.clientWidth || 400;
|
|
627
|
+
const initialHeight = height || container.clientHeight || 400;
|
|
628
|
+
|
|
629
|
+
const globe = createGlobe(canvas, {
|
|
630
|
+
width: initialWidth,
|
|
631
|
+
height: initialHeight,
|
|
632
|
+
phi: rotationRef.current.phi,
|
|
633
|
+
theta: rotationRef.current.theta,
|
|
634
|
+
dark,
|
|
635
|
+
diffuse,
|
|
636
|
+
scale,
|
|
637
|
+
offset,
|
|
638
|
+
mapSamples,
|
|
639
|
+
mapBrightness,
|
|
640
|
+
mapBaseBrightness,
|
|
641
|
+
baseColor: activeBaseColor,
|
|
642
|
+
markerColor,
|
|
643
|
+
glowColor,
|
|
644
|
+
arcColor,
|
|
645
|
+
arcWidth,
|
|
646
|
+
arcHeight,
|
|
647
|
+
markerElevation,
|
|
648
|
+
markers,
|
|
649
|
+
arcs,
|
|
650
|
+
});
|
|
651
|
+
|
|
652
|
+
globeInstanceRef.current = globe;
|
|
653
|
+
|
|
654
|
+
let isMounted = true;
|
|
655
|
+
|
|
656
|
+
const loop = () => {
|
|
657
|
+
if (!isMounted) return;
|
|
658
|
+
|
|
659
|
+
if (autoRotate && !isDraggingRef.current) {
|
|
660
|
+
rotationRef.current.phi += autoRotateSpeed;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
const state = {
|
|
664
|
+
phi: rotationRef.current.phi,
|
|
665
|
+
theta: rotationRef.current.theta,
|
|
666
|
+
width: width || container.clientWidth || 400,
|
|
667
|
+
height: height || container.clientHeight || 400,
|
|
668
|
+
dark,
|
|
669
|
+
diffuse,
|
|
670
|
+
scale,
|
|
671
|
+
offset,
|
|
672
|
+
mapSamples,
|
|
673
|
+
mapBrightness,
|
|
674
|
+
mapBaseBrightness,
|
|
675
|
+
baseColor: activeBaseColor,
|
|
676
|
+
markerColor,
|
|
677
|
+
glowColor,
|
|
678
|
+
arcColor,
|
|
679
|
+
arcWidth,
|
|
680
|
+
arcHeight,
|
|
681
|
+
markerElevation,
|
|
682
|
+
markers,
|
|
683
|
+
arcs,
|
|
684
|
+
};
|
|
685
|
+
|
|
686
|
+
if (onRender) {
|
|
687
|
+
onRender(state);
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
globe.update(state);
|
|
691
|
+
animFrameIdRef.current = requestAnimationFrame(loop);
|
|
692
|
+
};
|
|
693
|
+
|
|
694
|
+
animFrameIdRef.current = requestAnimationFrame(loop);
|
|
695
|
+
|
|
696
|
+
return () => {
|
|
697
|
+
isMounted = false;
|
|
698
|
+
if (animFrameIdRef.current) {
|
|
699
|
+
cancelAnimationFrame(animFrameIdRef.current);
|
|
700
|
+
}
|
|
701
|
+
globe.destroy();
|
|
702
|
+
globeInstanceRef.current = null;
|
|
703
|
+
};
|
|
704
|
+
}, [
|
|
705
|
+
width,
|
|
706
|
+
height,
|
|
707
|
+
autoRotate,
|
|
708
|
+
autoRotateSpeed,
|
|
709
|
+
dark,
|
|
710
|
+
diffuse,
|
|
711
|
+
scale,
|
|
712
|
+
offset,
|
|
713
|
+
mapSamples,
|
|
714
|
+
mapBrightness,
|
|
715
|
+
mapBaseBrightness,
|
|
716
|
+
activeBaseColor,
|
|
717
|
+
markerColor,
|
|
718
|
+
glowColor,
|
|
719
|
+
arcColor,
|
|
720
|
+
arcWidth,
|
|
721
|
+
arcHeight,
|
|
722
|
+
markerElevation,
|
|
723
|
+
markers,
|
|
724
|
+
arcs,
|
|
725
|
+
onRender,
|
|
726
|
+
]);
|
|
727
|
+
|
|
728
|
+
return (
|
|
729
|
+
<div
|
|
730
|
+
ref={containerRef}
|
|
731
|
+
className={className}
|
|
732
|
+
onPointerDown={handlePointerDown}
|
|
733
|
+
onPointerMove={handlePointerMove}
|
|
734
|
+
onPointerUp={handlePointerUp}
|
|
735
|
+
onPointerCancel={handlePointerUp}
|
|
736
|
+
style={{
|
|
737
|
+
position: 'relative',
|
|
738
|
+
width: width ? `${width}px` : '100%',
|
|
739
|
+
height: height ? `${height}px` : '100%',
|
|
740
|
+
maxWidth: '100%',
|
|
741
|
+
aspectRatio: !height ? '1 / 1' : undefined,
|
|
742
|
+
display: 'flex',
|
|
743
|
+
alignItems: 'center',
|
|
744
|
+
justifyContent: 'center',
|
|
745
|
+
touchAction: draggable ? 'none' : 'auto',
|
|
746
|
+
cursor: draggable ? (isDraggingRef.current ? 'grabbing' : 'grab') : 'default',
|
|
747
|
+
userSelect: 'none',
|
|
748
|
+
...style,
|
|
749
|
+
}}
|
|
750
|
+
>
|
|
751
|
+
<canvas
|
|
752
|
+
ref={canvasRef}
|
|
753
|
+
style={{
|
|
754
|
+
width: '100%',
|
|
755
|
+
height: '100%',
|
|
756
|
+
display: 'block',
|
|
757
|
+
contain: 'layout paint size',
|
|
758
|
+
}}
|
|
759
|
+
/>
|
|
760
|
+
</div>
|
|
761
|
+
);
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
export { Globe };
|
package/ui/components/Input.jsx
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { useRef, useState } from 'react';
|
|
2
|
+
import { Eye, EyeOff } from 'lucide-react';
|
|
2
3
|
|
|
3
4
|
const SIZES = {
|
|
4
5
|
xs: { height: 24, fontSize: 11, paddingH: 6 },
|
|
@@ -23,18 +24,25 @@ export default function Input({
|
|
|
23
24
|
className = '',
|
|
24
25
|
style = {},
|
|
25
26
|
value,
|
|
27
|
+
toggle,
|
|
28
|
+
togglePassword,
|
|
26
29
|
...props
|
|
27
30
|
}) {
|
|
28
31
|
const inputRef = useRef(null);
|
|
29
32
|
const [focused, setFocused] = useState(false);
|
|
30
33
|
const [hovered, setHovered] = useState(false);
|
|
34
|
+
const [revealed, setRevealed] = useState(false);
|
|
31
35
|
const s = SIZES[size] || SIZES.md;
|
|
32
36
|
|
|
33
37
|
const isPasswd = type === 'passwd';
|
|
34
|
-
const
|
|
38
|
+
const isPassword = type === 'password';
|
|
39
|
+
const isSecret = isPasswd || isPassword;
|
|
40
|
+
const showToggle = isSecret && toggle !== false && togglePassword !== false && !props.disabled && !multiline;
|
|
41
|
+
|
|
42
|
+
const resolvedType = isPassword ? (revealed ? 'text' : 'password') : (isPasswd ? 'text' : type);
|
|
35
43
|
const hasValue = value !== undefined && value !== null && value !== '';
|
|
36
44
|
|
|
37
|
-
const passwdStyle = isPasswd ? {
|
|
45
|
+
const passwdStyle = (isPasswd && !revealed) ? {
|
|
38
46
|
WebkitTextSecurity: 'disc',
|
|
39
47
|
textSecurity: 'disc',
|
|
40
48
|
fontFamily: hasValue ? 'monospace' : 'var(--f,var(--sans,inherit))',
|
|
@@ -42,6 +50,13 @@ export default function Input({
|
|
|
42
50
|
letterSpacing: hasValue ? '0.15em' : 'normal',
|
|
43
51
|
} : {};
|
|
44
52
|
|
|
53
|
+
const passwdAttrs = isPasswd ? {
|
|
54
|
+
autoComplete: props.autoComplete !== undefined ? props.autoComplete : 'off',
|
|
55
|
+
'data-1p-ignore': 'true',
|
|
56
|
+
'data-form-type': 'other',
|
|
57
|
+
'data-bwignore': 'true',
|
|
58
|
+
} : {};
|
|
59
|
+
|
|
45
60
|
function handleClear() {
|
|
46
61
|
if (props.onChange) props.onChange({ target: { value: '' } });
|
|
47
62
|
if (onClear) onClear();
|
|
@@ -52,7 +67,8 @@ export default function Input({
|
|
|
52
67
|
const hasIconRight = !!iconRight;
|
|
53
68
|
const showClear = clearable && !!value && !props.disabled && !multiline;
|
|
54
69
|
const hasSuffix = !!suffix && !multiline;
|
|
55
|
-
const showRightSlot = showClear || hasSuffix;
|
|
70
|
+
const showRightSlot = showClear || hasSuffix || showToggle;
|
|
71
|
+
const rightSlotPadding = (showClear && showToggle) ? 54 : (hasIconRight || showRightSlot) ? 34 : s.paddingH;
|
|
56
72
|
|
|
57
73
|
return (
|
|
58
74
|
<div style={{
|
|
@@ -164,7 +180,7 @@ export default function Input({
|
|
|
164
180
|
height: s.height,
|
|
165
181
|
padding: `0 ${s.paddingH}px`,
|
|
166
182
|
paddingLeft: hasIconLeft ? 34 : s.paddingH,
|
|
167
|
-
paddingRight:
|
|
183
|
+
paddingRight: rightSlotPadding,
|
|
168
184
|
outline: 'none',
|
|
169
185
|
transition: 'border-color .14s, box-shadow .14s',
|
|
170
186
|
boxShadow: error
|
|
@@ -178,6 +194,7 @@ export default function Input({
|
|
|
178
194
|
...style,
|
|
179
195
|
}}
|
|
180
196
|
value={value}
|
|
197
|
+
{...passwdAttrs}
|
|
181
198
|
{...props}
|
|
182
199
|
onFocus={e => { setFocused(true); props.onFocus?.(e); }}
|
|
183
200
|
onBlur={e => { setFocused(false); setHovered(false); props.onBlur?.(e); }}
|
|
@@ -186,6 +203,39 @@ export default function Input({
|
|
|
186
203
|
/>
|
|
187
204
|
)}
|
|
188
205
|
|
|
206
|
+
{showToggle && (
|
|
207
|
+
<button
|
|
208
|
+
type="button"
|
|
209
|
+
tabIndex={-1}
|
|
210
|
+
onClick={() => setRevealed(r => !r)}
|
|
211
|
+
style={{
|
|
212
|
+
position: 'absolute',
|
|
213
|
+
right: showClear ? 28 : 8,
|
|
214
|
+
top: '50%',
|
|
215
|
+
transform: 'translateY(-50%)',
|
|
216
|
+
display: 'flex',
|
|
217
|
+
alignItems: 'center',
|
|
218
|
+
justifyContent: 'center',
|
|
219
|
+
width: 20,
|
|
220
|
+
height: 20,
|
|
221
|
+
borderRadius: 'var(--radxs,4px)',
|
|
222
|
+
background: 'transparent',
|
|
223
|
+
border: 'none',
|
|
224
|
+
cursor: 'pointer',
|
|
225
|
+
color: 'var(--t3,#9b9daa)',
|
|
226
|
+
padding: 0,
|
|
227
|
+
lineHeight: 0,
|
|
228
|
+
transition: 'color .15s',
|
|
229
|
+
}}
|
|
230
|
+
>
|
|
231
|
+
{revealed ? (
|
|
232
|
+
<EyeOff size={size === 'xs' ? 12 : size === 'sm' ? 13 : 15} />
|
|
233
|
+
) : (
|
|
234
|
+
<Eye size={size === 'xs' ? 12 : size === 'sm' ? 13 : 15} />
|
|
235
|
+
)}
|
|
236
|
+
</button>
|
|
237
|
+
)}
|
|
238
|
+
|
|
189
239
|
{showClear && (
|
|
190
240
|
<button
|
|
191
241
|
type="button"
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { useState, useLayoutEffect, useEffect, useRef, useCallback } from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Universal display and viewport measurement hook (Ruler).
|
|
5
|
+
* Measures element rect relative to window viewport, computes remaining available height,
|
|
6
|
+
* and dynamically calculates rows for grid / table / skeleton layouts.
|
|
7
|
+
*
|
|
8
|
+
* @param {Object|number} arg1 - Options object or rowHeight (for backward compatibility)
|
|
9
|
+
* @param {number} [arg2] - initialOffset (for backward compatibility)
|
|
10
|
+
*/
|
|
11
|
+
export function useRuler(arg1 = 51, arg2 = 240) {
|
|
12
|
+
const isObject = arg1 && typeof arg1 === 'object' && !Array.isArray(arg1);
|
|
13
|
+
const options = isObject ? arg1 : {};
|
|
14
|
+
const rowHeight = isObject ? (options.rowHeight ?? 51) : (typeof arg1 === 'number' ? arg1 : 51);
|
|
15
|
+
const initialOffset = isObject ? (options.initialOffset ?? 240) : (typeof arg2 === 'number' ? arg2 : 240);
|
|
16
|
+
const bottomBuffer = options.bottomBuffer ?? 24;
|
|
17
|
+
const minLimit = options.minHeight ?? 200;
|
|
18
|
+
const maxLimit = options.maxHeight ?? null;
|
|
19
|
+
const minRows = options.minRows ?? 3;
|
|
20
|
+
const maxRows = options.maxRows ?? 25;
|
|
21
|
+
|
|
22
|
+
const containerRef = useRef(null);
|
|
23
|
+
|
|
24
|
+
const [availableHeight, setAvailableHeight] = useState(() => {
|
|
25
|
+
if (typeof window === 'undefined') return minLimit;
|
|
26
|
+
return Math.max(minLimit, window.innerHeight - initialOffset - bottomBuffer);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const [rows, setRows] = useState(() => {
|
|
30
|
+
if (typeof window === 'undefined') return 8;
|
|
31
|
+
return Math.max(minRows, Math.floor((window.innerHeight - initialOffset) / rowHeight));
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const calculate = useCallback(() => {
|
|
35
|
+
if (!containerRef.current) return;
|
|
36
|
+
const rect = containerRef.current.getBoundingClientRect();
|
|
37
|
+
|
|
38
|
+
let avail = window.innerHeight - rect.top - bottomBuffer;
|
|
39
|
+
if (avail < minLimit) avail = minLimit;
|
|
40
|
+
if (maxLimit && avail > maxLimit) avail = maxLimit;
|
|
41
|
+
const roundedAvail = Math.round(avail);
|
|
42
|
+
|
|
43
|
+
// Guard against infinite re-renders from subpixel fluctuations
|
|
44
|
+
setAvailableHeight((prev) => (Math.abs(prev - roundedAvail) > 1 ? roundedAvail : prev));
|
|
45
|
+
|
|
46
|
+
const thead = containerRef.current.querySelector('thead');
|
|
47
|
+
const theadHeight = thead ? thead.offsetHeight : 0;
|
|
48
|
+
const cardHeader =
|
|
49
|
+
containerRef.current.firstElementChild !== thead?.parentElement &&
|
|
50
|
+
!containerRef.current.firstElementChild?.contains(thead)
|
|
51
|
+
? containerRef.current.querySelector(':scope > div:first-child')
|
|
52
|
+
: null;
|
|
53
|
+
const cardHeaderHeight = cardHeader ? cardHeader.offsetHeight : 0;
|
|
54
|
+
|
|
55
|
+
const bodyAvailable = roundedAvail - theadHeight - cardHeaderHeight;
|
|
56
|
+
if (bodyAvailable > 0) {
|
|
57
|
+
const calculated = Math.floor(bodyAvailable / rowHeight);
|
|
58
|
+
const nextRows = Math.max(minRows, Math.min(maxRows, calculated));
|
|
59
|
+
setRows((prev) => (prev !== nextRows ? nextRows : prev));
|
|
60
|
+
}
|
|
61
|
+
}, [rowHeight, bottomBuffer, minLimit, maxLimit, minRows, maxRows]);
|
|
62
|
+
|
|
63
|
+
useLayoutEffect(() => {
|
|
64
|
+
calculate();
|
|
65
|
+
}, [calculate]);
|
|
66
|
+
|
|
67
|
+
useEffect(() => {
|
|
68
|
+
calculate();
|
|
69
|
+
window.addEventListener('resize', calculate);
|
|
70
|
+
|
|
71
|
+
let observer = null;
|
|
72
|
+
if (typeof ResizeObserver !== 'undefined' && containerRef.current?.parentElement) {
|
|
73
|
+
observer = new ResizeObserver(() => {
|
|
74
|
+
calculate();
|
|
75
|
+
});
|
|
76
|
+
observer.observe(containerRef.current.parentElement);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return () => {
|
|
80
|
+
window.removeEventListener('resize', calculate);
|
|
81
|
+
if (observer) observer.disconnect();
|
|
82
|
+
};
|
|
83
|
+
}, [calculate]);
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
containerRef,
|
|
87
|
+
availableHeight,
|
|
88
|
+
minHeight: availableHeight,
|
|
89
|
+
rows,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export const useAutoSkeletonRows = useRuler;
|
|
94
|
+
export default useRuler;
|
package/ui/index.cjs
CHANGED
|
@@ -44,6 +44,7 @@ __export(index_exports, {
|
|
|
44
44
|
Drawer: () => Drawer,
|
|
45
45
|
Dropzone: () => Dropzone,
|
|
46
46
|
FilePicker: () => FilePicker,
|
|
47
|
+
Globe: () => Globe,
|
|
47
48
|
HoverCard: () => HoverCard,
|
|
48
49
|
ImageUpload: () => ImageUpload,
|
|
49
50
|
InfoBadge: () => InfoBadge,
|
|
@@ -87,7 +88,10 @@ __export(index_exports, {
|
|
|
87
88
|
XThead: () => XThead,
|
|
88
89
|
XTr: () => XTr,
|
|
89
90
|
confirm: () => confirm,
|
|
91
|
+
createGlobe: () => createGlobe,
|
|
90
92
|
toast: () => toast,
|
|
93
|
+
useAutoSkeletonRows: () => useAutoSkeletonRows,
|
|
94
|
+
useRuler: () => useRuler,
|
|
91
95
|
useToast: () => useToast
|
|
92
96
|
});
|
|
93
97
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -133,6 +137,8 @@ var import_Layout = __toESM(require("./components/Layout.jsx"), 1);
|
|
|
133
137
|
var import_Sidebar = __toESM(require("./components/Sidebar.jsx"), 1);
|
|
134
138
|
var import_LoginPage = __toESM(require("./components/LoginPage.jsx"), 1);
|
|
135
139
|
var import_PrivacyBlur = __toESM(require("./components/PrivacyBlur.jsx"), 1);
|
|
140
|
+
var import_Globe = __toESM(require("./components/Globe.jsx"), 1);
|
|
141
|
+
var import_useRuler = require("./components/useRuler.jsx");
|
|
136
142
|
const Layout = import_Layout.default;
|
|
137
143
|
const Sidebar = import_Sidebar.default;
|
|
138
144
|
const LoginPage = import_LoginPage.default;
|
|
@@ -195,3 +201,7 @@ const CardHdr = import_Card.CardHdr;
|
|
|
195
201
|
const StatCard = import_Card.StatCard;
|
|
196
202
|
const StatCardSkeleton = import_Card.StatCardSkeleton;
|
|
197
203
|
const PrivacyBlur = import_PrivacyBlur.default;
|
|
204
|
+
const Globe = import_Globe.default || import_Globe.Globe;
|
|
205
|
+
const createGlobe = import_Globe.createGlobe;
|
|
206
|
+
const useRuler = import_useRuler.useRuler;
|
|
207
|
+
const useAutoSkeletonRows = import_useRuler.useAutoSkeletonRows;
|
package/ui/index.js
CHANGED
|
@@ -42,6 +42,8 @@ import { default as _Layout } from './components/Layout.jsx';
|
|
|
42
42
|
import { default as _Sidebar } from './components/Sidebar.jsx';
|
|
43
43
|
import { default as _LoginPage } from './components/LoginPage.jsx';
|
|
44
44
|
import { default as _PrivacyBlur } from './components/PrivacyBlur.jsx';
|
|
45
|
+
import { default as _Globe, createGlobe as _createGlobe, Globe as _GlobeNamed } from './components/Globe.jsx';
|
|
46
|
+
import { useRuler as _useRuler, useAutoSkeletonRows as _useAutoSkeletonRows } from './components/useRuler.jsx';
|
|
45
47
|
|
|
46
48
|
export const Layout = _Layout;
|
|
47
49
|
export const Sidebar = _Sidebar;
|
|
@@ -105,3 +107,7 @@ export const CardHdr = _CardHdr;
|
|
|
105
107
|
export const StatCard = _StatCard;
|
|
106
108
|
export const StatCardSkeleton = _StatCardSkeleton;
|
|
107
109
|
export const PrivacyBlur = _PrivacyBlur;
|
|
110
|
+
export const Globe = _Globe || _GlobeNamed;
|
|
111
|
+
export const createGlobe = _createGlobe;
|
|
112
|
+
export const useRuler = _useRuler;
|
|
113
|
+
export const useAutoSkeletonRows = _useAutoSkeletonRows;
|