1.Canvas
顧名思義,畫布,你在上面畫畫
XML/Html Code復制內容到剪貼板
- <!DOCTYPE Html>
- <Html>
- <canvas id="canvas" style="border: 1px solid;" width="250" height="250"> </canvas>
- <script>
- function drawTriangle(context){//畫三角形
- context.beginPath();
- context.moveTo(0, 0);
- context.lineTo(30, -30);
- context.lineTo(60, 0);
- context.lineTo(0, 0);
- context.fillStyle = '#339900';
- context.fill();
- context.closePath();
- }
- function draw() {
- var canvas = document.getElementById('canvas');//獲取畫布
- var context = canvas.getContext('2d');//獲取畫筆
-
- context.save();
-
- context.translate(30, 60);//移動基准位置
- drawTriangle(context);//畫第一個三角形
- context.stroke();
-
- context.translate(60, 90);//移動基准位置
- drawTriangle(context);//畫第二個三角形
- context.stroke();
-
- context.restore();
- }
- window.addEventListener("load", draw, true);
- </script>
- </Html>
在浏覽器裡的樣子
2.Audio/Video
無需插件,播放音頻,視頻,每個浏覽器能支持的格式不一樣,自己掂量吧
XML/Html Code復制內容到剪貼板
- <!DOCTYPE Html>
- <Html>
- <audio controls>
- <source src="johann_sebastian_bach_air.ogg">
- <source src="johann_sebastian_bach_air.mp3">
- An audio clip from Johann Sebastian Bach.
- </audio>
- </Html>
Chrome中Audio的樣子
如何在JS中控制Audio的播放
XML/Html Code復制內容到剪貼板
- <!DOCTYPE Html>
- <Html>
- <audio id="clickSound">
- <source src="johann_sebastian_bach_air.ogg">
- <source src="johann_sebastian_bach_air.mp3">
- </audio>
-
- <button id="toggle" onclick="toggleSound()">Play</button>
-
- <script type="text/Javascript">
- function toggleSound() {
- var music = document.getElementById("clickSound");
- var toggle = document.getElementById("toggle");
- if (music.paused) {
- music.play();
- toggle.innerHtml = "Pause";
- }
- else {
- music.pause();
- toggle.innerHtml ="Play";
- }
- }
- </script>
- </Html>
Video播放和控制
XML/Html Code復制內容到剪貼板
- <!DOCTYPE Html>
- <Html>
- <video id="movIEs" controls onmouSEOver="this.play()" onmouSEOut="this.pause()" autobuffer="true"
- width="400px" height="300px">
- <source src="Intermission-Walk-in.ogv" type='video/ogg; codecs="theora, vorbis"'>
- <source src="Intermission-Walk-in_512kb.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'>
- </video>
- </Html>
Chrome中Video的樣子
3.Geolocation
獲取用戶地理位置,用戶可以選擇是否願意,目前來說相當的不靠譜,fanqiang後可以在Firefox測試成功,因為Firefox使用的Google的地理服務,看看代碼,也比較簡單
JavaScript Code復制內容到剪貼板
- <script type="text/Javascript">
- function loadDemo() {
- if(navigator.geolocation) {//檢測浏覽器是否支持Geolocation
- navigator.geolocation.getCurrentPosition(updateLocation);
- }
- }
- function updateLocation(position) {
- var latitude = position.coords.latitude;
- var longitude = position.coords.longitude;
- if (!latitude || !longitude) {
- return;
- }
- document.getElementById("latitude").innerHtml = latitude;
- document.getElementById("longitude").innerHtml = longitude;
- }
- </script>