<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/controls/OrbitControls.js"></script>
<p class="top-menu">
Press spacebar to reset the canvas size to window
</p>
<!-- you can still offscreen the canvas html and still guarantee that it can still display what it should display -->
<!-- the container size doesn't really constrain the canvas render size. canvs will still bleed out of ocntainer if need to -->
<div id="container">
</div>
<div class="bottom-menu">
<button onClick="displaySnapShot()">Capture</button>
<button onClick="resize(500, 156)">Force Resize</button>
<button onClick="toggleCanvasHide()">Canvas Toggle Visbility</button>
<button onClick="toggleCamera()">Camera Toggle</button>
<button onClick="fitToScreen()">FTS</button>
</div>
<!-- for image captures -->
<div id="img-container">
</div>
var scene, renderer, camera, perspectiveCam, orthoCam;
var controls;
var forcedWidth, forcedHeight;
init();
lightSetup();
animate();
function init() {
renderer = new THREE.WebGLRenderer({
antialias: true,
// required for capturing snapshot
preserveDrawingBuffer: true
});
var container = document.getElementById('container');
if(!container) {
console.error('no container');
} else {
console.error('all good to go');
}
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setSize(width, height);
container.appendChild(renderer.domElement);
scene = new THREE.Scene();
scene.rotation.x = -Math.PI / 2;
var gridXZ = new THREE.GridHelper(1000, 100);
scene.add(gridXZ);
//gridXZ.rotation.x = Math.PI / 2;
var axes = new THREE.AxesHelper(1000);
scene.add(axes);
var ortho = false;
if(ortho) {
camera = orthoCam = createOrthoCamera(width, height);
} else {
camera = perspectiveCam = createPerspectiveCamera(width, height);
}
createOrbitControl(camera, renderer.domElement);
//controls = new THREE.OrbitControls(camera, renderer.domElement);
//camera.updateProjectionMatrix();
/* var points = [new THREE.Vector3(0, 30, 0)];
for(var point of points) {
createSphere(point, 0xffff00);
} */
document.addEventListener('keydown', (event) => {
const key = event.key;
const code = event.code;
if(code === 'Space') {
resize();
console.log('resizing');
}
console.log('event: ', key, code);
});
}
/**
OrbitControls is designed to create listeners on the container at creation time only.
Therefore, we cannot assign the container after creation of orbitalControl.
It's better to create and dispose old orbit controls should you create new camera
There might be a way to maintain the old orbit when switch camera, but probably
best to dump and recreate due to the fact that the controls have listeners bindded
and could cause conflicts where old...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.