The curtains fail to load when transitioning to a new page

Currently, I am using curtainjs on my website. The issue I am facing is that when I navigate from my landing page to the about page, curtainjs doesn't work initially. However, upon refreshing the page, everything starts working as expected.

Below is the implementation of curtainjs:

mounted(){
    window.addEventListener("load", this.curtainjs);
}
methods: {
   curtainjs() {
      const curtains = new Curtains({
        container: "canvas",
        pixelRatio: Math.min(1.5, window.devicePixelRatio),
      });

      // Rest of the implementation code
    },
}

I am puzzled as to why this issue is occurring since the function call is in the mounted part with no other suitable location.

Answer №1

One reason for this issue is that the window load event has already occurred when transitioning from the landing page to the about page.

To resolve this, you can directly invoke this.curtainsjs() within the mounted hook, which should resolve the issue.

However, this approach may result in memory leaks each time you navigate away from the about page since the curtains instance may not be disposed of correctly. There are a few solutions to prevent this:

Solution 1 - Quick Fix

Assign the curtains instance to a private component variable and dispose of the instance before unmounting the component.

Although this method may work, it is not optimal in terms of performance and resources since a new webgl context will be created each time you visit the about page.

mounted(){
    this.curtainjs();
},
beforeUnmount() {
    if(this.$curtains) {
        // dispose curtains instance
        this.$curtains.dispose();
    }
},
methods: {
   curtainjs() {
      // Code for initializing Curtains and handling planes
   }
}

Solution 2 - Best Practice

The recommended approach in the context of Vue is to create a curtains plugin and a Curtains component at the root of your App to handle the webgl context creation only once during initialization.

Then, you can create multiple Plane components as needed with their own lifecycle methods using the plugin for creation and removal procedures.

While omitting the full code example here, creating a Vue use case for the library could be beneficial. Additionally, consider enhancing your code by managing errors, events, and variables with Vue's data, computed, and methods, importing shaders from external files, and other improvements.

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

Setting up ckeditor5 in Nuxt the right way

Recently, I encountered the CkEditor5 setting in Nuxt js for the first time and found it to be quite confusing with plenty of errors surfacing. I attempted to customize my version of ckeditor5 in nuxt.js by following this guide However, none of it seems ...

Ways to incorporate JSON web token into every query string of my requests

Just recently, I grasped the concept of using JSON web tokens. Successfully, I can generate a JSON web token upon sign-in and have also established the middleware to authenticate my token and secure the routes that fall under the JSON verification middlewa ...

Identifying all Images with JavaScript, Including Asynchronous Ones

Is it possible to use JavaScript to identify all images within a document, even those that are loaded asynchronously (possibly after the DOM is fully loaded)? I am interested in developing a function that can determine if Google Analytics has been loaded ...

Vue: Optimizing JSON response filtering

I am struggling with filtering a JSON response using Vue. return this.offers.filter(type => type.offers == 'Junior'); When I keep it as return this.offers, the following is displayed in my HTML: {"-MN5agCddYAdy7c8GSSz": { "comp ...

AngularJS tips for resolving an issue when trying to add duplicates of a string to an array

Currently dealing with a bug that occurs when attempting to push the same string into an array that has already been added. The app becomes stuck and prevents the addition of another string. How can I prevent the repeat from causing the app to get stuck w ...

Executing jQuery after the body has finished loading

I'm trying to execute a JavaScript function once the body has finished loading. My framework loads the header and footer from static files, while the body content is dynamic. I read online that I should place the $(elem).load(function()) at the end of ...

Enhance User Experience with Dynamic Scroll Page Transition

Hey everyone! I've been working on revamping a website and stumbled upon this fascinating page transition that I would love to replicate. However, I haven't been able to find a JQuery library that can achieve this effect. Does anyone have any i ...

I'm having trouble getting my HTML POST request form to connect with the Express app.post. Any tips on how to properly pass on numeric variables to a different POST request?

There seems to be a misunderstanding or error on my part regarding POST and GET requests based on what I've read online. On myNumber.ejs, I have a submit form. Upon submission, the view switches to Add.ejs. The goal is for Add.ejs to display both the ...

Enhance the step implementation in Cucumber-js

Background In my TypeScript project, I am utilizing https://github.com/cucumber/cucumber-js. The code snippet below showcases a typical cucumber implementation: import { Given, Then, When } from 'cucumber' Given(`Page is up and run ...

"Enhancing the Today Button in FullCalendar with Custom Functionality

My issue lies with the implementation of fullCalendar. Specifically, I am utilizing the week view (defaultView: 'basicWeek') with the toolbar buttons for 'today', 'prev', and 'next'. The problem arises when I click t ...

Using Vuejs for Seamless Facebook Social Login

Currently utilizing Laravel Vuejs, I'm attempting to implement social login with the vue-social-auth plugin. However, upon clicking the 'login with Facebook' button, a new Facebook window opens but unfortunately the client id and redirect ar ...

Is there a way to retrieve the line number of an error within a dynamically inserted <script> element?

When I dynamically create a script element and add it to the page, the errors do not give me the line numbers of the script itself, but instead provide the line number where I append the script. The following code in a .js file will result in an error mes ...

Is there a way to extract the MIME type from a base64 string?

My base64 String appears as "UklGRkAdEQBXQVZFZm10IBIAAAABAAEAg...". I am attempting to download this file using my browser. Therefore, I convert it to a blob with the following function: b65toBlob: function(b64Data, sliceSize = 512) { ...

Next JS Dynamic Routing displays successful message on invalid routes

I have been using the App Router feature in Next JS along with Strapi as my CMS. When I make a query to the API endpoint in Postman, I receive the expected results. Requests for routes without corresponding slugs return a 404 error, while only routes with ...

ReactJS implementation for selecting names from a dropdown menu

Hello, I am new to using React. I have a dropdown form with a search engine in it that is working perfectly as I need. The only issue is that when I type, for example, AR in the Symbol field, it shows me ARDRBTC as expected, but I am unable to click on it ...

Can you provide guidance on integrating this jQuery script into my Next.Js application effectively?

I have been using a script on a plain HTML website, importing it via the <script> tag: (function($) { var wa_time_out, wa_time_in; $(document).ready(function() { $(".wa__btn_popup").on("click", function() { if ($(&qu ...

Designing personalized buttons on React Google Maps

Currently tackling a personal project that involves react-google-maps, but I'm struggling to display custom buttons on the map. My goal is to create a menu button that hovers over the map, similar to the search bar in the top left corner of Google Map ...

Even after configuring a proxy, the API calls are still not being redirected to the correct destination

Even after setting up a proxy, the API requests are not being directed to the correct target URL. I've built a chatbot application with create-react-app. My goal is to reroute all API calls originating from http://localhost:3000/ to http://localhost: ...

Currently in the process of executing 'yarn build' to complete the integration of the salesforce plugin, encountering a few error messages along the way

I've been referencing the Github repository at this link for my project. Following the instructions in the readme file, I proceeded with running a series of commands which resulted in some issues. The commands executed were: yarn install sfdx plugi ...

Utilize dynamic Google Maps markers in Angular by incorporating HTTP requests

Struggling to find a solution for this issue, but it seems like it should be simple enough. Initially, I fetch my JSON data using the code snippet below: testApp.controller("cat0Controller", function($scope, $http){ var url = "../../../Data/JSONs/randomd ...