效果图

五角星计算方式

代码

<body style="margin:0px;padding:0px;width:100%;height:100%;overflow:hidden;">
<canvas id="canvas" style="border: 1px solid #aaa" width="1920" height="962">
你的浏览器不支持canvas,请更换浏览器再试!
</canvas>
<script>
var c=document.getElementById("canvas");
c.width=document.body.clientWidth;
c.height=document.body.clientHeight; window.onload=function(){
if(c.getContext("2d")){
var cxt= c.getContext("2d");
}else{
document.write("你的浏览器不支持canvas,请更换浏览器再试!");
}
//星空背景颜色
var linearCrad=cxt.createLinearGradient(0,0,0, c.height* 0.68);
linearCrad.addColorStop(0.0, '#7ac4e1');
linearCrad.addColorStop(0.2, '#5eb8dd');
linearCrad.addColorStop(1.0, '#b6e2e5');
cxt.fillStyle=linearCrad;
cxt.fillRect(0,0, c.width, c.height);
//循环绘制100颗星星
for(var i=0 ; i<100 ; i++){
var r=Math.random() * 6 + 2;//半径
var x=Math.random() * c.width * 0.98 ; // x偏移量
var y=Math.random() * c.height * 0.6; //y偏移量
var a=Math.random() * 360; //旋转角度
drawStart(cxt , r/2.0 , r , x , y , a);
}
//月亮
fillMoon(cxt ,2, c.width * 0.88 , c.height * 0.24 ,100 ,20 , "#fff");
//绿地
drawLand(cxt);
}
/*
* cxt:绘图环境
* r:小圆半径
* R:大圆半径
* x:x轴偏移量
* y:y轴偏移量
* rot:旋转角度(逆时针)*/
function drawStart(cxt,r,R,x,y,rot){
cxt.beginPath();
cxt.fillStyle="#fff"
cxt.strokeStyle="#88b9dd";
for(var i=0 ; i < 5 ;i++){
cxt.lineTo( Math.cos((18+ i*72 - rot )/180 * Math.PI) * R + x//大圆
, -Math.sin((18 + i*72 - rot)/180 *Math.PI) * R + y);
cxt.lineTo( Math.cos((54+ i*72 - rot )/180 * Math.PI) * r + x//小圆
, -Math.sin((54 + i*72 - rot)/180 *Math.PI) * r + y);
}
cxt.closePath();
cxt.fill();
cxt.stroke();
}
/*
*参数说明
*R: 半径
* rot:旋转角度
*fillColor: 填充颜色
* */
function fillMoon(cxt ,d , x , y , R , rot , fillColor){
cxt.save();
cxt.translate(x , y);
cxt.rotate(rot * Math.PI / 180);
cxt.scale(R , R);
pathMoon(cxt , d);
cxt.fillStyle=fillColor || "#fb5";
cxt.shadowColor = "rgba(58,88,124,0.5)";
cxt.shadowOffsetX = 14 ;
cxt.shadowOffsetY = 14;
cxt.shadowBlur = 30;
cxt.fill(); cxt.restore();
}
/*
d :坐标
*/
function pathMoon(cxt , d){
cxt.beginPath();
/*
*圆心(0,0)
*半径:1
* */
cxt.arc(0 , 0 , 1 , 0.5 * Math.PI , 1.5 * Math.PI , true);
cxt.moveTo(0 , -1);
cxt.arcTo(d , 0 , 0 , 1 , dis(0 , -1 , d , 0) / d);
cxt.closePath();
}
function dis(x1 , y1 ,x2 , y2){
return Math.sqrt( (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) );
} /*
* 绿地
* */
function drawLand(cxt){
cxt.save();
cxt.beginPath();
cxt.moveTo(0 , c.height * 0.7);
/*cxt.bezierCurveTo(540 , 400 , 660 , 800 , 1200 , 600);*/
cxt.bezierCurveTo(c.width * 0.4 , c.height * 0.5 , c.width * 0.5 , c.height * 0.9 , c.width , c.height * 0.8);
cxt.lineTo(c.width , c.height);
cxt.lineTo(0 , c.height);
cxt.closePath();
//添加渐变
var landStyle = cxt.createLinearGradient(0 , 800 , 0 ,0);
landStyle.addColorStop(0.0 ,'#7dbf44');
landStyle.addColorStop(0.2 ,'#b9d532');
landStyle.addColorStop(1 ,'#79bd46');
cxt.fillStyle=landStyle;
cxt.fill();
cxt.restore();
}
</script>
</body>

使用canvas绘制一片星空的更多相关文章

  1. HTML5 Canvas ( 绘制一片星空 )

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. HTML5 Canvas ( 绘制一轮弯月, 星空中的弯月 )

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. 用HTML5绘制的一个星空特效图

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. HTML5学习总结——canvas绘制象棋(canvas绘图)

    一.HTML5学习总结——canvas绘制象棋 1.第一次:canvas绘制象棋(笨方法)示例代码: <!DOCTYPE html> <html> <head> & ...

  5. 用canvas绘制折线图

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 封装 用canvas绘制直线的函数--面向对象

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 学习笔记:HTML5 Canvas绘制简单图形

    HTML5 Canvas绘制简单图形 1.添加Canvas标签,添加id供js操作. <canvas id="mycanvas" height="700" ...

  8. canvas绘制经典折线图(一)

    最终效果图如下: 实现步骤如下:注-引用了jQuery HTML代码 <!doctype html> <html lang="en"> <head&g ...

  9. Canvas绘制图形

    1.Canvas绘制一个蓝色的矩形 <!DOCTYPE html> <html> <head lang="en"> <meta chars ...

随机推荐

  1. 了解HTML 盒模型

    HTML在布局上, 有一个非常重要的模型, 那就是盒子模型, 在盒子模型中把标签内容理解为一个物品, 而css样式理解为包容着这个物品的盒子, 一般的块级标签都具有盒子模型的特征, 你可以在css中对 ...

  2. [windows]win10家庭版切换到管理员账户

    背景:很多时候,在安装或者运行某些程序时会需要到管理员账户运行.而在win10家庭版却没有明显的位置可以让用户简单的进行切换.因此,有了以下的方法. 方法: 1.在搜索框中输入CMD,右键以管理员方式 ...

  3. 迁移Reporting Services的方法与WMI错误

    今天上午,接到一个任务:迁移SQL SERVER 2005的报表服务到另外一台SQL SERVER 2008服务器,结果等我备份了两边服务器的ReportServer,ReportServerTemp ...

  4. BOOST.Asio——扫盲

    以下内容来自互联网. 鉴于版权之类的东西,我只贴出标题和URL. (无法考证下述资料是否原创.) asio串口编程                                            ...

  5. linux ssh和scp实例

    ssh 192.160.1.100 -p 40012 scp user 192.169.72.2:/ scp -i /id_rsa -P40027 root@221.212.235.17:/sdzw/ ...

  6. python2不同版本安装json模块

    1.常用json库主要有json-py和simplejson 1) json-py 包含json和minjson,用法一样 Python (#, Jan , ::) [GCC (Red Hat -)] ...

  7. 常用ADC滤波处理

    #define N 70 XDATA WORD Value_buf[N]; XDATA DWORD ADCValue; static BYTE v_gu8cnt=0; static BYTE i=0; ...

  8. tomcat的简单安装及配置

    实验系统:CentOS 6.6_x86_64 实验前提:防火墙和selinux都关闭 实验软件:apache-tomcat-8.0.24 jdk-8u60-linux-x64 jeecms-v6 一. ...

  9. http 状态码含义

    HTTP状态码被分为五大类, 目前我们使用的HTTP协议版本是1.1, 支持以下的状态码.随着协议的发展,HTTP规范中会定义更多的状态码. 小技巧: 假如你看到一个状态码518, 你并不知道具体51 ...

  10. spring的注入

    1 可能遇到的问题: 异常信息 NoSuchBeanDefinitionException: No matching bean of type [...]或是NoSuchBeanDefinitionE ...