File size: 10,678 Bytes
587459d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
"use client";

import React, { useRef, useEffect } from "react";
import * as THREE from "three";

interface ThreeTicketCardProps {
  email: string;
  ticketNumber: string;
  rotateX: number;
  rotateY: number;
  glareX: number;
  glareY: number;
  onMouseMove: (e: React.MouseEvent<HTMLDivElement>) => void;
  onMouseLeave: () => void;
}

export function ThreeTicketCard({
  email,
  ticketNumber,
  rotateX,
  rotateY,
  glareX,
  glareY,
  onMouseMove,
  onMouseLeave,
}: ThreeTicketCardProps) {
  const mountRef = useRef<HTMLDivElement>(null);
  const canvasRef = useRef<HTMLCanvasElement>(null);

  // Mask email for aesthetics, e.g. u***r@domain.com
  const getMaskedEmail = (rawEmail: string) => {
    const parts = rawEmail.split("@");
    if (parts.length !== 2) return rawEmail;
    const name = parts[0];
    const domain = parts[1];
    if (name.length <= 2) return `${name}***@${domain}`;
    return `${name.charAt(0)}***${name.charAt(name.length - 1)}@${domain}`;
  };

  useEffect(() => {
    if (!canvasRef.current || !mountRef.current) return;

    const width = 340;
    const height = 210;

    // 1. Create Offscreen Canvas for Card Texture
    const offscreen = document.createElement("canvas");
    offscreen.width = 1024;
    offscreen.height = 640;
    const ctx = offscreen.getContext("2d");

    if (ctx) {
      // Background base
      ctx.fillStyle = "#030303";
      ctx.fillRect(0, 0, 1024, 640);

      // Cyber Grid Pattern
      ctx.strokeStyle = "rgba(255, 255, 255, 0.02)";
      ctx.lineWidth = 2;
      const gridSpacing = 32;
      for (let x = 0; x < 1024; x += gridSpacing) {
        ctx.beginPath();
        ctx.moveTo(x, 0);
        ctx.lineTo(x, 640);
        ctx.stroke();
      }
      for (let y = 0; y < 640; y += gridSpacing) {
        ctx.beginPath();
        ctx.moveTo(0, y);
        ctx.lineTo(1024, y);
        ctx.stroke();
      }

      // Neon glows on offscreen texture
      const grad = ctx.createLinearGradient(0, 0, 1024, 640);
      grad.addColorStop(0, "rgba(245, 158, 11, 0.05)");
      grad.addColorStop(0.5, "rgba(0, 0, 0, 0)");
      grad.addColorStop(1, "rgba(99, 102, 241, 0.05)");
      ctx.fillStyle = grad;
      ctx.fillRect(0, 0, 1024, 640);

      // Outer border outline
      ctx.strokeStyle = "rgba(255, 255, 255, 0.15)";
      ctx.lineWidth = 4;
      ctx.strokeRect(30, 30, 964, 580);

      // Cyber Corners
      ctx.strokeStyle = "#fbbf24"; // Amber 400
      ctx.lineWidth = 6;
      // Top-Left corner
      ctx.beginPath(); ctx.moveTo(50, 90); ctx.lineTo(50, 50); ctx.lineTo(90, 50); ctx.stroke();
      // Top-Right corner
      ctx.beginPath(); ctx.moveTo(974, 90); ctx.lineTo(974, 50); ctx.lineTo(934, 50); ctx.stroke();
      // Bottom-Left corner
      ctx.beginPath(); ctx.moveTo(50, 550); ctx.lineTo(50, 590); ctx.lineTo(90, 590); ctx.stroke();
      // Bottom-Right corner
      ctx.beginPath(); ctx.moveTo(974, 550); ctx.lineTo(974, 590); ctx.lineTo(934, 590); ctx.stroke();

      // Brand Logo Header
      ctx.shadowColor = "#fbbf24";
      ctx.shadowBlur = 10;
      ctx.font = "bold 44px monospace";
      ctx.fillStyle = "#fbbf24";
      ctx.fillText("CPU_HUBAPI", 70, 110);
      ctx.shadowBlur = 0; // reset

      ctx.font = "14px monospace";
      ctx.fillStyle = "rgba(255, 255, 255, 0.4)";
      ctx.fillText("NEURAL GATEWAY MESH ACCESS", 70, 140);

      // Secure Badge
      ctx.fillStyle = "rgba(245, 158, 11, 0.08)";
      ctx.fillRect(720, 70, 230, 45);
      ctx.strokeStyle = "rgba(245, 158, 11, 0.3)";
      ctx.lineWidth = 2;
      ctx.strokeRect(720, 70, 230, 45);

      ctx.font = "bold 16px monospace";
      ctx.fillStyle = "#fbbf24";
      ctx.textAlign = "center";
      ctx.fillText("SECURE // LINK", 835, 98);
      ctx.textAlign = "left"; // reset

      // Access Pass Title
      ctx.font = "20px monospace";
      ctx.fillStyle = "rgba(255, 255, 255, 0.35)";
      ctx.fillText("ACCESS PASS IDENTIFICATION", 70, 270);

      // Large Ticket Number
      ctx.font = "extrabold 88px monospace";
      ctx.fillStyle = "#ffffff";
      ctx.fillText(`# ${ticketNumber}`, 70, 365);

      // Bottom Area: Masked Email & Date
      ctx.font = "18px monospace";
      ctx.fillStyle = "rgba(255, 255, 255, 0.35)";
      ctx.fillText("AUTHORIZED SIGNATORY", 70, 480);

      ctx.font = "bold 26px monospace";
      ctx.fillStyle = "#fbbf24";
      ctx.fillText(getMaskedEmail(email), 70, 515);

      ctx.font = "14px monospace";
      ctx.fillStyle = "rgba(255, 255, 255, 0.25)";
      ctx.fillText(`ISSUED_AT // ${new Date().toISOString().substring(0, 10)}`, 70, 550);

      // Barcode
      const barcodeX = 690;
      const barcodeY = 410;
      const barcodeWeights = [5, 3, 7, 3, 5, 10, 3, 5, 3, 7, 5, 3, 10, 3, 5, 7, 3];
      ctx.fillStyle = "rgba(255, 255, 255, 0.85)";
      let currX = barcodeX;
      barcodeWeights.forEach((w) => {
        ctx.fillRect(currX, barcodeY, w * 2.2, 85);
        currX += w * 2.2 + 4;
      });

      ctx.font = "14px monospace";
      ctx.fillStyle = "rgba(255, 255, 255, 0.3)";
      ctx.fillText(`HUBAPI_${ticketNumber.replace(",", "")}`, barcodeX + 25, 520);
    }

    // 2. Setup Three.js Scene, Camera, and WebGLRenderer
    const scene = new THREE.Scene();

    const camera = new THREE.PerspectiveCamera(40, width / height, 0.1, 100);
    camera.position.z = 4.8;

    const renderer = new THREE.WebGLRenderer({
      canvas: canvasRef.current,
      alpha: true,
      antialias: true,
    });
    renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
    renderer.setSize(width, height);

    // 3. Create Card Texture & Materials
    const texture = new THREE.CanvasTexture(offscreen);
    texture.minFilter = THREE.LinearFilter;
    texture.magFilter = THREE.LinearFilter;

    // We make a sleek box with thickness for full 3D feel
    const geometry = new THREE.BoxGeometry(3.3, 2.05, 0.07);

    // 6 Materials representing the 6 faces of the Box
    // Face indices: 0:Right, 1:Left, 2:Top, 3:Bottom, 4:Front, 5:Back
    const glassParams = {
      transparent: true,
      opacity: 0.95,
      roughness: 0.12,
      metalness: 0.3,
      clearcoat: 1.0,
      clearcoatRoughness: 0.05,
      transmission: 0.65,
      thickness: 0.2,
      ior: 1.55,
    };

    const frontMaterial = new THREE.MeshPhysicalMaterial({
      ...glassParams,
      map: texture,
    });

    const backMaterial = new THREE.MeshPhysicalMaterial({
      ...glassParams,
      color: 0x09090c,
    });

    const edgeMaterial = new THREE.MeshStandardMaterial({
      color: 0x22252a,
      roughness: 0.15,
      metalness: 0.9,
    });

    const materials = [
      edgeMaterial,  // Right
      edgeMaterial,  // Left
      edgeMaterial,  // Top
      edgeMaterial,  // Bottom
      frontMaterial, // Front (main face)
      backMaterial,  // Back
    ];

    const cardMesh = new THREE.Mesh(geometry, materials);
    scene.add(cardMesh);

    // 4. Lighting Setup for realistic reflection
    const ambientLight = new THREE.AmbientLight(0xffffff, 0.65);
    scene.add(ambientLight);

    const dirLight = new THREE.DirectionalLight(0xffffff, 2.5);
    dirLight.position.set(0, 0, 4);
    scene.add(dirLight);

    // Moving colored lights for cyberpunk iridescent sheen
    const orangeLight = new THREE.PointLight(0xf59e0b, 8, 10); // Amber
    orangeLight.position.set(2, 2, 1.2);
    scene.add(orangeLight);

    const indigoLight = new THREE.PointLight(0x6366f1, 8, 10); // Indigo
    indigoLight.position.set(-2, -2, 1.2);
    scene.add(indigoLight);

    // 5. Animation Loop
    let animationFrameId: number;

    const animate = () => {
      const currentRotateX = stateRef.current.rotateX;
      const currentRotateY = stateRef.current.rotateY;
      const currentGlareX = stateRef.current.glareX;
      const currentGlareY = stateRef.current.glareY;

      const targetX = (currentRotateX * Math.PI) / 180;
      const targetY = (currentRotateY * Math.PI) / 180;

      // Smooth interpolation (lerp)
      cardMesh.rotation.x += (targetX - cardMesh.rotation.x) * 0.12;
      cardMesh.rotation.y += (targetY - cardMesh.rotation.y) * 0.12;

      // Subtle automatic breathing movement if mouse/gyroscope is neutral
      if (currentRotateX === 0 && currentRotateY === 0) {
        const time = Date.now() * 0.0015;
        cardMesh.rotation.x += Math.sin(time) * 0.0015;
        cardMesh.rotation.y += Math.cos(time) * 0.0015;
      }

      // Shifting specular light beams based on glare position
      const px = (currentGlareX / 100) - 0.5;
      const py = (currentGlareY / 100) - 0.5;

      orangeLight.position.x = 2.0 + px * 3;
      orangeLight.position.y = 2.0 - py * 3;

      indigoLight.position.x = -2.0 - px * 3;
      indigoLight.position.y = -2.0 + py * 3;

      dirLight.position.x = px * 5;
      dirLight.position.y = -py * 5;

      renderer.render(scene, camera);
      animationFrameId = requestAnimationFrame(animate);
    };

    animate();

    // Resize observer to handle dynamic sizing if needed
    const resizeObserver = new ResizeObserver(() => {
      if (!canvasRef.current) return;
      // Fixed size for the ticket container for perfect alignment, but matches CSS
      renderer.setSize(width, height);
    });
    if (mountRef.current) {
      resizeObserver.observe(mountRef.current);
    }

    // Clean up
    return () => {
      cancelAnimationFrame(animationFrameId);
      resizeObserver.disconnect();
      geometry.dispose();
      frontMaterial.dispose();
      backMaterial.dispose();
      edgeMaterial.dispose();
      texture.dispose();
      renderer.dispose();
    };
  }, [email, ticketNumber]);

  // Sync ref with latest props for the animation loop (legitimate animation pattern)
  const stateRef = useRef({ rotateX, rotateY, glareX, glareY });
  // eslint-disable-next-line react-hooks/immutability, react-hooks/refs
  stateRef.current = { rotateX, rotateY, glareX, glareY };

  return (
    <div
      ref={mountRef}
      onMouseMove={onMouseMove}
      onMouseLeave={onMouseLeave}
      className="relative w-[340px] h-[210px] select-none cursor-grab active:cursor-grabbing"
      style={{ perspective: "1000px" }}
    >
      <canvas
        ref={canvasRef}
        className="w-full h-full rounded-xl"
        style={{ backfaceVisibility: "hidden" }}
      />
      {/* 3D Reflection overlay highlights on CSS for extra sheen */}
      <div 
        className="absolute inset-0 rounded-xl pointer-events-none border border-white/10 mix-blend-overlay"
        style={{
          background: `radial-gradient(circle at ${glareX}% ${glareY}%, rgba(255, 255, 255, 0.15) 0%, transparent 60%)`,
        }}
      />
    </div>
  );
}