先看看最简单的效果图:
代码如下:
javascript code复制内容到剪贴板
var canvas = document.getelementbyid('my'), ctx = canvas.getcontext('2d'); setinterval(function(){ ctx.clearrect(0,0,400,400); ctx.save(); ctx.translate(200,200); var ci =90, pi = math.pi / ci, x1 = 100, y1 =0, x2 =0, y2 =0, x3 =0, y3 =0; ctx.beginpath(); for(var i = ci *2; i >0; i--){ ctx.rotate(pi); ctx.moveto(x1,y1); y2 = x1 * math.sin(pi); x2 = x1 * math.cos(pi); x3 = (x1 - x2) /2+ x2 +10+ math.random() *20; y3 = y2 /2; ctx.lineto(x3,y3); ctx.lineto(x2,y2); } ctx.stroke(); ctx.restore(); },100);
在上面多角形的基础上进一步之后为:
代码如下:
javascript code复制内容到剪贴板
var canvas = document.getelementbyid('my'), ctx = canvas.getcontext('2d'), r =10; setinterval(function(){ ctx.clearrect(0,0,400,400); ctx.save(); ctx.translate(200,200); var grad = ctx.createradialgradient(0,0,0,0,0,r+20); grad.addcolorstop(0.2,'white'); grad.addcolorstop(0.7,'yellow'); grad.addcolorstop(0.8,'orange'); ctx.beginpath(); ctx.fillstyle = grad; ctx.arc(0,0,r,0,math.pi*2,true); ctx.fill(); var ci =90, pi = math.pi / ci, x2 =0, y2 =0, x3 =0, y3 =0; x1 =100; y1 =0; ctx.beginpath(); for(var i = ci *2; i >0; i--){ ctx.rotate(pi); ctx.moveto(r,0); y2 = r * math.sin(pi); x2 = r * math.cos(pi); x3 = (r - x2) /2+ x2 +10+ math.random() *20; y3 = y2 /2; ctx.lineto(x3,y3); ctx.lineto(x2,y2); } ctx.fill(); ctx.restore(); r },100);
以上就是本文的全部内容,希望对大家的学习有所帮助。