• 小强的HTML5移动开发之路(2)——HTML5的新特性


    来自:http://blog.csdn.net/dawanganban/article/details/17592787

    一、画布(Canvas)

    画布是网页中的一块区域,可所以用JavaScript在上面绘图。下面我们来创建一个画布并在上面绘制一个坦克(后面将用HTML5做一个坦克大战游戏),代码如下:

    1. <!DOCTYPE html>  
    2. <html>  
    3. <head>  
    4. <meta charset="utf-8"/>  
    5. </head>  
    6. <body>  
    7. <h1>html5-坦克大战</h1>  
    8. <!--坦克大战的战场-->  
    9. <canvas id="tankMap" width="400px" height="300px" style="background-color:black"></canvas>  
    10. <script type="text/javascript">  
    11.     //得到画布  
    12.     var canvas1 = document.getElementById("tankMap");  
    13.       
    14.     //定义一个位置变量  
    15.     var heroX = 80;  
    16.     var heroY = 80;  
    17.       
    18.     //得到绘图上下文  
    19.     var cxt = canvas1.getContext("2d");  
    20.     //设置颜色  
    21.     cxt.fillStyle="#BA9658";  
    22.     //左边的矩形  
    23.     cxt.fillRect(heroX,heroY,5,30);  
    24.     //右边的矩形  
    25.     cxt.fillRect(heroX+17,heroY,5,30);  
    26.     //画中间的矩形  
    27.     cxt.fillRect(heroX+6,heroY+5,10,20);  
    28.     //画出坦克的盖子  
    29.     cxt.fillStyle="#FEF26E";  
    30.     cxt.arc(heroX+11,heroY+15,5,0,360,true);  
    31.     cxt.fill();  
    32.     //画出炮筒  
    33.     cxt.strokeStyle="#FEF26E";  
    34.     cxt.lineWidth=1.5;  
    35.     cxt.beginPath();  
    36.     cxt.moveTo(heroX+11,heroY+15);  
    37.     cxt.lineTo(heroX+11,heroY);  
    38.     cxt.closePath();  
    39.     cxt.stroke();  
    40.       
    41.       
    42. </script>  
    43. </body>  
    44. </html>  

    运行效果:


    二、地理位置

    HTML5的地理位置特性可以返回网页访问者的地理位置。运行下面代码进行测试:

    1. <!DOCTYPE html>  
    2. <html>  
    3. <body>  
    4. <p id="demo">点击这个按钮,获得您的位置:</p>  
    5. <button onclick="getLocation()">试一下</button>  
    6. <div id="mapholder"></div>  
    7. <script src="http://maps.google.com/maps/api/js?sensor=false"></script>  
    8. <script>  
    9. var x=document.getElementById("demo");  
    10. function getLocation()  
    11.   {  
    12.   if (navigator.geolocation)  
    13.     {  
    14.     navigator.geolocation.getCurrentPosition(showPosition,showError);  
    15.     }  
    16.   else{x.innerHTML="Geolocation is not supported by this browser.";}  
    17.   }  
    18.   
    19. function showPosition(position)  
    20.   {  
    21.   lat=position.coords.latitude;  
    22.   lon=position.coords.longitude;  
    23.   latlon=new google.maps.LatLng(lat, lon)  
    24.   mapholder=document.getElementById('mapholder')  
    25.   mapholder.style.height='250px';  
    26.   mapholder.style.width='500px';  
    27.   
    28.   var myOptions={  
    29.   center:latlon,zoom:14,  
    30.   mapTypeId:google.maps.MapTypeId.ROADMAP,  
    31.   mapTypeControl:false,  
    32.   navigationControlOptions:{style:google.maps.NavigationControlStyle.SMALL}  
    33.   };  
    34.   var map=new google.maps.Map(document.getElementById("mapholder"),myOptions);  
    35.   var marker=new google.maps.Marker({position:latlon,map:map,title:"You are here!"});  
    36.   }  
    37.   
    38. function showError(error)  
    39.   {  
    40.   switch(error.code)   
    41.     {  
    42.     case error.PERMISSION_DENIED:  
    43.       x.innerHTML="User denied the request for Geolocation."  
    44.       break;  
    45.     case error.POSITION_UNAVAILABLE:  
    46.       x.innerHTML="Location information is unavailable."  
    47.       break;  
    48.     case error.TIMEOUT:  
    49.       x.innerHTML="The request to get user location timed out."  
    50.       break;  
    51.     case error.UNKNOWN_ERROR:  
    52.       x.innerHTML="An unknown error occurred."  
    53.       break;  
    54.     }  
    55.   }  
    56. </script>  
    57. </body>  
    58. </html>  

    运行结果:


    三、丰富强大的表单

    HTML5提供了表单增强特性,这些功能是由复杂的JavaScript编写的,以便能在所有浏览器上工作.

    四、本地存储

    HTML5本地存储类似于cookies,但它支持存储的数据量更大,并且提供了一个本地数据库引擎,从而使保持和获取数据更加容易。这个特点可以很好的将数据分发给用户缓解与服务器的连接压力。另外可以使用JavaScript从本地Web页面中访问本地数据库,这意味着你可以将网页保存到你本地从公司回到家里不用连接互联网就能打开。

    五、媒体

    HTML5规范中最具亮点的部分也许就是HTML5浏览器内置的多媒体播放功能,不需要Flash、Microsoft Media Player等插件。

    1. <!DOCTYPE HTML>  
    2. <html>  
    3. <body>  
    4.   
    5. <video src="/i/movie.ogg" controls="controls">  
    6. your browser does not support the video tag  
    7. </video>  
    8.   
    9. </body>  
    10. </html>  
    运行效果:

    六、语音搜素功能:

    大家现在可以在好多网站上看到语音搜素功能,HTML5提供了强大的语音搜素功能属性,只需要添加一个属性就可以实现。

    1. <!DOCTYPE html>  
    2. <head>  
    3.     <meta charset="utf-8"/>  
    4. </head>  
    5. <body>  
    6.     <h1>语音搜素功能</h1>  
    7.     <input type="text" name="yuyin" id="yuyin" x-webkit-speech/>  
    8. </body>  

  • 相关阅读:
    tp5.1接入支付宝网站支付
    go类型转换
    mysql常见sql练习题
    php 获取当前时间的 前一小时、一天、一个月、一年
    php如何检测是否有环
    Go的切片:长度和容量
    go实现小项目
    知乎网页版不登录如何浏览内容
    图挂了
    centos搭建php环境
  • 原文地址:https://www.cnblogs.com/wanghang/p/6298980.html
Copyright © 2020-2023  润新知