咱们先来画一个长方形:css
.Rectangle { height: 100px; width: 200px; background: darkgray; border-width: 50px; border-style: solid; border-top-color: cyan; border-bottom-color: blue; border-left-color: orange; border-right-color: green; }
有没有发现什么? 对,四个边的交界处是45°角。那咱们能够用这个东东干点什么呢?往下看。html
若是咱们把左边的border变宽,右边的border设为0,上下的border设为透明,自身长宽都设为0,猜猜会发生什么?前端
.Triangle { height: 0; width: 0; border-left: 50px solid orange; border-top: 50px solid transparent; border-bottom: 50px solid transparent; }
不错,咱们获得了一个三角形。那么根据以上思路,咱们能够调整各边border长度,获取各类不一样形状和大小的三角形。html5
固然,梯形也是同样的道理。canvas
来,咱们先画一个圆形。 什么?不知道怎么画?Look浏览器
.Cycle { width: 120px; height: 120px; background: orange; border-radius: 60px; }
border-radius容许咱们设置上下二条边的左右侧弧度,那么采用border-radius能够画出各类圆角图形,如椭圆,鸡蛋等。svg
border-radius:2em;
border-radius是如下四种写法的简写spa
border-top-left-radius:2em;
border-top-right-radius:2em;
border-bottom-right-radius:2em;
border-bottom-left-radius:2em;
Rotate容许咱们把元素向某个方向进行旋转3d
.NoRotate { width: 100px; height: 100px; background: orange; /* transform: rotate(-45deg); */ } .Rotate { width: 100px; height: 100px; background: orange; transform: rotate(-45deg); }
:before 和 :after 伪元素是存在于真实元素前和后的虚拟元素,一般咱们能够借用这二个伪元素实现某些css效果。code
好比,恩,红星闪闪放光彩,来个红星?
好想法,以上图形能够拆解为3个三角形的叠加,使用伪元素+Rotate咱们能够巧妙的把图形叠加起来
#star { width: 0; height: 0; margin: 50px 0; color: #fc2e5a; position: relative; display: block; border-right: 100px solid transparent; border-bottom: 70px solid #fc2e5a; border-left: 100px solid transparent; transform: rotate(35deg); } #star:before { height: 0; width: 0; position: absolute; display: block; top: -45px; left: -65px; border-bottom: 80px solid #fc2e5a; border-left: 30px solid transparent; border-right: 30px solid transparent; content: ''; transform: rotate(-35deg); } #star:after { content: ''; width: 0; height: 0; position: absolute; display: block; top: 3px; left: -105px; color: #fc2e5a; border-right: 100px solid transparent; border-bottom: 70px solid #fc2e5a; border-left: 100px solid transparent; transform: rotate(-70deg); }
根据以上基础技能,咱们如今掌握了 三角形+圆形+旋转+伪元素 的技能组合,那么基本上咱们能够把常见的非规则图形拆解为以上图形进行组合。好比:
六角形,五边形,心形等。
好了,自信满满了,组合技能在手,who怕who。
BOSS: 那谁谁谁,你给我画个中国地图出来。
纳尼?Are you kidding me?
Canvas 经过 JavaScript 来绘制 2D 图形。
Canvas 是逐像素进行渲染的。
在 canvas 中,一旦图形被绘制完成,它就不会继续获得浏览器的关注。若是其位置发生变化,那么整个场景也须要从新绘制,包括任何或许已被图形覆盖的对象。
示例代码以下:
var canvas = document.getElementById('test-shape-canvas'), var ctx = canvas.getContext('2d'); ctx.clearRect(0, 0, 200, 200); // 擦除(0,0)位置大小为200x200的矩形,擦除的意思是把该区域变为透明 ctx.fillStyle = '#dddddd'; // 设置颜色 ctx.fillRect(10, 10, 130, 130); // 把(10,10)位置大小为130x130的矩形涂色 // 利用Path绘制复杂路径: var path=new Path2D(); path.arc(75, 75, 50, 0, Math.PI*2, true); path.moveTo(110,75); path.arc(75, 75, 35, 0, Math.PI, false); path.moveTo(65, 65); path.arc(60, 65, 5, 0, Math.PI*2, true); path.moveTo(95, 65); path.arc(90, 65, 5, 0, Math.PI*2, true); ctx.strokeStyle = '#0000ff'; ctx.stroke(path);
SVG 是一种使用 XML 描述 2D 图形的语言。
SVG 基于 XML,这意味着 SVG DOM 中的每一个元素都是可用的。您能够为某个元素附加 JavaScript 事件处理器。
在 SVG 中,每一个被绘制的图形均被视为对象。若是 SVG 对象的属性发生变化,那么浏览器可以自动重现图形。
示例代码以下:
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="190"> <polygon points="100,10 40,180 190,60 10,60 160,180" style="fill:lime;stroke:purple;stroke-width:5;fill-rule:evenodd;"> </svg>
总的来讲,随着CSS3和HTML5新特性的逐渐发力,前端图形展现不少时候脱离了原始的图片模式,采用CSS绘图能够减小图片的使用,缩小页面大小,加快加载速度,特别是对于移动端具备较高的性价比。
refers:
http://www.w3school.com.cn/html5/html_5_canvas_vs_svg.asp
https://www.cnblogs.com/liangxiaofeng/p/5936760.html
https://www.liaoxuefeng.com/wiki