码迷,mamicode.com
首页 > 其他好文 > 详细

canvas绘制小人开口和闭口

时间:2017-09-12 15:52:50      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:ext   path   element   sem   round   func   yellow   contex   center   

css:
<style>
body{
text-align: center;
}
canvas{
background: #ddd;
}
</style>
canvas标签:
<canvas id="canvas" width="500" height="400"></canvas>
js:
var elem = document.getElementById("canvas");
var canvas = elem.getContext("2d");
//开始路径

//张嘴函数
function openMouth(){
canvas.beginPath();
canvas.arc(250,200,100,30*Math.PI/180,330*Math.PI/180);
canvas.lineTo(250,200);
canvas.fillStyle = "yellow";
canvas.fill();
canvas.closePath();
canvas.stroke();
//眼睛
canvas.beginPath();
canvas.arc(270,150,25,Math.PI*2,false);
canvas.fillStyle = "#38f";
canvas.fill();
canvas.stroke();
//眼神
canvas.beginPath();
canvas.arc(275,140,8,Math.PI*2,false);
canvas.fillStyle = "white";
canvas.fill();
canvas.stroke();
}
//openMouth();
//闭嘴函数
function closeMouth(){
canvas.beginPath();
canvas.arc(250,200,100,0,2*Math.PI);
canvas.fillStyle = "orange";
canvas.fill();
canvas.lineTo(250,200);
canvas.stroke();
//眼睛
canvas.beginPath();
canvas.arc(260,150,25,Math.PI*2,false);
canvas.fillStyle = "#38f";
canvas.fill();
canvas.stroke();
//眼神
canvas.beginPath();
canvas.arc(270,145,8,Math.PI*2,false);
canvas.fillStyle = "white";
canvas.fill();
canvas.stroke();
}
// closeMouth();
//定是闭嘴或者张嘴
var index = 1 ;
var timer = setInterval(function(){
canvas.clearRect(0,0,500,400);
index ++;
if(index%2==0){
openMouth();
}else{
closeMouth();
}
},1000);

canvas绘制小人开口和闭口

标签:ext   path   element   sem   round   func   yellow   contex   center   

原文地址:http://www.cnblogs.com/wangruifang/p/7509586.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!