canvas
尺寸分为两种,一种是canvas
元素自己的尺寸,另外一种是canvas
画布的尺寸canvas
canvas
自己尺寸能够经过style
样式来设置浏览器
.canvas{ width:100px; height:100px; } /* 设置了元素在浏览器能够看见的尺寸 */
canvas
画布尺寸经过元素width
和height
两个属性设置,也能够经过js
给画布设置尺寸。默认尺寸为300*150
spa
<canvas width="100" height="100"></canvas>
或者code
var canvas=document.getElementById("canvas"); canvas.width = 100; canvas.height = 100;
若是两个尺寸不一致,那么画出来的图形,和想象中的图形是有差距的。对象
扇形渐变的语法:blog
var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); ctx.createRadialGradient(x1, y1, r1, x2, y2, r2); // 建立一个从以(x1, y1)点为圆心、r1为半径的圆到以(x2, y2)点为圆心、r2为半径的圆的径向渐变对象
渐变规律能够分为两种状况:图片
两个圆同圆心,或者两个圆属于包含关系get
渐变从一个圆的圆周向另外一个圆的圆周辐射渐变it
// 包含 var grb = ctx.createRadialGradient(245,175,20,285,175,75); grb.addColorStop(0, "red"); grb.addColorStop(0.5, "green"); grb.addColorStop(1, "yellow"); ctx.fillStyle = grb; ctx.fillRect(210,100,150,150);
效果以下图的包含关系:class
相交或相离
在两个圆的切线与圆周组成范围内,从开始圆的圆周向结束圆的圆周渐变
// 相交 grb = ctx.createRadialGradient(440,175,75,520,175,50); grb.addColorStop(0, "red"); grb.addColorStop(0.5, "green"); grb.addColorStop(1, "yellow"); ctx.fillStyle = grb; ctx.fillRect(380,100,200,150); // 相离 grb = ctx.createRadialGradient(675,175,75,900,175,50); grb.addColorStop(0, "red"); grb.addColorStop(0.5, "green"); grb.addColorStop(1, "yellow"); ctx.fillStyle = grb; ctx.fillRect(600,100,300,150);
效果如上图的相交和相离,在切线与圆周组成的范围内渐变