HTML5 canvas 之 小丑鱼

来源:互联网 发布:车管家软件是什么 编辑:程序博客网 时间:2024/05/15 21:57
HTML5CSSJavaCC++
使用html 的新特性绘制的小丑鱼,的确很丑哦。 
之前使用canvas做过复杂的应用,性能还不错 :) 

小丑鱼图片: 
 

<canvas> element 介绍 
Java代码  收藏代码
  1. <canvas id="tutorial" width="150" height="150"></canvas>    
  2.   
  3. <canvas id="stockGraph" width="150" height="150">    
  4.   current stock price: $3.15 +0.15    
  5. </canvas>  


创建canvas 
Java代码  收藏代码
  1. var canvas = document.getElementById('tutorial');    
  2. var ctx = canvas.getContext('2d');    



页面前端代码 
Java代码  收藏代码
  1. <body>  
  2. <!-- Start Draw View Range -->  
  3. <div id="placeholder" style="WIDTH: 600px; HEIGHT: 600px"></div>  
  4. <!-- End Draw View Range -->  
  5. </body>  

页面canvas代码,支持IE版本 
Java代码  收藏代码
  1. function constructCanvas() {  
  2.             function makeCanvas(width, height) {  
  3.                 var c = document.createElement('canvas');  
  4.                 c.width = width;  
  5.                 c.height = height;  
  6.                 if ($.browser.msie) // excanvas hack  
  7.                     c = window.G_vmlCanvasManager.initElement(c);  
  8.                 return c;  
  9.             }              
  10.             canvasWidth = target.width();  
  11.             canvasHeight = target.height();  
  12.             target.html(""); // clear placeholder  
  13.             if (target.css("position") == 'static')  
  14.                 target.css("position""relative"); // for positioning labels and overlay  
  15.   
  16.             if (canvasWidth <= 0 || canvasHeight <= 0)  
  17.                 throw "Invalid dimensions for plot, width = " + canvasWidth + ", height = " + canvasHeight;  
  18.   
  19.             if ($.browser.msie) // excanvas hack  
  20.                 window.G_vmlCanvasManager.init_(document); // make sure everything is setup  
  21.               
  22.             // the canvas  
  23.             canvas = $(makeCanvas(canvasWidth, canvasHeight)).appendTo(target).get(0);  
  24.             ctx = canvas.getContext("2d");  
  25.   
  26.             // overlay canvas for interactive features  
  27.             overlay = $(makeCanvas(canvasWidth, canvasHeight)).css({ position: 'absolute', left: 0, top: 0 }).appendTo(target).get(0);  
  28.             octx = overlay.getContext("2d");  
  29.             octx.stroke();  
  30.         }  
  31.       

原创粉丝点击