利用H5api實(shí)現(xiàn)時(shí)鐘的繪制(javascript)
HTML5的canvas標(biāo)簽用于繪制圖像(通過腳本,通常是 JavaScript)。不過,canvas元素本身并沒有繪制能力(它僅僅是圖形的容器)必須使用腳本來完成實(shí)際的繪圖任務(wù)。
下面,具體總結(jié)了一下使用畫布canvas的步驟:
畫布:
canvas
在頁(yè)面上規(guī)劃出一塊空間,canvas標(biāo)簽,通過javascript控制畫布完成繪制
1.獲取畫布
var canvas=document.getElementById("");
2.獲取上下文對(duì)象 (獲取畫筆)
var cx=canvas.getContext(“2d”);
3.設(shè)置畫筆樣式
cx.fillStyle=‘red'; cx.strokeStyle=‘blue';
4.開始繪制
下面是對(duì)于canvas使用,繪制一個(gè)簡(jiǎn)單鐘表的小例子:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
window.onload=function(){
//1.獲取畫布
var canvas=document.getElementById("canvas");
//2.獲取畫筆
var cx=canvas.getContext("2d");
function clock(){
//3.設(shè)置畫筆樣式
cx.fillStyle="orange";
//4.繪制圖形
//繪制表盤
cx.beginPath();
cx.arc(300,300,200,0,Math.PI*2)
cx.closePath();
cx.fill();
//繪制時(shí)刻度
cx.lineWidth=2;
cx.strokeStyle="black";
for(var i=0;i<12;i++){
cx.save();
cx.translate(300,300);
cx.rotate(i*(Math.PI/6));
// cx.beginPath();
cx.moveTo(0,-180);
cx.lineTo(0,-200);
// cx.closePath();
cx.stroke();
cx.fillStyle="black";
cx.font="16px blod";
cx.rotate(Math.PI/6)
cx.fillText(i+1,-5,-220);
cx.restore();
}
//繪制分刻度
for(var i=0;i<60;i++){
cx.save();
cx.translate(300,300);
cx.rotate(i*(Math.PI/30));
cx.beginPath();
cx.moveTo(0,-190);
cx.lineTo(0,-200);
cx.closePath();
cx.stroke();
cx.restore();
}
//獲取當(dāng)前時(shí)間
var today=new Date();
var hour=today.getHours();
var min=today.getMinutes();
var sec=today.getSeconds();
hour=hour+min/60;
//繪制時(shí)針
cx.lineWidth=5;
cx.save();
cx.beginPath();
cx.translate(300,300);
cx.rotate(hour*(Math.PI/6));
cx.moveTo(0,10);
cx.lineTo(0,-100);
cx.closePath();
cx.stroke();
cx.restore();
//繪制分針
cx.lineWidth=3;
cx.save();
cx.beginPath();
cx.translate(300,300);
cx.rotate(min*(Math.PI/30));
cx.moveTo(0,10);
cx.lineTo(0,-120);
cx.closePath();
cx.stroke();
cx.restore();
//繪制秒針
cx.lineWidth=1;
cx.strokeStyle="red";
cx.save();
cx.beginPath();
cx.translate(300,300);
cx.rotate(sec*(Math.PI/30));
cx.moveTo(0,10);
cx.lineTo(0,-160);
cx.closePath();
cx.stroke();
cx.restore();
//繪制交叉處
cx.fillStyle="#ccc";
cx.strokeStyle="red";
cx.save();
cx.translate(300,300);
cx.beginPath();
cx.arc(0,0,5,0,Math.PI*2);
cx.closePath();
cx.fill();
cx.stroke();
cx.restore();
setTimeout(clock,1000);
}
clock()
}
</script>
</head>
<body>
<canvas id="canvas" width="600px" height="600px" style="background-color: #ccc;"></canvas>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- js實(shí)現(xiàn)一個(gè)簡(jiǎn)單的數(shù)字時(shí)鐘效果
- html5 canvas js(數(shù)字時(shí)鐘)實(shí)例代碼
- 五步輕松實(shí)現(xiàn)JavaScript HTML時(shí)鐘效果
- JavaScript實(shí)現(xiàn)簡(jiǎn)單的時(shí)鐘實(shí)例代碼
- 基于javascript實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘效果
- JavaScript實(shí)現(xiàn)抖音羅盤時(shí)鐘
- javascript入門·動(dòng)態(tài)的時(shí)鐘,顯示完整的一些方法,新年倒計(jì)時(shí)
- 一個(gè)簡(jiǎn)易時(shí)鐘效果js實(shí)現(xiàn)代碼
- Javascript實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘效果
- JS實(shí)現(xiàn)的網(wǎng)頁(yè)倒計(jì)時(shí)數(shù)字時(shí)鐘效果
相關(guān)文章
微信小程序地圖繪制線段并且測(cè)量(實(shí)例代碼)
這篇文章主要介紹了微信小程序地圖繪制線段并且測(cè)量,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
微信小程序?qū)崿F(xiàn)保存影集和圖片到相冊(cè)
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)保存影集和圖片到相冊(cè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
js和html5實(shí)現(xiàn)手機(jī)端刮刮卡抽獎(jiǎng)效果完美兼容android/IOS
手機(jī)完美支持html5,所以如果手機(jī)端想要做個(gè)抽獎(jiǎng)模塊的話,用刮刮卡抽獎(jiǎng)效果,相信這個(gè)互動(dòng)體驗(yàn)是非常棒的,本人親自完成,有錯(cuò)誤請(qǐng)大家指出2013-11-11
js實(shí)現(xiàn)圖片數(shù)組中圖片切換效果
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)圖片數(shù)組中圖片切換效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
JavaScript制作簡(jiǎn)易的微信打飛機(jī)
這篇文章主要介紹了JavaScript制作簡(jiǎn)易的微信打飛機(jī),只有簡(jiǎn)單的功能,大神們可以自由擴(kuò)展哈。有需要的小伙伴可以參考下。2015-03-03
js獲取當(dāng)前日期代碼適用于網(wǎng)頁(yè)頭部
大家在瀏覽網(wǎng)頁(yè)時(shí)可能會(huì)注意到網(wǎng)頁(yè)頭部有個(gè)不錯(cuò)的時(shí)間在顯示,看起來感覺不錯(cuò)于是自己也實(shí)現(xiàn)了一個(gè),下面是具體的代碼,感興趣的朋友可以參考下哈2013-06-06
一個(gè)JS函數(shù)搞定網(wǎng)頁(yè)標(biāo)題(title)閃動(dòng)效果
這篇文章主要介紹了使用JS函數(shù)實(shí)現(xiàn)網(wǎng)頁(yè)標(biāo)題(title)閃動(dòng)效果的代碼,需要的朋友可以參考下2014-05-05

