canvas實現的千輪眼程式碼例項

admin發表於2017-02-13
本章節分享一段程式碼例項,它利用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>

相關文章