Sunday, February 7, 2016

How to Read Mouse Coordinates using Javascript Code

In this tutorial, you will learn how to read mouse coordinates in your screen. This will show the X and Y positioning of the mouse. This is useful in adjusting elements on your page by determining its position.
Hope you learn from this simple yet useful function


<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function readMouseMove(e){
var X = document.getElementById('x_coords');
var Y = document.getElementById('y_coords');
X.innerHTML = e.clientX;
Y.innerHTML = e.clientY;
}
document.onmousemove = readMouseMove;
</script>
<title>Read Mouse Coordinates</title>
</head>
<body>
<table>
<tr>
        <td colspan="2"><h1>Read Mouse Coordinates</h1></td>
</tr>
<tr>
<td><h2 id="x_coords">0</h2></td>
<td>X Positioning</td>
</tr>
<tr>
<td><h2 id="y_coords">0</h2></td>
<td>Y Positioning</td>
</tr>
</table>
</body
</html>

Tuesday, February 2, 2016

Google Map Marker ID

Get Google Map Marker ID when we click on it

When we use Google Map in our web pages, we will have to think about getting the Google map marker ID, when it is clicked. Google Map marker ID is required for displaying some data which is associated with the Pin or marker on google map. Or we use the google map marker ID for storing the latitude and and longitude with the marker ID. On google map, each pin has got a unique id. So if you click on different markers, you will be getting different IDs. Here I am going to show an example for adding IDs to the markers on the Google Map and getting/alerting the ID of the clicked marker.


HTML For showing Google Map Markers

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Google Map</title>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false"></script>
<style type="text/css">
.google-map {
width: 60%;
height: 500px;
margin: 0 auto;
border: 1px solid #c5c5c5;
}
</style>
</head>
<body>
<div class="google-map" id="map-canvas"></div>
</body>
</html>
Here, we are loading the map inside id=”map-canvas” div. In order to load Google map, we have to add Google map API url in the head tag of the HTML page.

Javascript For Google Map

<!--Script for google map-->
<script type="text/javascript">
function initialize_list() {
var latlng = new google.maps.LatLng(33.22949814144931,-99.82177934375);
var myOptions = {
zoom: 5,
center: latlng
};

var map = new google.maps.Map(document.getElementById('map-canvas'), myOptions);

var hotels = [
['Hotel 1, Place Name1, City1, State1', 33.22949814144931,-99.82177934375, 4, 1],
['Hotel 2, Place Name2, City2, State2', 33.376412351246586,-96.78955278125, 3, 2],
['Hotel 3, Place Name3, City3, State3', 35.08395557927625,-93.84521684375, 2, 3],
['Hotel 4, Place Name4, City4, State4', 37.631634755806274,-105.31494340625, 1, 4]
];

for (var i = 0; i < hotels.length; i++) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(hotels[i][1], hotels[i][2]),
map: map,
id: hotels[i][4], //we are adding IDs to the marker here
title: hotels[i][0]
});

google.maps.event.addListener(marker, "click", function() {
var marker = this;
alert('ID is :'+ this.id);
});
}
}

window.onload = initialize_list;
</script>
We are storing the details of the hotel, including the latitude and longitude values of the place in the ‘hotels’ array. And we set the latitude and longitude for the initial display of the map.
Here using ‘id’ we are assigning ID value to the marked. And on the click event or when we click on the marker, we will alert the clicked markers ID.

CSS3 3D Animation

The introduction of CSS 3 has changed the over all look and feel of the websites. CSS3 animations; transitions and transforms are the most interesting things in CSS3 ie. CSS3 3D animation . CSS transforms allows elements styled with CSS to be transformed in two-dimensional or three-dimensional space. No javascrip, no flash is required for creating 3D animations. It works well in latest browsers and just fine in less capable browsers.


CSS3 3D animation Explained

While we do CSS3 3D animation, we have to know two properties in CSS 3D transforms. One is “Perspective”. To activate 3D space, an element needs perspective. This can be applied with transform property, with the perspective as a functional notation
transform: perspective
Second thing is 3D transform functions. 3D transform uses the same transform propety used for 2D transforms.

CSS3 3D animation Example

Here I am going to give an example of 3D animation using a Mobile. When we hover the phone image it rotates and we are able to see the other side of the mobile.

HTML required for CSS3 3D animation

<div class="container">
<div class="mobile-front"></div>
<div class="mobile-back"></div>
</div>

CSS required CSS3 3D animation Example

/*Adding a container for the animation*/
.container{
 perspective: 800px;
 -webkit-perspective: 800px;
 width:500px;
 height:500px;
 margin:0 auto;
 border-radius:6px;
 position:relative;
 background: #1e5799;
 background: -moz-linear-gradient(top, #1e5799 0%, #2989d8 81%, #7db9e8 83%, #3297e5 100%);
 background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#1e5799), color-stop(81%,#2989d8), color-stop(83%,#7db9e8), color-stop(100%,#3297e5));
 background: -webkit-linear-gradient(top, #1e5799 0%,#2989d8 81%,#7db9e8 83%,#3297e5 100%);
 background: -o-linear-gradient(top, #1e5799 0%,#2989d8 81%,#7db9e8 83%,#3297e5 100%);
 background: -ms-linear-gradient(top, #1e5799 0%,#2989d8 81%,#7db9e8 83%,#3297e5 100%);
 background: linear-gradient(to bottom, #1e5799 0%,#2989d8 81%,#7db9e8 83%,#3297e5 100%);
}
.mobile-front,
.mobile-back{
        transform-style: preserve-3d; /* Enabling 3D transforms */
 -webkit-transform-style: preserve-3d;
 backface-visibility: hidden;
 -webkit-backface-visibility: hidden;
        width:255px;
 height: 470px;
        position:absolute;
 top:50%;
 left:50%;
 margin:-235px 0 0 -120px;
        background:url(mobile1.png) no-repeat left center;
 transition:0.8s;
}
.mobile-back{
 transform:rotateY(180deg);
 -webkit-transform:rotateY(180deg);
        background-position:right center;
}
.container:hover .mobile-front{
 transform:rotateY(180deg);
 -webkit-transform:rotateY(180deg);
}

.container:hover .mobile-back{
 transform:rotateY(360deg);
 -webkit-transform:rotateY(360deg);
}

CSS for CSS3 3D animation Explained

In the first place, we are enabling the 3D transforms. We are using two DIVs for the two sides of the mobile. And by using backface-visibility: hidden, we are hiding the DIVs when they are flipped. We animate the transitions with transition: 0.8s. With the class ‘mobile-back’, we are flipping the DIV which contains the other side of the mobile, to 180 degree as its default state. When we hover the container, we’ll flip the front side and hide the DIV and at the same time, we’ll flip the second container with the back side of the mobile.