標籤:
HTML5 canvas產生圖片馬賽克特效外掛程式
簡要教程這是一款使用html5 canvas來將圖片製作成馬賽克效果的js外掛程式。該外掛程式的靈感來自於美國肖像畫家Chuck Close。已經有人使用這個js外掛程式來製作了一個完整的圖片馬賽克應用程式框架:The Pixelator。使用方法這個馬賽克效果js外掛程式需要使用同源的圖片,根據 HTML5 規範,瀏覽器在解析getImageData()方法時會阻止擷取跨瀏覽器的圖片。該canvas外掛程式的html結構非常簡單,就是使用一個<img>標籤:<img id="portrait-image" src="img/portrait.jpg" /> 然後可以使用下面的方法調用外掛程式:function init() { document.getElementById(‘portrait-image‘).closePixelate([ { resolution : 24 }, { shape : ‘circle‘, resolution : 24, size: 16, offset: 12, alpha: 0.5 } ]);};window.addEventListener( ‘load‘, init, false); 你可以使用一個可用參數數組中來控制輸出的馬賽克效果。參數數組中每一個集合都是一個對象,代表了某種馬賽克輸出效果。在上面的例子中,第一組參數{ resolution : 24 }控制指令碼每24像素就畫一個大的正方形像素,然後為每一個正方形像素的中心填充一個精確的顏色值。第二組參數使用了所有的可用參數:{ shape : ‘circle‘, resolution : 24, size: 16, offset: 12, alpha: 0.5 }。和第一組參數一樣,它的解析度是24px,像素的形狀是圓形,每一個圓形的大小是16像素,每一個圓形的右邊都有12像素的位移量,最後這個圓形的透明度被設定為50%。可用參數resolution :被渲染像素之間的距離。必須設定。shape :像素的形狀。可選值:square、circle 和 diamond,預設值:square。可選。size :渲染像素的大小。可選。預設值為resolution。offset :像素之間的位移值。可選,預設值為0.可以只設定一個值,這時為對角位移。也可以設定為一個數組或對象:[ 15, 5 ]或{ x: 15, y: 5 }。alpha :渲染像素的透明度,可選,預設值為1。建構函式和方法var img = document.getElementById(‘portrait-img‘);// create a new Close Pixelation instance with ClosePixelation// requires two arguments: the original image element// and an array of optionsvar myPixelation = new ClosePixelation( img, [ { resolution : 24 }]);// re-render the canvas with different optionsmyPixelation.render([ { resolution: 32 }, { resolution: 16, shape: ‘circle‘, offset: 8 }]);// render a single option-set on topmyPixelation.renderClosePixels({ resolution: 48, alpha: 0.5}); 應用舉例html5 canvas圖片馬賽克效果-1{ shape : ‘diamond‘, resolution : 48, size: 50 },{ shape : ‘diamond‘, resolution : 48, offset : 24 },{ shape : ‘circle‘, resolution : 8, size: 6 } html5 canvas圖片馬賽克效果-2{ resolution: 32 },{ shape : ‘circle‘, resolution : 32, offset: 15 },{ shape : ‘circle‘, resolution : 32, size: 26, offset: 13 },{ shape : ‘circle‘, resolution : 32, size: 18, offset: 10 },{ shape : ‘circle‘, resolution : 32, size: 12, offset: 8 } html5 canvas圖片馬賽克效果-3{ resolution: 48 },{ shape: ‘diamond‘, resolution: 48, offset: 12, alpha: 0.5 },{ shape: ‘diamond‘, resolution: 48, offset: 36, alpha: 0.5 },{ shape: ‘circle‘, resolution: 16, size: 8, offset: 4, alpha: 0.5 } html5 canvas圖片馬賽克效果-4{ shape: ‘circle‘, resolution: 32, size: 6, offset: 8 },{ shape: ‘circle‘, resolution: 32, size: 9, offset: 16 },{ shape: ‘circle‘, resolution: 32, size: 12, offset: 24 },{ shape: ‘circle‘, resolution: 32, size: 9, offset: 0 } html5 canvas圖片馬賽克效果-5{ shape: ‘diamond‘, resolution: 24, size: 25 },{ shape: ‘diamond‘, resolution: 24, offset: 12 },{ resolution: 24, alpha: 0.5 } html5 canvas圖片馬賽克效果-6{ shape: ‘square‘, resolution: 32 },{ shape: ‘circle‘, resolution: 32, offset: 16 },{ shape: ‘circle‘, resolution: 32, offset: 0, alpha: 0.5 },{ shape: ‘circle‘, resolution: 16, size: 9, offset: 0, alpha: 0.5 } html5 canvas圖片馬賽克效果-7{ shape : ‘circle‘, resolution : 24 },{ shape : ‘circle‘, resolution : 24, size: 9, offset: 12 } html5 canvas圖片馬賽克效果-8{ shape : ‘square‘, resolution : 48, offset: 24 },{ shape : ‘circle‘, resolution : 48, offset : 0 },{ shape : ‘diamond‘, resolution : 16, size: 15, offset : 0, alpha : 0.6 },{ shape : ‘diamond‘, resolution : 16, size: 15, offset : 8, alpha : 0.6 } html5 canvas圖片馬賽克效果-9{ shape : ‘square‘, resolution : 48 },{ shape : ‘diamond‘, resolution : 12, size: 8 },{ shape : ‘diamond‘, resolution : 12, size: 8, offset : 6 } 本文地址:http://www.htmleaf.com/html5/html5-canvas/201502021314.html
HTML5 canvas產生圖片馬賽克特效外掛程式