Skip to content

Instantly share code, notes, and snippets.

@jasonsturges
Created December 11, 2020 04:34
Show Gist options
  • Save jasonsturges/d6625dad323b48613b319f58845b933c to your computer and use it in GitHub Desktop.
Save jasonsturges/d6625dad323b48613b319f58845b933c to your computer and use it in GitHub Desktop.
3D with Svelte and Three.js
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