canvas實現的千輪眼程式碼例項
本章節分享一段程式碼例項,它利用canvas實現了千輪眼效果。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <style> body { -webkit-animation: monthly 1.5s ease-in 2s 2 alternate; -moz-animation: monthly 1.5s ease-in 2s 2 alternate; animation: monthly 1.5s ease-in 2s 2 alternate; } @-webkit-keyframes monthly { 0% { background: white } 50% { background: red } 100% { background: #712222 } } @-moz-keyframes monthly { 0% { background: white } 50% { background: red } 100% { background: #712222 } } @keyframes monthly { 0% { background: white } 50% { background: red } 100% { background: #712222 } } .rolling { margin: 100px auto; width: 320px; height: 340px; -webkit-animation: rollingeye 2.5s ease-in-out 2; -moz-animation: rollingeye 2.5s ease-in-out 2; animation: rollingeye 2.5s ease-in-out 2; } @-webkit-keyframes rollingeye { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } @-moz-keyframes rollingeye { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } @keyframes rollingeye { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } #rimofeye { position: absolute; left: 50%; margin-left: -180px; display: block; } .window { -webkit-perspective: 800px; -webkit-perspective-origin: 50% 50%; -moz-perspective: 800px; -moz-perspective-origin: 50% 50%; perspective: 800px; perspective-origin: 50% 50%; } .view { -webkit-transform-style: preserve-3d; transform-style: preserve-3d; -webkit-transform: translateZ(-400px); -webkit-animation: magic 1.5s ease-in 2s 2 alternate; -moz-transform: translateZ(-400px); -webkit-animation: magic 1.5s ease-in 2s 2 alternate; -moz-transform: translateZ(-400px); animation: magic 1.5s ease-in 2s 2 alternate; } @-webkit-keyframes magic { 0% { transform: translateZ(-400px) } 100% { transform: translateZ(100px) } } @-moz-keyframes magic { 0% { transform: translateZ(-400px) } 100% { transform: translateZ(100px) } } @keyframes magic { 0% { transform: translateZ(-400px) } 100% { transform: translateZ(100px) } } </style> </head> <body> <div class="window"> <div class="view"> <canvas id="rimofeye"></canvas> <div class="rolling"> <canvas id="Canvas" style="position:absolute;display: block;"></canvas> </div> </div> </div> <script> var ctxa; var ctxb; var eye = { x: 160, y: 170, r: 100 }; var eyer = { x: 180, y: 170, r: 100 }; var canvasA = document.getElementById("Canvas"); var canvasB = document.getElementById("rimofeye"); canvasA.width = 320; canvasA.height = 340; canvasB.width = 360; canvasB.height = 340; window.onload = init; function init() { ctxa = canvasA.getContext("2d"); ctxb = canvasB.getContext("2d"); drawEye(); } function drawEye() { //rim of the eye ctxb.beginPath(); ctxb.arc(eyer.x, eyer.y + eyer.r - 1.4 * eyer.r / 10, eyer.r * 2, Math.PI, Math.PI * 2, false); ctxb.strokeStyle = "black"; ctxb.lineWidth = "4"; ctxb.stroke(); ctxb.closePath(); ctxb.beginPath(); ctxb.arc(eyer.x, eyer.y - eyer.r + 1.4 * eyer.r / 10, eyer.r * 2, 0, Math.PI, false); ctxb.strokeStyle = "black"; ctxb.lineWidth = "4"; ctxb.stroke(); ctxb.closePath(); //eyeball ctxa.beginPath(); ctxa.arc(eye.x, eye.y, eye.r, 0, Math.PI * 2, false); ctxa.strokeStyle = "red"; ctxa.lineWidth = "3"; ctxa.stroke(); ctxa.closePath(); //pupil ctxa.beginPath(); ctxa.arc(eye.x, eye.y, eye.r / 3, 0, Math.PI * 2, false); ctxa.fillStyle = "#712222"; ctxa.fill(); ctxa.closePath(); ctxa.beginPath(); ctxa.arc(eye.x, eye.y - eye.r, eye.r / 10, 0, Math.PI * 2, false); ctxa.fillStyle = "#712222"; ctxa.lineWidth = "5"; ctxa.fill(); ctxa.closePath(); ctxa.beginPath(); ctxa.arc(eye.x, eye.y - eye.r, Math.cos(Math.PI / 6) * eye.r * 2, Math.PI / 3, Math.PI * 2 / 3, false); ctxa.strokeStyle = "#712222"; ctxa.stroke(); ctxa.closePath(); //redspot(left) ctxa.beginPath(); ctxa.arc(eye.x - Math.cos(Math.PI / 6) * eye.r, eye.y + Math.sin(Math.PI / 6) * eye.r, eye.r / 10, 0, Math.PI * 2, false); ctxa.fillStyle = "#712222"; ctxa.fill(); ctxa.closePath(); ctxa.beginPath(); ctxa.arc(eye.x - Math.cos(Math.PI / 6) * eye.r, eye.y + Math.sin(Math.PI / 6) * eye.r, Math.cos(Math.PI / 6) * eye.r * 2, (Math.PI * 5 / 3), 0, false); ctxa.strokeStyle = "#712222"; ctxa.stroke(); ctxa.closePath(); //redspot(right) ctxa.beginPath(); ctxa.arc(eye.x + Math.cos(Math.PI / 6) * eye.r, eye.y + Math.sin(Math.PI / 6) * eye.r, eye.r / 10, 0, Math.PI * 2, false); ctxa.fillStyle = "#712222"; ctxa.fill(); ctxa.closePath(); ctxa.beginPath(); ctxa.arc(eye.x + Math.cos(Math.PI / 6) * eye.r, eye.y + Math.sin(Math.PI / 6) * eye.r, Math.cos(Math.PI / 6) * eye.r * 2, -(Math.PI / 3) * 2, Math.PI, true); ctxa.strokeStyle = "#712222"; ctxa.stroke(); ctxa.closePath(); } </script> </body> </html>
相關文章
- canvas實現的刮刮樂程式碼例項Canvas
- canvas實現的美猴王程式碼例項Canvas
- canvas實現的驗證碼效果程式碼例項Canvas
- canvas實現的變幻線程式碼例項Canvas線程
- canvas實現的多邊形程式碼例項Canvas
- canvas實現的鋸齒效果程式碼例項Canvas
- canvas實現的鐘表效果程式碼例項Canvas
- 利用canvas實現的驗證碼效果程式碼例項Canvas
- canvas實現的貪吃蛇遊戲程式碼例項Canvas遊戲
- canvas實現的雪花飄落效果程式碼例項Canvas
- canvas實現的藍色雨滴效果程式碼例項Canvas
- canvas實現的圖片縮放程式碼例項Canvas
- canvas實現的彈力小球效果程式碼例項Canvas
- canvas實現煙花燃放效果程式碼例項Canvas
- canvas實現的隨機生成漂浮小球程式碼例項Canvas隨機
- canvas實現的簡單餅狀圖程式碼例項Canvas
- canvas實現的七巧板效果程式碼例項Canvas
- canvas實現的動態心形效果程式碼例項Canvas
- canvas實現的簡單塗鴉板程式碼例項Canvas
- canvas實現的五子棋程式碼例項Canvas
- canvas實現的七色板程式碼例項Canvas
- canvas實現的圓形鐘錶效果程式碼例項Canvas
- canvas實現的簡單畫板效果程式碼例項Canvas
- angularJS結合canvas實現的畫圖程式碼例項AngularJSCanvas
- canvas實現的賽貝爾曲線效果程式碼例項Canvas
- canvas實現的小球四壁碰撞效果程式碼例項Canvas
- canvas刮刮樂程式碼例項Canvas
- canvas實現的旋轉的太極圖效果程式碼例項Canvas
- canvas實現文字線性漸變效果程式碼例項Canvas
- canvas實現的卡通人物形象程式碼例項Canvas
- canvas畫圖程式碼例項Canvas
- canvas小遊戲程式碼例項Canvas遊戲
- canvas火焰效果程式碼例項Canvas
- canvas繪製實心圓形程式碼例項Canvas
- canvas實現點選產生放射性效果程式碼例項Canvas
- canvas繪製扇形程式碼例項Canvas
- canvas繪製星星程式碼例項Canvas
- canvas字母雨效果程式碼例項Canvas