This will be a take-home exam, available in Oncourse Assignments Friday, Oct. 12, and due Monday, Oct. 15. You may use books and notes. You may use a computer and the Internet for reference and testing. You may not give or receive aid: your answers must be your own work. (You may ask the instructor for clarification of questions.)
You may return the exam through Oncourse (scan and upload attachment), FAX, or delivery of the paper to my office.
Question types include essay, short answer, sketching lines and curves, and writing short segments of JavaScript code. There are no multiple choice questions.
II. General Learning Objectives
By the time of the midterm, students should have acquired some knowledge about basic JavaScript programming, graphics concepts, and specific graphics know-how for the canvas.
A. Basic JavaScript Programming
Be able to describe the JavaScript language and compare it with another well-known language, such as Java.
Be able to use JavaScript to:
Declare variables; distinguish between global and local variables.
Use assignment and expressions.
Define and call functions; make use of closures, anonymous functions, and functions as first-class objects.
Use control statements for decisions and loops.
Use arrays.
Use simple objects (JSON level).
Follow safer practices for robust scripts.
Respond to user actions such as mouse clicks and keystrokes.
B. Graphics Concepts
Be able to
Explain color concepts including the RGB and HSL color models, alpha, and gradients.
Explain the concepts of images, patterns, and sprites.
Distinguish between stroke and fill.
Explain the concepts of lines, arcs, Bézier curves, open and closed paths.
Explain how animation works.
Explain the concept of a scene graph.
C. Canvas Know-How
Be able to
Create a canvas and access the 2D drawing context.
Draw lines, curves, text, and images on the canvas.
Create gradients and patterns; set fill and stroke styles with colors, gradients, and patterns.
Animate objects in a scene.
III. Specific Topics
Here follows a list of more specific learning outcomes and review questions, arranged chronologically by lesson.
1 Introduction
Learning Outcomes
Be able to
Explain the role of the HTML5 canvas and of SVG in web graphics.
Define the terms immediate mode, retained mode, scene graph.
Review Questions
Distinguish between an immediate mode and a retained mode graphics API.
2 JavaScript Fundamentals
Learning Outcomes
Be able to
Write the minimum required HTML tags for an HTML5 page with a script.
Write JavaScript comments.
Know when and how to declare variables.
Name and describe the three primitive types of JavaScript.
Write and evaluate JavaScript arithmetic expressions and boolean expressions.
Write and interpret control statements, including if, while, and for.
Explain the difference between === and ==, and between !== and !=.
Access the canvas and its 2D drawing context from JavaScript.
Distinguish between stroke and fill actions.
Explain the concept of a path, and distinguish between open and closed subpaths.
Write JavaScript to draw simple figures, including lines, rectangles, and arcs.
Review Questions
Distinguish between stroke and fill.
Write JavaScript statements to draw (and, if appropriate, fill):
A line.
An arc of 90 degrees.
A hexagon (6 sides).
4 JavaScript Functions
Learning Outcomes
Be able to
Define JavaScript functions with parameters and return values.
Explain the concepts of closures, nested functions, anonymous functions, functional parameters, functional values, functions as first-class objects, and why they are use
Review Questions
What is a closure, and why is it useful?
What is an anonymous function, and why is it useful?
5 Simple Canvas Animations
Learning Outcomes
Be able to
Explain how animation works.
Explain the concept of a callback function.
Set up a timeout or an interval timer.
Review Questions
Explain how animation works.
Assume that an animate function has been defined, with no parameters. Write a JavaScript statement that creates an interval timer, calling the animate function every 1/50 second, and stores the interval timer ID in a variable.
6 Colors and Gradients
Learning Outcomes
Be able to
Find and use a list of the color keywords that may be used as fill and stroke style values.
Explain the RGB and HSL color models.
Estimate the HSL parameters for a particular hue (described in words, such as “yellow-green”), saturation, and lightness, or for a color that you are shown; or, conversely, explain the HSL numbers of a color in plain English that a typical small business owner could understand.
Explain the alpha parameter is in the RGBA and HSLA color models.
Describe some effects that can be produced using gradients, or some objects or phenomena that are suitably rendered with gradients.
Explain the concepts of linear gradient and radial gradient.
Write code to set the fill or stroke style to a particular color or gradient.
Review Questions
How would you describe the color hsl(30, 100%, 12%) in plain English?
Explain the fourth parameter in rgba(0, 0, 0, 0.25).
Write code to draw a rectangle, filling it with a linear gradient that flows from red on the left to yellow on the right.
7 JavaScript Objects
Learning Outcomes
Be able to
Explain what an object is in JavaScript.
Explain how JavaScript objects are different from Java objects.
Create an object using object literal notation.
Access and set properties of an object, using dot notation, and using bracket notation.
Write a for loop that iterates through all the properties of an object.
Putting a function into an object, making a method.
Define a function to make an object.
Use a certain built-in object to get the number π, random numbers, and trigonometric functions such as sine and cosine.
Review Questions
Write a statement that makes the variable mydog refer to a new object with the properties name = “Mutt”, age = 6, hungry = true, using object literal notation.
Write a statement that changes the name property of mydog to “Sam”.
Write a statement that writes the name property of mydog to the console log.
Write a statement that writes a random real number between 0 and 12 to the console log.
8 JavaScript Arrays
Learning Outcomes
Be able to
Create an array with an array literal expression.
Create an array with 50 undefined elements.
Given an array, access and set the values of its elements.
Determine the length (number of elements in) an array.
Write a loop to iterate through an array.
Compare JavaScript arrays with Java arrays (i.e., tell how they are similar and how they are different).
Review Questions
Write a JavaScript statement to create an array with the elements “bat”, “cat”, and “rat”, and store it in the variable varmints.
Write a loop that prints every element of the array gems on the console log.
Increase the value of the 5th element of nums (an array of numbers) by 25.
Compare JavaScript arrays with Java arrays.
9 Controls and Events
Learning Outcomes
Be able to
Explain the concepts of events (such as keyboard and mouse events) and callbacks.
Create HTML form elements such as button inputs.
Define and set up callback functions for handling mouse events and keyboard events for the canvas or for control elements such as button inputs.
Distinguish between screen coordinates and client coordinates of a mouse event.
Name and describe the three events that are produced when a mouse button is clicked, and similarly the three events when a character is typed on the keyboard.
Review Questions
Name and describe the three events that are produced when a mouse button is clicked.
10 Images and Patterns
Learning Outcomes
Be able to
Explain why, when an Image object is created, we set a callback for its load event instead of immediately executing instructions to deal with the image.
Write statements to draw an image on the canvas (a) simply; (b) scaled; (c) partially.
Explain the concept of a pattern used as “paint” for the stroke or fill.
Write statements to create a pattern and set it as the stroke or fill style.
Explain the concepts and uses of sprites and pixel buffers.
Review Questions
Assume that the variable im refers to an Image object which is known to be loaded. Write a statement to draw the image on the canvas at the point (50, 100).
Explain the concept of a pattern used as “paint” for the stroke or fill.
11 Strings
Learning Outcomes
Be able to
Write a string literal in JavaScript.
Explain, interpret, and use the JavaScript string methods split, charAt, and slice.
Review Questions
What does this do? "ace,ten,jack".split(",")
Write a string slice expression that extracts the characters “tap” from the string “catapult”.
12 Drawing Text
Learning Outcomes
Be able to
Write a JavaScript statement that draws some text (i.e., a string) at a given point.
Explain why, when drawing text at a small font size, you should normally fill and not stroke.
Explain the differences between the text alignment values “left”, “right”, and “center”, and between the text baseline values “top”, “middle”, “bottom”.
Write statements that find the width of text at the current font setting.
Review Questions
Write a JavaScript statement that draws the text “Oily” on the canvas starting at the point (400, 300).
If “Oily” is drawn starting at (400, 300), with text alignment “left” and baseline “bottom”, and its width at the current font setting is 40 pixels, then what is the x coordinate at the end of the drawing?
13 Robust JavaScript
Learning Outcomes
Be able to
Work around or avoid misfeatures of the JavaScript language to make scripts more robust, including the lack of a module system, implicit type conversions; and the usage of “use strict” and JSLint.
Use the try/catch statement to handle exceptions.
Review Questions
Explain how to protect your script from unfortunate interactions with other scripts (used on the same web page) in the absence of a proper module system.
What is JSLint?
14 More about Lines and Curves
Learning Outcomes
Be able to
Set the line width, caps, and joins properties.
Draw quadratic and cubic Bézier curves.
Review Questions
Write the JavaScript statement to set the line width to 8.
Write JavaScript statements to draw a quadratic curve from (0, 100), “through” (which really means “influenced by” (50, 200), ending at (100, 100).