Canvas, SVG, Workers Doncho Minkov Telerik Corporation www.telerik.com.

Slides:



Advertisements
Similar presentations
New Tags, New Attributes, New JavaScript APIs, Forms, Validation, Audio, Video, SVG, Canvas Doncho Minkov Telerik Corporation
Advertisements

An Introduction Timmy Kokke | UNIT4 Internet Solutions | Expression Blend MVP / Webguy.
The Document Object Model (DOM) 1 JavaScript is an object-based language—that is, it’s based on manipulating objects by changing each object’s properties.
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
Introduction to HTML5 Programming donghao. HTML5 is the New HTML Standard New Elements, Attributes. Full CSS3 Support Video and Audio 2D/3D Graphics Local.
Lesson 12- Unit L Programming Web Pages with JavaScript.
HTML 5 Previous version: HTML4.01, 1999 Still work in progress, most browsers support some of it.
Chapter 16 Dynamic HTML and Animation The Web Warrior Guide to Web Design Technologies.
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.
Naveen Prabhu Quadwave Consulting Pvt.Ltd.. Agenda  HTML 5 – The Standards  Browsers and HTML5 support  HTML5 Learnability  Perspectives of Web Designer,
INTRODUCTION TO HTML5 Drag and Drop in HTML5. Browsers Support  Currently, most of the major HTML5 desktop web browsers support the new HTML5 drag-and-drop.
DHTML. What is DHTML?  DHTML is the combination of several built-in browser features in fourth generation browsers that enable a web page to be more.
Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.
4.1 JavaScript Introduction
JQuery 10/21. Today jQuery Some cool tools around the web JavaScript Libraries Drawing libraries HTML Frameworks Conventions.
A really fairly simple guide to: mobile browser-based application development (part 4, JQuery & DOM) Chris Greenhalgh G54UBI / Chris Greenhalgh.
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
DOM and JavaScript Aryo Pinandito.
D2L Notes Be sure to submit your link in the dropbox provided on D2L You can just upload an empty text file if a file upload is required Do not use D2L.
New Tags, New Attributes, New JavaScript APIs, Forms, Validation, Audio, Video, SVG, Canvas Doncho Minkov Telerik Corporation Technical.
CITS1231 Web Technologies JavaScript and Document Object Model.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
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.
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.
INTRODUCTION TO HTML5 Using jQuery with HTML5. Introducing jQuery  Although it is not a part of any W3C or WHATWG specification, jQuery performs an important.
Using Client-Side Scripts to Enhance Web Applications 1.
New Tags, New Attributes, New JavaScript APIs, Forms, Validation, Audio, Video, SVG, Canvas Doncho Minkov Telerik Corporation
Unleash the Power of jQuery Doncho Minkov Telerik Software Academy academy.telerik.com Senior Technical Trainer
Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54
Web Development 101 Presented by John Valance
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.
Unleash the Power of jQuery Learning & Development Team Telerik Software Academy.
Programming games Context of what we are doing. Drawing on canvas. Homework: [Complete coin toss examples.] Do your own drawings. Upload files to website.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
 Scripts that execute in response to some event  User clicking on something  Script does NOT execute as part of page loading  DOM facilities like.
Web Programming Overview. Introduction HTML is limited - it cannot manipulate data How Web pages are extended (include): –Java: an object-oriented programming.
Host Objects: Browsers and the DOM
Understanding JavaScript and Coding Essentials Lesson 8.
Document Object Model Nasrullah. DOM When a page is loaded,browser creates a Document Object Model of the Page.
08 – Canvas(2) Informatics Department Parahyangan Catholic University.
Introduction to JavaScript LIS390W1A Web Technologies and Techniques 24 Oct M. Cameron Jones.
Chapter 10 Dynamic HTML (DHTML) JavaScript, Third Edition.
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Making web pages interactive with JavaScript.
07 – HTML5 Canvas (1) Informatics Department Parahyangan Catholic University.
Jozef Goetz contribution, © by Pearson Education, Inc. All Rights Reserved.
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
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.
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 5 Host Objects: Browsers.
Web Storage and Cookies Cookies, Local and Session Storage SoftUni Team Technical Trainers Software University
REEM ALMOTIRI Information Technology Department Majmaah University.
XHTML. What Is XHTML? XHTML stands for EXtensible HyperText Markup Language XHTML is almost identical to HTML XHTML is stricter than HTML XHTML is HTML.
THE DOM.
Introduction to.
DHTML.
What is a Function Expression?
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Internet of the Past.
Web Programming Language
JavaScript Functions.
Application with Cross-Platform GUI
Drawing Graphics in JavaScript
JavaScript: BOM and DOM CS Programming Languages for Web Applications
JavaScript – Let’s Draw!
Presentation transcript:

Canvas, SVG, Workers Doncho Minkov Telerik Corporation

 New JavaScript APIs  New Selectors  Canvas JavaScript API  Web Workers  Drag and Drop  HTML5 Storage  HTML DOM Extensions  Event Listeners

What a programmer must know?

 New selectors  Threading (Web Workers)  UI APIs  Canvas  Drag and Drop  Local and Session Storage  Extension to HTMLDocument

 In HTML5 we can select elements by ClassName  Moreover there’s now possibility to fetch elements that match provided CSS syntax var elements = document.getElementsByClassName('entry'); var elements = document.querySelectorAll("ul li:nth-child(odd)"); document.querySelectorAll("ul li:nth-child(odd)"); var first_td = document.querySelector("table.test > tr > td"); document.querySelector("table.test > tr > td");

 Selecting the first div met  Selecting the first item with class SomeClass  Selecting the first item with id someID  Selecting all the divs in the current container var elements = document.querySelector("div"); var elements = document.querySelectorAll("div"); var elements = document.querySelector(".SomeClass"); var elements = document.querySelector("#someID");

How to Manipulate Canvas Dynamically?

 Canvas  Dynamic, Scriptable rendering of 2D images  Uses JavaScript to draw  Resolution-dependent bitmap  Can draw text as well This text is displayed if your browser does not support This text is displayed if your browser does not support HTML5 Canvas. HTML5 Canvas.</canvas> var example = document.getElementById('ExampleCanvas'); var context = example.getContext('2d'); context.fillStyle = "rgb(255,0,0)"; context.fillRect(30, 30, 50, 50);

 fillStyle  Sets the drawing color  Default fillStyle is solid black  But you can set it to whatever you like  fillRect(x, y, width, height)  Draws a rectangle  Filled with the current fillStyle

 strokeStyle  Sets the stroke color  strokeRect(x, y, width, height)  Draws an rectangle with the current strokeStyle  strokeRect doesn’t fill in the middle  It just draws the edges  clearRect(x, y, width, height) clears the pixels in the specified rectangle

 What is a Path?  Something that is about to be drawn  It is not drawn yet context.beginPath(); context.moveTo(0, 40); context.lineTo(240, 40); context.moveTo(260, 40); context.lineTo(500, 40); context.moveTo(495, 35); context.lineTo(500, 40); context.lineTo(495, 45);

Live Demo

Multithreading in JavaScript

 API for running scripts in the background  Independently of any user interface scripts  Workers are expected to be long-lived  Have a high start-up performance cost and a high per-instance memory cost  Might be partially replaced by Window.setTimeout() function

 Workers are separate JavaScript processes running in separate threads  Workers execute concurrently  Workers don’t block the UI  Workers allow you to extract up to the last drop of juice from a multicore CPU  Workers can be dedicated (single tab) or shared among tabs/windows  Workers will be persistent too (coming soon)  They’ll keep running after the browser has quit

 If we call function by setTimeout, the execution of script and UI are suspended  When we call function in worker, it doesn’t affect UI and execution flow in any way  To create Worker, we put JavaScript in separate file and create new Worker instance:  We can communicate with worker using postMessage function and onmessage listener var worker = new Worker(‘extra_work.js');

 Messages are send to all threads in our application: main.js: var worker = new Worker(‘extra_work.js'); worker.onmessage = function (event) { alert(event.data); }; extra_work.js: //do some work; when done post message. // some_data could be string, array, object etc. postMessage(some_data);

Live Demo

Drag and Drop, Local and Session Storage

 Drag and Drop  attribute draggable="true"  Events: dragstart, dragstop, dragenter, dragleave, dropend

Live Demo

Local and Session

 Local Storage  Store data locally  Similar to cookies, but can store much larger amount of data  Same Origin Restrictions  localStorage.setItem(key, value)  localStorage.getItem(key)  Local and Session Storages can only store strings!

function saveState(text){ localStorage["text"] = text; localStorage["text"] = text;} function restoreState(){ return localStorage["text"]; return localStorage["text"];}  Local Storage function saveState(text){ localStorage.setValue("text", text); localStorage.setValue("text", text);} function restoreState(){ return localStorage.getValue("text"); return localStorage.getValue("text");}  Same as

 Session Storage  Similar to Local Storage  Lasts as long as browser is open  Opening page in new window or tab starts new sessions  Great for sensitive data (e.g. banking sessions)

function incrementLoads() { if (!sessionStorage.loadCounter) { if (!sessionStorage.loadCounter) { sessionStorage["loadCounter"] = 0; sessionStorage["loadCounter"] = 0; } var currentCount = var currentCount = parseInt(sessionStorage["loadCounter"]); parseInt(sessionStorage["loadCounter"]); currentCount++; currentCount++; sessionStorage["loadCounter"] = currentCount; sessionStorage["loadCounter"] = currentCount; document.getElementById("countDiv").innerHTML = document.getElementById("countDiv").innerHTML = currentCount; currentCount;}  Session Storage

Live Demo

 HTML DOM Extensions:  getElementsByClassName()  innerHTML  hasFocus  getSelection()

Live Demo

How to Listen for Something to Happen?

 Event Listener is an event that tracks for something to happen  i.e. a key to be pressed, a mouse click, etc.  Available in JavaScript  addEventListener() registers a single event listener on a single target  The event target may be  A single node in a document  The document itself  A window

 Adding a click event listener to a div element document.GetElementById("someElement"). addEventListener("click", addEventListener("click", function (e) { function (e) { alert("element clicked"); alert("element clicked"); }, false); }, false);

Live Demo

Questions?

1. Write wrapper function as follows:  $(ID) to return either a single element, whose ID is the one passed or null  $$(selector) to return an array of elements or empty array with results;  Use mapping to existing DOM methods  e.g. getElementByClassName, querySelectorAll

2. Write an event delegate function member of the Event global object e.g. Event.delegate("selector", "eventName", handlerName) using the previously written functions to match selectors.  Map the global listeners to the document or body element;  Use w3c style events.