Skip to content

Commit

Permalink
Adding donuts
Browse files Browse the repository at this point in the history
  • Loading branch information
alexristinmaa committed Apr 9, 2024
1 parent b17ac6e commit fb6e158
Show file tree
Hide file tree
Showing 8 changed files with 1,182 additions and 7 deletions.
15 changes: 15 additions & 0 deletions alexanderristinmaa/app/(experiments)/donut/page.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.text {
position: absolute;
z-index: 100;
display:inline;
width: auto;
height: auto;
}

#thebox {
top: 10%;
left: 50%;
transform: translateX(-50%);
color: white;
font-size: 30px;
}
75 changes: 75 additions & 0 deletions alexanderristinmaa/app/(experiments)/donut/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'use client'

import { useEffect, useRef } from 'react';
// style
import styles from './page.module.css';

// next

// other
import * as THREE from 'three';
import { GLTF, GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';

export default function Home() {
const containerRef = useRef<HTMLDivElement>(null);

useEffect(() => {
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.01, 10000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);

containerRef.current?.appendChild(renderer.domElement);

camera.position.z = 5;

const loader = new GLTFLoader();

(async () => {
let donut = await new Promise((resolve, reject) => loader.load('/donuts.gltf', data => resolve(data), undefined, reject)) as GLTF;

scene.add(donut.scene);
// Render the scene and camera

// Add this function inside the useEffect hook
const renderScene = () => {
donut.scene.rotation.x += 0.01;
donut.scene.rotation.y += 0.01;

renderer.render(scene, camera);
requestAnimationFrame(renderScene);
};

// Call the renderScene function to start the animation loop
renderScene(0);
})();


const handleResize = () => {
const width = window.innerWidth;
const height = window.innerHeight;

camera.aspect = width / height;
camera.updateProjectionMatrix();

renderer.setSize(width, height);
};

window.addEventListener('resize', handleResize);

return () => {
// Cleanup of the renderer element
// Cleanup of the window element
// Good article here: https://react.dev/learn/synchronizing-with-effects#step-3-add-cleanup-if-needed
renderer.domElement.remove();
window.removeEventListener('resize', handleResize);
}
}, []);

return (
<div>
<span className={styles.text} id={styles.thebox}>THE DONUT</span>
<div ref={containerRef} />
</div>
);
}
19 changes: 19 additions & 0 deletions alexanderristinmaa/app/(experiments)/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const metadata = {
title: 'Next.js',
description: 'Generated by Next.js',
}

// styles
import "../globals.css";

export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="sv">
<body>{children}</body>
</html>
)
}
2 changes: 1 addition & 1 deletion alexanderristinmaa/app/(homepage)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default function RootLayout({
<Link href='/' id={styles.navTitle}>Alex Ställe</Link>
</div>
<div className={styles.navRight}>
<Link className={styles.navLink} href='/blog'>Bloggen</Link>
<Link className={styles.navLink} href='/blog'><b>Bloggen</b></Link>
<Link className={styles.navLink} href='/alex'>Vem är jag</Link>
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions alexanderristinmaa/app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

* {
box-sizing: border-box;
padding: 0;
margin: 0;
padding: 0px;
margin: 0px;
}

html,
Expand Down
51 changes: 51 additions & 0 deletions alexanderristinmaa/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions alexanderristinmaa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
"lint": "next lint"
},
"dependencies": {
"next": "14.1.4",
"react": "^18",
"react-dom": "^18",
"next": "14.1.4"
"react-dom": "^18"
},
"devDependencies": {
"typescript": "^5",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18"
"@types/react-dom": "^18",
"@types/three": "^0.163.0",
"three": "^0.163.0",
"typescript": "^5"
}
}
1,013 changes: 1,013 additions & 0 deletions alexanderristinmaa/public/donuts.gltf

Large diffs are not rendered by default.

0 comments on commit fb6e158

Please sign in to comment.