用canvas绘制星星和月亮
2015-11-07 11:23
363 查看
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<canvas id="canvas" style="border:1px solid; display:block; margin:50px auto;"></canvas>
<script>
window.onload = function(){
var canvas = document.getElementById("canvas");
canvas.width = 800;
canvas.height = 800;
var cxt = canvas.getContext("2d");
//绘制星星
for(var i=0; i<200; i++){
var r = Math.random()*10;
fillStar(r,r*2,Math.random()*800-20,Math.random()*390,cxt);
}
//绘制月亮
cxt.beginPath();
cxt.moveTo(600,100);
//外圆
cxt.quadraticCurveTo(730,130,670,230);
//内圆
cxt.quadraticCurveTo(700,140,600,100);
cxt.fillStyle = "#c0c0c0";
cxt.stroke();
cxt.fill();
}
function fillStar(r,R,x,y,cxt){
cxt.beginPath();
for(i=0;i<5;i++){
cxt.lineTo(Math.cos((18+i*72)/180*Math.PI)*R+x,-Math.sin((18+i*72)/180*Math.PI)*R+y);
cxt.lineTo(Math.cos((52+i*72)/180*Math.PI)*r+x,-Math.sin((52+i*72)/180*Math.PI)*r+y);
}
cxt.fillStyle = "yellow";
cxt.closePath();
cxt.fill();
cxt.stroke();
}
</script>
</head>
</html>
<html>
<head>
<meta charset="UTF-8"/>
<canvas id="canvas" style="border:1px solid; display:block; margin:50px auto;"></canvas>
<script>
window.onload = function(){
var canvas = document.getElementById("canvas");
canvas.width = 800;
canvas.height = 800;
var cxt = canvas.getContext("2d");
//绘制星星
for(var i=0; i<200; i++){
var r = Math.random()*10;
fillStar(r,r*2,Math.random()*800-20,Math.random()*390,cxt);
}
//绘制月亮
cxt.beginPath();
cxt.moveTo(600,100);
//外圆
cxt.quadraticCurveTo(730,130,670,230);
//内圆
cxt.quadraticCurveTo(700,140,600,100);
cxt.fillStyle = "#c0c0c0";
cxt.stroke();
cxt.fill();
}
function fillStar(r,R,x,y,cxt){
cxt.beginPath();
for(i=0;i<5;i++){
cxt.lineTo(Math.cos((18+i*72)/180*Math.PI)*R+x,-Math.sin((18+i*72)/180*Math.PI)*R+y);
cxt.lineTo(Math.cos((52+i*72)/180*Math.PI)*r+x,-Math.sin((52+i*72)/180*Math.PI)*r+y);
}
cxt.fillStyle = "yellow";
cxt.closePath();
cxt.fill();
cxt.stroke();
}
</script>
</head>
</html>
相关文章推荐
- Canvas 在高清屏下绘制图片变模糊的解决方法
- 使用canvas实现仿新浪微博头像截取上传功能
- js+HTML5实现canvas多种颜色渐变效果的方法
- javascript+HTML5的Canvas实现Lab单车动画效果
- html5 canvas js(数字时钟)实例代码
- javascript+canvas制作九宫格小程序
- 通过javascript把图片转化为字符画
- js+html5通过canvas指定开始和结束点绘制线条的方法
- PHP实现将HTML5中Canvas图像保存到服务器的方法
- js+html5实现canvas绘制简单矩形的方法
- javascript基于HTML5 canvas制作画箭头组件
- javascript+HTML5的canvas实现七夕情人节3D玫瑰花效果代码
- Android 游戏开发之Canvas画布的介绍及方法
- Android中使用Canvas绘制南丁格尔玫瑰图(Nightingale rose diagram)
- javascript结合canvas实现图片旋转效果
- JS+Canvas实现的俄罗斯方块游戏完整实例
- 利用HTML5的画布Canvas实现刮刮卡效果
- js+html5实现canvas绘制圆形图案的方法
- angularJS结合canvas画图例子
- 使用JavaScript+canvas实现图片裁剪