Brent M. Dingle, Ph.D. 2015 Game Design and Development Program Mathematics, Statistics and Computer Science University of Wisconsin - Stout HTML5 – Canvas.

Slides:



Advertisements
Similar presentations
Chapter 3 – Web Design Tables & Page Layout
Advertisements

Internet Services and Web Authoring (CSET 226) Lecture # 5 HyperText Markup Language (HTML) 1.
HTML5 CANVAS.  Rectangles  Arcs  Lines  Features we won’t look at  Images  Drag and drop  Animation  Widely supported DRAWING PICTURES IN HTML.
Cascading Style Sheets
Floating Elements CS380.
Introduction to JavaScript Module 1 Client Side JS Programming M-GO Sponsored By
HTML 5 Previous version: HTML4.01, 1999 Still work in progress, most browsers support some of it.
Programming games More drawing. Text. Radian measure. Faces. Homework: Do your own drawings. Create index.html file. Upload work.
CM143 - Web Week 2 Basic HTML. Links and Image Tags.
CANVAS Fundamentals. What is Canvas? Canvas is a medium for oil painting. One of the earliest oils on canvas is a French Madonna from Canvas is.
JavaScript Basics JavaScript: What Is it? – A browser-based language – An object-based language – No relation to Java 1.
Chapter 14 Introduction to HTML
Canvas, SVG, Workers Doncho Minkov Telerik Corporation
Javascript & HTML5 Intro. Why Javascript? Next big thing in online games Flash is slowly on its way out Can be coded via any text editor, flash costs.
JQuery CS 268. What is jQuery? From their web site:
Web Programming Language Week 13 Ken Cosh HTML 5.
IT Engineering I Instructor: Rezvan Shiravi
JQuery Page Slider. Our goal is to get to the functionality of the Panic Coda web site.Panic Coda web site.
ITP 104.  While you can do things like this:  Better to use styles instead:
Eat Your Words! Creating webpage Layout. CONTENT The Layout of a Webpage HEADER FOOTER Each of these sections represents a ‘div’ (or divider). By linking.
HTML, HTML5 Canvas, JavaScript PART 3 LOOKING MORE CLOSELY AT FULL BLOWN GAME DESIGN A PATTERNED BACKGROUND (CREATED WTIH LOOPS) WITH A MOVING OBJECT CHALLENGES.
Programming games Recap. Upload work (Filezilla, index.html). Drawing lines. Drawing stars. Homework: Drawing exercises.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
 This presentation introduces the following: › 3 types of CSS › CSS syntax › CSS comments › CSS and color › The box model.
Getting Started with Canvas IDIA Spring 2013 Bridget M. Blodgett.
Session: 17. © Aptech Ltd. 2Canvas and JavaScript / Session 17  Describe Canvas in HTML5  Explain the procedure to draw lines  Explain the procedure.
Element. The element Used to dynamically draw graphics using javascript. Capable of drawing paths, circles, rectangles, text, and images.
Programming Games Show project. Refresher on coordinates. Scaling, translation. HTML5 logo. Refresher on animation. Bouncing ball. Homework: Do your own.
Brent M. Dingle, Ph.D Game Design and Development Program Mathematics, Statistics and Computer Science University of Wisconsin - Stout Filtering.
1 Graphics CSCI 343, Fall 2015 Lecture 2 Introduction to HTML, JavaScript and WebGL.
Brent M. Dingle, Ph.D Game Design and Development Program Mathematics, Statistics and Computer Science University of Wisconsin - Stout Quick Review.
Brent M. Dingle, Ph.D Game Design and Development Program Mathematics, Statistics and Computer Science University of Wisconsin - Stout HTML5 – Canvas.
CS 174: Web Programming October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Digital Image Processing
Brent M. Dingle, Ph.D Game Design and Development Program Mathematics, Statistics and Computer Science University of Wisconsin - Stout HTML5 – Canvas.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
Brent M. Dingle, Ph.D Game Design and Development Program Mathematics, Statistics and Computer Science University of Wisconsin - Stout Edge Detection.
Brent M. Dingle, Ph.D Game Design and Development Program Mathematics, Statistics and Computer Science University of Wisconsin - Stout Edge Detection:
Programming games Context of what we are doing. Drawing on canvas. Homework: [Complete coin toss examples.] Do your own drawings. Upload files to website.
CS543: WEB APPLICATION PROGRAMMING Lab 1: HTML tags & SW installation Computer Science Department.
Digital Image Processing
HTML 5 Hands On By Rohit Ghatol
Programming games Review concepts. Crooked coin toss. Drawing on canvas. Homework: Complete [static] drawings. Upload files to website.
Brent M. Dingle, Ph.D Game Design and Development Program Mathematics, Statistics and Computer Science University of Wisconsin - Stout Image Processing.
School of Computing and Information Systems RIA Animation, part I.
07 – HTML5 Canvas (1) Informatics Department Parahyangan Catholic University.
HTML 5 (Part 1) – Start from SCRATCH. HTML 5 – Start from SCRATCH.
COM621: Advanced Interactive Web Development Lecture 6 – JavaScript (cont.)
HTML5 ProgLan HTML5 Making a Game on the Canvas Part 1.
Brent M. Dingle, Ph.D Game Design and Development Program Mathematics, Statistics and Computer Science University of Wisconsin - Stout Color Image.
That Gauge is COOL! 
What is a Function Expression?
Internet of the Past.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Canvas Notes
Web Programming Language
Image Warping (Geometric Transforms)
מדעי המחשב ורובוטיקה בחט"ב
ISC440: Web Programming II Ch14: HTML5 Canvas
The Canvas.
Introduction to Web Page Design
Drawing Graphics in JavaScript
Context of what we are doing.
HTML5 – Canvas JavaScript Simple Drawing
Graphics Canvas.
CSc 337 Lecture 20: Canvas.
Programming games Share your plans for your virtual something.
JavaScript – Let’s Draw!
Presentation transcript:

Brent M. Dingle, Ph.D Game Design and Development Program Mathematics, Statistics and Computer Science University of Wisconsin - Stout HTML5 – Canvas JavaScript Simple Drawing Image Processing

Lecture Objectives Identify a text editor for coding Identify a web browser for testing Provide Examples – Basic HTML5 canvas – Simple Drawing and Manipulation in JavaScript

What this is Not To complete your projects – You must learn more about HTML5 and JavaScript than what is about to be shown This is an “on-your-own” activity – Instructor can help, but you must try on your own A prereq to this course is CS 244 – So you have programmed before – This stuff is “easy” compared to that =) – Likewise on the math topics In Sum: The following is just a place to start – More examples will follow throughout the course

Suggested Text Editor Notepad works in Windows TextEdit works on Macs vi and emacs work on LINUX Notepad++ has worked well for students in the past – – Let the instructor know outside of class if obtaining an editor will be problematic for you » You may use what you are comfortable with, but it must result in clean and easy to read (by humans) HTML and javaScript files when the files are opened using editors such as the above

Web Browsers Instructor tends to use Firefox – used for grading Others that you should test your stuff with – Safari, Chrome, IE… variations of mobile devices » Let the instructor know outside of class if testing using Firefox will be problematic for you

Drawing a Rectangle: HTML Your browser does NOT support canvas! HTML File: simpleRectangle.html Declares the Type of document being defined

Drawing a Rectangle: HTML Your browser does NOT support canvas! HTML File: simpleRectangle.html Declares the Start of the document and the language it is written in/for

Drawing a Rectangle: HTML Your browser does NOT support canvas! HTML File: simpleRectangle.html Header portion of your document I suggest using this to “include” your javascript file(s) …Rather than writing code in the html file itself…

Drawing a Rectangle: HTML Your browser does NOT support canvas! HTML File: simpleRectangle.html Body portion of your document with a “division” declared

Drawing a Rectangle: HTML Your browser does NOT support canvas! HTML File: simpleRectangle.html Creates a canvas on your webpage The id=“myCanvas” is important as your javaScript code will use that id Width and Height are also important to note, They define the size of the canvas display (in pixels) on the webpage

Drawing a Rectangle: HTML Your browser does NOT support canvas! HTML File: simpleRectangle.html The end of your document This HTML file is typical of the ones you will use for this class Most of the changes will be in the size of the canvas, any words/text you want on the page and the rest will (typically) be in the javaScript code file(s)…

Drawing a Rectangle: HTML Your browser does NOT support canvas! HTML File: simpleRectangle.html Questions on the HTML ?

Drawing a Rectangle: JS var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); ctx.fillStyle = "#aaaaaa"; ctx.fillRect(0, 0, 300, 200); // draw a grey background ctx.strokeRect(20, 10, 80, 60); // draw a black rectangle }, }; // end theProgram variable window.onload = function() { theProgram.Main(); }; JavaScript File: simpleRectangle.js theProgram is a Singleton variable object Useful for isolating your functions and ‘global’ variables from other “stuff” that might be on the webpage

Drawing a Rectangle: JS var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); ctx.fillStyle = "#aaaaaa"; ctx.fillRect(0, 0, 300, 200); // draw a grey background ctx.strokeRect(20, 10, 80, 60); // draw a black rectangle }, }; // end theProgram variable window.onload = function() { theProgram.Main(); }; JavaScript File: simpleRectangle.js Main is a ‘member’ function of theProgram This helps identify the entry point function which is first called after the webpage loads

Drawing a Rectangle: JS var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); ctx.fillStyle = "#aaaaaa"; ctx.fillRect(0, 0, 300, 200); // draw a grey background ctx.strokeRect(20, 10, 80, 60); // draw a black rectangle }, }; // end theProgram variable window.onload = function() { theProgram.Main(); }; JavaScript File: simpleRectangle.js window.onload is automatically called by the browser This event occurs “after” the html page has been loaded including all the page’s content: images, css, scripts…

Drawing a Rectangle: JS var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); ctx.fillStyle = "#aaaaaa"; ctx.fillRect(0, 0, 300, 200); // draw a grey background ctx.strokeRect(20, 10, 80, 60); // draw a black rectangle }, }; // end theProgram variable window.onload = function() { theProgram.Main(); }; JavaScript File: simpleRectangle.js So… This function’s code is what will be executed when the page finishes loading

Drawing a Rectangle: JS var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); ctx.fillStyle = "#aaaaaa"; ctx.fillRect(0, 0, 300, 200); // draw a grey background ctx.strokeRect(20, 10, 80, 60); // draw a black rectangle }, }; // end theProgram variable window.onload = function() { theProgram.Main(); }; JavaScript File: simpleRectangle.js Gets a “handle” to the canvas element on the webpage It is important that the HTML file has a canvas element with id = “myCanvas” If not, then the variable theCanvas will be assigned a non-value and the rest of the code will not run

Drawing a Rectangle: JS var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); ctx.fillStyle = "#aaaaaa"; ctx.fillRect(0, 0, 300, 200); // draw a grey background ctx.strokeRect(20, 10, 80, 60); // draw a black rectangle }, }; // end theProgram variable window.onload = function() { theProgram.Main(); }; JavaScript File: simpleRectangle.js Gets a “handle” to the drawing context of theCanvas This context defines where “stuff” will actually be drawn

Drawing a Rectangle: JS var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); ctx.fillStyle = "#aaaaaa"; ctx.fillRect(0, 0, 300, 200); // draw a grey background ctx.strokeRect(20, 10, 80, 60); // draw a black rectangle }, }; // end theProgram variable window.onload = function() { theProgram.Main(); }; JavaScript File: simpleRectangle.js Sets the drawing fillstyle to be grey The numbers are hex: RRGGBB

Drawing a Rectangle: JS var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); ctx.fillStyle = "#aaaaaa"; ctx.fillRect(0, 0, 300, 200); // draw a grey background ctx.strokeRect(20, 10, 80, 60); // draw a black rectangle }, }; // end theProgram variable window.onload = function() { theProgram.Main(); }; JavaScript File: simpleRectangle.js Draws a filled rectangle with diagonally opposed corners at (0, 0) and (300, 200) In this case this matches the size of the canvas (hence background) The fill color is whatever the current fillstyle is set to

Drawing a Rectangle: JS var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); ctx.fillStyle = "#aaaaaa"; ctx.fillRect(0, 0, 300, 200); // draw a grey background ctx.strokeRect(20, 10, 80, 60); // draw a black rectangle }, }; // end theProgram variable window.onload = function() { theProgram.Main(); }; JavaScript File: simpleRectangle.js Draws the edges of a rectangle with diagonally opposed corners at (20, 10) and (80, 60) The line color is whatever the current strokestyle is set to (it defaults to = black)

Drawing a Rectangle: JS var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); ctx.fillStyle = "#aaaaaa"; ctx.fillRect(0, 0, 300, 200); // draw a grey background ctx.strokeRect(20, 10, 80, 60); // draw a black rectangle }, }; // end theProgram variable window.onload = function() { theProgram.Main(); }; JavaScript File: simpleRectangle.js Questions on the JavaScript?

Challenges for Home Code is available online (zipped) Download and attempt the following – Change the background rectangle to be size: 0, 0, 300, 100 – Use ctx.scale(s1, s2) to scale the drawn rectangle to be bigger – Alter the drawn rectangle to be filled RED

Rotating a Rectangle: HTML loopRotate.html Your browser does NOT support canvas!

Rotating a Rectangle: HTML loopRotate.html Your browser does NOT support canvas! JavaScript filename changed from previous example

Rotating a Rectangle: HTML loopRotate.html Your browser does NOT support canvas! Canvas size changed from previous example

Rotating a Rectangle: HTML loopRotate.html Your browser does NOT support canvas! Questions on the HTML ?

Rotating a Rectangle: JS var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); // grey background ctx.fillStyle="#aaaaaa"; ctx.fillRect(0, 0, 320, 240); // for subsequent boxes: ctx.fillStyle="#00ffff"; ctx.lineWidth = 3; loopRotate.js window.onload = function() { theProgram.Main(); }; Much the same as in the previous example

Rotating a Rectangle: JS var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); // grey background ctx.fillStyle="#aaaaaa"; ctx.fillRect(0, 0, 320, 240); // for subsequent boxes: ctx.fillStyle="#00ffff"; ctx.lineWidth = 3; loopRotate.js window.onload = function() { theProgram.Main(); }; We will be drawing a series of rectangles Each will be colored light-blue / cyan with a thick border line

setTransform var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); // grey background ctx.fillStyle="#aaaaaa"; ctx.fillRect(0, 0, 320, 240); // for subsequent boxes: ctx.fillStyle="#00ffff"; ctx.lineWidth = 3; loopRotate.js for (var i = 0; i < 50; i++) { var t = i / 50.0; // time parameter [0..1) ctx.setTransform(1,0,0,1,0,0); // reset to identity ctx.translate( * t, * t); ctx.scale(1 + t * 1.5, 1 + t * 1.5); ctx.rotate(3.5 * t); ctx.beginPath(); ctx.rect(0, 0, 30, 20); ctx.stroke(); ctx.fill(); ctx.closePath(); } }, }; // end theProgram variable window.onload = function() { theProgram.Main(); };

setTransform var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); // grey background ctx.fillStyle="#aaaaaa"; ctx.fillRect(0, 0, 320, 240); // for subsequent boxes: ctx.fillStyle="#00ffff"; ctx.lineWidth = 3; loopRotate.js for (var i = 0; i < 50; i++) { var t = i / 50.0; // time parameter [0..1) ctx.setTransform(1,0,0,1,0,0); // reset to identity ctx.translate( * t, * t); ctx.scale(1 + t * 1.5, 1 + t * 1.5); ctx.rotate(3.5 * t); ctx.beginPath(); ctx.rect(0, 0, 30, 20); ctx.stroke(); ctx.fill(); ctx.closePath(); } }, }; // end theProgram variable window.onload = function() { theProgram.Main(); }; Iterative for-loop i counts from 0 to 49

setTransform var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); // grey background ctx.fillStyle="#aaaaaa"; ctx.fillRect(0, 0, 320, 240); // for subsequent boxes: ctx.fillStyle="#00ffff"; ctx.lineWidth = 3; loopRotate.js for (var i = 0; i < 50; i++) { var t = i / 50.0; // time parameter [0..1) ctx.setTransform(1,0,0,1,0,0); // reset to identity ctx.translate( * t, * t); ctx.scale(1 + t * 1.5, 1 + t * 1.5); ctx.rotate(3.5 * t); ctx.beginPath(); ctx.rect(0, 0, 30, 20); ctx.stroke(); ctx.fill(); ctx.closePath(); } }, }; // end theProgram variable window.onload = function() { theProgram.Main(); }; A ‘time’ parameter t controls translation and rotation amounts

setTransform var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); // grey background ctx.fillStyle="#aaaaaa"; ctx.fillRect(0, 0, 320, 240); // for subsequent boxes: ctx.fillStyle="#00ffff"; ctx.lineWidth = 3; loopRotate.js for (var i = 0; i < 50; i++) { var t = i / 50.0; // time parameter [0..1) ctx.setTransform(1,0,0,1,0,0); // reset to identity ctx.translate( * t, * t); ctx.scale(1 + t * 1.5, 1 + t * 1.5); ctx.rotate(3.5 * t); ctx.beginPath(); ctx.rect(0, 0, 30, 20); ctx.stroke(); ctx.fill(); ctx.closePath(); } }, }; // end theProgram variable window.onload = function() { theProgram.Main(); }; This is interesting

ctx.setTransform(1,0,0,1,0,0); // reset to identity setTransform loopRotate.js var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); // grey background ctx.fillStyle="#aaaaaa"; ctx.fillRect(0, 0, 320, 240); // for subsequent boxes: ctx.fillStyle="#00ffff"; ctx.lineWidth = 3; for (var i = 0; i < 50; i++) { var t = i / 50.0; // time parameter [0..1) ctx.setTransform(1,0,0,1,0,0); // reset to identity ctx.translate( * t, * t); ctx.scale(1 + t * 1.5, 1 + t * 1.5); ctx.rotate(3.5 * t); ctx.beginPath(); ctx.rect(0, 0, 30, 20); ctx.stroke(); ctx.fill(); ctx.closePath(); } }, }; // end theProgram variable window.onload = function() { theProgram.Main(); }; This is interesting

setTransform loopRotate.js ctx.setTransform(1,0,0,1,0,0); // reset to identity

setTransform loopRotate.js ctx.setTransform(1,0,0,1,0,0); // reset to identity Resets the transform matrix to the identity matrix: Its parameter order is a little strange: setTransform(A, b, c, D, e, f) Conceptually: AScales the drawings horizontally bSkews the drawings horizontally cSkews the drawings vertically DScales the drawings vertically eMoves the drawings horizontally fMoves the drawings vertically

Transforms in General setTransform() – good for clearing transform matrix back to identity rotate(), translate(), and scale() – tend to be more intuitive to use – HOWEVER they are cumulative so ORDER matters and what you ALREADY DID matters – thus resetting transform matrix back to identity becomes important

translate, scale, rotate loopRotate.js var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); // grey background ctx.fillStyle="#aaaaaa"; ctx.fillRect(0, 0, 320, 240); // for subsequent boxes: ctx.fillStyle="#00ffff"; ctx.lineWidth = 3; for (var i = 0; i < 50; i++) { var t = i / 50.0; // time parameter [0..1) ctx.setTransform(1,0,0,1,0,0); // reset to identity ctx.translate( * t, * t); ctx.scale(1 + t * 1.5, 1 + t * 1.5); ctx.rotate(3.5 * t); ctx.beginPath(); ctx.rect(0, 0, 30, 20); ctx.stroke(); ctx.fill(); ctx.closePath(); } }, }; // end theProgram variable window.onload = function() { theProgram.Main(); }; Examples of translate() scale() rotate()

translate, scale, rotate loopRotate.js var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); // grey background ctx.fillStyle="#aaaaaa"; ctx.fillRect(0, 0, 320, 240); // for subsequent boxes: ctx.fillStyle="#00ffff"; ctx.lineWidth = 3; for (var i = 0; i < 50; i++) { var t = i / 50.0; // time parameter [0..1) ctx.setTransform(1,0,0,1,0,0); // reset to identity ctx.translate( * t, * t); ctx.scale(1 + t * 1.5, 1 + t * 1.5); ctx.rotate(3.5 * t); ctx.beginPath(); ctx.rect(0, 0, 30, 20); ctx.stroke(); ctx.fill(); ctx.closePath(); } }, }; // end theProgram variable window.onload = function() { theProgram.Main(); }; Begins or resets a path Needed here to make certain each box is fully drawn

translate, scale, rotate loopRotate.js var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); // grey background ctx.fillStyle="#aaaaaa"; ctx.fillRect(0, 0, 320, 240); // for subsequent boxes: ctx.fillStyle="#00ffff"; ctx.lineWidth = 3; for (var i = 0; i < 50; i++) { var t = i / 50.0; // time parameter [0..1) ctx.setTransform(1,0,0,1,0,0); // reset to identity ctx.translate( * t, * t); ctx.scale(1 + t * 1.5, 1 + t * 1.5); ctx.rotate(3.5 * t); ctx.beginPath(); ctx.rect(0, 0, 30, 20); ctx.stroke(); ctx.fill(); ctx.closePath(); } }, }; // end theProgram variable window.onload = function() { theProgram.Main(); }; Closes a path Needed here to make certain each box is fully drawn

translate, scale, rotate loopRotate.js var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); // grey background ctx.fillStyle="#aaaaaa"; ctx.fillRect(0, 0, 320, 240); // for subsequent boxes: ctx.fillStyle="#00ffff"; ctx.lineWidth = 3; for (var i = 0; i < 50; i++) { var t = i / 50.0; // time parameter [0..1) ctx.setTransform(1,0,0,1,0,0); // reset to identity ctx.translate( * t, * t); ctx.scale(1 + t * 1.5, 1 + t * 1.5); ctx.rotate(3.5 * t); ctx.beginPath(); ctx.rect(0, 0, 30, 20); ctx.stroke(); ctx.fill(); ctx.closePath(); } }, }; // end theProgram variable window.onload = function() { theProgram.Main(); }; Without the beginPath and closePath The boxes are all drawn ‘at the same time’ Resulting in a different image

translate, scale, rotate loopRotate.js var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); // grey background ctx.fillStyle="#aaaaaa"; ctx.fillRect(0, 0, 320, 240); // for subsequent boxes: ctx.fillStyle="#00ffff"; ctx.lineWidth = 3; for (var i = 0; i < 50; i++) { var t = i / 50.0; // time parameter [0..1) ctx.setTransform(1,0,0,1,0,0); // reset to identity ctx.translate( * t, * t); ctx.scale(1 + t * 1.5, 1 + t * 1.5); ctx.rotate(3.5 * t); ctx.beginPath(); ctx.rect(0, 0, 30, 20); ctx.stroke(); ctx.fill(); ctx.closePath(); } }, }; // end theProgram variable window.onload = function() { theProgram.Main(); }; Draws a rectangle with opposing diagonal corners of (0, 0) and (30, 20) Note: It will be transformed based on current Transform Matrix

translate, scale, rotate loopRotate.js var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); // grey background ctx.fillStyle="#aaaaaa"; ctx.fillRect(0, 0, 320, 240); // for subsequent boxes: ctx.fillStyle="#00ffff"; ctx.lineWidth = 3; for (var i = 0; i < 50; i++) { var t = i / 50.0; // time parameter [0..1) ctx.setTransform(1,0,0,1,0,0); // reset to identity ctx.translate( * t, * t); ctx.scale(1 + t * 1.5, 1 + t * 1.5); ctx.rotate(3.5 * t); ctx.beginPath(); ctx.rect(0, 0, 30, 20); ctx.stroke(); ctx.fill(); ctx.closePath(); } }, }; // end theProgram variable window.onload = function() { theProgram.Main(); }; Questions on the JavaScript?

Challenges for Home Code is available online (zipped) – Download it and experiment Try adding a ctx.transform() call to skew each rectangle before it is drawn Try using ctx.save() and ctx.store() to “push” and “pop” the transform matrix’s state – instead of explicitly resetting it to the identity Try other things – arcs, gradients, pixel manipulation, pattern fills

Questions? Beyond D2L – Examples and information can be found online at: Continue to more stuff as needed

Extra Reference Stuff Follows

Credits Much of the content derived/based on slides for use with the book: – Digital Image Processing, Gonzalez and Woods Some layout and presentation style derived/based on presentations by – Donald House, Texas A&M University, 1999 – Bernd Girod, Stanford University, 2007 – Shreekanth Mandayam, Rowan University, 2009 – Igor Aizenberg, TAMUT, 2013 – Xin Li, WVU, 2014 – George Wolberg, City College of New York, 2015 – Yao Wang and Zhu Liu, NYU-Poly, 2015 – Sinisa Todorovic, Oregon State, 2015 – Beej’s Bit Bucket / Tech and Programming Fun – w3schools.com