Graphics Canvas.

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.
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.
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.
Spring /6.831 User Interface Design and Implementation1 Lecture 11: Output.
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
Director of Computer Center, DaYeh University Ku-Yaw Chang.
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.
Create Interfaces College exercise. Continue HTML5: HTML5 logo, drawing images. Coin toss. Homework: acquire video & audio.
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.
CHAPTER 2 HTML & HTML5 II อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
New Features of HTML 5.0 Barbara Ericson
Brent M. Dingle, Ph.D Game Design and Development Program Mathematics, Statistics and Computer Science University of Wisconsin - Stout HTML5 – Canvas.
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.
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.
Introduction What is it? What does this mean? Features Animated Logo Html5 Flash Canvas Tunneler Html5 Animation Html5 Canvas Caching.
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.
CS 174: Web Programming October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Creating Graphics for the Web Learning & Development Team Telerik Software Academy.
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");
HTML – Other Tags Semantic Tags, Frames, Embed Multimedia Content, Others SoftUni Team Technical Trainers Software University
HTML – Other Tags Semantic Tags, Frames, Embed Multimedia Content, Others SoftUni Team Technical Trainers Software University
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.
Programming games Review concepts. Crooked coin toss. Drawing on canvas. Homework: Complete [static] drawings. Upload files to website.
School of Computing and Information Systems RIA Animation, part I.
Lab 5: More on Events and Introduction to the Canvas Tag User Interface Lab: GUI Lab Sep. 23 rd, 2013.
07 – HTML5 Canvas (1) Informatics Department Parahyangan Catholic University.
HTML 5 (Part 1) – Start from SCRATCH. HTML 5 – Start from SCRATCH.
Part – 3 : HTML 5 SVG Tag. † SVG stands for Scalable Vector Graphics. † SVG is used to define vector-based graphics for the Web. † SVG defines the graphics.
HTML5 ProgLan HTML5 Making a Game on the Canvas Part 1.
Game Development with JS and Canvas 2D
That Gauge is COOL! 
What is a Function Expression?
Internet of the Past.
4.01 Cascading Style Sheets
CMPE 280 Web UI Design and Development September 12 Class Meeting
Chapter 4: Scalable Vector Graphics (SVG)
Canvas Notes
PHP Image Manipulation
A framework to create SVG graphics
מדעי המחשב ורובוטיקה בחט"ב
ISC440: Web Programming II Ch14: HTML5 Canvas
Patrick Dengler Senior Program Manager Microsoft Corporation
The Canvas.
Basics of Web Design Chapter 11 Media and Interactivity Basics Key Concepts Copyright © 2013 Terry Ann Morris, Ed.D.
Types of Spatial Data Sites
Drawing Graphics in JavaScript
Context of what we are doing.
HTML5 – Canvas JavaScript Simple Drawing
CSc 337 Lecture 20: Canvas.
Programming Games Refresher on coordinates. Scaling, translation. HTML5 logo. Refresher on animation. Bouncing ball. Show project. Classwork/Homework:
4.01 Cascading Style Sheets
JavaScript – Let’s Draw!
Javascript canvas graphics
Presentation transcript:

Graphics Canvas

Introduction Canvas is a HTML element that we can control using JavaScript to create graphics Graphics ranging from texts, graphs, pictures, or animations Relatively new technology but all major browsers support it now

Syntax <canvas id=’myCanvas’ height=’100px’ width=’100px’> Anything missing from the syntax above? Height and width attributes are optional and default to 150px and 300px respectively

No browser support <canvas id=’yourCanvas’> Include elements to be displayed in case browser does not support canvas or if the agent is a textual browser <canvas id=’yourCanvas’> <!-- Valid HTML/Text here---> </canvas> This means the canvas closing tag is required Why?

Rendering Context 2D / 3D / Image processing var canvas = document.getElementById(‘myCanvas’); var context = canvas.getContext(‘2d’|’webgl’|’bitmaprenderer’); The rendering context object is what is used to draw graphics

Shapes & Paths <canvas> only supports a single shape, Rectangle fillRect(x,y, width, height) strokeRect(x, y, width, height) clearRect(x, y, width, height) How do you draw other shapes?

Rectangle Example function draw(){ } var canvas = document.getElementById('cnvs'); var context = canvas.getContext('2d'); context.fillRect(25, 25, 100, 100); context.clearRect(45, 45, 60, 60); ctx.strokeRect(50, 50, 50, 50); }

Rect. example Output:

Paths Series of points connected by a line (straight, curved, stroke, filled, any line) Steps: Create a path beginPath() Draw into the path Stroke or fill Stroke() fill()

Path to a Triangle What you’ll need: moveTo(x,y): This moves the pen(cursor) to the coordinates (x,y) lineTo(x,y): Draws a line from the current location to the coordinates (x,y) fill()/stroke(): Stroke for outline or fill for coloured path fill() automatically closes path while stroke does not. You must call clostPath() before calling stroke()

Triangle Example function draw() { var canvas = document.getElementById('canvas') ; if (canvas.getContext) { var ctx = canvas.getContext('2d'); // Filled triangle ctx.beginPath(); ctx.moveTo(25, 25); ctx.lineTo(105, 25); ctx.lineTo(25, 105); ctx.fill(); // Stroked triangle ctx.moveTo(125, 125); ctx.lineTo(125, 45); ctx.lineTo(45, 125); ctx.closePath(); ctx.stroke(); }

Triangle Output Output:

Drawing Text We can draw text using the following: fillText(text, x, y [,maxWidth]) strokeText(text, x, y [,maxWidth])

Text Example function draw() { var ctx = document.getElementById('canvas').getContext('2d'); ctx.font = '48px serif'; ctx.fillText('Hello world', 10, 50); }

Text Output Output:

Colours Use fillStyle property of the context object to specify colour. The following all set the colour to orange ctx.fillStyle = 'orange'; ctx.fillStyle = '#FFA500'; ctx.fillStyle = 'rgb(255, 165, 0)'; ctx.fillStyle = 'rgba(255, 165, 0, 1)';

Example function draw() { var ctx = document.getElementById('canvas').getContext ('2d'); for (var i = 0; i < 6; i++) { for (var j = 0; j < 6; j++) { ctx.fillStyle = 'rgb(' + Math.floor(255 - 42.5 * i) + ', ' + Math.floor(255 - 42.5 * j) + ', 0)'; ctx.fillRect(j * 25, i * 25, 25, 25); }

Output Output: