Skip to content

Instantly share code, notes, and snippets.

@stephensmitchell
Forked from jasonsturges/App.svelte
Created October 6, 2021 02:16
Show Gist options
  • Save stephensmitchell/24a4e8f2f4bc2dd025e11971460673ed to your computer and use it in GitHub Desktop.
Save stephensmitchell/24a4e8f2f4bc2dd025e11971460673ed to your computer and use it in GitHub Desktop.
3D with Svelte and Three.js
<script>
import { onMount } from 'svelte';
import { createScene } from "./scene";
let el;
onMount(() => {
createScene(el)
});
</script>
<canvas bind:this={el}></canvas>
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0
}
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