JavaScript – Let’s Draw!

Slides:



Advertisements
Similar presentations
HTML5 CANVAS.  Rectangles  Arcs  Lines  Features we won’t look at  Images  Drag and drop  Animation  Widely supported DRAWING PICTURES IN HTML.
Advertisements

JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
กระบวนวิชา CSS. What is CSS? CSS stands for Cascading Style Sheets Styles define how to display HTML elements Styles were added to HTML 4.0 to.
 By carefully incrementing the X and/or Y values of our set of lines, we can create a game board for a boat game.  Lines that go from left to right are.
Creating Tables Text Tables -created by using preformatted tags. Graphical Tables - created using Table Structure with HTML.
HTML 5 Previous version: HTML4.01, 1999 Still work in progress, most browsers support some of it.
Aim: Use the given examples to record examples and our own ideas in our journal 1.Write technical examples in journal and/or participate in full.
Text, Masks, and Live Effects – Lesson 41 Text, Masks, and Live Effects Lesson 4.
Introduction to Shape Programming When we make geometric shapes with computers, we have to give specific numbers to tell the computer exactly what to do.
CSS (Cascading Style Sheets): How the web is styled Create Rules that specify how the content of an HTML Element should appear. CSS controls how your web.
Canvas, SVG, Workers Doncho Minkov Telerik Corporation
ITP 104.  While you can do things like this:  Better to use styles instead:
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.
Brent M. Dingle, Ph.D Game Design and Development Program Mathematics, Statistics and Computer Science University of Wisconsin - Stout HTML5 – Canvas.
More Basic HTML. Add spacing (single & double space) Save Refresh Add horizontal rule Add comments Add styles Add headings Add features Add alignments.
Getting Started with Canvas IDIA Spring 2013 Bridget M. Blodgett.
HTML 5 Tutorial Chapter 5 Canvas. Canvas The tag is used to display graphics. The Canvas is a rectangular area we control each and every pixel of it.
HTML5 CANVAS. SAVING INFORMATION BETWEEN FUNCTION CALLS.
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.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Introduction to Microsoft publisher
Making Python Pretty!. How to Use This Presentation… Download a copy of this presentation to your ‘Computing’ folder. Follow the code examples, and put.
CS 174: Web Programming October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
Programming games Context of what we are doing. Drawing on canvas. Homework: [Complete coin toss examples.] Do your own drawings. Upload files to website.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 19 – Dynamic HTML: Structured Graphics ActiveX Control Outline 19.1Introduction 19.2Shape.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 - Dynamic HTML: Structured Graphics ActiveX Control Outline 17.1 Introduction 17.2 Shape Primitives.
JavaScript Challenges Answers for challenges
Programming games Review concepts. Crooked coin toss. Drawing on canvas. Homework: Complete [static] drawings. Upload files to website.
Session 2: Basic HTML HTML Coding Spring 2009 The LIS Web Team Presents.
School of Computing and Information Systems RIA Animation, part I.
07 – HTML5 Canvas (1) Informatics Department Parahyangan Catholic University.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
HTML 5 (Part 1) – Start from SCRATCH. HTML 5 – Start from SCRATCH.
That Gauge is COOL! 
HTML Elements with JavaScript and DOM Events
What is a Function Expression?
MOM! Phineas and Ferb are … Aims:
Pixels, Colors and Shapes
Internet of the Past.
CSS Layouts: Positioning and Navbars
CSS Layouts: Grouping Elements
CMPE 280 Web UI Design and Development September 12 Class Meeting
Flash Interface, Commands and Functions
Canvas Notes
Advanced CSS BIS1523 – Lecture 20.
Python: Simple Graphics and Event-driven Programming
MS PowerPoint 2010 Week 2.
Objectives Create a figure box Add a background image
ISC440: Web Programming II Ch14: HTML5 Canvas
The Canvas.
A (gentle???) Introduction to JavaScript
Unit 11 – PowerPoint Interaction
Introduction to TouchDevelop
Chapter 17 - Dynamic HTML: Structured Graphics ActiveX Control
گزارش فعالیت سه ماهه دبستان ابن سینا
More Basic HTML 2 assignments: 1—complete the worksheet
Drawing Graphics in JavaScript
Topics Graphical User Interfaces Using the tkinter Module
CSc 337 Lecture 21: Canvas.
Context of what we are doing.
Graphics Canvas.
Processing and Drawing CSE 120 Winter 2019
CSc 337 Lecture 20: Canvas.
Chapter 1 Introducing Small Basic
CSc 337 Lecture 5: Grid layout.
CSc 337 Lecture 19: Canvas.
Presentation transcript:

JavaScript – Let’s Draw! Use that canvas, play some games

Goal Learn to about coordinate system Learn to draw lines, rectangles and arc/circles Learn to use variables to control size etc. We’ll cover all the drawing commands, then you’ll have a chance to explore

Start with a Canvas CANVAS is an HTML element The canvas is a container for graphics (appears like a box) Specify the width and height and other properties of the canvas in HTML Actual drawing is done in JavaScript <canvas id="myCanvas" width="200" height="100" style="border:1px solid #000000;"> </canvas> id is needed so JavaScript can find it width/height specify size in pixels style is optional inside JavaScript, canvas is an OBJECT that contains values (soon we’ll see width & height) and also methods (see below) // Find the canvas var canvas = document.getElementById("myCanvas"); // Create a drawing object (context) var ctx = canvas.getContext("2d");

Canvas coordinates x y Drawing commands are based on x/y coordinates Unlike what you may be used to from a math class, canvas coordinates start with 0,0 in the top left corner X coordinates go horizontally from 0 to pixel width specified in HTML Y coordinates go vertically from 0 to pixel height specified in HTML Assume; width="200" height="100" x 200 y 100

Draw a line If you gave a person instructions to draw a line, you might put a mark at the start location and a mark at the end location, then tell the person to draw a line between those marks. JavaScript commands beginPath(); starts a new path (each path can have attributes like color) moveTo(x,y); tells JS the start location lineTo(x,y); tells JS the end location stroke(); actually draws the line strokeStyle = 'red'; optional, sets style (color, gradient, pattern) Pedagogy sidebar: You might want small exercises so students can practice drawing one type of object (e.g., lines) before moving on to other shapes. We’re going full speed ahead!

Draw Rectangle Start with canvas and context Four parameters Upper x coordinate Upper y coordinate Width Height Can set strokeStyle/linewidth rect command draws ctx.rect(5, 5, 290, 140); For a solid rectangle, set fillStyle fillRect command draws ctx.fillStyle = "green"; ctx.fillRect(30, 30, 50, 50);

Draw a Circle context.arc(x,y,r,startAngle,endAngle, counterclockwise); x: x-coordinate of center y: y-coordinate of center r: radius of circle startAngle: start angle in radians, with 0 corresponding to 3:00 position endAngle: end angle in radians Counterclockwise: optional, false is default To create a circle with arc(): Set start angle to 0 and end angle to 2*Math.PI.

Draw an Arc Same parameters, but you’ll need to figure out angles and may want to change the direction Pedagogy sidebar: You might want to provide example files (as I have), which provide similar function to TryItEditor of w3schools. We’re still moving full speed ahead, but you’ll have a chance to try it in just a minute. https://www.w3resource.com/html5-canvas/html5-canvas-arc.php

Use Variables to Draw For some drawings, elements will be positioned relative to each other Instead of hard-coding values, try to plan the drawing and use variables You may want to base sizing on width and height of canvas. var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); ctx.fillStyle="lightGray"; // Create gray “background” for entire canvas ctx.fillRect(0,0,canvas.width,canvas.height);

Recap To draw in Javascript Find the canvas (name match HTML) Create the context (drawing object) Set the style(s) as desired For a line Create a path, determine start/end locations, stroke For a rectangle Use draw or fill commands with desired coordinates For a circle Determine parameters based on picture with radians

Relax and Play Experiment with the provided files Ask questions! We do have a more structured exercise coming up. Pedagogy sidebar: Unstructured play may not work with your students. We have a lot to cover in a short time.

JavaScript in separate file For more complex programs, it’s cleaner to have the JavaScript in a separate file File extension is .js As with CSS, we must tell the HTML file what type of file and where to find it: <script type="text/javascript" src="facesPlan.js"></script> We’ve seen button clicks as one type of event. Another type of event is when the program first starts. This event is named onload. window.onload = function() { // alert("here"); // draw the face when the window loads drawFace(happy); } Pedagogy sidebar: Challenge your assumptions. If it looks like function isn’t doing anything, see if it’s even being called.

Exercise: Draw a Smiley Face Plan first! Plan the variables on paper - Worksheet I will come around and share my design with you Use the provided facesPlan.js template Test incrementally (see examples) Read Pedagogy on next slide

Pedagogy Students often struggle with planning Specify program structure via comments (see facesPlan.js) Comments identify sub-tasks Easier to support if all students use similar approach Students more likely to have success (blank page is intimidating) Specifying function parameters also helpful for novice programmers (see DrawCircle) Think about what info will function need Use meaningful names to get parameter order correct. Writing comments first is an actual technique used by some professionals Encourage students to use code from examples (remember, effective use of examples is critical) As students gain experience, require them to do this planning step DO NOT write the entire program then try to run it!!!!! Model incremental testing. Comments can specify intermediate test points. If you can get students to do this, your job will be MUCH EASIER!

Optional Extension – more challenging Add a parameter to drawFace method. If parameter is set to 0, draw happy face. If set to 1, draw sad face. TEST – just hard-code function call to either 0 or 1 in the onload method. Add this button to your html (div just helps with spacing) <div> <input type="button" id="toggle" value="Make me sad" onclick="toggleIt();" /> </div> Add a global var named happy Write the method named toggleIt which: if happy = 0, set to 1 (and vice versa) calls drawFace with happy as a parameter sets the text of the button. Hint: you know how to find an element by id (in this case, “toggle”). Setting the button text is like the syntax for innerHTML, but instead of innerHTML you will set value. We covered innerHTML at the end of yesterday’s JS lecture.