效果图

五角星计算方式

代码

<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. 带交互的 iOS 产品原型可以用什么软件制作?

    摘自知乎http://www.zhihu.com/question/20326729 来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 首先如果你小团队或者个人开发,当然 ...

  2. Android 最全Activity生命周期

    新进入Activity:onCreate > onStart > onResume 退出Activity:onPause > onStop > onDestroy 目前处于该A ...

  3. W3School-CSS 内边距 (padding) 实例

    CSS 内边距 (padding) 实例 CSS 实例 CSS 背景实例 CSS 文本实例 CSS 字体(font)实例 CSS 边框(border)实例 CSS 外边距 (margin) 实例 CS ...

  4. 【转】天啦噜!原来Chrome自带的开发者工具还能这么用!(提升JS调试能力的10个技巧)

    天啦噜!原来Chrome自带的开发者工具还能这么用! (提升JS调试能力的10个技巧)   Chrome自带开发者工具.它的功能十分丰富,包括元素.网络.安全等等.今天我们主要介绍JavaScript ...

  5. strstr 函数的实现

    strstr函数:返回主串中子字符串的位置后的所有字符. #include <stdio.h> const char *my_strstr(const char *str, const c ...

  6. Ubuntu配置OpenLDAP

    sudo apt-get install slapd ldap-utils sudo dpkg-reconfigure slapd sudo apt-get purge slapd sudo apt- ...

  7. 数据结构(Java描述)之线性表

    基础概念 数据结构:是相互之间存在一种或多种关系的数据元素的集合. 逻辑结构和物理结构 关于数据结构,我们可以从逻辑结构和物理结构这两个维度去描述 逻辑结构是数据对象中数据元素之间的关系,是从逻辑意义 ...

  8. 【读书笔记《Bootstrap 实战》】1.初识Bootstrap

    作为Web前端开发框架,Bootstrap为大多数标准的UI设计常见提供了用户友好.扩浏览器的解决方案. 1.下载Bootstrap 打开官方网址 http://getbootstrap.com/ 进 ...

  9. 高阶Laplace曲面形变算法(Polyharmonic Deformation)

    数学上曲面的连续光滑形变可以通过最小化能量函数来建模得到,其中能量函数用来调节曲面的拉伸或弯曲程度,那么能量函数最小化同时满足所有边界条件的最优解就是待求曲面. 能量函数通常是二次函数形式: 其中S* ...

  10. hibernate inverse属性的作用

    hibernate配置文件中有这么一个属性inverse,它是用来指定关联的控制方的.inverse属性默认是false,若为false,则关联由自己控制,若为true,则关联由对方控制.见例子: 一 ...