The Canvas.

Slides:



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

Iframes & Images Using HTML.
Neal Stublen Why Use a Canvas?  Flexibility… Use of drawing primitives (lines, arcs, text) Direct access to element display pixels.
HTML 5 Previous version: HTML4.01, 1999 Still work in progress, most browsers support some of it.
Image Maps and Graphics Internet Basics and Far Beyond! Mrs. Wilson.
XP Creating Web Pages with HTML Using Tables. XP Objectives Create a text table Create a table using the,, and tags Create table headers and captions.
CHAPTER 21 INTRODUCING THE HTML 5 CANVAS. LEARNING OBJECTIVES How to create a canvas using the and tag pair How to test if a browser supports canvas operations.
Web Programming Language Week 13 Ken Cosh HTML 5.
CS7026 – HTML5 Canvas. What is the element 2  HTML5 defines the element as “a resolution-dependent bitmap canvas which can be used for rendering graphs,
Tutorial 4: Using CSS for Page Layout. 2 Objectives Session 4.1 Explore CSS layout Compare types of floating layouts Examine code for CSS layouts View.
ITP 104.  While you can do things like this:  Better to use styles instead:
Getting Started with Canvas IDIA Spring 2013 Bridget M. Blodgett.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 20 – Dynamic HTML: Path, Sequencer and Sprite ActiveX Controls Outline 20.1Introduction 20.2DirectAnimation.
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.
Designing a Web Page with Tables. A text table: contains only text, evenly spaced on the Web page in rows and columns uses only standard word processing.
به نام خدا تنظیم کننده : فرانه حدادی استاد : مهندس زمانیان تابستان 92.
More CSS.
CS 174: Web Programming October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CHAPTER 22 ADVANCED CANVAS PROGRAMMING. LEARNING OBJECTIVES How the HTML 5 canvas supports linear and radial gradients How to fill a shape with a pattern.
Graphics Concepts CS 2302, Fall /17/20142 Drawing in Android.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
Project 5: Using Pop-Up Windows Essentials for Design JavaScript Level One Michael Brooks.
IN THE DOCUMENT OBJECT MODEL, EACH LINE OF HTML IS AN ELEMENT (AN OBJECT), ABLE TO HAVE ATTRIBUTES, PROPERTIES AND VALUES. CSS TELLS THE BROWSER HOW TO.
- WE’LL LOOK AT THE POSITION PROPERTY AND HOW CSS RESOLVES CONFLICTS BETWEEN STYLING INSTRUCTIONS The position property; CSS specificity hierarchy.
Can SAS Do Canvas? Creating Graphs Using HTML 5 Canvas Elements Edwin J. van Stein Astellas Pharma Global Development.
07 – HTML5 Canvas (1) Informatics Department Parahyangan Catholic University.
HTML 5 (Part 1) – Start from SCRATCH. HTML 5 – Start from SCRATCH.
Introducing Macromedia Flash 8
More CSS.
HTML5 Basics.
CSS Box Model.
Objective % Select and utilize tools to design and develop websites.
Chapter 9 HTML 5 Video and Audio
Images, Hyperlinks, and Sound
CNIT 131 HTML5 - Tables.
CSS Box Model.
Cascading Style Sheets Boxes
SVG & DOM Manipulation via Javascript
Internet of the Past.
CMPE 280 Web UI Design and Development September 12 Class Meeting
Inserting and Working with Images
Introduction to CSS.
Tutorial 4 Topic: CSS 3.0 Li Xu
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Advanced CSS BIS1523 – Lecture 20.
Web Programming Language
Objective % Select and utilize tools to design and develop websites.
Objectives Create a figure box Add a background image
Playing Audio (Part 1).
Playing Video (Part 1).
ISC440: Web Programming II Ch14: HTML5 Canvas
Positioning.
Browser Support for HTML5
CSS Box Model.
More CSS.
Tutorial 6 Creating Dynamic Pages
DREAMWEAVER MX 2004 Chapter 7 Working with Layers
Images, Hyperlinks, and Sound
SEEM4570 Tutorial 3: CSS + CSS3.0
More CSS 22-Feb-19.
Drawing Graphics in JavaScript
CSc 337 Lecture 21: Canvas.
Introduction to CSS.
CSS Box Model.
Positioning.
HTML5 - 2 Forms, Frames, Graphics.
JavaScript – Let’s Draw!
CSc 337 Lecture 19: Canvas.
Presentation transcript:

The Canvas

Introducing the Canvas The canvas is completely new to HTML5 and enables designers and programmers to create and place graphics directly onto a web page. Prior to HTML5, an external plug-in, usually Adobe Flash, was required to accomplish many of the features that the canvas can now perform. The canvas is extremely flexible. It can be used to draw simple lines and geometric figures, but it can also be used to create highly complex and interactive games and applications. With its relatively broad browser support, this new feature promises to bring faster and more reliable graphics to the web in the near future. Unlike the new HTML5 elements we have covered thus far, the canvas absolutely requires the use of JavaScript to function. For this reason, we will be getting an overview of the tool's capabilities in this course, but we won't be programming directly in JavaScript.

The <canvas> Element To create a canvas to begin drawing on, we use the <canvas> element with three attributes: <body> <canvas id="drawing" width="500" height="300"></canvas> </body> Until we add any actual drawing on the canvas, the element will be invisible on the screen. So let's add a border around it, using CSS: #drawing { border: 2px dashed blue; } The id attribute is essential, as it will be used by JavaScript to identify which element is to be manipulated. Though the width and height attributes are not strictly required, they should always be defined. As before, the closing </canvas> tag is mandatory.

Canvas Coordinates To be able to draw on the canvas, we'll first need to understand how the coordinate system works. Just as in algebra, there are x and y axes to define specific locations: 500px (500,0) is the top right corner of the canvas. (0,0) is the top left corner of the canvas. 300px (500,300) is the bottom right corner of the canvas. (0,300) is the bottom left corner of the canvas. The upper limits of the x and y coordinates are dependent, of course, on the width and height, respectively, we defined for the <canvas> element. In this example, we defined a canvas with the dimensions of 500 pixels wide by 300 pixels high.

Drawing a Straight Line Before we begin drawing on the canvas, we need to run a few lines of JavaScript code: <script> window.onload = function() { var canvas = document.getElementById("drawing"); var context = canvas.getContext("2d"); } </script> The above script tells the browser to wait until the page has loaded before running. Then, it defines which screen element to select (in this case, the one whose id is "drawing"). Now let's add the code to draw a single straight line: ... context.moveTo(100,100); context.lineTo(400,100); context.stroke();

Line Options Here is the full script for drawing the line, with three new options defined: <script> window.onload = function() { var canvas = document.getElementById("drawing"); var context = canvas.getContext("2d"); context.lineWidth = 5; context.strokeStyle = "red"; context.lineCap = "round"; context.moveTo(100,100); context.lineTo(400,100); context.stroke(); } </script> In the highlighted new code, we increased the width of the line to 5 pixels, we changed the color of the line to red, and we rounded off the ends of the line.

Drawing a Rectangle A special function is available to draw a rectangle on the canvas: <script> window.onload = function() { var canvas = document.getElementById("drawing"); var context = canvas.getContext("2d"); context.lineWidth = 3; context.strokeStyle = "#f1b2dc"; context.strokeRect(50,50,200,200); } </script> We first define the line width and color, as before. Then, as we draw the actual rectangle, we define the top left x and y location, the height, and the width.

Filling the Rectangle Instead of (or in addition to) the outline of the rectangle, we can fill it with color: <script> window.onload = function() { var canvas = document.getElementById("drawing"); var context = canvas.getContext("2d"); context.fillStyle = "#f1b2dc"; context.fillRect(50,50,200,200); } </script>

Drawing a Circle or Arc Another special function is available to draw a circle (or portion of circle) on the canvas: <script> window.onload = function() { var canvas = document.getElementById("drawing"); var context = canvas.getContext("2d"); context.lineWidth = 2; context.strokeStyle = "orange"; context.arc(200,150,75,0,2*Math.PI); context.stroke(); } </script> We first define the line width and color, as before. Then, we define the circle by specifying the x and y coordinates of its center, the circle radius, and the beginning and ending angles (which define how much of the circle is visible).

Filling the Circle Instead of (or in addition to) the line outline of the circle, we can fill it with color: <script> window.onload = function() { var canvas = document.getElementById("drawing"); var context = canvas.getContext("2d"); context.fillStyle = "orange"; context.arc(200,150,75,0,2*Math.PI); context.fill(); } </script>

Drawing Another Shape Any other shape using straight lines can be created: ... context.lineWidth = 1; context.strokeStyle = "green"; context.moveTo(150,200); context.lineTo(250,100); context.lineTo(400,125); context.lineTo(350,200); context.lineTo(150,200); context.stroke(); After defining the line width and color, we specify where to begin drawing and then each subsequent point. Finally, we return to the starting point to complete the shape.

Filling the Shape We can fill the shape, as before: ... context.lineWidth = 1; context.fillStyle = "green"; context.moveTo(150,200); context.lineTo(250,100); context.lineTo(400,125); context.lineTo(350,200); context.lineTo(150,200); context.closePath(); context.fill(); To fill the shape, we must add an extra line to tell the canvas to close off the shape first.

Overlapping Shapes Multiple lines, shapes, and other objects can be placed on the canvas at the same time. By default, the later ones take precedence – and appear on top – of earlier ones. However, designers can set transparency settings to control how much objects will show beneath those objects on top of them.

Advanced Canvas Features The canvas is powerful and flexible. We have many tools at our fingertips: Add images and text. Apply shadows to objects. Fill shapes with patterns or gradients. Move objects' locations over time, animating the canvas. Respond to what the user clicks or types and alter objects' locations or properties, making the canvas interactive. Combine interactivity and animation, creating fully functional applications and games solely with the HTML5 canvas. We won't be looking at the JavaScript code for these features, but let's see some actual examples of what the canvas can do in the hands of experienced programmers.

HTML5 Canvas Examples Game Animation Application Tool

Browser Support for <canvas> The <canvas> element is well supported: The need for a fallback is mainly for versions of Internet Explorer 8.0 and earlier.

Fallback for <canvas> The <canvas> element can use the same fallback mechanism that we saw with <audio> and <video> to run lines of code only if the element is not recognized by the browser: <canvas id="drawing" width="500" height="300"> <!-- place alternate content here --> </canvas> There are several ways web designers provide fallback content for the HTML5 canvas: Using the standard Flash fallback via <object> or <embed>. For newly created canvas applications, this might not be practical, as developing the same thing on a second, completely different platform (Flash) would require considerable time and resources. Using a fallback JavaScript library, such as ExplorerCanvas, that emulates the HTML5 canvas for earlier versions of Internet Explorer. Slow responsiveness and glitches can become a factor, especially for complex applications. Provide static content, such as a notice that the current browser cannot display the intended content. A difficult choice, but one that some web designers are now making, leaving older browsers (and their users) behind.