JavaScript Event Handling For Interaction and Casual Games

Slides:



Advertisements
Similar presentations
© Park University, 2006 Creating an Interactive Web Page with DHTML and JavaScript Park University.
Advertisements

Bring Life to Your Web Pages with JavaScript and HTML5 Ole Ildsgaard Hougaard -
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
Chapter 6 © 2012 by Addison Wesley Longman, Inc Introduction Def: A dynamic HTML document is one whose tag attributes, tag contents, or element.
Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.
Cos 381 Day 7. © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Agenda Assignment 2 Posted –Program a web-based Version of Soduku using JavaScript.
UNIT 6 JAVASCRIPT EVENT HANDLERS &WEB BROWSERS DETECTION.
JQuery 10/21. Today jQuery Some cool tools around the web JavaScript Libraries Drawing libraries HTML Frameworks Conventions.
Chapter 19: Adding JavaScript
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 Dynamic Documents With JavaScript.
Execution Environment for JavaScript " Java Script Window object represents the window in which the browser displays documents. " The Window object provides.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
Intro to JavaScript. Use the tag to tell the browser you are writing JavaScript.
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.
DOM Events. JS Events Outline About Events – Introduction – Registering events – Getting information from events Event order/phases – Preventing default.
Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54
HTML Form Widgets. Review: HTML Forms HTML forms are used to create web pages that accept user input Forms allow the user to communicate information back.
Event JavaScript's interaction with HTML is handled through events that occur when the user or browser manipulates a page. When the page loads, that is.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.
Dialog boxes in JavaScript Events in JavaScript – What are they – “Which events are there?” – “How do I register event handlers to an HTML element?” –
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.
JavaScript, Fourth Edition Chapter 4 Manipulating the Browser Object Model.
Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling – Inline – Scripting –
JQuery JavaScript is a powerful language but it is not always easy to work with. jQuery is a JavaScript library that helps with: – HTML document traversal.
DOM Events. JS Events Outline About Events – Introduction – Registering events – Getting information from events Event order/phases – Preventing default.
Event Handling. Objectives Using event handlers Simulating events Using event-related methods.
 Scripts that execute in response to some event  User clicking on something  Script does NOT execute as part of page loading  DOM facilities like.
JavaScript Functions A function is a block of code that can be reused. It is similar to a method in Java. It consists of a function name, an argument.
5 th and 4 th ed: some from chapters 9, 12, 13 SY306 Web and Databases for Cyber Operations Slide Set #8: Dynamic HTML.
7. JavaScript Events. 2 Motto: Do you think I can listen all day to such stuff? –Lewis Carroll.
Event Handling & AJAX IT210 Web Systems. Question How do we enable users to dynamically interact with a website? Answer: Use mouse and keyboard to trigger.
Web Programming Overview. Introduction HTML is limited - it cannot manipulate data How Web pages are extended (include): –Java: an object-oriented programming.
Chapter 6 © 2014 by Pearson Education Introduction Def: A dynamic HTML document is one whose tag attributes, tag contents, or element style properties.
Events Event Handling in JavaScript SoftUni Team Technical Trainers Software University
Events CS The keyword this  all JavaScript code actually runs inside of an object  by default, code runs inside the global window object  all.
LESSON : EVENTS AND FORM VALIDATION -JAVASCRIPT. EVENTS CLICK.
Introduction 1.HTML DOM events allow JavaScript to register different event handlers on elements in an HTML document. 2.Events are normally used in combination.
Dr. Ahmet Cengizhan Dirican BIL 374 Internet Technologies 6. Dynamic Documents With JavaScript.
Introduction to JQuery COGS 187A – Fall JQuery jQuery is a JavaScript library, and allows us to manipulate HTML and CSS after the page has been.
CGS 3066: Web Programming and Design Spring 2016 Programming in JavaScript.
JavaScript Events Java 4 Understanding Events Events add interactivity between the web page and the user You can think of an event as a trigger that.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
Chapter 6 Dynamic Documents with JavaScript. © 2006 Pearson Addison-Wesley. All rights reserved Introduction Def: A dynamic HTML document is.
Introduction to JavaScript Events Instructor: Sergey Goldman 1.
Internet & World Wide Web How to Program, 5/e.  JavaScript events  allow scripts to respond to user interactions and modify the page accordingly  Events.
CSE 154 LECTURE 10: MORE EVENTS. Problems with reading/changing styles Click Me HTML window.onload = function() { document.getElementById("clickme").onclick.
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
Chapter 6 © 2003 by Addison-Wesley, Inc Introduction - Dynamic HTML is not a new markup language - A dynamic HTML document is one whose tag attributes,
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Handling events from DOM objects.
Third lecture Event 27/2/2016 JavaScript Tutorial.
6.1 Introduction 6.2 Element Positioning
THE DOM.
Introduction to JavaScript Events
JavaScript Functions.
JavaScript and Events.
JavaScript Events.
DHTML Javascript Internet Technology.
CSE 154 Lecture 11: More Events.
Working with the Event Model
Cleveland Tech Consulting, LLC
Working with the Event Model
DHTML Javascript Internet Technology.
Customizing Maps Custom controls to change the content of the map
Web Design and Development
Javascript and JQuery SRM DSC.
Code Topic 9 Standard JavaScript Events Laura Friedman
CSE 337 Lecture 10: Events.
Web Programming and Design
6.1 Introduction 6.2 Positioning Elements
Presentation transcript:

JavaScript Event Handling For Interaction and Casual Games IGME 230 Fall 2016

Events in JavaScript are (typically) handled with callbacks. Mention that there are also many reactive programming libraries for JavaScript, designed to avoid callbacks for DOM manipulation… React.js is currently the most popular. Callback: a callback is a piece of executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at some convenient time. var btn = document.querySelector('button'); btn.onclick = function() { console.log( 'button has been pressed.'); };

Callbacks are often passed Event objects. var btn = document.querySelector('button'); btn.onclick = function( event ) { console.log( 'btn.text:', event.target.innerText ); }; The “target” property of the Event object usually refers to the DOM element that the triggered callback was assigned to. Here we use the “innerText” property to read the text inside of a <button> element.

Event Objects: DOMEvent = { target, timestamp, clientX, clientY, offsetX, offsetY, button, shiftKey, metaKey, ctrlKey }; The target property of the Event object usually refers to the DOM element that the triggered callback was assigned to. Timestamp gives a floating-point offset in milliseconds from when the page was loaded. clientX and clientY provide the position in the browser window of the event, measured from the upper-left corner. offsetX and offsetY provide the position of the event relative to the upper-left corner of the target element. shiftKey, metaKey, and ctrlKey denote if their respective modifier keys are being held down when the event occurs.

The window object can also be assigned events. btn = document.createElement( 'button' ); btn.innerText = 'foo foo'; btn.style.position = 'absolute'; document.querySelector('body').appendChild( btn ); window.onmousemove = function( event ) { btn.style.top = event.clientY + 'px'; btn.style.left = event.clientX + 'px'; }; Demo: paste the code above into the developer’s console in any webpage and then move your mouse in around in the browser window. The button will follow your mouse. Note that you need to add ‘px’ to the clientY and clientX positions to make a valid CSS value.

Using ‘on’ to add events (onclick, onmousedown, onmousemove etc Using ‘on’ to add events (onclick, onmousedown, onmousemove etc.) has problems.

Equivalent: // pure JavaScript btn = document.createElement( 'button' ); btn.onclick = function() { console.log('YO') }; btn.innerText = 'YO'; // vs inline HTML <button onclick="console.log('YO');">YO</button> The problem with both of these methods is that we can only have one callback registered (only one function can be assigned to the onclick property). What if we have many different events that we want to dynamically assign?

Enter addEventListener + removeEventListener btn = document.createElement( 'button' ); btn.innerText = 'YO'; function func1() { console.log( 'YO 1' ); }; function func2() { console.log( 'YO 2 ') ; btn.removeEventListener( 'click', func1 ); }; btn.addEventListener( 'click', func1 ); btn.addEventListener( 'click', func2 ); document.querySelector('body').appendChild( btn ); addEventListener lets us add multiple events. Note that we do not prefix the event names with ‘on’ when we pass them to addEventListener. We can also remove events associated with particular functions. In the above code example (copy and paste into the developer’s console to run) func1 is only called once, because when func2 runs it removes the eventListener that calls func1.

Mouse vs. Touch onmousedown vs. ontouchstart onmouseup vs. ontouchend onmousemove vs. ontouchmove onclick… the same for both Point out how annoying it would be to have to write two entirely different sets of event handlers for mouse vs. touch events.

Pointer Events http://caniuse.com/#search=Pointer “Pointer Events” is link to spec, which provides a unified event system based on MouseEvents. Supports mouse, touch, and pen events. Point out with caniuse link that it is not yet supported on mobile devices (argggh!). So we use a polyfill, PEP.js.

PEP.js (PointerEvent Polyfill) demo: PEP.demo.html Describe what a “polyfill” is: a library that implements (typically standardized) features on browsers that do not natively support the feature. PEP.js lets us use pointer events in any browser. Show PEP.demo.html

Game Demo demo: gameDemo.html This demo randomly places circles on the screen. The player must click them before they fade from view; if they succeed they get points, if they fail they lose health. The game ends health <= 0.