Drawing Graphics in JavaScript

Slides:



Advertisements
Similar presentations
Bring Life to Your Web Pages with JavaScript and HTML5 Part 2 Ole Ildsgaard Hougaard -
Advertisements

HTML5 CANVAS.  Rectangles  Arcs  Lines  Features we won’t look at  Images  Drag and drop  Animation  Widely supported DRAWING PICTURES IN HTML.
CHAPTER 20 CREATING SVG GRAPHICS. LEARNING OBJECTIVES How to embed a graphic stored within a.SVG file in an HTML page How to use the and tag pair to specify.
Lecture # 11 JavaScript Graphics. Scalable Vector Graphics (SVG) Scalable Vector Graphics (SVG), as the name implies, are - scalable (without pixelation):
Using Bitmap graphics Having looked at vector graphics in the canvas, we will now look at using bitmap graphics.
Neal Stublen Why Use a Canvas?  Flexibility… Use of drawing primitives (lines, arcs, text) Direct access to element display pixels.
Programming Club IIT Kanpur
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.
Graphics in Android 1 Fall 2012 CS2302: Programming Principles.
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.
Canvas, SVG, Workers Doncho Minkov Telerik Corporation
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,
IT Engineering I Instructor: Rezvan Shiravi
INT222 – Internet Fundamentals Weekly Notes: AJAX and Canvas 1.
Programming games Recap. Upload work (Filezilla, index.html). Drawing lines. Drawing stars. Homework: Drawing exercises.
Images (1) Three most popular formats – Graphics Interchange Format (GIF) – Joint Photographic Experts Group (JPEG) – Portable Network Graphics (PNG) –
Getting Started with Canvas IDIA Spring 2013 Bridget M. Blodgett.
2D Graphics: Rendering Details
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.
Canvas academy.zariba.com 1. Lecture Content 1.What is Canvas? 2.Including Canvas in your HTML 3.Commands in Canvas 4.Drawing Shapes with Canvas 5.Animations.
Session: 17. © Aptech Ltd. 2Canvas and JavaScript / Session 17  Describe Canvas in HTML5  Explain the procedure to draw lines  Explain the procedure.
Default Fill & Stroke D Swap Fill & Stroke Shift X None / Gradient > Color < Stroke X Fill (Click to activate) X More about Fill and Stroke:
Element. The element Used to dynamically draw graphics using javascript. Capable of drawing paths, circles, rectangles, text, and images.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
New Tags, New Attributes, New JavaScript APIs, Forms, Validation, Audio, Video, SVG, Canvas Doncho Minkov Telerik Corporation
SVG for Designers Tom Hoferek. Objectives Introduce SVG Illustrate its capabilities Demonstrate SVG in action Speculate, discuss, answer questions.
CS 174: Web Programming October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Tkinter Canvas.
Creating Graphics for the Web Learning & Development Team Telerik Software Academy.
FIREWORKS graphics software Intro to Basic Shapes.
Intro to the Canvas Canvas is an HTML5 element If you see this, your browser doesn't support the canvas Canvas is manipulated with javascript c = document.getElementById("mycanvas");
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.
Programming games Review concepts. Crooked coin toss. Drawing on canvas. Homework: Complete [static] drawings. Upload files to website.
Graphics in Android 1 CS7030: Mobile App Development.
07 – HTML5 Canvas (1) Informatics Department Parahyangan Catholic University.
What is HTML5? HTML5 will be the new standard for HTML. The previous version of HTML, HTML 4.01, came in The web has changed a lot since then. HTML5.
HTML 5 (Part 1) – Start from SCRATCH. HTML 5 – Start from SCRATCH.
Martin Kruliš by Martin Kruliš (v1.1)1.
Main characteristics of Vector graphics  Vector graphics provide an elegant way of constructing digital images (diagrams, technical illustration and.
Intro to Graphics from Chapter 2 of Java Software Solutions
Laying out Elements with CSS
That Gauge is COOL! 
What is a Function Expression?
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
Canvas Notes
گرافیک رایانه ای.
Web Programming Language
A framework to create SVG graphics
Lesson One: The Beginning Chapter 1: Pixels Learning Processing Daniel Shiffman Presentation by Donald W. Smith Graphics from
ISC440: Web Programming II Ch14: HTML5 Canvas
The Canvas.
Graphics in Android Fall 2012 CS2302: Programming Principles.
Exam1 Review CSE113 B.Ramamurthy 11/29/2018 B.Ramamurthy.
Cascading Style Sheets™ (CSS)
OpenGL program.
CSc 337 Lecture 21: Canvas.
Context of what we are doing.
Common Page Design Elements
Graphics Canvas.
Exam1 Review CSE113 B.Ramamurthy 4/17/2019 B.Ramamurthy.
CSc 337 Lecture 20: Canvas.
JavaScript – Let’s Draw!
CSc 337 Lecture 19: Canvas.
Presentation transcript:

Drawing Graphics in JavaScript

Topics <canvas> element The canvas context The canvas coordinate system Basic Usage: Drawing Rectangles Drawing paths strokeStyle() and fillStyle

<canvas> element Used to draw graphics on web pages via JavaScript. Added in HTML5 <canvas> was originally introduced by Apple for the OS X Dashboard and Safari. <canvas> element has only two attributes: width and height.

The canvas context The <canvas> element creates a fixed-size drawing surface. Other contexts may exist that provide different types of rendering, such OpenGL. To display something, a script first needs to access the canvas context. <body> <canvas id=“drawingBoard” width=“500” height=“600”> </canvas> </body> var canvas = document.getElementById('drawingBoard'); var canvasContext = canvas.getContext('2d'); HTML JavaScript

Canvas Coordinate System The 0,0 coordinate is positioned in the top left corner.

Drawing Rectangles <canvas> only supports rectangles and lines Non-rectangular shapes must be created using lines functions that draw rectangles on a canvas: fillRect(x, y, width, height) Draws a filled rectangle. strokeRect(x, y, width, height) Draws a rectangular outline. clearRect(x, y, width, height) Clears a rectangular area

Drawing Paths A path is a collection of line segments. moveTo(), lineTo(), and arc() are used to create line segments. Example: canvasContext.beginPath(); canvasContext.moveTo(25, 25); canvasContext.lineTo(105, 25); canvasContext.lineTo(25, 105); canvasContext.fill(); canvasContext.closePath();

Drawing Paths Example 2 var startPt = 0; var endPt = 2 * Math.PI; canvasContext.beginPath(); canvasContext.arc(25, 25, beginPt, endPt); canvasContext.fill(); canvasContext.closePath();

fillStyle() and strokeStyle() fillStyle = color Sets the style used when filling shapes. strokeStyle = color Sets the style for shapes' outlines. By default, the stroke and fill color are set to black (#000000). A newly set strokeStyle and/or fillStyle property becomes the default for all shapes. Example: canvasContext.fillStyle = “blue”; canvasContext.strokeStyle = “#034AAD”;

Exercise 1: Write the HTML and JavaScript to produce the image shown below.

Exercise 2: Write the HTML and JavaScript to produce the image shown below.