html5模擬3d擲骰子代碼如下:
南康網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站設(shè)計(jì)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營(yíng)維護(hù)。成都創(chuàng)新互聯(lián)于2013年成立到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)。
1 !DOCTYPE
2 html
3 title柯樂義/title
4 head
5 script
6 var leftX = 150;
7 var topY = 100;
8 var diceX = 80;
9 var diceY = 80;
10 var dotR = 4;
11 var count = 0;
12 var lastNum = 0;
13 var flag = false;
14
15 function clickMe() {
16 count = 0;
17 if(flag) {
18 return false;
19 }
20 flag = true;
21 var ctx = document.getElementById("canvas").getContext('2d');
22 ctx.beginPath();
23 //ctx.arc(100,100,50,0,Math.PI,false);
24 ctx.strokeRect(leftX,topY,diceX,diceY);
25
26 setTimeout(function(){
27 random(ctx);
28 },200);
29
30 }
31
32 function drawDice(ctx,randomNum) {
33 ctx.clearRect(leftX,topY,diceX,diceY);
34 switch(randomNum) {
35 case 1:
36 draw1();
37 break;
38 case 2:
39 draw2();
40 break;
41 case 3:
42 draw3();
43 break;
44 case 4:
45 draw4();
46 break;
47 case 5:
48 draw5();
49 break;
50 case 6:
51 draw6();
52 break;
53 }
54 count++;
55 if(count=20) {
56 if(randomNum==6) {
57 alert("哇!你走狗屎運(yùn)啦,今天可以去買彩票啦");
58 } else if(randomNum =3) {
59 alert("今天運(yùn)氣不太好哦!再試一把");
60 } else {
61 alert("請(qǐng)?jiān)俳釉賲?,在來一?);
62 }
63 flag = false;
64 return false;
65 } else {
66 setTimeout(function(){
67 random(ctx);
68 },200-count);
69 }
70 }
71
72 function random(ctx) {
73 var randomNum = Math.floor(Math.random()*6)+1;
74 if(randomNum == lastNum) {
75 random(ctx)
76 } else {
77 lastNum = randomNum;
78 drawDice(ctx,randomNum);
79 }
80
81 }
82
83 function commonDraw(ctx,dotX,dotY) {
84 ctx.beginPath();
85 ctx.arc(dotX,dotY,dotR,0,2*Math.PI,false);
86 ctx.stroke();
87 ctx.fill();
88 }
89
90 function draw1() {
91 var ctx = document.getElementById("canvas").getContext('2d');
92 ctx.fillStyle="#0000ff";
93 var dotX = leftX+diceX/2;
94 var dotY = topY+diceY/2;
95 commonDraw(ctx,dotX,dotY);
96 }
97
98 function draw2() {
99 var ctx = document.getElementById("canvas").getContext('2d');
100 ctx.fillStyle="#99FF66";
101 var dotX = leftX+4*dotR;
102 var dotY = topY+4*dotR;
103 commonDraw(ctx,dotX,dotY);
104 var dotX = leftX+diceX-4*dotR;
105 var dotY = topY+diceY-4*dotR;
106 commonDraw(ctx,dotX,dotY);
107 }
108
109 function draw3() {
110 draw1();
111 draw2();
112 }
113
114 function draw4() {
115 draw2();
116 var ctx = document.getElementById("canvas").getContext('2d');
117 ctx.fillStyle="#99CC00";
118 var dotX = leftX+diceX-4*dotR;
119 var dotY = topY+4*dotR;
120 commonDraw(ctx,dotX,dotY);
121 var dotX = leftX+4*dotR;
122 var dotY = topY+diceY-4*dotR;
123 commonDraw(ctx,dotX,dotY);
124 }
125
126 function draw5(){
127 draw1();
128 draw4();
129 }
130 //
131 function draw6(){
132 var ctx = document.getElementById("canvas").getContext('2d');
133 ctx.fillStyle="#996633";
134 var dotX = leftX+4*dotR;
135 var dotY = topY+diceY/2
136 commonDraw(ctx,dotX,dotY);
137 var dotX = leftX+diceY-4*dotR;
138 commonDraw(ctx,dotX,dotY);
139 draw4();
140 }
141
142 function init() {
143 var ctx = document.getElementById("canvas").getContext('2d');
144 ctx.beginPath();
145 ctx.strokeRect(leftX,topY,diceX,diceY);
146 ctx.stroke();
147 draw6();
148
149 }
150 /script
151 /head
152
153 body onload="init();"
154 canvas id="canvas" width="400" height="300" style="background-color:#CCFFCC"
155 your brower is not support html5
156 /canvas
157
158 input type="button" value="擲骰子" onclick="clickMe();"/
159 /body
160 /html
我寫了個(gè)正方體的模板,你可以按照你想要的把每個(gè)面旋轉(zhuǎn)不同角度就行了
style?type="text/css"
html{
font-size:62.5%;
}
img{
width:300px;
height:300px;
}
#stage{
margin-top:200px;
margin-left:auto;
margin-right:auto;
width:300px;
height:300px;
perspective:1200px;
font-size:5em;
font-weight:bold;
color:#cc00ff;
}
.cube{
position:relative;
transform:rotateX(-45deg)?rotateY(45deg);
transform-style:preserve-3d;
transition:?all?.6s;
}
.side{
color:blue;
text-align:center;
width:300px;
height:300px;
line-height:300px;
position:absolute;
background:#cc66ff;
opacity:0.5;
border:1px?solid?rgba(117,4,24,0.5);
??}
?.front{
transform:translateZ(150px);
?}
?.back{
transform:rotateY(180deg)?translateZ(150px);
?}
?.left{
transform:rotateY(-90deg)?translateZ(150px);
?}
?.right{
transform:rotateY(90deg)?translateZ(150px);
?}
?.top{
transform:rotateX(90deg)?translateZ(150px);
?}
?.bottom{
transform:rotatex(-90deg)?translateZ(150px);
?}
#stage:hover?.cube{
transform:rotateX(-45deg)?rotateY(225deg);
transition:transform?.6s;
}
/style
/head
body
div?id="stage"
div?class="cube"
div?class="side?front"img?src="6.gif"?alt=""?//div
div?class="side?back"img?src="2.jpg"?alt=""?//div
div?class="side?left"img?src="3.jpg"?alt=""?//div
div?class="side?right"img?src="4.jpg"?alt=""?//div
div?class="side?top"img?src="5.jpg"?alt=""?//div
div?class="side?bottom"img?src="1.jpg"?alt=""?//div
/div
/div
/body
1、首先打開html文件編輯器,這里使用vscode新建一個(gè)html文檔,文檔中寫入基本的html結(jié)構(gòu),然后插入img標(biāo)簽并插入一張圖片,給img一個(gè)class屬性:
2、然后在上方的head標(biāo)簽中的style標(biāo)簽設(shè)置樣式,這里設(shè)置圖片的寬度和高度并設(shè)置相對(duì)定位,然后設(shè)置圖片的鼠標(biāo)懸浮樣式,其中設(shè)置動(dòng)畫的形式為3d以及設(shè)置圖片3d旋轉(zhuǎn)偏移的角度,添加一個(gè)動(dòng)畫,設(shè)置好延時(shí)即可:
3、最后打開瀏覽器,就會(huì)看到一個(gè)圖片:
4、當(dāng)鼠標(biāo)移動(dòng)上去,圖片就會(huì)自動(dòng)3d旋轉(zhuǎn)了:
我寫了個(gè)正方體的模板,你可以按照你想要的把每個(gè)面旋轉(zhuǎn)不同角度就行了
style?type="text/css"
html{
font-size:62.5%;
}
img{
width:300px;
height:300px;
}
#stage{
margin-top:200px;
margin-left:auto;
margin-right:auto;
width:300px;
height:300px;
perspective:1200px;
font-size:5em;
font-weight:bold;
color:#cc00ff;
}
.cube{
position:relative;
transform:rotateX(-45deg)?rotateY(45deg);
transform-style:preserve-3d;
transition:?all?.6s;
}
.side{
color:blue;
text-align:center;
width:300px;
height:300px;
line-height:300px;
position:absolute;
background:#cc66ff;
opacity:0.5;
border:1px?solid?rgba(117,4,24,0.5);
??}
?.front{
transform:translateZ(150px);
?}
?.back{
transform:rotateY(180deg)?translateZ(150px);
?}
?.left{
transform:rotateY(-90deg)?translateZ(150px);
?}
?.right{
transform:rotateY(90deg)?translateZ(150px);
?}
?.top{
transform:rotateX(90deg)?translateZ(150px);
?}
?.bottom{
transform:rotatex(-90deg)?translateZ(150px);
?}
#stage:hover?.cube{
transform:rotateX(-45deg)?rotateY(225deg);
transition:transform?.6s;
}
/style
/head
body
div?id="stage"
div?class="cube"
div?class="side?front"img?src="6.gif"?alt=""?//div
div?class="side?back"img?src="2.jpg"?alt=""?//div
div?class="side?left"img?src="3.jpg"?alt=""?//div
div?class="side?right"img?src="4.jpg"?alt=""?//div
div?class="side?top"img?src="5.jpg"?alt=""?//div
div?class="side?bottom"img?src="1.jpg"?alt=""?//div
/div
/div
/body
CSS3除了為開發(fā)者提供二維變形之外,還將動(dòng)畫從二維平面推動(dòng)到了三維立體狀態(tài),能夠?qū)崿F(xiàn)真正的三維特效。
三維變形和二維變形一樣,均使用的是transform屬性。想要觸發(fā)三維變形有兩種方式:一種方式是通過語法告知瀏覽器“請(qǐng)采用三維方式進(jìn)行變形處理”,另一種方式是直接使用CSS3三維變形的語法。
觸發(fā)方法1:告知瀏覽器變形方式
-webkit-transform-style:preserve-3d;
Tips:IE不支持三維變形,在移動(dòng)端,絕大多數(shù)的瀏覽器均為WebKit內(nèi)核,因此,在此句代碼之前需要書寫-webkit-的前綴內(nèi)核。
Tips:不要為body元素設(shè)置-webkit- transform-style: preserve 3d,否則會(huì)對(duì)position:fixed定位的元素造成布局影響。在開發(fā)當(dāng)中,如果當(dāng)前元素屬于body的子級(jí)元素,又希望應(yīng)用三維變形,則在body和當(dāng)前元素之間多嵌套一層結(jié)構(gòu),并為這層元素應(yīng)用三維變形即可。
觸發(fā)方法2:直接使用CSS3變形語法
!DOCTYPE?html
head
meta?charset="UTF-8"
title言成科技/title
style
.box1?{
width:?150px;
height:?150px;
border:?2px?solid?blue;
}
.box1?div?{
height:?150px;
background:?rgba(0,?0,?0,?0.5);
-webkit-transform:?translate3d(30px,?60px,?20px)?rotateX(30deg);
transform:?translate3d(30px,?60px,?20px)?rotateX(30deg);
}
/style
/head
body
div?class="box1"
div/div
/div
/body
/html
具體三維變形的具體屬性詳見《CSS3-3D相關(guān)知識(shí)詳解—視角以及變形方向》
3D效果制作
需求
制作一個(gè)立方體,并進(jìn)行旋轉(zhuǎn)
代碼實(shí)例
!DOCTYPE?HTML
html
head
meta?charset="utf-8"?/
title言成科技/title
link?rel="stylesheet"?type="text/css"?href=""?/
style
.main-bac?{?-webkit-perspective:1500;?}?/*設(shè)定透視距離*/
.main{
width:200px;?
height:200px;?
margin:?0?auto;
position:relative;
-webkit-transform-style:preserve-3d;
-webkit-transition:-webkit-transform?2s?ease?0s;/*過渡時(shí)間*/
}
/*基本樣式*/
.main?p{
position:?absolute;?
margin:?0;?
padding:?0;?
width:?200px;?
height:?200px;??
text-align:?center;?
line-height:?200px;?
font-size:?26px;?
opacity:0.5;
}
/*將第一個(gè)元素Z軸向前移動(dòng)100px,形成第一個(gè)面(正面)*/
.main?p:nth-of-type(1)?{
background-color:red;
-webkit-transform:translateZ(100px);
}
/*將第一個(gè)元素Z軸向前移動(dòng)100px,繞x軸旋轉(zhuǎn)90度形成上面的面*/
.main?p:nth-of-type(2)?{
background-color:orange;?
-webkit-transform:rotateX(90deg)?translateZ(100px);
}
/*將第一個(gè)元素Z軸向前移動(dòng)100px,繞x軸旋轉(zhuǎn)-90度形成下邊的面*/
.main?p:nth-of-type(3)?{
background-color:yellow;
-webkit-transform:rotateX(-90deg)?translateZ(100px);
}
/*將第一個(gè)元素Z軸向前移動(dòng)100px,繞y軸旋轉(zhuǎn)90度形成右側(cè)的面*/
.main?p:nth-of-type(4)?{
background-color:green;
-webkit-transform:rotateY(90deg)?translateZ(100px);
}
/*將第一個(gè)元素Z軸向前移動(dòng)100px,繞y軸旋轉(zhuǎn)-90度形成左側(cè)的面*/
.main?p:nth-of-type(5)?{
background-color:#b435bf;
-webkit-transform:rotateY(-90deg)?translateZ(100px);
}
/*將第一個(gè)元素Z軸向前移動(dòng)100px,繞y軸旋轉(zhuǎn)180度形成后面(背面)*/
.main?p:nth-of-type(6)?{
background-color:blue;
-webkit-transform:rotateY(180deg)?translateZ(100px);
}
/*鼠標(biāo)移入時(shí)繞Y軸旋轉(zhuǎn)180度,繞Z軸旋轉(zhuǎn)180度*/
.main:hover?{-webkit-transform:rotateY(180deg)?rotateZ(180deg);?}???
/style
/head
body
div?class="main-bac"
div?class="main"
p言成科技/p
p3D立方體/p
pHTML5學(xué)堂/p
p3D立方體/p
p碼匠/p
pJavaScript/p
/div??????????????
/div
/body
/html
代碼解析
當(dāng)鼠標(biāo)移入的時(shí)候,立方體逐漸的發(fā)生旋轉(zhuǎn)(非突變),圍繞X軸旋轉(zhuǎn)45度的同時(shí),圍繞Y軸旋轉(zhuǎn)45度。
當(dāng)鼠標(biāo)移出立方體時(shí),立方體恢復(fù)到初始狀態(tài)。在最開始狀態(tài)時(shí),并沒有采用無限遠(yuǎn)的視角,設(shè)置一定的視角,讓剛開始時(shí)直視立方體時(shí),不會(huì)覺得是一個(gè)平面。
3D效果制作-目標(biāo)效果圖
以上資料來源:《HTML5布局之路》
本文題目:html53d效果,html3d動(dòng)畫
網(wǎng)頁(yè)鏈接:http://aaarwkj.com/article24/dssgjje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、商城網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)公司、手機(jī)網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)