Skip to content

Instantly share code, notes, and snippets.

@shopglobal
Created September 29, 2024 17:42
Show Gist options
  • Save shopglobal/78b599125e7ba65e588968463b7b7e40 to your computer and use it in GitHub Desktop.
Save shopglobal/78b599125e7ba65e588968463b7b7e40 to your computer and use it in GitHub Desktop.
React 3d

Below is a basic outline for a modern cryptocurrency website with 3D elements using React and Three.js. This example demonstrates a hero section with a spinning 3D coin, basic styling using TailwindCSS, and a responsive layout.

Steps:

  1. Install dependencies:

    • Install React, Three.js, and TailwindCSS by running the following commands in your project directory:
      npx create-react-app crypto-website
      cd crypto-website
      npm install three @react-three/fiber @react-three/drei tailwindcss
  2. Set up TailwindCSS:

    • Initialize Tailwind by running:
      npx tailwindcss init
    • In tailwind.config.js, add the following:
      module.exports = {
        content: [
          "./src/**/*.{js,jsx,ts,tsx}",
        ],
        theme: {
          extend: {},
        },
        plugins: [],
      };
    • In src/index.css, import Tailwind styles:
      @tailwind base;
      @tailwind components;
      @tailwind utilities;
  3. 3D Coin Model: Use a simple 3D model from Three.js (this example shows a rotating 3D cylinder that mimics a coin).

Code Example

  1. App.js (Main Component with 3D Coin)
import React from 'react';
import { Canvas } from '@react-three/fiber';
import { OrbitControls } from '@react-three/drei';
import './App.css';

const Coin = () => {
  return (
    <mesh rotation={[90, 0, 20]}>
      <cylinderBufferGeometry args={[1, 1, 0.2, 32]} />
      <meshStandardMaterial color={'#FFD700'} />
    </mesh>
  );
};

const App = () => {
  return (
    <div className="bg-gray-900 h-screen text-white">
      <header className="text-center p-8">
        <h1 className="text-5xl font-bold mb-4">Crypto Website</h1>
        <p className="text-lg">Welcome to the future of decentralized finance</p>
      </header>
      <div className="h-96 flex justify-center items-center">
        <Canvas>
          <ambientLight intensity={0.5} />
          <spotLight position={[10, 15, 10]} angle={0.3} />
          <Coin />
          <OrbitControls enableZoom={false} />
        </Canvas>
      </div>
      <footer className="text-center mt-8">
        <p>© 2024 Crypto Website. All rights reserved.</p>
      </footer>
    </div>
  );
};

export default App;
  1. App.css (Optional for additional styles)
body {
  margin: 0;
  padding: 0;
  font-family: 'Inter', sans-serif;
}

canvas {
  width: 100%;
  height: 100%;
}

Explanation:

  • Three.js Coin:

    • The Coin component is a basic 3D model of a cylinder, simulating a coin using cylinderBufferGeometry. The material is set to gold color with meshStandardMaterial.
  • Canvas:

    • The 3D scene is rendered inside the Canvas component from @react-three/fiber, which is a React renderer for Three.js.
  • OrbitControls:

    • Controls the 3D camera's orbit around the coin. You can disable zoom with enableZoom={false} to fix the zoom level.
  • TailwindCSS Styling:

    • The layout is responsive, with a centered hero section that uses TailwindCSS classes like bg-gray-900, h-screen, text-white, and text-center to manage the layout.

Customizing Further:

  • 3D Coin Model: You can replace the simple cylinder with an actual 3D coin model, using Blender to create a more detailed coin and exporting it to Three.js-friendly formats like .glb or .gltf.

  • Additional 3D Elements: Add more 3D animations (like blockchain visualizations, transaction flows, etc.) using Three.js for a more interactive experience.

  • Real-time Data: You can integrate a cryptocurrency API (e.g., CoinGecko) to display live pricing and statistics alongside the 3D elements.

This basic setup creates the foundation for a modern cryptocurrency website with 3D elements. Let me know if you'd like further customizations or additional sections!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment