cancel
Showing results for 
Search instead for 
Did you mean: 
SOLVED

Show and disable local script(ThreeJS)

Show and disable local script(ThreeJS)

RobotART_Walter
Established Member

Hello All,

I have made a local script containing a simulation in Threejs.

This works pretty good when only using the local script. However, now I want to show this local script inside a window inside my 'main' HMI. The issue I have is that I cannot correctly show the ThreeJS visualisation inside this window.

When I switch to this screen, the visualisation appears underneath the screen, not inside the container. 

And when I switch screens, the visualisation remains to be visiable under the screen. But it should be closed. 

RobotART_Walter_0-1682951067524.png

 

I am not too familiar with javascript. Is there anything I am doing wrong?

 

 

8 REPLIES 8

webiq-sk
Contributor

WebIQ does not have any "Window" widget - are you talking about a dialog?

In any case this looks more like a general JavaScript question or ThreeJS question than a WebIQ question to me.

With the limited given information: you are probably rendering ThreeJS inside the wrong HTML element.

Hello,

You are right. What I meant to say is that I created a screen. The screen contains a container. And inside this container I call the local script.

I agree that this is a more general question. I am fairly new with programming in Javascript so sometimes it's a little hard to differentiate webiq and javascript. I based the script on this WebIQ - Render a 3D model (boschrexroth.com) tutorial. 

I will look for more information on this subject online, 

Thank you anyway. 

 

Hello,

I did some furter investigating. And you are absolutely right.

The threejs script is rendered in a seperate HTML element like this:

        // Add 3D rendering div to document
        var body = document.body;
        //shmi.ctrl(".localScript")
        let container = document.createElement( 'div' );
        body.appendChild( container );
 
After opening the screen with the local script, the newly created div appears underneat the HTML created by web-iq.
 
I would like to render it in the container called: threejs_script.
RobotART_Walter_0-1683032095005.png

 

I tried doing it like this: body = document.getElementsByName("threejs_script") but this does not work. 

When I log the shmi.ctrl(".threejs_script") it appears that the object does exist.

RobotART_Walter_0-1683032366288.png

 

 I am aware that this has noting to do with webiq. But I am a bit lost.

Is there a way to render the threejs script inside the container I created via webiq?

Thank you so much!

1. document.getElementsByName("threejs_script") is looking for an HTML element that has an attribute with the name "threejs_script" - please see https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByName

2. shmi.ctrl(".threejs_script") - we are working with modern web technology here which is asynchronous so the script might not have been loaded. In any case this would not help as you have to change where the script renders the element - changing the position of the LocalScript will not make any difference here.

3. "Is there a way to render the threejs script inside the container I created via webiq?" - it all comes down to an HTML element where you have to insert the ThreeJS element. As WebIQ is completely web-based a container is simply a <div> HTML tag.

I can't help you any further as it is impossible to know what your code does without seeing the code utilizing ThreeJS

Hello,

This is the code containing the threejs objects. 
But I think you would need my full project. Is there an easy way to share this?

 

Nevermind, I have solved it with your hint "simply a <div> HTML tag".

I was thinking too complicated. I insterted a HTML widget with a new DIV. And inside the script I use this DIV. 

Easy as that.

Thank you for your help and time. Much appreciated. 

RobotART_Walter_0-1683038168469.png

 

 

The code does exactly what you're telling it to do: append the ThreeJS object to the end of the HTML document. I highly recommend getting to know JavaScript better - there are lots of free resources available, otherwise you'll run into such issues over and over again probably.

Remove this code:

let container = document.createElement( 'div' );
body.appendChild( container );

Wrap all of your code inside the module.run method (excluding the self.onDisable code) inside this code:

shmi.onReady(
    {
        controls: {
            cont: ".YOUR_CONTAINER_NAME"
        }
    }, function (resolved) {
        const myContainer = resolved.controls.cont,
            container = myContainer.element;

            // Put all your code in here (except onDisable)!
            // ...
            // ....


    }
);

Name your container where you want to add it YOUR_CONTAINER_NAME.

Your code also causes memory leaks as you're always creating a new listener when the LocalScript is executed and storing it in the variable tok, but never unsubscribe. Rename tok to subscriptionToken to fix this issue (see onDisable!)

As I wrote before everything is executed asynchronously so we have to wait until our container widget has been initialized! Only then ThreeJS can actually find the HTML element we want to use.

I haven't tested the code above, but it should work.

This code can wreak havoc - you now have created invalid HTML code - you cannot have two <body> elements in your HTML code! However, browsers are extremely tolerant, unfortunately. Whatever happens here is undefined. However, removing the <body></body> should work.

https://www.w3schools.com/tags/tag_body.asp

Also, this might work or might not work all depending on timings - this means that it might work on your (fast) system, but it might not work on other systems or only intermittently.

Therefore you should always use shmi.onReady to wait for widgets!

Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist