CHAP 2. CANVAS API.  Dynamically generate and render graphics, charts, images and animations  SVG (Scalable Vector Graphics) vs. Canvas  Bitmap canvas.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter 6 More CSS Basics Key Concepts Copyright © 2013 Terry Ann Morris, Ed.D 1.
Lecture # 11 JavaScript Graphics. Scalable Vector Graphics (SVG) Scalable Vector Graphics (SVG), as the name implies, are - scalable (without pixelation):
CSS level 3. History of CSS CSS level 1 – original CSS CSS level 2 CSS level 2 Revision 1 (CSS2.1) – up to CSS2.1 monolithic structure CSS level 3 – specification.
Advanced #1. 그림자 스타일 window.onload = function(){ var canvas = document.getElementById("myCanvas"); var context = canvas.getContext("2d"); context.rect(188,
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.
CS4506. HTML5 The HTML5 specification was developed by the Web Hypertext Application Technology Working Group (WHATWG), which was founded in 2004 by was.
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
PRO HTML5 PROGRAMMING POWERFUL APIS FOR RICHER INTERNET APPLICATION DEVELOPMENT.
Web Programming Language Week 13 Ken Cosh HTML 5.
PRO HTML5 PROGRAMMING POWERFUL APIS FOR RICHER INTERNET APPLICATION DEVELOPMENT.
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,
Getting Started with Canvas IDIA Spring 2013 Bridget M. Blodgett.
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.
XP Tutorial 1 Introduction to Macromedia Flash MX 2004.
Adding Graphics to Web Pages. CSS does not have any properties specifically aimed at formatting images Use properties you have already learned, i.e.,
Element. The element Used to dynamically draw graphics using javascript. Capable of drawing paths, circles, rectangles, text, and images.
COMP213 – Web Interface Design
New Tags, New Attributes, New JavaScript APIs, Forms, Validation, Audio, Video, SVG, Canvas Doncho Minkov Telerik Corporation
INTRODUCTORY Tutorial 4 Exploring Graphic Elements and Images.
SVG for Designers Tom Hoferek. Objectives Introduce SVG Illustrate its capabilities Demonstrate SVG in action Speculate, discuss, answer questions.
Lecture 16 Image Document Formats. Bitmap vs. Vector Images Bitmaps do not generally.
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");
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.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
08 – Canvas(2) Informatics Department Parahyangan Catholic University.
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.
Main characteristics of Vector graphics  Vector graphics provide an elegant way of constructing digital images (diagrams, technical illustration and.
Drawing with the HTML5 Canvas
Web Development & Design Foundations with HTML5 8th Edition
What is a Function Expression?
SVG & DOM Manipulation via Javascript
CSS3 Style of the HTML document CSS2+New Specifications Differences
Chapter 5 Introduction to Cascading Style Sheets (CSS): Part 2
CMPE 280 Web UI Design and Development September 12 Class Meeting
Inserting and Working with Images
Tutorial 4 Topic: CSS 3.0 Li Xu
Canvas Notes
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Advanced CSS BIS1523 – Lecture 20.
Web Programming Language
Web Systems & Technologies
HTML5 Application Development Fundamentals
Building beautiful and interactive apps with HTML5 & CSS3
Chapter 6 More CSS Basics Key Concepts
Objectives Create a figure box Add a background image
4 Visual Elements and Graphics.
Basics of Web Design Chapter 6 More CSS Basics Key Concepts
ISC440: Web Programming II Ch14: HTML5 Canvas
The Canvas.
Box model, spacing, borders, backgrounds
Basics of Web Design Chapter 6 More CSS Basics Key Concepts
Creating a Webpage with External CSS
Basics of Web Design Chapter 6 More CSS Basics Key Concepts
Progress <progress value="22" max="100"></progress>
Drawing Graphics in JavaScript
Simple Canvas Example Your browser doesn’t support canvases var canvas = document.getElementById("canvas1"); var context.
CSc 337 Lecture 21: Canvas.
Graphics Canvas.
Week 10 - HTML SVG Student: Vladislovas Karalius
CSc 337 Lecture 19: Canvas.
HTML5 – A few features L. Grewe.
Presentation transcript:

CHAP 2. CANVAS API

 Dynamically generate and render graphics, charts, images and animations  SVG (Scalable Vector Graphics) vs. Canvas  Bitmap canvas  Images drawn on a canvas are final and not be resized  Objects drawn on a canvas are not part of the page’s DOM or any namespace  SVG images can be scaled seamlessly at different resolutions and allow hit detection  HTML5 Canvas API  It performs well because it does not have to store objects for every primitive it draws  Relatively easy to implement the Canvas API WHAT IS CANVAS

 Element:  Coordinates: CANVAS BrowserDetails ChromeSupported in version 1.0 and greater FirefoxSupported in version 1.5 and greater Internet Explorer Not supported OperaSupported in version 9.0 and greater SafariSupported in version 1.3 and greater

CHECKING BROWSER SUPPORT try { document.createElement("canvas").getContext("2d"); document.getElementById("support").innerHTML = "HTML5 Canvas is supported in your browser."; } catch (e) { document.getElementById("support").innerHTML = "HTML5 Canvas is not supported  in your browser."; }

 A basic canvas with solid border   Get the canvas element and its context var canvas = document.getElementById('canvas'); var context = canvas.getContext('2d');  Draw line  Prepare graph context.beginPath(); context.moveTo(70, 140); context.lineTo(140, 70);  Stroke graph onto the canvas  context.stroke(); CANVAS, DRAW LINE Excerise

TRANSFORMATION (TRANSLATE, SCALE, ROTATE)

 Get the canvas element and its context  Save the current drawing state context.save();  Move to new coordinate, ex: (50, 100) context.translate(50, 100);  Draw line  Restore the old drawing state context.restore(); TRANSLATION Excerise

PATH context.beginPath(); context.moveTo(-25, -50); context.lineTo(-10, -80); context.lineTo(-20, -80); context.lineTo(-5, -110; context.lineTo(-15, -110); context.lineTo(0, -140); context.lineTo(15, -110); context.lineTo(5, -110); context.lineTo(20, -80); context.lineTo(10, -80); context.lineTo(25, -50); ontext.closePath();

 Line width  context.lineWidth = 4;  Corner style at path joins (round: 圓角, bevel: 斜角, miter)  context.lineJoin = 'round';  Line style at endpoints (round, square, butt: 預設值 )  Context.lineCap = ‘square';  Stroke style  Change color  context.strokeStyle = '#663300';  Background pattern  Fill Style  Change color  context.fillStyle = '#339900';  Background pattern  Fill the region inside all the closed paths  context.fill();  Fill rectangular content  context.fillRect(x, y, w, h); //ex: context.fillRect(-5, -50, 10, 50); STROKE STYLE

 Starting Point: current location  context.quadraticCurveTo(ControlPointX, ControlPointY, EndPointX, EndPointY);  Example:  context.save();  context.translate(-10, 350);  cucontext.moveTo(0, 0);  context.quadraticCurveTo(170, -50, 260, -190);  context.quadraticCurveTo(310, -250, 410, -250);  context.lineWidth = 20;  context.strokeStyle = '#663300';  ontext.stroke();  context.restore(); QUADRATIC CURVE

 Load image var img = new Image(); img.src = “bark.jpg”;  Confirm the image is loaded img.onload = function(){ //Draw image onto canvas }  Draw image onto canvas  context.drawImage(image, dx, dy)  context.drawImage(image, dx, dy, dw, dh)  context.drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh)  Example  var bark = new Image();  bark.src = "bark.jpg";  bark.onload = function(){  context.drawImage(bark, -5, -50, 10, 50);  context.stroke();  context.restore();  } IMAGE

 Linear Gradient ( 漸層 )  Usage  context.createLinearGradient(x0, y0, x1, y1)  x0, y0 - Line start  x1, y1 - Line end  gradient.addColorStop(offset, color)  offset - From 0.0 to 1.0  color - Use rgba() or HEX  Example  var trunkGradient = context.createLinearGradient(-5, -50, 5, -50);  trunkGradient.addColorStop(0, '#663300');  trunkGradient.addColorStop(0.4, '#996600');  trunkGradient.addColorStop(1, '#552200');  context.fillStyle = trunkGradient;  context.fillRect(-5, -50, 10, 50);  Radical Gradient  Usage  Context.createRadicalGradient(x0, y0, r0, x1, y1, r1)  x0, y0, r0 – First circle center at (x0, y0) with radius r0  x1, y1, r1 – Second circle center at (x1, y1) with radius r1 GRADIENT

 Usage  context.createPattern(image, repeat)  repeat - repeat, repeat-x, repeat-y, no-repeat  Example  var gravel = new Image();  gravel.src = "gravel.jpg";  context.save();  context.translate(-10, 390);  gravel.onload = function(){  context.beginPath();  context.moveTo(0, 0);  context.quadraticCurveTo(170, -50, 260, -190);  context.quadraticCurveTo(310, -250, 410, -250);  context.lineWidth = 20;  context.strokeStyle = context.createPattern(gravel, 'repeat');  context.stroke();  context.restore();  } BACKGROUND PATTERN

 Usage  context.scale(rx, ry);  rx – width scale ratio  ry – height scale ratio  Example  var canvas = document.getElementById('canvas');  var context = canvas.getContext('2d');  context.save();  context.translate(260, 500);  context.scale(2, 2);  drawTree(context);  context.restore(); SCALE

 Usage  context.rotate(angle)  angel can be express with Math.PI/180 * degree  Example  var canvas = document.getElementById('canvas');  var context = canvas.getContext('2d');  context.save();  context.translate(260, 500);  context.rotate(Math.PI/180 * 40);  drawTree(context);  context.restore(); ROTATE

 Context.transform(rx, sy, sx, ry, dx, dy)  rx – width scale ratio  ry – height scale ratio  sy – vertical shear  sx – horizontal shear  Example  var canvas = document.getElementById('canvas');  var context = canvas.getContext('2d');  context.save();  context.translate(130, 250);  context.transform(1, 0, -0.5, 1, 0, 0);  context.scale(1, 0.6);  context.fillStyle = 'rgb(0, 0, 0, 0.2)';  context.fillRect(-5, -50, 10, 50);  createCanopyPath(context);  context.fill();  context.restore(); TRANSFORM

 Usage  context.fillText(text, x, y, maxwidth)  context.strokeText(text, x, y, maxwidth)  Property  context.font = Font String  context.textAlign = start, end, left, right, center  context.textBaseLine = top, middle, bottom, …  Example  context.save();  context.font = '60px 標楷體 ';  context.fillStyle = '#996600';  context.textAlign = 'center';  context.fillText(' 快樂圖畫 ', 200, 60, 400);  context.restore(); CANVAS TEXT

 Usage  shadowColor – Any CSS Color  shadowOffsetX – Pixel Count  shadowOffsetY – Pixel Count  Shadowblur – Gaussian blur  Example  context.shadowColor = 'rgba(0, 0, 0, 0.2)';  context.shadowOffsetX = 15;  context.shadowOffsetY = -10;  context.shadowBlur = 2;  context.font = '60px 標楷體 ';  context.fillStyle = '#996600';  context.textAlign = 'center';  context.fillText(' 快樂圖畫 ', 200, 60, 400); SHADOWS