Using Bitmap graphics Having looked at vector graphics in the canvas, we will now look at using bitmap graphics.

Slides:



Advertisements
Similar presentations
Graphics Programming. Introduction GOAL: Build the Indexer Client Event-driven vs. Sequential programs Terminology – Top-level windows are called “frame.
Advertisements

CS2984: Introduction to Media Computation Drawing directly on images.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
IDIS 110 Foundations of Information Technology Keith Vander Linden Calvin College.
Lecture # 11 JavaScript Graphics. Scalable Vector Graphics (SVG) Scalable Vector Graphics (SVG), as the name implies, are - scalable (without pixelation):
Neal Stublen Why Use a Canvas?  Flexibility… Use of drawing primitives (lines, arcs, text) Direct access to element display pixels.
HTML 5 Previous version: HTML4.01, 1999 Still work in progress, most browsers support some of it.
Objectives Define photo editing software
User Interface Programming in C#: Graphics
ObjectDraw and Objects Early Chris Nevison Barbara Wells.
Higher-level PHP constructs for manipulating image files.
Exercise 1 - Poisson Image completion using Discrete Poisson Linear Systems.
Director of Computer Center, DaYeh University Ku-Yaw Chang.
Images (1) Three most popular formats – Graphics Interchange Format (GIF) – Joint Photographic Experts Group (JPEG) – Portable Network Graphics (PNG) –
Introduction To CSS.. HTML Review What is HTML used for? Give some examples of formatting tags in HTML? HTML is the most widely used language on the Web.
Department of Information Technology Chapter 8 - Creating Hypertext links Lecturer: Ms Melinda Chung.
Getting Started with Canvas IDIA Spring 2013 Bridget M. Blodgett.
Manipulating the DOM CST 200 – JavaScript 3 –
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 TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
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.
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
Tkinter Canvas.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Adobe Photoshop CS5 – Illustrated Unit A: Getting Started with Photoshop CS5.
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.
CSI 3125, Preliminaries, page 1 Data Type, Variables.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
08 – Canvas(2) Informatics Department Parahyangan Catholic University.
Chapter 10 Dynamic HTML (DHTML) JavaScript, Third Edition.
07 – HTML5 Canvas (1) Informatics Department Parahyangan Catholic University.
1 Sections 5.1 – 5.2 Digital Image Processing Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Internet & World Wide Web How to Program, 5/e.  JavaScript events  allow scripts to respond to user interactions and modify the page accordingly  Events.
Introduction to Web Site Development Department of Computer Science California State University, Los Angeles Lecture 9: JavaScript.
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.
CompSci 4 Java 4 Apr 14, 2009 Prof. Susan Rodger.
HTML5 ProgLan HTML5 Making a Game on the Canvas Part 1.
Photoshop CS6 – Nelson Unit 3: Photoshop CS6. Objectives Define photo editing software Start Photoshop and view the workspace Use the Zoom tool and the.
Unit 2.6 Data Representation Lesson 3 ‒ Images
Programming Web Pages with JavaScript
Getting Started with Adobe Photoshop CS6
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
Canvas Notes
Scope, Objects, Strings, Numbers
Images Presentation Name Course Name Unit # – Lesson #.# – Lesson Name
ISC440: Web Programming II Ch14: HTML5 Canvas
The Canvas.
Geb Thomas Adapted from the OpenGL Programming Guide
JavaScript.
Images Presentation Name Course Name Unit # – Lesson #.# – Lesson Name
Pertemuan 1b
Drawing Graphics in JavaScript
CSc 337 Lecture 21: Canvas.
Graphics Canvas.
Programming games Share your plans for your virtual something.
JavaScript – Let’s Draw!
CSc 337 Lecture 19: Canvas.
Presentation transcript:

Using Bitmap graphics Having looked at vector graphics in the canvas, we will now look at using bitmap graphics

Bitmap for a new canvas To understand some of the examples on some of the following slides, it must be remembered that When a canvas is initialized, its bitmap is set to transparent black ( Reference: )

Capturing bitmap data for (any part of) the canvas The context class provides a method for accessing the bitmap for the entire canvas or for any part of the canvas Format: getImageData(X,Y,width,height), where (X,Y) is the upper-left corner of the area whose bitmap is required width and height specify the size of the area whose bitmap is required Example usage: var imageData = context.getImageData(0, 0, canvas.width, canvas.height); The value returned by the function is an object of class ImageData interface ImageData { readonly attribute unsigned long width; readonly attribute unsigned long height; readonly attribute Uint8ClampedArray data; }; We can now read and/or alter the contents of the data array in this object Each pixel in the bitmap is represented by four elements in the image data, representing the red, green, blue, and alpha component of the pixel

Accessing bitmap data Below, we access the bitmap data for a 50x50 rectangle at the upper left corner of a fresh canvas We see that the RGBA data for the pixel in the upper-left corner is (0,0,0,0) That is this pixel is transparent black - so are all the others <script var canvas= document.getElementById('myCanvas'); var context = canvas.getContext("2d"); var bitmap = context.getImageData(0, 0,50,50); var data=bitmap.data; alert('RGBA for first pixel is ('+data[0]+','+data[1]+','+data[2]+','+data[3]+ ')');

Accessing bitmap data (contd.) Below, we fill the canvas with semi-opaque red before accessing the bitmap data for a 50x50 rectangle at the upper left corner We see that the RGBA data for the pixel in the upper- left corner is (255,0,0,128), where 128/255 is the best available approximation to 0.5 <script … context.fillStyle="rgba(255,0,0,0.5)"; context.fillRect(0,0,canvas.width,canvas.height); var bitmap = context.getImageData(0, 0, 50,50); var data=bitmap.data; alert('RGBA for first pixel is ('+data[0]+','+data[1]+','+data[2]+','+data[3]+')');

Modifying the image by editing the bitmap Below, we swop the red and green bytes in the bitmap data for a 50x50 rectangle at the upper left corner of the canvas and then use the putImageData method to write the modified bitmap data into the context <script … context.fillStyle=“rgb(255,0,0)"; context.fillRect(0,0,100,100); var bitmap = context.getImageData(0, 0, 50,50); var data=bitmap.data; //Remember that Javascript arrays are assigned by reference //so changing data will change the bitmap for (var i = 0; i < data.length; i += 4) { var temp1 = data[i]; //red var temp2 = data[i + 1]; //green data[i] = temp2; data[i + 1] = temp1; } context.putImageData(bitmap, 0, 0);

Reminder: Javascript Image object A JavaScript Image Object can be created in several ways: –by using one of the constructor methods below –by an element in the HTML source code (or by using the createElement method of the DOM document object) Constructors Image(), Image(in unsigned long width) Image(in unsigned long width, in unsigned long height) interface HTMLImageElement : HTMLElement { attribute DOMString alt; attribute DOMString src; attribute DOMString useMap; attribute boolean isMap; attribute unsigned long width; attribute unsigned long height; readonly attribute boolean complete; }; Reference:

We can import external images into the context bitmap Create an Image object to access an external image Then use drawImage to write image's data into the context <script … var imageObj = new Image(); imageObj.src = 'uccQuad.jpg'; var destX = 10; var destY = 10; var width = 200; var height = 160; context.drawImage(imageObj, destX,destY,width,height);

Modifying an imported image Note how, since initial canvas was transparent, we can modify all of it <script … var imageObj = new Image(); imageObj.src = 'uccQuad.jpg'; var destX = 10; var destY = 10; var width = 200; var height = 160; context.drawImage(imageObj, destX,destY,width,height); var imageData = context.getImageData(0,0,canvas.width,canvas.height); var data=imageData.data; for (var i = 0; i < data.length; i += 4) {data[i]=255-data[i]; data[i+1]=255-data[i+1]; data[i+2]=255-data[i+2]; } context.putImageData(imageData, 0, 0); Modifying the pixels outside the imported image does not cause any problem, since they are all transparent

Animation We will be considering animation in the canvas but, first, we will consider a range of related topics

Reminder: variable scope in Javascript In JavaScript, variable scope is a bit strange. It is determined –not just by where a variable is declared –but, in some cases, whether the keyword var is used Global variables –A variable declared outside any function definition can be referenced anywhere in the Javascript on the current page –A variable declared inside a function without the var keyword become global once the function is called Local variables –A variable declared with the keyword var inside a function is local