-
-
Save stephensmitchell/24a4e8f2f4bc2dd025e11971460673ed to your computer and use it in GitHub Desktop.
3D with Svelte and Three.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
import { onMount } from 'svelte'; | |
import { createScene } from "./scene"; | |
let el; | |
onMount(() => { | |
createScene(el) | |
}); | |
</script> | |
<canvas bind:this={el}></canvas> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
html, body { | |
width: 100%; | |
height: 100%; | |
margin: 0; | |
padding: 0 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as THREE from 'three'; | |
const scene = new THREE.Scene(); | |
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); | |
const geometry = new THREE.BoxGeometry(); | |
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 }); | |
const cube = new THREE.Mesh(geometry, material); | |
let renderer; | |
scene.add(cube); | |
camera.position.z = 5; | |
const animate = () => { | |
requestAnimationFrame(animate); | |
cube.rotation.x += 0.01; | |
cube.rotation.y += 0.01; | |
renderer.render(scene, camera); | |
}; | |
const resize = () => { | |
renderer.setSize(window.innerWidth, window.innerHeight) | |
camera.aspect = window.innerWidth / window.innerHeight; | |
camera.updateProjectionMatrix(); | |
}; | |
export const createScene = (el) => { | |
renderer = new THREE.WebGLRenderer({ antialias: true, canvas: el }); | |
resize(); | |
animate(); | |
} | |
window.addEventListener('resize', resize); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment