Guide on building a spherical frame WebGL window scene on a website with the use of three.js

Is it possible to change the rectangular scene frame into a circular frame, so that only the cube is visible within the website frame? I would like to modify the scene "var scene = new THREE.Scene();" to achieve this effect. I couldn't find a solution on Google, so any help would be greatly appreciated as I am just starting out with web development!

<!DOCTYPE HTML>
<html>
<head>
    <title>Learning 3JS</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0" />
    <meta name="theme-color" content="#ff3400" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <script src="../three.js-master/build/three.js"></script>
    <script src="detector.js">

        if (Detector.webgl) {
            animate();
        } else {
            var warning = Detector.getWebGLErrorMessage();
            document.getElementById('container').appendChild(warning);
        }

    </script>

    <script src="OrbitControls.js"></script>
    <script src="TrackballControls.js"></script>
</head>

<body>
    <div id="container" style="width: 100%; height: 640px; background: aqua;"></div> 

    <script>

        var container = document.querySelector('#container');

        var scene = new THREE.Scene();
        var camera = new THREE.PerspectiveCamera(75, container.clientWidth/container.clientHeight, 0.1, 1000);
        var renderer = new THREE.WebGLRenderer();

        var geometry = new THREE.BoxGeometry(20, 20, 20);
        var material = new THREE.MeshLambertMaterial( { color: 0xffffff } );
        var mesh = new THREE.Mesh(geometry, material);

        scene.add(mesh);
        camera.position.z = 50;

        var light = new THREE.PointLight(0xffff00);
        /* position the light so it shines on the cube (x, y, z) */
        light.position.set(-15, -10, 30);
        scene.add(light);

        var controls = new THREE.OrbitControls(camera);
        //var controls = new THREE.TrackballControls( camera );
        controls.target.set( 0, 0, 0 )

        var loop = function()
        {
            requestAnimationFrame(loop);
             controls.update();
            renderer.render(scene, camera);
            mesh.rotation.x += 0.02;
            mesh.rotation.y += 0.005;
            //mesh.rotation.z += 0.1;
        }

        loop();

        renderer.setSize(container.clientWidth, container.clientHeight);
        container.appendChild(renderer.domElement);

    </script>
</body>
</html>

Answer №1

It is not possible to have a circular canvas or frame buffer, similar to how you cannot have a circular image. This limitation is a common rule in the world of computing.

However, just like with an image, it is possible to simulate a circular "viewport" using various methods. This can be achieved by incorporating transparent pixels or painting the rectangular background of the image to match the background color, among other techniques.

Furthermore, navigating THREE.js may be hindering your understanding due to its automated functions. Starting from the basics and grasping concepts such as Canvas functionality and WebGL integration could provide you with a clearer foundation:

https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Tutorial/Getting_started_with_WebGL

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there ample documentation available for Asp.Net Ajax controls?

Struggling to locate the client side functionality of Asp.net Ajax controls such as calendarextender. How can I determine the specific client side functionalities of each control or Calendar control? So far, I have only discovered the properties ._selected ...

Tips for clearing the sessionstorage upon browser refresh without removing data when clicking the back button in the browser

I am looking to clear the session storage only when the page is refreshed, without affecting the browser's back button functionality. I want to achieve this using AngularJS/JavaScript. Currently, I am storing data in sessionStorage on a back button c ...

How can I retrieve the data returned by an ajax call using jQuery?

I have a function that is making an AJAX call to return data. Here is the code: function reqNodelistByField( i ) { $.ajax({ type: 'post', url: './req.nodelist.by.field.php', dataType: 'text', ...

Changing the Selected Index with Javascript

I currently have a dropdown labeled Grade with the following values: ["a", "b", "c"] When a selection is made in the Grade dropdown, the options in the Student dropdown should update accordingly. For "a" selected, populate Student dropdown with ["Anna", ...

What is the method for altering the background color of an HTML table cell when a particular event occurs?

As I create an html table, I am looking to dynamically change the background color of specific boxes after running a selenium webdriver. For instance, if the webdriver successfully navigates the site, I want the corresponding box in the table to turn gre ...

What is the best way to send numerous forms to a PHP page when the quantity of forms produced varies randomly?

In my project, I have a code that dynamically populates input fields at the final step based on a user's selection. Each choice can have up to 6 input fields, resulting in a maximum of 4 forms or 24 input fields in total. For data persistence, I need ...

unable to retrieve parent ID

I am having trouble retrieving the ID of the parent's parent of the event target. It keeps coming back as undefined, but I have verified through firebug that the ID does exist. Below is my HTML markup: <div class="grid-stack-item ui-draggable ui- ...

Ajax is coming back with a value that is not defined

Currently, I am working on a search function that is responsible for searching a name from the database. When the user clicks "add", the selected item should appear below the search field so that it can be saved in the database. However, I am encountering ...

The link appears to be broken when trying to access the notFound component in Next.js version 13

In my Next.js 13.4 app directory, I added a not-found.tsx component that functions correctly when entering the wrong route: import Link from 'next/link' function NotFound() { return ( <section> 404, page not found ...

FF and IE9 cause issues with Backbone application

I recently finished developing a web application using Backbone, ICanHaz, and jQuery. If you want to check it out, click on this link: While the app works flawlessly in Chrome (version 12.0.742.122), it encounters issues in Firefox 5 (5.0.1) and Internet ...

Export various individual objects with separate geometry and materials to json using three.js

As a beginner with three.js, I am familiar with the JSONLoader for loading JSON from a file or URL. However, I am now wondering if it is possible to export models (objects, meshes) along with their geometries and materials separately to JSON files, and the ...

Display a message with `console.log` when a button is clicked in

I would like the console.log to display the coin.id of the element when I click on the row element. import React, { useState, useEffect } from "react"; import { Col, Image, Row } from "react-bootstrap"; import "./Company.scss" ...

Having trouble getting the Vue.js sample app to run using "npm run dev"? The error message "Error: Cannot find module 'node:url'" may

As a beginner in vuejs, I am currently following the steps below to create my vue js app on my local system: Step: npm init vue@latest ✔ Project name: … VueDemo ✔ Add TypeScript? … No / Yes ✔ Add JSX Support? … No / Yes ✔ Add Vue Router f ...

What is the best way to access the value chosen in a TextBox using a JavaScript Function in C# code-behind?

aspx.cs: <script type="text/javascript"> $(function () { //ALPHA $('#COLOR_ALPHA_TEXTBOX1').colorPicker({ pickerDefault: "E1E1E1", colors: ["E1E1E1", "33CC00", "FFF000", "CC0000", "996600", "FF9900", "303030", "0066FF", ...

Creating seamless scrolling in ReactJS without any pre-built components

Can anyone guide me on how to implement an infinite scroll in reactJs using a JSON dataset? I prefer building it from scratch without relying on any library. {data?.map((conto) => { return ( <Suspense key={conto._uid} fallback={<p>Loadin ...

Dynamically submitting a form generated within the AJAX success callback in CodeIgniter

After creating a form dynamically within the success function of an AJAX call, I expected it to submit like a normal form in CodeIgniter. However, upon clicking the submit button, the form data is not being picked up by the appropriate controller. The ori ...

Working with XML files in Node.js

I'm currently running an Arch Linux system with KDE plasma and I have a 50mb XML file that I need to parse. This XML file contains custom tags. Here is an example of the XML: <JMdict> <entry> <ent_seq>1000000</ent_seq&g ...

Displaying a popover upon page load using JavaScript

Currently, I am attempting to implement a JavaScript function that displays a popup on my webpage whenever a user visits the site. The script that I have found uses jQuery, but I am wondering if it is possible to achieve the same result using pure JavaScri ...

Objects and strings cannot be inserted into a JavaScript array

For hours, I've been attempting to troubleshoot this code. Currently, it successfully finds the number of anchors, and I have an array that is of undetermined size. Within the for loop, it retrieves the anchor and extracts the .href. I've confirm ...

"Experience the stunning transformation of 3D objects from poor rendering to exceptional quality in the GLTF-Files Online

I am looking to showcase a model created in Blender on a webpage. The issue is that the rendered quality of the model seems poor, especially with the edges not being properly displayed. Interestingly, when viewing the model in an online viewer, the edges a ...