【Canvas与图标】绿色数据库图标

打印 上一主题 下一主题

主题 1043|帖子 1043|积分 3129

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
【成图】

120*120的png icon:



各种巨细图:









【代码】


  1. <!DOCTYPE html>
  2. <html lang="utf-8">
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  4. <head>
  5.      <title>绿色数据库图标 Draft1</title>
  6.      <style type="text/css">
  7.      .centerlize{
  8.         margin:0 auto;
  9.         width:1200px;
  10.      }
  11.      </style>
  12.      </head>
  13.      <body οnlοad="init();">
  14.         <div class="centerlize">
  15.             <canvas id="myCanvas" width="12px" height="12px" style="border:1px dotted black;">
  16.                 如果看到这段文字说您的浏览器尚不支持HTML5 Canvas,请更换浏览器再试.
  17.             </canvas>
  18.         </div>
  19.      </body>
  20. </html>
  21. <script type="text/javascript">
  22. <!--
  23. /*****************************************************************
  24. * 将全体代码(从<!DOCTYPE到script>)拷贝下来,粘贴到文本编辑器中,
  25. * 另存为.html文件,再用chrome浏览器打开,就能看到实现效果。
  26. ******************************************************************/
  27. // canvas的绘图环境
  28. var ctx;
  29. // 高宽
  30. const WIDTH=120;
  31. const HEIGHT=120;
  32. // 舞台对象
  33. var stage;
  34. //-------------------------------
  35. // 初始化
  36. //-------------------------------
  37. function init(){
  38.     // 获得canvas对象
  39.     var canvas=document.getElementById('myCanvas');  
  40.     canvas.width=WIDTH;
  41.     canvas.height=HEIGHT;
  42.     // 初始化canvas的绘图环境
  43.     ctx=canvas.getContext('2d');  
  44.     ctx.translate(WIDTH/2,HEIGHT/2);// 原点平移
  45.     // 准备
  46.     stage=new Stage();   
  47.     stage.init();
  48.     // 开幕
  49.     animate();
  50. }
  51. // 播放动画
  52. function animate(){   
  53.     stage.update();   
  54.     stage.paintBg(ctx);
  55.     stage.paintFg(ctx);     
  56.     // 循环
  57.     if(true){
  58.         //sleep(100);
  59.         window.requestAnimationFrame(animate);   
  60.     }
  61. }
  62. // 舞台类
  63. function Stage(){
  64.     // 初始化
  65.     this.init=function(){
  66.         
  67.     }
  68.     // 更新
  69.     this.update=function(){   
  70.         
  71.     }
  72.     // 画背景
  73.     this.paintBg=function(ctx){
  74.         ctx.clearRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);// 清屏   
  75.     }
  76.     // 画前景
  77.     this.paintFg=function(ctx){
  78.         // 底色
  79.         ctx.save();
  80.         ctx.fillStyle = "white";
  81.         ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);
  82.         ctx.restore();
  83.         const R=55;// 基准尺寸
  84.         // 第1圈
  85.         ctx.save();   
  86.         var r=R*1.00;
  87.         var gnt=ctx.createRadialGradient(0,0,0,0,0,r*Math.sqrt(2));// 辐射渐变圈
  88.         gnt.addColorStop(0,"rgb(3,163,3)");
  89.         gnt.addColorStop(1,"rgb(32,69,33)");
  90.         ctx.fillStyle=gnt;
  91.         drawRoundRect(ctx,0,0,2*r,2*r,R/6);
  92.         ctx.fill();
  93.         ctx.restore();
  94.         // 数据库
  95.         ctx.save();   
  96.         var r=R*0.60;
  97.         drawDB(ctx,0,0,r);
  98.         // 三个眼
  99.         drawSolidCircle(ctx,-r*0.8,-r*0.4,r/12,"rgb(15,127,17)");
  100.         drawSolidCircle(ctx,-r*0.8,-r*0.4+r*0.55,r/12,"rgb(15,127,17)");
  101.         drawSolidCircle(ctx,-r*0.8,-r*0.4+r*1.10,r/12,"rgb(15,127,17)");
  102.         ctx.restore();
  103.         // 第2圈上半部分
  104.         ctx.save();   
  105.         var r=R*0.94;
  106.         drawRoundRect(ctx,0,0,2*r,2*r,R/6-0.06*R);
  107.         ctx.clip();
  108.         var a=createPt(-r,-r);
  109.         var b=createPt(-r,0);
  110.         var c=createPt(0,r/6);
  111.         var d=createPt(r,0);
  112.         var e=createPt(r,-r);
  113.         var gnt=ctx.createLinearGradient(0,-r,0,r/6);
  114.         gnt.addColorStop(0,"rgba(240,240,240,0.3)");
  115.         gnt.addColorStop(0.5,"rgba(215,215,215,0.2)");   
  116.         gnt.addColorStop(1,"rgba(190,190,190,0.1)");
  117.         ctx.fillStyle=gnt;
  118.         ctx.beginPath();
  119.         ctx.moveTo(a.x,a.y);
  120.         ctx.lineTo(b.x,b.y);
  121.         ctx.quadraticCurveTo(c.x,c.y,d.x,d.y);
  122.         ctx.lineTo(d.x,d.y);
  123.         ctx.lineTo(e.x,e.y);
  124.         ctx.closePath();
  125.         ctx.fill();
  126.         ctx.restore();        
  127.                
  128.         //writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火制图","8px consolas","grey");// 版权
  129.     }
  130. }
  131. /*----------------------------------------------------------
  132. 函数:用于绘制数据库三层桶
  133. ctx:绘图上下文
  134. x:矩形中心横坐标
  135. y1:矩形中心纵坐标
  136. radius:椭圆长轴半径
  137. ----------------------------------------------------------*/
  138. function drawDB(ctx,x,y1,radius){
  139.     var r=radius;
  140.     const GAP=radius/10.5;
  141.     const THICKNESS=radius/2.1;
  142.     ctx.save();   
  143.     ctx.translate(x,y1-r/12-GAP*3/2-THICKNESS*3/2);
  144.     ctx.fillStyle="rgb(129,253,130)";
  145.     // 盖
  146.     ctx.beginPath();
  147.     ctx.ellipse(0, 0, r, r/3.4, 0, 0,  2*Math.PI,false);
  148.     ctx.closePath();
  149.     ctx.fill();   
  150.     // 1半圈
  151.     ctx.beginPath();
  152.     var y=GAP;
  153.     ctx.ellipse(0, y, r, r/3.4, 0, 0,  1*Math.PI,false);
  154.     ctx.lineTo(-r,y);
  155.     ctx.lineTo(-r,y+THICKNESS);
  156.     ctx.ellipse(0, y+THICKNESS, r, r/3.4, 0, 1*Math.PI,  0,true);
  157.     ctx.closePath();
  158.     ctx.fill();   
  159.     // 2半圈
  160.     ctx.beginPath();
  161.     var y=GAP*2+THICKNESS;
  162.     ctx.ellipse(0, y, r, r/3.4, 0, 0,  1*Math.PI,false);
  163.     ctx.lineTo(-r,y);
  164.     ctx.lineTo(-r,y+THICKNESS);
  165.     ctx.ellipse(0, y+THICKNESS, r, r/3.4, 0, 1*Math.PI,  0,true);
  166.     ctx.closePath();
  167.     ctx.fill();
  168.     // 3半圈
  169.     ctx.beginPath();
  170.     var y=GAP*3+2*THICKNESS;
  171.     ctx.ellipse(0, y, r, r/3.4, 0, 0,  1*Math.PI,false);
  172.     ctx.lineTo(-r,y);
  173.     ctx.lineTo(-r,y+THICKNESS);
  174.     ctx.ellipse(0, y+THICKNESS, r, r/3.4, 0, 1*Math.PI,  0,true);
  175.     ctx.closePath();
  176.     ctx.fill();   
  177.    
  178.     ctx.restore();
  179. }
  180. /*----------------------------------------------------------
  181. 函数:用于绘制圆角矩形
  182. ctx:绘图上下文
  183. x:矩形中心横坐标
  184. y:矩形中心纵坐标
  185. width:矩形宽
  186. height:矩形高
  187. radius:圆角半径
  188. ----------------------------------------------------------*/
  189. function drawRoundRect(ctx,x,y,width,height,radius){
  190.     ctx.beginPath();
  191.     ctx.moveTo(x-width/2+radius,y-height/2);
  192.     ctx.lineTo(x+width/2-radius,y-height/2);
  193.     ctx.arcTo(x+width/2,y-height/2,x+width/2,y-height/2+radius,radius);
  194.     ctx.lineTo(x+width/2,y-height/2+radius);
  195.     ctx.lineTo(x+width/2,y+height/2-radius);
  196.     ctx.arcTo(x+width/2,y+height/2,x+width/2-radius,y+height/2,radius);
  197.     ctx.lineTo(x+width/2-radius,y+height/2);
  198.     ctx.lineTo(x-width/2+radius,y+height/2);
  199.     ctx.arcTo(x-width/2,y+height/2,x-width/2,y+height/2-radius,radius);
  200.     ctx.lineTo(x-width/2,y+height/2-radius);
  201.     ctx.lineTo(x-width/2,y-height/2+radius);
  202.     ctx.arcTo(x-width/2,y-height/2,x-width/2+radius,y-height/2,radius);
  203.     ctx.closePath();
  204. }
  205. /*----------------------------------------------------------
  206. 函数:用于绘制实心圆,用途是标记点以辅助作图
  207. ctx:绘图上下文
  208. x:矩形中心横坐标
  209. y:矩形中心纵坐标
  210. r:圆半径
  211. color:填充圆的颜色
  212. ----------------------------------------------------------*/
  213. function drawSolidCircle(ctx,x,y,r,color){
  214.     ctx.fillStyle=color;
  215.     ctx.beginPath();
  216.     ctx.arc(x,y,r,0,Math.PI*2,false);
  217.     ctx.closePath();
  218.     ctx.fill();
  219. }
  220. /*----------------------------------------------------------
  221. 函数:创建一个二维坐标点
  222. x:横坐标
  223. y:纵坐标
  224. Pt即Point
  225. ----------------------------------------------------------*/
  226. function createPt(x,y){
  227.     var retval={};
  228.     retval.x=x;
  229.     retval.y=y;
  230.     return retval;
  231. }
  232. /*----------------------------------------------------------
  233. 函数:延时若干毫秒
  234. milliseconds:毫秒数
  235. ----------------------------------------------------------*/
  236. function sleep(milliSeconds) {
  237.     const date = Date.now();
  238.     let currDate = null;
  239.     while (currDate - date < milliSeconds) {
  240.         currDate = Date.now();
  241.     }
  242. }
  243. /*----------------------------------------------------------
  244. 函数:书写文字
  245. ctx:绘图上下文
  246. x:横坐标
  247. y:纵坐标
  248. text:文字
  249. font:字体
  250. color:颜色
  251. ----------------------------------------------------------*/
  252. function writeText(ctx,x,y,text,font,color){
  253.     ctx.save();
  254.     ctx.textBaseline="bottom";
  255.     ctx.textAlign="center";
  256.     ctx.font = font;
  257.     ctx.fillStyle=color;
  258.     ctx.fillText(text,x,y);
  259.     ctx.restore();
  260. }
  261. /*-------------------------------------------------------------
  262. 一种商品销量大用户多评论多,好评中评差评都有;
  263. 另一种商品销量低,评论区却是清一色的好评和默认好评,
  264. 你会买哪个?
  265.                                      --24年珠海航展观感
  266. --------------------------------------------------------------*/
  267. //-->
  268. </script>
复制代码

END


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

圆咕噜咕噜

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表