Attempting to implement Three.js - ShaderMaterial on a JSF page, encountering an error: Uncaught TypeError - Unable to access property 'textContent' of null

After referencing a previous post that utilized the code found at http://jsfiddle.net/XGWGn/118/, I am attempting to integrate this code from the HTML pane into a JSF 2.1 page. I suspect that my JavaServer Face page content might be the cause of the error appearing in the JavaScript console:

Uncaught TypeError: Cannot read property 'textContent' of null

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui">

    <h:head>
        <title>BYOS screen</title>
        <h:outputStylesheet library="css" name="byos.css"/>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r71/three.js" type="text/javascript"/>

        <h:outputScript name="OrbitControls.js" library="js"/>

        <style>
            body {
                /* set margin to 0 and overflow to hidden, to use the complete page */
                margin: 0;
                overflow: hidden;
            }
        </style>
    </h:head>
    <h:body>

        <!-- Div which will hold the Output -->
        <div id="WebGL-output">
        </div>
        <div id="Stats-output">
        </div>

        <script src="./resources/js/06-mesh-properties-4-shader.js" type="text/javascript"/>
        <script id="vertex_shader" type="x-shader/x-vertex">
        
        varying vec2 vUv;
        varying vec3 vNormal;
        varying vec3 vViewPosition;
        
        void main() {
        
                vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
        
                vUv = uv;
                vNormal = normalize( normalMatrix * normal );
                vViewPosition = -mvPosition.xyz;
        
                gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
        
        }
        
        </script>
        
        <script id="fragment_shader" type="x-shader/x-fragment">
        
        uniform sampler2D texture;
        uniform sampler2D texture2;
        
        varying vec2 vUv;
        varying vec3 vNormal;
        varying vec3 vViewPosition;
        
        void main() {
        
                vec4 tColor = texture2D( texture, vUv );
                vec4 tColor2 = texture2D( texture2, vUv );
        
                // hack in a fake pointlight at camera location, plus ambient
                vec3 normal = normalize( vNormal );
                vec3 lightDir = normalize( vViewPosition );
        
                float dotProduct = max( dot( normal, lightDir ), 0.0 ) + 0.2;
        
                gl_FragColor = vec4( mix( tColor.rgb, tColor2.rgb, tColor2.a ), 1.0 ) * dotProduct;
        
        }
        
        </script>
        
        
    </h:body>
</html>

I am experimenting with the ShaderMaterial on a JSF 2.1 page, which is a Facelets file. While the 3D shape renders correctly in the jsfiddle URL, it does not render at all in the JSF page.

Answer №1

Placing the 06-mesh-properties-4-shader.js file (which contains the code for the jsFiddle JavaScript pane) as the final script element within the h:body element resolved the issue. Previously, the script was being called before the element with id="vertex_shader" had finished loading, resulting in a null textContent property. This issue was unrelated to the fact that it was on a JSF 2.1 page.

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

What are the steps for showcasing the output of a JavaScript function on an HTML page?

After searching the community for solutions, I'm still struggling to understand them. I have a JavaScript function that reads data from a .json file and displays a specific object in VisualCodee console. Now, I want to show this object on an HTML fil ...

The number of subscribers grows every time $rootscope.$on is used

I currently have an Angular controller that is quite simple: angular.controller('appCtrl', function ($scope, $rootScope) { $rootscope.$on('channel.message', function () { // do something here } }); In addition, I have a ...

What is the best way to empower additional controls as a user enters text into a box, but disable them once the text is deleted or the box is left empty

I'm attempting to dynamically enable or disable a set of controls based on the input from a text box. When the user inputs a value into the text box, the controls should be enabled; conversely, if the user deletes the value or leaves it empty, the con ...

Looking to conceal a JavaScript file or minimize it from the web browser following production build in React

Upon executing the npm run build command using the script provided in my React App's package.json file, "scripts": { "start": "react-scripts start", "build": "set 'GENERATE_SOURCEMAP=false&apos ...

What about a sliding element feature?

Is there a popular slider component that many startups are using, or are these types of sliders typically built from scratch? It seems like they are everywhere on startup websites. I would appreciate it if someone could point me in the right direction with ...

jQuery UI Slider: Enable handles to move across each other

I have implemented a customized range slider on my webpage located at (see 'Day & Night Shift') to work with hours and minutes in 15-minute increments. $(function() { $("#slider-range").slider({ range: true, min: 0, ...

Determine the card type based on the card number

My array of card types is structured like this: var cards = new Array(); cards [0] = {name: "VISA", length: "13,16", prefixes: "4", checkdigit: true}; cards [1] = {name: "VISA_DELTA/ELECTRON", length: "16", prefixes: "417500,4917,4913", checkdigit: tr ...

What is the best way to extract a single-word object from an array without any special characters?

Is there a way to extract a list of single words from wordnet without any special characters included? I'm attempting to achieve something like this: const wordnet = require('wordnet') await wordnet.init(); let results = await wordnet.lis ...

Swap out one input for a newly generated input on the fly

Is there a way to replace a file input field with a text input field in jQuery while maintaining all properties such as id and name? Background: Currently, I have an <input type="file" name=".." id=".." /> being used for AJAX uploads. For Internet ...

Develop a trail of links that reflect the structure of the nodes

The hierarchical structure of nodes is as follows: { "nodes":[ { "assetId":"cfe-3a2b-47e7-b7e9-e2e090ca0d34", "assetName":"IRCTC", "assetType&quo ...

DataTables: Display action button based on specified condition in query

I am using jquery DataTables to display data via an ajax call. Everything is loading smoothly and I have included an action button in the last column that will add the data to another database. However, I am struggling with one aspect: After the button ...

The Google timezone API is displaying an inaccurate daylight saving offset

London now has an additional +1 hour of daylight saving time. I made a request to the Google timezone API for the London timezone, but unfortunately, it is not displaying the correct time. https://maps.googleapis.com/maps/api/timezone/json?location=51.507 ...

The MEAN stack consistently shows an error message of 'Invalid password' whenever a user attempts to log in

I have been working on creating a user login system in node.js with mongoose and MongoDB. Everything works fine when registering new users, but after a few successful logins, an error stating "Invalid password" starts to appear. I would appreciate any assi ...

Utilizing JavaScript and jQuery to make a query to mySQL

Looking to solve a SQL database query challenge using JavaScript or jQuery? Here's the scenario: I have a SQL db and typically use the following query to retrieve data based on distance from specified coordinates: SELECT id, ( 3959 * acos( cos( rad ...

Launching App with dynamically fetched data via Ajax

I have a question regarding best practices for structuring my app. After receiving data from the server, I would like to set objects, ideally on the global scope. How can I achieve this without using async: false, as I've been advised against it? What ...

Tips on resetting the position of a div after filtering out N other divs

Check out this code snippet. HTML Code: X.html <input type="text" id="search-criteria"/> <input type="button" id="search" value="search"/> <div class="col-sm-3"> <div class="misc"> <div class="box box-info"> ...

Creating types for React.ComponentType<P> in Material-UI using TypeScript

I am currently working with Typescript and incorporating Material-UI into my project. I am trying to define the component type for a variable as shown below: import MoreVert from '@material-ui/icons/MoreVert' import { SvgIconProps } from '@ ...

The && operator is not executed when the button is disabled

There is a button on the page that has been disabled using the [disabled] property. <button class="btn btn-primary center-block" (click)="sign(sf.value)" [disabled]="validateInsuredFirstName(firstName.value) && validateInsuredLastName(l ...

Is it possible to integrate a Bluetooth button with a button on my website?

I've been experimenting with my web application (using HTML and JavaScript) to create a process where a click on my Bluetooth device can simulate a button click on the page. Do you think this is achievable? Appreciate any help in advance. ...

Issue with JQuery Ajax button functionality

I am brand new to navigating the world of writing ajax and using a restful api...so please bear with me. On the backend, I have a Laravel 5.2 RESTful API that I'm working with, and my current task involves loading a list of categories using Jquery / ...