The DOM
DOM = Document Object Model
This lab explores how you can use JavaScript to modify web page content.
Start with this base code: lab15base
Slider
Some useful functions and methods for working with sliders:
var aSlider;
– declare a global variable to contain a slider objectaSlider = createSlider(smallestValue, largestValue, startingValue);
– create a new slideraSlider.size(sliderWidth, AUTO);
– optional, set the width of the slideraSlider.position(x, y);
– optional, put the slider somewhere in your canvasvar v = aSlider.value()
– get the current slider value
Programming Exercise
Make a canvas full of vertical lines, separated by an adjustable amount controlled by a slider. (Your TA should demonstrate an example.)
Image on Web Page
Some useful functions and methods for working with images:
var img;
– declare a global variable to contain an image objectimg = createImg("https://imgur.com/lmz4IcH.png");
– create an image from URLimg.position(x, y);
– put the image on web page, use x = -1000 to hide imageimg.style("transform", "scale(-1, 1)");
– optional, flip the image horizontallyimg.style("transform", "scale(1, 1)");
– optional, unflip the image horizontally
Programming Exercise
Put an image on your web page. Move it or hide it randomly from time to time. (Your TA should demonstrate an example.)
Changing Text on Web Page
To modify text on a web page you have to find the text. By adding an ID to the HTML tag, you can make the text easy to search. For example, the HTML file in the base code replaced <h2>About</h2>
(a typical HTML heading) with <h2 id="about">About</h2>
(the same heading, but now with an ID attribute with value “about”).
Some useful functions and methods for working with ID’s and text:
var anElem;
– declare a global variable to contain a document elementanElem = document.getElementById("about");
– search by ID for name “about”anElem.style.color = "red";
– set the color attribute of a text elementanElem.innerHTML = "Course Description!";
– change the HTML inside a document element
Programming Exercise
Animate text in a web page by changing color, innerHTML, or other attributes. (Your TA should demonstrate an example.)