Neal Stublen Why Use a Canvas?  Flexibility… Use of drawing primitives (lines, arcs, text) Direct access to element display pixels.

Slides:



Advertisements
Similar presentations
HTML5 Overview HOANGPT2. 1. General 2. New Elements List 3.
Advertisements

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.
Working with images and scenes CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.5 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
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.
Programming Club IIT Kanpur
HTML 5 Previous version: HTML4.01, 1999 Still work in progress, most browsers support some of it.
1 The MITRE Using XSLT and XPath to Generate SVG Roger L. Costello XML Technologies.
Graphics Standard Grade Computing. Graphics Package n A graphics package is another General Purpose Package. n It is used to draw pictures on the monitor.
Scalable Vector Graphics (SVG) Aug’10 – Dec ’10. Introduction Scalable Vector Graphics (SVG), an extremely versatile 2-D graphics format designed primarily.
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
Chapter 3 Adding Images in HTML. Agenda Understanding Web Page Images Prepare Your Images for the Web Insert an Image Specify an Image Size Add Alternative.
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,
IT Engineering I Instructor: Rezvan Shiravi
1 Using XSLT and XPath to Generate SVG Roger L. Costello XML Technologies.
Images (1) Three most popular formats – Graphics Interchange Format (GIF) – Joint Photographic Experts Group (JPEG) – Portable Network Graphics (PNG) –
HTML5 – The Power of HTML5 – The Power of Thomas Lewis HTML5 Principal Technical Evangelist Microsoft | asimplepixel.tumblr.com.
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.
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.
SVG for Designers Tom Hoferek. Objectives Introduce SVG Illustrate its capabilities Demonstrate SVG in action Speculate, discuss, answer questions.
1 Scalable Vector Graphics (SVG). 2 SVG SVG is an application language of XML. “SVG is a language for describing two- dimensional graphics in XML. SVG.
Lecture 16 Image Document Formats. Bitmap vs. Vector Images Bitmaps do not generally.
McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. SVG Ellen Pearlman Eileen Mullin Programming the Web Using XML.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
Brent M. Dingle, Ph.D Game Design and Development Program Mathematics, Statistics and Computer Science University of Wisconsin - Stout HTML5 – Canvas.
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.
Digital Media Dr. Jim Rowan ITEC So far… We have compared bitmapped graphics and vector graphics We have discussed bitmapped images, some file formats.
Cascading Style Sheets Robin Burke ECT 360. Outline Midterm CSS CSS selection Positioning SVG Final project.
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.
Digital Media Lecture 5: Vector Graphics Georgia Gwinnett College School of Science and Technology Dr. Jim Rowan.
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.
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.
Main characteristics of Vector graphics  Vector graphics provide an elegant way of constructing digital images (diagrams, technical illustration and.
HTML5 ProgLan HTML5 Making a Game on the Canvas Part 1.
CHAP 2. CANVAS API.  Dynamically generate and render graphics, charts, images and animations  SVG (Scalable Vector Graphics) vs. Canvas  Bitmap canvas.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
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
Chapter 4: Scalable Vector Graphics (SVG)
Canvas Notes
Web Programming Language
A framework to create SVG graphics
Introduction to D3.js and Bar Chart in D3.js
ISC440: Web Programming II Ch14: HTML5 Canvas
The Canvas.
Digital Media Dr. Jim Rowan ITEC 2110.
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
JavaScript – Let’s Draw!
CSc 337 Lecture 19: Canvas.
HTML5 – A few features L. Grewe.
Presentation transcript:

Neal Stublen

Why Use a Canvas?  Flexibility… Use of drawing primitives (lines, arcs, text) Direct access to element display pixels  Use a element in your HTML  Supply an “id” attribute to access the element from JavaScript

Canvas Coordinate System  Upper-left corner at (0, 0)  Lower right at (width, height)

Filling a Rectangle $('document').ready(function() { var canvas = document.getElementById('mycanvas'); var context = canvas.getContext('2d'); context.fillStyle = 'rgba(0, 0, 255, 0.5)'; context.fillRect(10, 10, 100, 100); context.strokeStyle = 'red'; context.strokeRect(10, 10, 100, 100); });

Filling with an Image $('document').ready(function() { var img = new Image(); img.src = ‘./images/bg-bike.png’; img.onload = function() { pattern = context.createPattern(img, 'repeat'); context.fillStyle = pattern; context.fillRect(10, 10, 100, 100); context.strokeRect(10, 10, 100, 100); }; });

Filling with a Gradient $('document').ready(function() { var gradient = context.createLinearGradient(0, 0, 0, 200); gradient.addColorStop(0, 'blue'); gradient.addColorStop(1, 'white'); context.fillStyle = gradient; context.fillRect(10, 10, 100, 100); context.strokeRect(10, 10, 100, 100) });

Drawing with Paths  We’ve seen drawing rectangles  We can draw other shapes, but we need to define a “path” for the shape

Drawing with Paths $('document').ready(function() { context.beginPath(); context.arc(50, 50, 30, 0, Math.PI * 2, true); context.closePath(); context.strokeStyle = 'red'; context.fillStyle = 'blue'; context.lineWidth = 3; context.fill(); context.stroke(); };

Experiment  Experiment inside the browser

Manipulating Pixels var image = document.getElementById('myimage'); context.drawImage(image, (canvas.width - image.width) / 2, (canvas.height - image.height) / 2);

Manipulating Pixels var imageData = context.getImageData(0, 0, canvas.width, canvas.height); var pixelData = imageData.data; for (var i = 0; i < pixelData.length; i += 4) { var r = pixelData[i]; var g = pixelData[i+1]; var b = pixelData[i+2]; var gray = r * g * b * 0.11; pixelData[i] = gray; pixelData[i+1] = gray; pixelData[i+2] = gray; } context.putImageData(imageData, 0, 0);

Displaying Text context.textAlign = 'left'; context.font = '18px LeagueGothic'; context.fillText('Hello!');

Scalable Vector Graphics  Describe vector graphics using XML  Preserves shape when size changes  Performs many tasks similar to canvas Gradients, paths, shapes, text  XML descriptions instead of JavaScript

An SVG Circle <svg xmlns=" viewbox=" ”>

An SVG Rectangle <svg xmlns=" viewbox=" "> <rect x="10" y="10" width="100" height="100" fill="blue" stroke="red" stroke-width="3" />

SVG Complexities  SVG drawings can become very complex  Inkscape – Free vector graphics editor  Raphael – JavaScript library for working with SVG

Why SVG?  SVG can provide a much smaller description of an image (smaller files) Rather than describing the RGBA values of each pixel, it can describe lines and shapes regardless of size  Scaling doesn’t produce artifacts No jagged edges

Canvas or SVG?  Canvas provides pixel manipulation  Canvas provides saving images to PNG and JPEG files  SVG provides DOM access to drawing elements  SVG has more tools

Steps to Drag and Drop  Set the draggable attribute on any elements you want to be draggable  Set an event listener for the dragstart event on any draggable elements  Set an event listener for the dragover and drop events on any elements that can accept draggable elements

Set Draggable  Set the draggable attribute on an element  This is not a boolean attribute

Watch for dragstart $('.src').bind('dragstart', function(event) { event.originalEvent.dataTransfer.setData('text/plain', event.target.getAttribute('id')); });

Watch for dragover $(‘.tgt').bind('dragover', function(event) { event.preventDefault(); });

Watch for drop $(‘.tgt').bind('drop', function(event) { var src = event.originalEvent.dataTransfer.getData("text/plain"); });

Cat and Mouse  HTML Herald supports drag and drop at the cat article Drag the mice onto the cat

Draggable Divs  Create a series of colored div elements that can be dragged onto other divs to change their color  Swap position of colored divs by dragging them onto one another