HubAPI / components /ThreeTicketCard.tsx
samirhosninet's picture
feat: complete HUBAPI overhaul (Security, i18n RTL, Dark/Light Mode, A11y & Performance fixes)
587459d
Raw History Blame Contribute Delete
10.7 kB
"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>
);
}