Basics of HTML Canvas. Material taken from http://www.w3schools.com CSCU9B2



Similar documents
CIS 467/602-01: Data Visualization

Fireworks CS4 Tutorial Part 1: Intro

HTML5 Canvas. Drawing on a Web page. Laura Farinetti - DAUIN

Website Login Integration

Website Development. 2 Text. 2.1 Fonts. Terry Marris September We see how to format text and separate structure from content.

DEVELOPING EFFECT OF HTML5 TECHNOLOGY IN WEB GAME

Web Design Basics. Cindy Royal, Ph.D. Associate Professor Texas State University

Recipes4Success. Animate a Rocket Ship. Frames 6 - Drawing Tools

Essential HTML & CSS for WordPress. Mark Raymond Luminys, Inc mraymond@luminys.com

TUTORIAL 4 Building a Navigation Bar with Fireworks

Web Design and Databases WD: Class 7: HTML and CSS Part 3

Last week we talked about creating your own tags: div tags and span tags. A div tag goes around other tags, e.g.,:

ITNP43: HTML Lecture 4

Single Page Web App Generator (SPWAG)

ICE: HTML, CSS, and Validation

Visualization: Combo Chart - Google Chart Tools - Google Code

Dashboard Skin Tutorial. For ETS2 HTML5 Mobile Dashboard v3.0.2

HTML CSS Basic Structure. HTML Structure [Source Code] CSS Structure [Cascading Styles] DIV or ID Tags and Classes. The BOX MODEL

Positioning Container Elements

Using Style Sheets for Consistency

What is CSS? Official W3C standard for controlling presentation Style sheets rely on underlying markup structure

CSS Techniques: Scrolling gradient over a fixed background

Dreamweaver CS4 Day 2 Creating a Website using Div Tags, CSS, and Templates

Web Building Blocks. Joseph Gilbert User Experience Web Developer University of Virginia Library

Web Development CSE2WD Final Examination June (a) Which organisation is primarily responsible for HTML, CSS and DOM standards?

CHAPTER 10. When you complete this chapter, you will be able to:

Web Design and Development ACS Chapter 9. Page Structure

Unit 21 - Creating a Button in Macromedia Flash

How to Use the Drawing Toolbar in Microsoft Word

Create Webpages using HTML and CSS

Yandex.Widgets Quick start

Adobe Illustrator CS6. Illustrating Innovative Web Design

jquery Tutorial for Beginners: Nothing But the Goods

Step 1: Setting up the Document/Poster

Web Design Revision. AQA AS-Level Computing COMP2. 39 minutes. 39 marks. Page 1 of 17

JavaScript By: A. Mousavi & P. Broomhead SERG, School of Engineering Design, Brunel University, UK

Web Design with CSS and CSS3. Dr. Jan Stelovsky

Style & Layout in the web: CSS and Bootstrap

Using HTML5 Pack for ADOBE ILLUSTRATOR CS5

CREATING A NEWSLETTER IN ADOBE DREAMWEAVER CS5 (step-by-step directions)

WEB DESIGN COURSE CONTENT

Outline of CSS: Cascading Style Sheets

Advanced Web Design. Zac Van Note.

CSS 101. CSS CODE The code in a style sheet is made up of rules of the following types

Lab 2: Visualization with d3.js

Overview of the Adobe Flash Professional CS6 workspace

How to Add a Transparent Flash FLV movie to your Web Page Tutorial by R. Berdan Oct

{color:blue; font-size: 12px;}

WHITEPAPER. Skinning Guide. Let s chat Velaro info@velaro.com by Velaro

PLAYER DEVELOPER GUIDE

Digital Marketing EasyEditor Guide Dynamic

HTML Tables. IT 3203 Introduction to Web Development

EUROPEAN COMPUTER DRIVING LICENCE / INTERNATIONAL COMPUTER DRIVING LICENCE WEB EDITING

Using Microsoft Word. Working With Objects

CST 150 Web Design I CSS Review - In-Class Lab

How to create pop-up menus

Joomla Article Advanced Topics: Table Layouts

Adobe InDesign Creative Cloud

04 Links & Images. 1 The Anchor Tag. 1.1 Hyperlinks

This document will describe how you can create your own, fully responsive. drag and drop template to use in the creator.

Create Your own Company s Design Theme

Fireworks 3 Animation and Rollovers

Dreamweaver and Fireworks MX Integration Brian Hogan

HTML TIPS FOR DESIGNING

Interactive Data Visualization for the Web Scott Murray

Building a Horizontal Menu in Dreamweaver CS3 Using Spry R. Berdan

Contents. Downloading the Data Files Centering Page Elements... 6

HOWTO annotate documents in Microsoft Word

Course Project Lab 3 - Creating a Logo (Illustrator)

Figure 3.5: Exporting SWF Files

D3.JS: Data-Driven Documents

Windows Presentation Foundation Tutorial 1

Graphic Design Basics Tutorial

Lession: 2 Animation Tool: Synfig Card or Page based Icon and Event based Time based Pencil: Synfig Studio: Getting Started: Toolbox Canvas Panels

Basics of HTML (some repetition) Cascading Style Sheets (some repetition) Web Design

Web Development 1 A4 Project Description Web Architecture

Creative Guidelines for s

THINKDIGITAL. ZOO Ad specifications

Embedding a Data View dynamic report into an existing web-page

MULTIMEDIA LABORATORY MANUAL FOR 2 ND SEM IS AND CS ( )

Visualizing an OrientDB Graph Database with KeyLines

Campaign Guidelines and Best Practices

Cascading Style Sheet (CSS) Tutorial Using Notepad. Step by step instructions with full color screen shots

Instructions for Creating a Poster for Arts and Humanities Research Day Using PowerPoint

MCH Strategic Data Best Practices Review

Working With Animation: Introduction to Flash

Mobile Web Site Style Guide

Slide.Show Quick Start Guide

Transcription:

Basics of HTML Canvas Material taken from http://www.w3schools.com CSCU9B2 CSCU9B2 1

We are going to cover What is HTML canvas and what it can do Most commonly used canvas methods Example of a simple animated application CSCU9B2 2

What is Canvas? Canvas is a medium for oil painting, typically stretched across a wooden frame.

What is HTML Canvas? HTML canvas is about drawing graphics There is a set of JavaScript methods (APIs) for drawing graphics (lines, boxes, circles, shapes). HTML canvas is a rectangular area on a web page, specified with the <canvas> element. The HTML <canvas> element (introduced in HTML5) is a container for HTML graphics. CSCU9B2 4

What can HTML canvas do? Draw colorful text Draw graphical shapes Can be animated. Everything is possible: from simple bouncing balls to complex animations Can be interactive and respond to events Offer lots of possibilities for HTML gaming applications CSCU9B2 5

Examples http://www.effectgames.com/demos/canvascycle/ http://hakim.se/experiments/html5/blob/03/ http://bomomo.com/ http://hakim.se/experiments/html5/magnetic/02/ CSCU9B2 6

Canvas element Looks like this: <canvas id="mycanvas" width="200" height="100"></canvas> Must have an id attribute so it can be referred to by JavaScript; The width and height attribute is necessary to define the size of the canvas. CSCU9B2 7

Drawing on the Canvas All drawing on the HTML canvas must be done with JavaScript in three steps: 1. Find the canvas element 2. Create a drawing object 3. Draw on the canvas E.g. var canvas = document.getelementbyid("mycanvas"); var ctx = canvas.getcontext("2d"); ctx.fillstyle = "#FF0000"; ctx.fillrect(0,0,150,75); CSCU9B2 8

HTML Canvas Coordinates The HTML canvas is two-dimensional. The upper-left corner of the canvas has the coordinates (0,0) In the previous example, you saw this method used: fillrect(0,0,150,75) This means: Start at the upper-left corner (0,0) and draw a 150 x 75 pixels rectangle. CSCU9B2 9

Canvas fillstyle and strokestyle The fillstyle property is used to define a fill-color (or gradient) for the drawing. The strokestyle defines the color of the line around the drawing. E.g. ctx.fillstyle="#ff0000"; ctx.strokestyle = blue ; ctx.fillrect(20,20,150,50); ctx.strokerect(20,20,200,100); CSCU9B2 10

Canvas - Gradients Gradients can be used to fill rectangles, circles, lines, text, etc. Two different types of gradient: createlineargradient(x,y,x1,y1) createradialgradient(x,y,r,x1,y1,r1) Need to specify two or more color "stops Eg start and finish colours To use the gradient, set the fillstyle or strokestyle property to the gradient CSCU9B2 11

Example of linear gradient Create a linear gradient. Fill rectangle with the gradient: var c=document.getelementbyid("mycanvas"); var ctx=c.getcontext("2d"); // Create gradient var grd=ctx.createlineargradient(0,0,200,0); grd.addcolorstop(0,"red"); grd.addcolorstop(1,"white"); // Fill with gradient ctx.fillstyle=grd; ctx.fillrect(0,0,150,80); CSCU9B2 12

Draw a Line To draw a straight line on a canvas, you use these methods: E.g. moveto(x,y) defines the starting point of the line lineto(x,y) defines the ending point of the line stroke() method draws the line var canvas = document.getelementbyid("mycanvas"); var ctx = canvas.getcontext("2d"); ctx.moveto(0,0); ctx.lineto(200,100); ctx.stroke(); CSCU9B2 13

Draw a Circle To draw a circle on a canvas, you use the following methods: Eg. beginpath(); arc(x,y,r,start,stop) var canvas = document.getelementbyid("mycanvas"); var ctx = canvas.getcontext("2d"); ctx.beginpath(); ctx.arc(95,50,40,0,2*math.pi); ctx.stroke(); CSCU9B2 14

Drawing Text on the Canvas To draw text on a canvas, the most important property and methods are: font - defines the font properties for the text textalign Aligns the text. Possible values: start, end, left, right, center stroketext(text,x,y) - Draws text on the canvas filltext(text,x,y) - Draws "filled" text on the canvas E.g., Write the text in orange with a blue border around the text ctx.font = "italic bold 56px Arial, sans-serif"; ctx.fillstyle="orange"; ctx.textalign = "start"; ctx.filltext("some text", 10, 50); ctx.linewidth = 3; ctx.strokestyle="blue"; ctx.stroketext("some text", 10, 50); CSCU9B2 15

Canvas Images To draw an image on a canvas, use the following method: drawimage(image,x,y,w,h) E.g. var myimage = new Image(); myimage.src = "image2.jpeg"; ctx.drawimage(myimage, 20, 20, 100, 100); CSCU9B2 16

Animation Changing things over time Animation on a canvas is achieved by: 1. Defining drawing operations in a function a) Clear the canvas b) Draw objects slightly differently from previous time eg location, size, rotation 2. Call function repeatedly at a defined time interval setinterval(animate, 30); CSCU9B2 17

A simple animation - example <canvas id="my_canvas" width="800" height="700"> <script> var ctx = document.getelementbyid("my_canvas").getcontext("2d"); var cw = ctx.canvas.width; var ch = ctx.canvas.height; var y = 0, x=0; function animate() { ctx.save(); ctx.clearrect(0, 0, cw, ch); ctx.rotate(-0.3); ctx.fillstyle="green"; ctx.fillrect(0, y, 50, 50); y++; ctx.rotate(0.8); ctx.fillstyle= red ; ctx.fillrect(x, 0, 50, 50); x++; ctx.restore(); } var animateinterval = setinterval(animate, 30); ctx.canvas.addeventlistener('click', function(event){ clearinterval(animateinterval);}); </script> CSCU9B2 18

Animation using CSS Animations can be created with CSS Lets an element gradually change styles Color, position or any other property Transitions Key frames Style values at a particular time point Timing CSCU9B2 19

CSS Transitions - example <html> <head> <style> div { width: 100px; height: 100px; background: red; transition: width 2s, height 2s, transform 2s; } div:hover { width: 300px; height: 300px; transform: rotate(180deg); } </style> </head> <body> <div><p>some text.</p></div> </body> </html> CSCU9B2 20

CSS Keyframes - example <html> <head> <style> div { width: 100px; height: 100px; background-color: red; animation-name: example; animation-duration: 4s; } @keyframes example { from {background-color: red; width: 100px;} to {background-color: yellow; width: 200px;} } </style> </head> <body> <div></div> </body> </html> CSCU9B2 21

Web Technologies The End CSCU9B2 22