本文實例講述了JavaScript+html5 canvas製作的圓中圓效果。分享給大家參考,具體如下:
運作效果截圖如下:
具體程式碼如下:
<!DOCTYPE html> <html> <head> <title>demo</title> <style type="text/css"> #canvas { background:#F2F2F2; height:500px; height:500px; margin-top:100px; margin-left:200px; } </style> </head> <body> <canvas id="canvas" width="500px" height="500px" ></canvas> </body> <script type="text/javascript"> (function() { var dyl = {}; dyl.getDom = function(id) { return document.getElementById(id); } dyl.getContext = function(canvasID) { var canvas = this.getDom(canvasID); if(!canvas) { return null; } return canvas.getContext("2d"); } if(!window.dyl) { window.dyl = dyl; } })(); cache = {}; cache.context = dyl.getContext('canvas'); // 每个圈的圆个数控制 cache.scaleNmb = 6; cache.createColor = function() { var color = "rgb("; color += Math.round(Math.random()*255); color += ","; color += Math.round(Math.random()*255); color += ","; color += Math.round(Math.random()*255); color += ")"; return color; }; cache.draw = function() { cache.context.fillRect(-10, -10, 20, 20); for(var i=1; i<10; i++) { cache.context.save(); for(var j=0; j<cache.scaleNmb*i; j++) { cache.context.rotate(Math.PI*2/(cache.scaleNmb*i)); cache.context.fillStyle = cache.createColor(); cache.context.beginPath(); cache.context.arc(0, 20*i, 5, 0,Math.PI*2, true); cache.context.closePath(); cache.context.fill(); } cache.context.restore(); } }; cache.init = function() { cache.context.translate(250, 250); cache.draw(); }; cache.init(); </script> </html>
更多關於js特效相關內容有興趣的讀者可查看本站專題:《jQuery動畫與特效用法總結》、《jQuery常見經典特效匯總》及《 JavaScript動畫特效與技巧總結》
希望本文所述對大家JavaScript程式設計有所幫助。