A guide to implementing VantaJS in SvelteKit

Can someone guide me on incorporating Vanta JS from a SvelteKit project?
I've attempted to add it in +page.svelte,

<script lang="ts">
    import VANTA from "vanta";
    import * as THREE from 'three';

    VANTA.NET({
        el: "#home-page",
        mouseControls: true,
        ...
    })
</script>

<div id="home-page">
    ...
</div>

However, I encountered issues while trying to import VANTA that way.
I also experimented with using CDN from app.html without success.

Updated

Thanks to the response from H.B., I managed to apply the Vanta JS background to my SvelteKit page.
Although, the visual output differed from the one on CodePen.

<script>
    import * as THREE from 'three';
    import NET from 'vanta/dist/vanta.net.min';

    function vanta(node) {
        NET({
            el: node,
            THREE: THREE,
            mouseControls: true,
            touchControls: true,
            gyroControls: false,
            minHeight: 200.00,
            minWidth: 200.00,
            scale: 1.00,
            scaleMobile: 1.00,
            color: 0x7accfe,
            backgroundColor: 0x010f18
        })
    }

</script>

<div use:vanta/>

On the SvelteKit web app, https://i.sstatic.net/gvdf1.png

On CodePen, https://i.sstatic.net/DxjBR.png

Upon checking the browser console, I came across this message:

THREE.Material: 'vertexColors' parameter is undefined.

I installed Vanta JS and Three.js by running the following commands,

npm install -D vanta
npm install -D three

Answer №1

When working with SvelteKit, it is essential to ensure that the code is executed only on the client side. This can be achieved by using either the onMount function or, even better, by utilizing an action that provides access to the node element.

Additionally, it is necessary to pass the Three.js module to Vanta as a property and import the desired effect directly from vanta/dist:

<script>
    import * as THREE from 'three';
    import NET from 'vanta/dist/vanta.net.min';

    function vanta(node) {
        NET({
            el: node,
            THREE: THREE,
            color: 0x000000,
        })
    }
</script>

<div use:vanta/>

Answer №2

The issue with the rendering was identified as a version mismatch between Vanta JS and Three.js.

THREE.Material: The 'vertexColors' parameter is not defined.

Currently,

  • the Vanta JS version being used is 0.5.24
  • This error is originating from a version of Three.js that is higher than 0.140.2 (refer to this thread).

Therefore, I had to specifically specify the version of Three.js like this.

npm install -D <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1e6a766c7b7b5e2e302f2a2e2e302c">[email protected]</a>

After applying this fix, the rendering on the SvelteKit webapp now matches the examples on CodePen perfectly.

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 add some flair to my ID "checkimg" using JavaScript

JavaScript if ((valid1 && valid2 && valid3 & valid4 && valid5 && valid6 && valid7 && valid8)==1) { // include an image in the specified ID. document.formElem.next1.disabled=false; } else { docume ...

Is it possible to dynamically change a form's input value using a JavaScript keyup function based on input from other form elements?

My form utilizes the keyup function to calculate the total value of 3 input fields by multiplying them and displaying the result. Below is a fiddle showcasing this functionality: $(document).ready(function () { $(".txtMult1 input").keyup(multInp ...

Interact with an element on one page and watch as it dynamically affects another element on a separate page using J

Is it possible to use jQuery to click a button on one page (index1.html) and change the color of text on another html page (index2.html)? I've tried various methods, but none seem to work due to the need to refresh the second page in order to view ch ...

Script for collapsing or expanding is non-functional

Although I am a beginner in Javascript coding, I am eager to learn more about it. I stumbled upon some tutorials on creating collapse/expand <div> blocks. After testing the code on jsfiddle, I found that it works fine there. You can find the code he ...

React.js: The function useDef has not been defined

Attempting to create a React.js calculator application, my initial step was to delete the entire src folder and replace it with a new one containing the necessary elements for the web app. Here is the content of the index.js file: import React,{ useEffect, ...

The sinuous waveform in JavaScript

track : function(x, y, top, ampl) { return { top : top + 2, x : x + ampl * Math.sin(top / 20), y : (top / this.screenHeight < 0.65) ? y + 2 : 1 + y + ampl * Math.cos(top / 25) }; } This specif ...

Changing letter cases in a textbox using Javascript

I have a challenge to create a code that can switch the case of text entered by the user. Here is what I currently have: var num; function toggleTextCase(str) { return str.replace(/\w\S*/g, function (txt) { return txt.charAt(0).toUpperCase( ...

retrieving the selected checkbox value

My challenge is to extract the values of dynamically changing checked checkBoxes on my webpage. For example: while ($row=myqli_fetch_array($result)){ echo"<div>"; echo"<select id=\"course\" onchange=getCheckBox()> <opt ...

Dealing with redirect issues in a React-Material menu: A guide to troubleshooting and

When working with my menu, I face a variety of issues. First and foremost, within the initial RETURN section, there is a TREEITEM with a LISTITEM and a LISTITETEXT. I have included an OnClick event in the LISTITETEXT so that if the menu's id matches ...

The page loads successfully at first, but upon refreshing, a 404 error occurs when using Zeit, Nextjs, and now

After recently updating from now v1 to v2, I encountered an issue where my pages would return a 404 error when reloading after pushing to production using now --prod. While everything worked fine locally with now dev, the routing seemed to be causing confu ...

What is the best way to remove any objects in this array that have empty strings as values?

I have been developing a form using Angular 14. The form consists of a primary section, which includes the user's necessary information, and a secondary section where users can input additional data (an array of residences displayed in a table) befor ...

What is the best way to display an object in Vue.js once it has been retrieved through an AJAX request?

Imagine having the following HTML code: <div id="app"> <h2>List of Items</h2> <table border="1"> <thead> <tr> <th>Name</th> ...

What is the best way to extract the last JSON object from a JSONArray based on a specified value

I am currently working with a JSONArray that looks like the following: [ { "id": 1, "firstName": "abc", "isActive": true }, { "id": 2, "firstName": "cde", "isActive": false }, { " ...

Jquery Triggers Failing to Work Following Ajax Request

I have worked on 2 PHP pages, called "booking.php" and "fetch_book_time.php". Within my booking.php (where the jquery trigger is) <?php include ("conn.php"); include ("functions.php"); ?> $(document).ready(function(){ $(".form-group"). ...

Tips for preventing double foreach loops in Laravel when dealing with backward relationships

I'm attempting to display variables using the following relationships: First: public function group(){ return $this->belongsTo(Group::class); } Second: public function joinGroups(){ return $this->hasMany(JoinGr ...

Grabbing the mouse in Firefox

Are there any alternatives to the .setCapture(); and .releaseCapture() functions in Firefox that do not involve using jQuery? (The client prefers not to use it) ...

When a key is pressed, apply a background color and fade out effect using JavaScript

Whenever the enter key is pressed, I want to make a red color appear briefly and then fade out quickly to create a blinking effect. I attempted adding a new class on Keypress that would transition the opacity to 0: function enterpressalert(e, text) { ...

Implementing authentication fallback during re-login when a session expires in a web application built with React, Node.js, and Mariadb database

Greetings everyone, this is my debut post here so kindly bear with me :D. Currently, I am in the process of developing a social app where I am incorporating a fallback mechanism for situations when my database goes offline. Within my Login.jsx component, I ...

The React Apexchart fails to correctly adjust its height based on its parent container when set to 100%

Currently, I am working with react-apexcharts and facing an issue where I am trying to set the height of the chart to be 100% of its parent element. However, instead of taking the height from its parent, it is only showing a minimum height of 445px. Even a ...

The error callback in Jquery ajax is triggered despite receiving a 200 status code in the response

I am facing an issue with my ajax code. The success callback contains an alert that is not working properly. Here is the code snippet: $(".change_forex_transaction_status").click(function(){ $("#insufficient_funds").css("display","none"); var id = $( ...