在顯示馬賽克圖像時,淡入動畫是逐漸從很模糊到很清晰馬賽克的過程,淡出動畫的處理是從圖像變得清晰後又慢慢變得模糊的過程,下面我們就來看看具體的內容。
我們先來看看馬賽克影像的淡入效果
程式碼如下
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <meta charset="utf-8" /> <script type="text/javascript"> var imageData; var fadeMosaicSize = 0; var THandle; var mem_canvas; var mem_context; var context; var img; function loadImage() { img = new Image(); mem_canvas = document.createElement('canvas'); img.onload = function onImageLoad() { mem_canvas.width = img.width; mem_canvas.height = img.height; mem_context = mem_canvas.getContext('2d'); mem_context.drawImage(img, 0, 0); imageData = mem_context.getImageData(0, 0, mem_canvas.width, mem_canvas.height); startFadeIn(); } img.src = 'img/luffy.jpg'; var canvas = document.getElementById('SimpleCanvas'); if (!canvas || !canvas.getContext) { return false; }else{ context = canvas.getContext('2d'); } } function startFadeIn() { fadeMosaicSize = 64; THandle = setInterval(onFadeIn, 50); } function onFadeIn() { if (fadeMosaicSize <= 1) { clearInterval(THandle); context.drawImage(img, 32, 32); } else { CreateMosaic(mem_context, mem_canvas.width, mem_canvas.height, fadeMosaicSize); context.drawImage(mem_canvas, 32, 32); fadeMosaicSize = Math.floor(fadeMosaicSize / 1.5); } } function CreateMosaic(context, width,height,mosaicSize) { var x=0; var y=0; for (y = 0; y < height; y = y + mosaicSize) { for (x = 0; x < width; x = x + mosaicSize) { var cR = imageData.data[(y * width + x) * 4]; var cG = imageData.data[(y * width + x) * 4 + 1]; var cB = imageData.data[(y * width + x) * 4 + 2]; context.fillStyle = "rgb("+cR+","+cG+","+cB+")"; context.fillRect(x, y, x + mosaicSize, y + mosaicSize); } } } </script> </head> <body onload="loadImage();" style="background-color:#D0D0D0;"> <canvas id="SimpleCanvas" width="640" height="360" style="background-color:#FFFFFF;"></canvas> <div>Canvas Demo</div> <div id="output"></div> </body> </html>
說明:
由於body標籤的onload事件,在頁面顯示時會透過呼叫loadImagen()函數開始處理。
頁面顯示後建立內部繪製的Canves對象,並讀取影像。獲得繪製後的像素資料。之後,啟動計時器處理,在計時器的事件中建立馬賽克影像並將其繪製到畫面上。在執行計時器處理時,會減少馬賽克的大小(fadeMosaicSize),從很粗的馬賽克開始變得很清晰的馬賽克動畫,以表示淡入效果。
要調整漸變速度,可以更改計時器的間隔,
fadeMosaicSize = Math.floor(fadeMosaicSize / 1.5);
運行結果:
顯示上面的HTML檔案。顯示很深的馬賽克影像。
這是一個動態的過程,影像會慢慢的變清晰,最後就會出現如下效果
看完了淡入的效果,接下來我們來看看馬賽克圖像淡入淡出的效果實現
代碼如下
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <meta charset="utf-8" /> <script type="text/javascript"> var imageData; var fadeMosaicSize = 0; var THandle; var mem_canvas; var mem_context; var context; var img; function loadImage() { img = new Image(); mem_canvas = document.createElement('canvas'); img.onload = function onImageLoad() { mem_canvas.width = img.width; mem_canvas.height = img.height; mem_context = mem_canvas.getContext('2d'); mem_context.drawImage(img, 0, 0); imageData = mem_context.getImageData(0, 0, mem_canvas.width, mem_canvas.height); startFadeIn(); } img.src = 'img/luffy.jpg'; var canvas = document.getElementById('SimpleCanvas'); if (!canvas || !canvas.getContext) { return false; }else{ context = canvas.getContext('2d'); } } function startFadeIn() { fadeMosaicSize = 64; THandle = setInterval(onFadeIn, 50); } function startFadeOut() { fadeMosaicSize = 1; THandle = setInterval(onFadeOut, 50); } function onFadeIn() { if (fadeMosaicSize <= 1) { clearInterval(THandle); context.drawImage(img, 32, 32); THandle = setInterval(onShow, 2000); } else { CreateMosaic(mem_context, mem_canvas.width, mem_canvas.height, fadeMosaicSize); context.drawImage(mem_canvas, 32, 32); fadeMosaicSize = Math.floor(fadeMosaicSize / 1.5); } } function onShow() { clearInterval(THandle); startFadeOut(); } function onFadeOut() { if (64 < fadeMosaicSize) { clearInterval(THandle); context.fillStyle = "#FFFFFF"; context.fillRect(32, 32, mem_canvas.width, mem_canvas.height); } else { CreateMosaic(mem_context, mem_canvas.width, mem_canvas.height, fadeMosaicSize); context.drawImage(mem_canvas, 32, 32); fadeMosaicSize = Math.ceil(fadeMosaicSize * 1.5); } } function CreateMosaic(context, width,height,mosaicSize) { var x=0; var y=0; for (y = 0; y < height; y = y + mosaicSize) { for (x = 0; x < width; x = x + mosaicSize) { var cR = imageData.data[(y * width + x) * 4]; var cG = imageData.data[(y * width + x) * 4 + 1]; var cB = imageData.data[(y * width + x) * 4 + 2]; context.fillStyle = "rgb("+cR+","+cG+","+cB+")"; context.fillRect(x, y, x + mosaicSize, y + mosaicSize); } } } </script> </head> <body onload="loadImage();" style="background-color:#D0D0D0;"> <canvas id="SimpleCanvas" width="640" height="360" style="background-color:#FFFFFF;"></canvas> <div>Canvas Demo</div> <div id="output"></div> </body> </html>
說明:
在處理完前一個程式碼之後,它執行2秒的間隔,然後在該間隔中執行onFadeOut,是淡出之前執行的程式碼。
運行結果
執行上面的HTML文件,將顯示如下馬賽克效果
然後逐漸變得清晰,如下在
之後,影像顯示約兩秒鐘,然後影像開始出現馬賽克效果,如下圖所示
然後,馬賽克逐漸變粗,最後隱藏,如下所示,頁面什麼都沒有了
以上是HTML5 canvas如何實現馬賽克的淡入淡出效果(程式碼)的詳細內容。更多資訊請關注PHP中文網其他相關文章!