In the top of the ui.js, the connection to the <div>s are set like this:
// Add the HTML
this.div = document.createElement('div');
this.div.classList.add('container');
this.div.classList.add('body');
within the structure I should be able to write:
var midWayPhotoURL = this.div.querySelector('.imgNrm');
or adjusted:
// Add the HTML
this.div = document.createElement('div');
this.div.classList.add('container');
this.div.classList.add('body');
this.div.classList.add('img'); // ?? is there another way to add img ??
var midWayPhotoURL = this.img.querySelector('.imgNrm');
None of the methods work for me :-/
{Have off course tried the forum already; where this came closest but without help: How to Html Images}
hmm … it looks like we are ending in a ‘classic pitfall’ … in this case we still have ‘the little anomoly’ in regards to JavaScript in general versus PlayCanvas:
When I aim to emphasize:
// Add the HTML
this.div = document.createElement('div');
this.div.classList.add('container');
this.div.classList.add('body');
I recognize that I (even) might have to set the img as a div:
Here you are adding classes to an element, you aren’t adding actual DOM elements. Check and study the links @yaustar provided to better understand how JS works.
// Copy and pasted from https://www.codegrepper.com/code-examples/javascript/how+to+insert+image+in+javascript+code
var img = document.createElement('img');
img.src = 'https://media.geeksforgeeks.org/wp-content/uploads/20190529122828/bs21.png';
document.body.appendChild(img); // Fixed the bad code from the example
// Add style to position the element
img.style.position = 'absolute';
img.style.top = '0px';
img.style.left = '0px';
img.style.zIndex = 1000;
For this specifically, it would adding a div element, giving it a class, creating a img element, giving it a class and adding the img element to the div.
Assuming that you have the HTML already and you just want to get the img element with the class name .imgNrm, the code you’ve posted works in a clean example: https://playcanvas.com/editor/scene/1251687
Ui.prototype.initialize = function () {
// create STYLE element
var style = document.createElement('style');
// append to head
document.head.appendChild(style);
style.innerHTML = this.css.resource || '';
// Add the HTML
this.div = document.createElement('div');
this.div.classList.add('container');
this.div.innerHTML = this.html.resource || '';
// append to body
// can be appended somewhere else
// it is recommended to have some container element
// to prevent iOS problems of overfloating elements off the screen
document.body.appendChild(this.div);
// Add an image to the image element
var imgElement = document.querySelector('.imgNrm');
imgElement.src = "https://media.geeksforgeeks.org/wp-content/uploads/20190529122828/bs21.png";
};
ok, my track record will show that I tend to have no problems with (vitually) all the JS I write to regular homepages, and I have had a lot of challenges with finding knowledge sources on DOM alone :-/ … so no wonder I tend to get confused.
{so, my new understanding of JS in PlayCanvas: "It differs from JS in the sense, that JS DOM some times needs to be taken in - [one have to add classes to the element at the top] "}
before commenting further (this video seem to be very useful*, with its connection and use of the DOM objects visible within the console {console(document.dir) and so on})
*) tangible for me at least - again I must have been ‘unlucky’ not to have found enough sources on DOM.