歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> ExtJS中運用HTML5 Canvas簡單例子

ExtJS中運用HTML5 Canvas簡單例子

日期:2017/3/1 10:23:55   编辑:Linux編程

在ExtJS的Panel組件上使用HTML5的Canvas對象畫圖

的簡單例子,效果如下:


怎麼運行源代碼:

新建兩個文件,分別命名為mydemo.html, mydemo.js以後,將對應的HTML源代碼

與JavaScript代碼copy到各自的文件中,在同一目錄下使用Google Chrome浏覽器

或者IE9.0打開html文件即可看到效果!


HTML的代碼如下:

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  5. <title>Example</title>
  6. <link rel="stylesheet" type="text/css" href="../../resources/css/ext-all.css" />
  7. <link rel="stylesheet" type="text/css" href="../shared/example.css" />
  8. <script type="text/javascript" src="../../bootstrap.js"></script>
  9. <script language="javascript" src="mydemo.js"></script>
  10. </head>
  11. <body>
  12. <h1>ExtJS with HTML5 Demo</h1>
  13. <p>The js is not minified so it is readable. See <a href="mydemo.js">source code</a>.</p>
  14. <div id="my-demo"></div>
  15. </body>
  16. </html>
ExtJS的代碼如下:
  1. /**
  2. * HTML5 Canvas Demo
  3. */
  4. // create namespace
  5. Ext.namespace('Test');
  6. // create application
  7. Test.app = function() {
  8. return {
  9. // public methods
  10. init: function() {
  11. var grid = new Ext.Panel({
  12. renderTo: 'my-demo',
  13. title:'Simple HTML5 Canvas Demo',
  14. bodyStyle: 'padding: 10px;',
  15. borders: true,
  16. plain: true,
  17. xtype: 'panel',
  18. width:400,
  19. height:400,
  20. html: '<canvas id="canvas" width="400" height="400"></canvas>'
  21. });
  22. }, // end of init
  23. onDraw: function() {
  24. this.canvas = document.getElementById('canvas');
  25. this.ctx = this.canvas.getContext("2d");
  26. // create a blank image data
  27. var canvas2Data = this.ctx.createImageData(this.canvas.width, this.canvas.height);
  28. for ( var x = 0; x < canvas2Data.width; x++) {
  29. for ( var y = 0; y < canvas2Data.height; y++) {
  30. // Index of the pixel in the array
  31. var idx = (x + y * canvas2Data.width) * 4;
  32. // assign gray scale value
  33. var distance = Math.sqrt((x - canvas2Data.width / 2) * (x - canvas2Data.width / 2) + (y - canvas2Data.height / 2) * (y - canvas2Data.height / 2));
  34. var cvalue = (128.0 + (128.0 * Math.sin(distance / 8.0)));
  35. canvas2Data.data[idx + 0] = cvalue; // Red channel
  36. canvas2Data.data[idx + 1] = cvalue; // Green channel
  37. canvas2Data.data[idx + 2] = cvalue; // Blue channel
  38. canvas2Data.data[idx + 3] = 255; // Alpha channel
  39. }
  40. }
  41. this.ctx.putImageData(canvas2Data, 0, 0); // at coords 0,0
  42. // draw author infomation
  43. this.ctx.fillStyle = "red";
  44. this.ctx.font = "24px Times New Roman";
  45. this.ctx.fillText("HTML5 Demo - by gloomyfish ", 50, 60);
  46. }
  47. };
  48. }();
  49. // end of app
  50. Ext.onReady(function(){
  51. Test.app.init();
  52. Test.app.onDraw()
  53. // alert('ext.onready')
  54. });
  55. // Ext.onReady(Test.app.init, Test.app);
一定要有ExtJS的庫文件支持!!!
Copyright © Linux教程網 All Rights Reserved