歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> HTML5 利用Canvas API 組合圖形

HTML5 利用Canvas API 組合圖形

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

在HTML5中有11種組合圖形的方式,只要把他們設置到context.globalCompositeOperation中就可以了,我這裡做了一個小例子來證明各種圖形組合方式的結果

HTML代碼很簡單,就2個控件,一個是下拉列表,讓用戶選擇組合方式,並且一旦用戶做出了選擇,就執行js函數draw(id),從而在第二個控件canvas上根據用戶當前選擇的組合方式進行畫圖。第二個控件就是一個canvas,用於顯示畫圖的內容。

  1. <!DOCTYPE html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title>HTML5 Combine Shape DEMO</title>
  5. <script type="text/javascript" src="js/drawCombinedShape.js"></script>
  6. </head>
  7. <body></body>
  8. <h2>canvas:顯示組合圖形</h2>
  9. <!-- 創建一個下拉列表來讓用戶選擇按照什麼方式來組合圖形 -->
  10. <!-- 一旦用戶做出了選擇,就會觸發onchange處理函數,於是調用js函數,讓其在canvas組件上畫圖 -->
  11. <select id="selectCombineMethod" onchange="draw('canvas')">
  12. <option >source-atop</option>
  13. <option>source-in</option>
  14. <option>source-out</option>
  15. <option>source-over</option>
  16. <option>destination-atop</option>
  17. <option>destination-in</option>
  18. <option>destination-out</option>
  19. <option>destination-over</option>
  20. <option>lighter</option>
  21. <option>copy</option>
  22. <option>xor</option>
  23. </select>
  24. <br><br>
  25. <!-- 指定一個canvas元素用於顯示結果 -->
  26. <canvas id="canvas" width="1000” height="1000"/>
  27. <br><br>

js函數就是負責響應下拉列表的onchange事件從而在canvas上畫圖,它先繪制原圖形(distination,在這裡是一個藍色正方形),然後取得用戶選擇的組合方式,再根據此方式畫出新圖形(source,在這裡是一個紅色的圓):

  1. /**
  2. * This file is confidential by Charles.Wang
  3. * Copyright belongs to Charles.wang
  4. * You can make contact with Charles.Wang ([email protected])
  5. */
  6. function draw(id){
  7. //得到用戶選擇的圖形組合選項:
  8. var selectComponent=document.getElementById("selectCombineMethod");
  9. //取得用戶的選擇的索引
  10. var selectedIndex =selectComponent.selectedIndex;
  11. //得到用戶的選擇的值,也就是選擇的圖形組合策略
  12. var selectedCombinedStrategy = selectComponent.options[selectedIndex].value;
  13. //得到頁面上的畫布對象
  14. var canvas=document.getElementById(id);
  15. if(canvas ==null)
  16. return false;
  17. var context = canvas.getContext('2d');
  18. //畫原來的圖形,藍色正方形
  19. context.fill;
  20. context.fillRect(40,40,60,60);
  21. //將用戶選擇的圖形組合方式設定到context中
  22. context.globalCompositeOperation=selectedCombinedStrategy;
  23. //畫新圖形,是一個紅色的圓
  24. //這時候,context會根據圖形的組合策略來決定如何繪制這2個圖形
  25. context.beginPath();
  26. context.fill;
  27. context.arc(40+60,40+60,30,0,Math.PI*2,false);
  28. context.fill();
  29. }

實驗可以根據你用戶的選擇來顯示不同結果:

這裡的source是紅色的圓(新圖形),distination是藍色正方形(舊圖形)

  • source-atop=新圖形中(新 and 老)+老圖形中(!(新 and 老))

650) this.width=650;" border=0>

  • source-in=新圖形中(新 and 老)

650) this.width=650;" border=0>

  • source-out=新圖形中(!(新 and 老))

650) this.width=650;" border=0>

  • source-over(默認值)=老圖形中(!(新 and 老))+新圖形中(全部)

650) this.width=650;" border=0>

  • destination-atop=老圖形中(新 and 老)+新圖形中(!(新 and 老))

650) this.width=650;" border=0>

  • destination-in=老圖形中(新 and 老)

650) this.width=650;" border=0>

  • destination-out=老圖形中(!(新 and 老))

650) this.width=650;" border=0>

  • destination-over=老圖形中(全部)+新圖形中(!(新 and 老))

650) this.width=650;" border=0>

  • lighter=老圖形中(!(新 and 老))+ 新圖形中(!(新 and 老))+新 and 老(色彩疊加)

650) this.width=650;" border=0>

  • copy=新圖形中(全部)

650) this.width=650;" border=0>

  • xor(對稱差)=老圖形中(!(新 and 老))+新圖形中(!(新 and 老))

650) this.width=650;" border=0>

Copyright © Linux教程網 All Rights Reserved