清空canvas畫布內容
1、重置寬或高
由于canvas每當高度或寬度被重設時,畫布內容就會被清空,因此可以用以下方法清空:(此方法僅限需要清除全部內容的情況)
var c=document.getElementById("myCanvas"); c.width=c.width;
2、clearRect
1 var c=document.getElementById("myCanvas"); 2 var ctx=c.getContext("2d"); 3 ctx.fillStyle="red"; 4 ctx.fillRect(0,0,300,150); 5 ctx.clearRect(20,20,100,50);
3、globalCompositeOperation
引用globalCompositeOperation()函數,這個函數是用來在畫布上組合顏色,我們可以利用這個原理,疊加(數學上的"或"原理)來制作橡皮。
首先看看 globalCompositeOperation屬性可以設置的值有哪些,分別是什么效果:
值 | 描述 |
---|---|
source-over | 默認。在目標圖像上顯示源圖像。 |
source-atop | 在目標圖像頂部顯示源圖像。源圖像位于目標圖像之外的部分是不可見的。 |
source-in | 在目標圖像中顯示源圖像。只有目標圖像內的源圖像部分會顯示,目標圖像是透明的。 |
source-out | 在目標圖像之外顯示源圖像。只會顯示目標圖像之外源圖像部分,目標圖像是透明的。 |
destination-over | 在源圖像上方顯示目標圖像。 |
destination-atop | 在源圖像頂部顯示目標圖像。源圖像之外的目標圖像部分不會被顯示。 |
destination-in | 在源圖像中顯示目標圖像。只有源圖像內的目標圖像部分會被顯示,源圖像是透明的。 |
destination-out | 在源圖像外顯示目標圖像。只有源圖像外的目標圖像部分會被顯示,源圖像是透明的。 |
lighter | 顯示源圖像 + 目標圖像。 |
copy | 顯示源圖像。忽略目標圖像。 |
xor | 使用異或操作對源圖像與目標圖像進行組合。 |
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <style> 5 canvas 6 { 7 border:1px solid #d3d3d3; 8 margin-right:10px; 9 margin-bottom:20px; 10 } 11 </style> 12 </head> 13 <body> 14 15 <script> 16 17 var gco=new Array(); 18 gco.push("source-atop"); 19 gco.push("source-in"); 20 gco.push("source-out"); 21 gco.push("source-over"); 22 gco.push("destination-atop"); 23 gco.push("destination-in"); 24 gco.push("destination-out"); 25 gco.push("destination-over"); 26 gco.push("lighter"); 27 gco.push("copy"); 28 gco.push("xor"); 29 for (n=0;n<gco.length;n++) 30 { 31 document.write("<div id='p_" + n + "' style='float:left;'>" + gco[n] + ":<br>"); 32 var c=document.createElement("canvas"); 33 c.width=120; 34 c.height=100; 35 document.getElementById("p_" + n).appendChild(c); 36 var ctx=c.getContext("2d"); 37 ctx.fillStyle="blue"; 38 ctx.fillRect(10,10,50,50); 39 ctx.globalCompositeOperation=gco[n]; 40 ctx.beginPath(); 41 ctx.fillStyle="red"; 42 ctx.arc(50,50,30,0,2*Math.PI); 43 ctx.fill(); 44 document.write("</div>"); 45 } 46 47 </script> 48 49 </body> 50 </html>
可以看出如果設置成destination-out,就可以清除canvas現有的像素點的圖像。
清除繪制到畫布上的線條(點擦除,線擦除)
在我最近實現的項目中有畫筆功能, 同時畫筆畫出的線條可以被橡皮擦擦除,有點擦除和線擦除兩種方式。
使用以上兩種方法也可以,但是如果這些線條不止繪制一次的話呢,中間有其他操作(例如繪制的內容變換一次后)那上面的方法就不容易做到了,因為要反復繪制存儲每次擦除后的數據,簡單的為了能達到該目的,可以將整個canvas畫布轉化成base64編碼的image,后面再次繪制的時候把這個image數據再繪制到canvas上,可以繼續在這個canvas上進行繪制和擦除內容。但是怎么樣也不好做到線擦除的功能了!
下面介紹另外一種存儲繪制路徑點坐標的方法去實現繪制線條后的點擦除和線擦除的功能。
首先介紹下存儲線條的數據結構,之前寫的一篇《js實現存儲對象的數據結構hashTable和list》大家可以先大致看看hash結構的實現,但是key和value快速查找的優勢需要清楚。另外在canvas畫的各種形狀和線條,我們是如何知道點擊到哪個元素哪條線?《軟件項目技術點(4)——實現點擊畫布上元素》這篇博客里有說明實現原理。
1. 線條存儲及繪制
項目中我存儲的線條hash結構的對象如下:
展開第一個線條key值為“#8c471a”的具體信息如下,value值其中有colorKey,lineColor,lineWidth,以及最重要的List結構的points對象,是一個存儲了該線條所有點坐標集合的List對象。
下面的一段代碼,實現了繪制該線條到畫布。使用二次貝塞爾函數使得繪制出來的線條流暢平滑沒有折痕,當只有一個點時可繪制出一個圓點。

1 var count = this.points.length(); 2 var p: Core.Point = this.points.get(0); 3 if (isDrawHit) { 4 ctx.strokeStyle = this.colorKey; 5 } 6 else { 7 ctx.strokeStyle = this.lineColor; 8 } 9 ctx.lineCap = "round"; 10 ctx.lineJoin = 'round';//轉折的時候不出現尖角 11 if (ctx.canvas.id == "hitCanvas") 12 ctx.lineWidth = this.lineWidth + eraserRadius;//擴大hit上線條的范圍,橡皮半徑 13 else 14 ctx.lineWidth = this.lineWidth; 15 ctx.beginPath(); 16 if (count >= 2) { 17 ctx.moveTo(p.x, p.y); 18 for (var i = 1; i < count - 2; i++) { 19 // p = this.points.get(i); 20 // ctx.lineTo(p.x, p.y); 21 if (this.points.get(i).x == this.points.get(i + 1).x && this.points.get(i).y == this.points.get(i + 1).y) 22 continue; 23 var c = (this.points.get(i).x + this.points.get(i + 1).x) / 2; 24 var d = (this.points.get(i).y + this.points.get(i + 1).y) / 2; 25 ctx.quadraticCurveTo(this.points.get(i).x, this.points.get(i).y, c, d); //二次貝塞曲線函數 26 } 27 // For the last 2 points 28 if (count >= 3) { 29 ctx.quadraticCurveTo( 30 this.points.get(i).x, 31 this.points.get(i).y, 32 this.points.get(i + 1).x, 33 this.points.get(i + 1).y 34 ); 35 } else if (count >= 2) { 36 ctx.lineTo(this.points.get(1).x, this.points.get(1).y); 37 } 38 ctx.stroke(); 39 } else { 40 if (isDrawHit) { 41 ctx.fillStyle = this.colorKey; 42 } 43 else { 44 ctx.fillStyle = this.lineColor; 45 } 46 if (ctx.canvas.id == "hitCanvas") 47 var radius = this.lineWidth + eraserRadius;//擴大hit上線條的范圍,橡皮半徑 48 else 49 var radius = this.lineWidth; 50 ctx.arc(this.points.get(0).x, this.points.get(0).y, radius, 0, 2 * Math.PI); 51 ctx.fill(); 52 } 53
其中繪制到hitCanvas上的時候將lineWidth擴大加上了eraserRadius(圓形橡皮擦半徑),下圖即為繪制到hitCanvas上的colorKey顏色線條,每個線條顏色值是上圖中的key值colorKey。另外線條粗細明顯比上面的白色線條要粗很多,因為橡皮擦是個cur鼠標樣式它的半徑很大,但獲取的鼠標點擊位置還只是一個像素點坐標,所以為了擴大鼠標點到線條上的范圍將其變粗。
2. 線擦除和點擦除
這樣線擦除就很容易實現,只需要找到橡皮擦點到畫布上的坐標點的色值,就其從hash集合中根據colorKey刪除掉該項,即實現了刪除整條線。
點擦除就需要考慮到從兩端擦除或者從中間擦除的情況:
1 if (that.isErasePoint) { 2 line.points.foreach(function (i, p) { 3 //橡皮擦距離該線條上點的距離是否在橡皮擦半徑范圍內 4 if (Math.pow(p.x - point.x, 2) + Math.pow(p.y - point.y, 2) <= Math.pow(eraserRadius, 2)) { 5 isSeparate = true;//已經找到橡皮擦半徑范圍內的點,該點不存入兩個集合中的任何一個 6 } else { 7 if (isSeparate)//找到后將之后的點存入另一個點集合points2中 8 points2.add(p); 9 else//找到之前將點存入點集合points1中 10 points1.add(p); 11 } 12 }) 13 //遍歷完線條points上的所有點后。根據points1和points2是否為空處理點擦除后的線條 14 if (points1.length() >= 1 && points2.length() >= 1) {//points1和points2都不為空,說明從中間擦除變為兩條線 15 var preLine = editor.commonEditLogic.clonePenLine(line); 16 line.points = points1; 17 var linePen = editor.bdCanvas.elementFactory.createPenLine(point, line.lineWidth, line.lineColor); 18 linePen.points = points2; editor.bdCanvas.activeElement.setPenLine(linePen.colorKey, linePen); 19 } else if (points1.length() == 0 && points2.length() >= 1) {//從一端擦除 20 line.points = points2; 21 } else if (points1.length() >= 1 && points2.length() == 0) {//從一端擦除 22 line.points = points1; 23 } else if (points1.length() == 0 && points2.length() == 0) {//線條上的點全部被擦除,刪除該線條 24 editor.bdCanvas.activeElement.delPenLine(line.colorKey);26 } 27 editor.courseware.currentBlackboard.draw(false, true);30 }
文章列表