Difficulty displaying a 2D SVG in Three.js and WebGL

I am struggling to incorporate my own SVG illustration into a ThreeJS Scene that already contains a cat object and a cube. I have tried using resources like the CodePen (link included) and various Stack Overflow threads related to LegacySVG Loader, but I find it difficult to apply them to my specific image as a novice in ThreeJs. The closest I have come is rendering the SVG onto a canvas separately, but now I am unsure how to integrate it into the existing scene alongside the other objects.

Despite experimenting with different approaches and loaders, such as the LegacySVG mentioned in one thread, I have hit a roadblock in understanding how to render the SVG onto the same canvas as the rest of the scene. Is there a simpler method to achieve this or can LegacySVG be used to render onto a canvas directly? Any guidance on this matter would be greatly appreciated.

let renderer;
let camera;
//let controls;

let scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(54, window.innerWidth / window.innerHeight, 0.1, 1000);

renderer = new THREE.WebGLRenderer({
    antialias: true,
    canvas: document.getElementById("viewport")
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(new THREE.Color(0xfefefe));
// document.body.appendChild(renderer.domElement);

camera.position.x = 1;
camera.position.y = 1;
camera.position.z = 15;


// code for adding objects to the scene

let animate = function() {
    requestAnimationFrame(animate);

    //controls.update();
    renderer.render(scene, camera);
};
//////////////////
animate();

function updateCamera(ev) {
  
camera.position.z = 15 - window.scrollY / 250.0;
}

window.addEventListener("scroll", updateCamera);
body {
overflow-x: hidden;
    overflow-y: scroll;
padding: 0;
margin: 0;
  
}

canvas {
position: fixed;
  height: 100vh;
}

#threeD {
    position: fixed;
margin: 0;
padding: 0;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    
    
}
.page-wrapper {
padding: 0px;
position: absolute;
left: 0;
top: 0;
width: 100%;

  height: 4000vh;
}



#container {
height: 500vh;
position: fixed;
}
<html>
    <script src="https://raw.githubusercontent.com/mrdoob/three.js/master/src/loaders/LoadingManager.js"></script>
 <script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="45312d37202005756b7475776b74">[email protected]</a>/build/three.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    <script src="https://cdn.rawgit.com/mrdoob/three.js/r68/examples/js/loaders/SVGLoader.js"></script>
    <script src="https://raw.githubusercontent.com/mrdoob/three.js/master/examples/js/renderers/SVGRenderer.js"></script>
    <link rel="stylesheet" type="text/css" href="index1.css" />

    
    <body>
            <canvas id="viewport"></canvas>
         
                <div class="page-wrapper" >
                  <h1> scroll! </h1>

                
                  
                </div>

            
    </body>
   

<script src="index1.js"></script>
    

</html>

Answer №1

When working with SVGRenderer, there are important distinctions to be aware of compared to using WebGLRenderer.

  1. SVGRenderer manipulates elements within an <svg> tag, such as <path>, <circle>, and <rect>, providing a 2D rendering that can appear 3D with rotation. For WebGL rendering, where true 3D geometry is employed, conversion of SVG to WebGL-compatible formats through THREE.SVGLoader is necessary as demonstrated in the webgl_loader_svg example.

  2. In a common 3D space, simultaneous presence of <svg> elements along with WebGL content within a <canvas> is not feasible. Opting for WebGLRenderer enables seamless integration of various shapes like cubes and planes including cat images.

  3. Harmonizing files from disparate sources and different Three.js versions (r102.1, r68, r113) can lead to conflicts. It's advisable to adhere to a single revision, for instance maintaining consistency by employing links such as:

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

I am looking to transmit information from flask to React and dynamically generate HTML content based on it

index.py @app.route('/visuals', methods=["GET", "POST"]) def visuals(): global visclass return render_template('visframe.html', images=visclass.get_visuals()) visframe.html <div id='gallery'></div> { ...

Vue not displaying local images

I'm having trouble creating an image gallery with Vue.js because local images are not loading. I have the src attributes set in the data attribute and can only load images from external sources. For example: data() { return { imag ...

Updating the state in React while controlling the change causes OrbitControls from three to malfunction

When using react three with drei orbitControls, I am trying to determine whether the camera is above or below zero while moving it around. Currently, I am using an onEnd listener to trigger my code in the ParentComponent. If I change the state of my parent ...

JavaScript recording speed

I am currently working on a project that involves video recording on my website. After creating a canvas and pushing the recorded frames to it, I have encountered an issue. The playback of the video is too fast - a 10-second video plays in around 2 second ...

Looking for a way to implement a vertical autoscroll <ul> list that responds to mouse movement using JavaScript and jQuery

Could you assist me in enabling the list items to be moved using mouse movement? When the user moves the mouse cursor vertically upward over the list, the list should scroll downward. If the user moves the mouse cursor downward over the list, the list shou ...

Using static assets within VueJS Components

I am currently working on a VueJS project that was customized from a base "webpack-simple" template provided by vue-cli, and it follows the folder structure below: https://i.sstatic.net/C3vxY.png My main focus right now is developing the "clickableimage. ...

Debugging Windows Node.js applications remotely on Azure Web Apps or Azure Functions

I'm working with Windows-based Node.js applications deployed on Azure Web Apps or Azure Functions. It looks like the Visual Studio Code (VSC) extensions are intended for Linux environments only. What is the best way for me to debug these application ...

Obtaining the value of a JavaScript confirm popup in C#.NET

I am trying to implement a javascript confirm popup that returns a value from the code behind. When the user selects the ok button on the confirm popup, certain code will execute, and if they select cancel, different code will run. Is there a way to retri ...

Ways to rearrange div elements using JavaScript

I need assistance with reordering div elements using JavaScript, as I am unsure of how to accomplish this task. Specifically, I have two divs implemented in HTML, and I would like the div with id="navigation" to appear after the div with class="row subhea ...

Encountering issues with tesseract callbacks in nodejs due to validation errors

I encountered the following error: fs.js:132 throw new ERR_INVALID_CALLBACK(); ^ TypeError [ERR_INVALID_CALLBACK]: Callback must be a function at makeCallback (fs.js:132:11) at Object.fs.unlink (fs.js:1002:14) at /home/bakedpanda/Documents/BTP/ ...

Exploring the mechanics of JavaScript and jQuery Ajax: What's the secret to its function

It's common knowledge that browsers are single-threaded, meaning they can only either render the UI or execute Javascript snippets. While web workers offer a way to achieve multi-threading, let's focus on the default behavior. I'm curious a ...

Jquery code failing to trigger any response

Recently, I quickly created a jQuery script to dynamically populate a paragraph element in order to easily switch between player and server interaction options. However, I am currently facing an issue where my script does not populate as expected. I have a ...

Creative Blueprint

Our company currently operates 3 browser based games with a team of just 4-5 coders. We are looking to enhance the collaboration within our coding team and streamline our processes. Considering the fact that our games are built using a mix of html, php, j ...

jquery activating the toggle function to switch between child elements

I'm facing a challenge where I can't use .children() in this scenario, as it won't work since the elements aren't technically children. Here's the snippet of HTML that I'm working with: <p class="l1">A</p> ...

Ways to correctly import various CSS files for individual routes in a Vuejs application

Currently, I am in the process of developing a project using Vue.js and Laravel. This project will have distinct layouts for the admin/user panel and landing page, requiring different CSS files to be loaded for each route. At the moment, I am utilizing va ...

What issues are present in the Ajax script and the PHP radio input?

I'm having trouble extracting the value of a radio input in this code so I can update the database: <script type="text/javascript> function getVote() { if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlh ...

Updating IP addresses in MongoDB

I am dealing with a nested schema in my mongoDB collection. Here's an example of how it looks: { "_id":"61d99bf5544f4822bd963bda0a9c213b", "execution": { "test_split":0, "artifacts&quo ...

Please type the file name into the provided text box

Is there a way to automatically insert the filename of an uploaded file into an existing text input using either or valums.com/ajax-upload/? (I am working with php) For example, in a textbox: <input name="image" type="text" value="file_name" /> ...

Refresh Form Following Submission

When using a react form that triggers a graphql mutation upon button click, the text entered in the form fields remains even after the mutation has been executed. This necessitates manual deletion of text for subsequent mutations to be run. Is there a way ...

Close button for body

I have created a form that floats in the center of the screen On this form, I have included a button designed to close it However, I would like for the form to be closed anywhere on the screen when clicked with the mouse Here is my code: $(".offer-clo ...