07 – HTML5 Canvas (1) Informatics Department Parahyangan Catholic University.

Slides:



Advertisements
Similar presentations
Internet Services and Web Authoring (CSET 226) Lecture # 5 HyperText Markup Language (HTML) 1.
Advertisements

HTML – A Brief introduction Title of page This is my first homepage. This text is bold  The first tag in your HTML document is. This tag tells your browser.
Lecture # 11 JavaScript Graphics. Scalable Vector Graphics (SVG) Scalable Vector Graphics (SVG), as the name implies, are - scalable (without pixelation):
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 4 Key Concepts 1 Copyright © Terry Felke-Morris.
Very quick intro HTML and CSS. Sample html A Web Title.
Cascading Style Sheets. CSS stands for Cascading Style Sheets and is a simple styling language which allows attaching style to HTML elements. CSS is a.
Cascading Style Sheets By: Valerie Kuna. What are Cascading Style Sheets? Cascading Style Sheets (CSS) are a standard for specifying the presentation.
Building a Website: Cascading Style Sheets (CSS) Fall 2013.
CSS(Cascading Style Sheets )
กระบวนวิชา 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.
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.
Text, Masks, and Live Effects – Lesson 41 Text, Masks, and Live Effects Lesson 4.
4.01 Cascading Style Sheets
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.
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
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,
Week 4.  Three ways to apply CSS: Inline CSS Internal style sheets ( header style information) External style sheets.
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.
 This presentation introduces the following: › 3 types of CSS › CSS syntax › CSS comments › CSS and color › The box model.
Getting Started with Canvas IDIA Spring 2013 Bridget M. Blodgett.
Today’s objectives  Assignment 1  Padding, Margins, Borders  Fluid Layout page  Building accessible Table  Element size with padding and border 
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.
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
Chapter 2: Color and Applets Coming up: Introduction to Graphics.
CIS234A- Lecture 7 Instructor Greg D’Andrea. Tables A table can be displayed on a Web page either in a text or graphical format. A text table: – contains.
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.
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.
1 Introduction to Graphics b The last one or two sections of each chapter of the textbook focus on graphical issues b Most computer programs have graphical.
School of Computing and Information Systems RIA Animation, part I.
`. Lecture Overview HTML Body Elements Linking techniques HyperText references Linking images Linking to locations on a page Linking to a fragment on.
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.
HTML 5 (Part 1) – Start from SCRATCH. HTML 5 – Start from SCRATCH.
3. Drawing Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
CHAP 2. CANVAS API.  Dynamically generate and render graphics, charts, images and animations  SVG (Scalable Vector Graphics) vs. Canvas  Bitmap canvas.
Create a new stylesheet called Hotel Style
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
Inserting and Working with Images
Canvas Notes
Web Programming Language
Web Development & Design Foundations with HTML5 7th Edition
ISC440: Web Programming II Ch14: HTML5 Canvas
The Canvas.
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.
CSc 337 Lecture 20: Canvas.
4.01 Cascading Style Sheets
JavaScript – Let’s Draw!
CSc 337 Lecture 19: Canvas.
HTML5 – A few features L. Grewe.
Presentation transcript:

07 – HTML5 Canvas (1) Informatics Department Parahyangan Catholic University

 The canvas element is mainly used for manipulating 2d graphics, or create them from scratch  It can…  create dynamic graphics and animations that react to user interaction (e.g., games)  data visualizations and graphs based on data in a HTML table, which updates on the fly.  construct a UI for a Web application  etc.

 Creating a blank canvas element with 500px width and 500px height  fallback content is shown when the browser doesn’t support canvas element The position of the canvas element is defined by its location within your HTML document. It can be moved around with CSS as required, just like other HTML elements.

 The purpose of the canvas element is to act as a wrapper around the 2d rendering context  It provides methods for manipulating 2d graphics  We actually draw on the 2d rendering context

 Uses flat Cartesian coordinate system with the origin (0, 0) at the top left

 Preparing the canvas: $(document).ready(function() { }); Drawing scripts written here

 Accessing the 2d rendering context using JS  Pure JS  JQuery var canvas = $("#myCanvas"); var context = canvas.get(0).getContext("2d"); var canvas = document.getElementById("myCanvas"); var context = canvas.getContext("2d");

 Drawing a Rectangle  the first two parameters are x and y coordinates for the top left corner  the last two parameters are width and height  default color is black (can be changed) context.fillRect(40, 40, 100, 100); context.strokeRect(40, 40, 100, 100); RESULT:

 Drawing a Line  They’re actually known as paths context.beginPath(); // Start the path context.moveTo(40, 40); // Set the path origin context.lineTo(340, 40); // Set the path destination context.closePath(); // Close the path context.stroke(); // Outline the path

 Drawing a Circle  there isn’t actually a special method in canvas to create a circle  there is is a method for drawing arcs context.beginPath(); // Start the path // Draw a circle context.arc(230, 90, 50, 0, Math.PI*2, false); context.closePath(); // Close the path context.fill(); // Fill the path SYNTAX: context.arc(x, y, radius, startAngle, endAngle, anticlockwise); SYNTAX: context.arc(x, y, radius, startAngle, endAngle, anticlockwise);

 Drawing an Arc

 Angles in canvas are in radians  Converting degrees  radians:  Or just use radians: var degrees = 1; // 1 degree var radians = degrees * (Math.PI / 180); // radians

 The fillStyle property of the 2d rendering context is the color that shapes and paths are filled in as. context.fillStyle = "rgb(255, 0, 0)"; context.fillRect(40, 40, 100, 100); // Red square context.fillRect(180, 40, 100, 100); // Red square context.fillStyle = "rgb(0, 0, 0)"; context.fillRect(320, 40, 100, 100); // Black square RESULT:

 The strokeStyle property of the 2d rendering context is the color that shapes and paths are drawn as. context.strokeStyle = "rgb(255, 0, 0)"; context.strokeRect(40, 40, 100, 100); // Red square context.strokeRect(180, 40, 100, 100); // Red square context.strokeStyle = "rgb(0, 0, 0)"; context.strokeRect(320, 40, 100, 100); // Black square RESULT:

 The lineWidth property of the 2d rendering context defines the thickness of the drawing line. context.lineWidth = 5; // Make lines thick context.strokeStyle = "rgb(255, 0, 0)"; context.beginPath(); context.moveTo(40, 180); context.lineTo(420, 180); // Red line context.closePath(); context.stroke(); context.lineWidth = 20; // Make lines even thicker context.strokeStyle = "rgb(0, 0, 0)"; context.beginPath(); context.moveTo(40, 220); context.lineTo(420, 220); // Black line context.closePath(); context.stroke(); RESULT:

 We can draw text inside a canvas using fillText method  It is a picture, thus not selectable  It can be manipulated as other pictures  Example:  default font size is 10px  default font is San-Serif var text = "Hello, World!"; context.fillText(text, 40, 40); Coordinate of bottom left corner of the text

 Changing text’s font and size:  Makes the font italic: context.font = "30px serif” context.font = "italic 30px serif"; RESULT:

 We also can create stroked text: var text = "Hello, World!"; context.font = "italic 60px serif"; context.strokeText(text, 40, 100); RESULT:

 Normal HTML element can be set to 100% width and 100% height. This doesn’t work with canvas element (100% is interpreted as 100px).  The easiest way to do it is to set the width and height of the canvas element precisely to the pixel width and height of the browser window.

 Example .attr() is JQuery method for changing an element’s attribute  innerWidth & innerHeight attributes are used instead of width and height, because the later are not working properly on all browsers var canvas = $("#myCanvas"); var context = canvas.get(0).getContext("2d"); canvas.attr("width", $(window).get(0).innerWidth); canvas.attr("height", $(window).get(0).innerHeight); context.fillRect(0, 0, canvas.width(), canvas.height());

 This approach is not really working properly. We see white gap around the canvas, and there are scrollbars in the browser window RESULT:

 To fix this, we need to use some CSS * { margin: 0; padding: 0; } html, body { height: 100%; width: 100%; } canvas { display: block; } RESULT:

 Visit : canvas-example/ canvas-example/

 Foundation HTML5 Canvas: For Games and Entertainment 1st Edition  Author: Rob Hawkes