From HTML to PostGISokulewiczm/downloads/html/HTML_3_JS_canv… · jQuery GoogleMaps API jQuery...

23
JavaScript From HTML to PostGIS Michal Okulewicz Wydzial Matematyki i Nauk Informacyjnych Politechnika Warszawska Michal Okulewicz HTML2PostGIS

Transcript of From HTML to PostGISokulewiczm/downloads/html/HTML_3_JS_canv… · jQuery GoogleMaps API jQuery...

Page 1: From HTML to PostGISokulewiczm/downloads/html/HTML_3_JS_canv… · jQuery GoogleMaps API jQuery jQuery is a simple JavaScript library supporting code simplification while dealing

JavaScript

From HTML to PostGIS

Michał Okulewicz

Wydział Matematyki i Nauk InformacyjnychPolitechnika Warszawska

Michał Okulewicz HTML2PostGIS

Page 2: From HTML to PostGISokulewiczm/downloads/html/HTML_3_JS_canv… · jQuery GoogleMaps API jQuery jQuery is a simple JavaScript library supporting code simplification while dealing

JavaScript

Lecture plan

1 JavaScriptCanvasjQueryGoogleMaps API

Michał Okulewicz HTML2PostGIS

Page 3: From HTML to PostGISokulewiczm/downloads/html/HTML_3_JS_canv… · jQuery GoogleMaps API jQuery jQuery is a simple JavaScript library supporting code simplification while dealing

JavaScriptCanvasjQueryGoogleMaps API

JavaScript - this lecture’s scope reminder

• Purpose and syntax of JavaScript• JavaScript based web applications (DOM and Events)

• Creating and changing the contents of the website• Event handler properties (element.onsth = handler ;)• Document Object Model Level 2 Events

(add/removeEventListener)• Creating and changing the styles on the website

• style property• using classes

• Selected additional topics• Utilizing canvas• jQuery• In-context library example: GoogleMaps API

Michał Okulewicz HTML2PostGIS

Page 4: From HTML to PostGISokulewiczm/downloads/html/HTML_3_JS_canv… · jQuery GoogleMaps API jQuery jQuery is a simple JavaScript library supporting code simplification while dealing

JavaScriptCanvasjQueryGoogleMaps API

JavaScript - this lecture’s scope reminder

• Purpose and syntax of JavaScript• JavaScript based web applications (DOM and Events)

• Creating and changing the contents of the website• Event handler properties (element.onsth = handler ;)• Document Object Model Level 2 Events

(add/removeEventListener)• Creating and changing the styles on the website

• style property• using classes

• Selected additional topics• Utilizing canvas• jQuery• In-context library example: GoogleMaps API

Michał Okulewicz HTML2PostGIS

Page 5: From HTML to PostGISokulewiczm/downloads/html/HTML_3_JS_canv… · jQuery GoogleMaps API jQuery jQuery is a simple JavaScript library supporting code simplification while dealing

JavaScriptCanvasjQueryGoogleMaps API

Canvas element

• <canvas> is an HTML block element designed to be used byJavaScript for drawing

• Without JavaScript <canvas> is utterly useless

• In order to draw one needs the context object:var context = canvas.getContext(’2d’);

Michał Okulewicz HTML2PostGIS

Page 6: From HTML to PostGISokulewiczm/downloads/html/HTML_3_JS_canv… · jQuery GoogleMaps API jQuery jQuery is a simple JavaScript library supporting code simplification while dealing

JavaScriptCanvasjQueryGoogleMaps API

Basic operations and properties

• context.beginPath();• context.closePath();• context.stroke();• context.fill();• context.strokeStyle;• context.fillStyle;• context.lineWidth; Basic canvas example

Michał Okulewicz HTML2PostGIS

Page 7: From HTML to PostGISokulewiczm/downloads/html/HTML_3_JS_canv… · jQuery GoogleMaps API jQuery jQuery is a simple JavaScript library supporting code simplification while dealing

JavaScriptCanvasjQueryGoogleMaps API

Shape defining operations

• context.moveTo(x,y);• context.lineTo(x,y);• context.arcTo(x1,y1,x2,y2,radius);• context.quadraticCurveTo(p1.x,p1.y,x,y);• context.bezierCurveTo(p1.x,p1.y,p2.x,p2.y,x,y);

Dummy WebPaint: a drawing example

Michał Okulewicz HTML2PostGIS

Page 8: From HTML to PostGISokulewiczm/downloads/html/HTML_3_JS_canv… · jQuery GoogleMaps API jQuery jQuery is a simple JavaScript library supporting code simplification while dealing

JavaScriptCanvasjQueryGoogleMaps API

High–level operations and transformations

• context.rect(x,y,width,height);• context.drawImage(HTML image,x,y);• context.fillText(text,x,y);• context.scale(x,y);• context.rotate(angle);

Move with arrows (by Karwowski) Move with WSAD (by Okulewicz)

Michał Okulewicz HTML2PostGIS

Page 9: From HTML to PostGISokulewiczm/downloads/html/HTML_3_JS_canv… · jQuery GoogleMaps API jQuery jQuery is a simple JavaScript library supporting code simplification while dealing

JavaScriptCanvasjQueryGoogleMaps API

jQuery

• jQuery is a simple JavaScript library supporting codesimplification while dealing with applying functions to multipleelements at once

• ...and much more

• jQuery speeds up writing the code and provides universal APIfor all browsers

• plain JavaScript code will always be faster (if properly written)

• Major releases:

1.x Supports IE 6, 7 and 8 (latest - 1.12.4)2.x Discontinued3.x Supports current browsers

Michał Okulewicz HTML2PostGIS

Page 10: From HTML to PostGISokulewiczm/downloads/html/HTML_3_JS_canv… · jQuery GoogleMaps API jQuery jQuery is a simple JavaScript library supporting code simplification while dealing

JavaScriptCanvasjQueryGoogleMaps API

jQuery

• jQuery is a simple JavaScript library supporting codesimplification while dealing with applying functions to multipleelements at once

• ...and much more

• jQuery speeds up writing the code and provides universal APIfor all browsers

• plain JavaScript code will always be faster (if properly written)

• Major releases:

1.x Supports IE 6, 7 and 8 (latest - 1.12.4)2.x Discontinued3.x Supports current browsers

Michał Okulewicz HTML2PostGIS

Page 11: From HTML to PostGISokulewiczm/downloads/html/HTML_3_JS_canv… · jQuery GoogleMaps API jQuery jQuery is a simple JavaScript library supporting code simplification while dealing

JavaScriptCanvasjQueryGoogleMaps API

jQuery

• jQuery is a simple JavaScript library supporting codesimplification while dealing with applying functions to multipleelements at once

• ...and much more

• jQuery speeds up writing the code and provides universal APIfor all browsers

• plain JavaScript code will always be faster (if properly written)

• Major releases:

1.x Supports IE 6, 7 and 8 (latest - 1.12.4)2.x Discontinued3.x Supports current browsers

Michał Okulewicz HTML2PostGIS

Page 12: From HTML to PostGISokulewiczm/downloads/html/HTML_3_JS_canv… · jQuery GoogleMaps API jQuery jQuery is a simple JavaScript library supporting code simplification while dealing

JavaScriptCanvasjQueryGoogleMaps API

Getting started and basic concepts

The $ operator

• Shorthand for jQuery

• An object with jQuery methods

• A method for wrapping DOM elements with jQueryfunctionality

DOM loaded

$(function() {// Handler for .ready() called.

});

Michał Okulewicz HTML2PostGIS

Page 13: From HTML to PostGISokulewiczm/downloads/html/HTML_3_JS_canv… · jQuery GoogleMaps API jQuery jQuery is a simple JavaScript library supporting code simplification while dealing

JavaScriptCanvasjQueryGoogleMaps API

Motivation

• Slightly shorter syntax in typical tasks

• Easier DOM manipulation

• Animation API

• AJAX API

Warning: don’t fall in love with jQuery

Michał Okulewicz HTML2PostGIS

Page 14: From HTML to PostGISokulewiczm/downloads/html/HTML_3_JS_canv… · jQuery GoogleMaps API jQuery jQuery is a simple JavaScript library supporting code simplification while dealing

JavaScriptCanvasjQueryGoogleMaps API

Style manipulation

direct styling$(’li.c’).css(’background-color’,’green’);

vs.document.querySelectorAll(’li.c’).forEach(function(item) {item.style.backgroundColor = ’green’;

});

classes$(’.c’).removeClass(’c’);

vs.document.getElementsByClassName(’c’).forEach(function(item){item.classList.remove(’c’);

});

Michał Okulewicz HTML2PostGIS

Page 15: From HTML to PostGISokulewiczm/downloads/html/HTML_3_JS_canv… · jQuery GoogleMaps API jQuery jQuery is a simple JavaScript library supporting code simplification while dealing

JavaScriptCanvasjQueryGoogleMaps API

Style manipulation

direct styling$(’li.c’).css(’background-color’,’green’);

vs.document.querySelectorAll(’li.c’).forEach(function(item) {item.style.backgroundColor = ’green’;

});

classes$(’.c’).removeClass(’c’);

vs.document.getElementsByClassName(’c’).forEach(function(item){item.classList.remove(’c’);

});

Michał Okulewicz HTML2PostGIS

Page 16: From HTML to PostGISokulewiczm/downloads/html/HTML_3_JS_canv… · jQuery GoogleMaps API jQuery jQuery is a simple JavaScript library supporting code simplification while dealing

JavaScriptCanvasjQueryGoogleMaps API

Style manipulation

direct styling$(’li.c’).css(’background-color’,’green’);

vs.document.querySelectorAll(’li.c’).forEach(function(item) {item.style.backgroundColor = ’green’;

});

classes$(’.c’).removeClass(’c’);

vs.document.getElementsByClassName(’c’).forEach(function(item){item.classList.remove(’c’);

});

Michał Okulewicz HTML2PostGIS

Page 17: From HTML to PostGISokulewiczm/downloads/html/HTML_3_JS_canv… · jQuery GoogleMaps API jQuery jQuery is a simple JavaScript library supporting code simplification while dealing

JavaScriptCanvasjQueryGoogleMaps API

Events attachment and DOM manipulation

$(’li’).click(listItemClick);

$(’p’).mousedown(mouseDownHandler);

$(’#container’).append($(’<p id="new">Lorem</p>’));

$(’#new’).mousedown(mouseDownHandler);

Michał Okulewicz HTML2PostGIS

Page 18: From HTML to PostGISokulewiczm/downloads/html/HTML_3_JS_canv… · jQuery GoogleMaps API jQuery jQuery is a simple JavaScript library supporting code simplification while dealing

JavaScriptCanvasjQueryGoogleMaps API

Events attachment and DOM manipulation

$(’li’).click(listItemClick);

$(’p’).mousedown(mouseDownHandler);

$(’#container’).append($(’<p id="new">Lorem</p>’));

$(’#new’).mousedown(mouseDownHandler);

Michał Okulewicz HTML2PostGIS

Page 19: From HTML to PostGISokulewiczm/downloads/html/HTML_3_JS_canv… · jQuery GoogleMaps API jQuery jQuery is a simple JavaScript library supporting code simplification while dealing

JavaScriptCanvasjQueryGoogleMaps API

Events attachment and DOM manipulation

$(’li’).click(listItemClick);

$(’p’).mousedown(mouseDownHandler);

$(’#container’).append($(’<p id="new">Lorem</p>’));

$(’#new’).mousedown(mouseDownHandler);

Michał Okulewicz HTML2PostGIS

Page 20: From HTML to PostGISokulewiczm/downloads/html/HTML_3_JS_canv… · jQuery GoogleMaps API jQuery jQuery is a simple JavaScript library supporting code simplification while dealing

JavaScriptCanvasjQueryGoogleMaps API

Events attachment and DOM manipulation

$(’li’).click(listItemClick);

$(’p’).mousedown(mouseDownHandler);

$(’#container’).append($(’<p id="new">Lorem</p>’));

$(’#new’).mousedown(mouseDownHandler);

Michał Okulewicz HTML2PostGIS

Page 21: From HTML to PostGISokulewiczm/downloads/html/HTML_3_JS_canv… · jQuery GoogleMaps API jQuery jQuery is a simple JavaScript library supporting code simplification while dealing

JavaScriptCanvasjQueryGoogleMaps API

Events attachment and DOM manipulation

$(’li’).click(listItemClick);

$(’p’).mousedown(mouseDownHandler);

$(’#container’).append($(’<p id="new">Lorem</p>’));

$(’#new’).mousedown(mouseDownHandler);

Michał Okulewicz HTML2PostGIS

Page 22: From HTML to PostGISokulewiczm/downloads/html/HTML_3_JS_canv… · jQuery GoogleMaps API jQuery jQuery is a simple JavaScript library supporting code simplification while dealing

JavaScriptCanvasjQueryGoogleMaps API

Animations

$(li).animate(

{marginLeft: ’+=3em’},2000);

Michał Okulewicz HTML2PostGIS

Page 23: From HTML to PostGISokulewiczm/downloads/html/HTML_3_JS_canv… · jQuery GoogleMaps API jQuery jQuery is a simple JavaScript library supporting code simplification while dealing

JavaScriptCanvasjQueryGoogleMaps API

And finally: some GIS example

Michał Okulewicz HTML2PostGIS