把照片打成一格一格的馬賽克,聽起來需要影像處理,其實用 Canvas 的 getImageData 讀像素再重畫就能做到。開放的 close-pixelate.js 腳本把這件事包裝成了一個方法呼叫。
https://hovertree.com/texiao/html5/1.htm
這個腳本的構想受到美國肖像畫家 Chuck Close 的啟發——他的畫作在近看是一塊塊色塊,遠看才浮現人像。程式做的事正是如此:把影像降採樣成粗顆粒的色塊。本質上,它是 Canvas imageData 功能的一個簡潔示範。腳本已放在 GitHub 上,可下載原始碼與範例。
使用方法接受一組物件陣列,每個物件代表一組設定:
resolution:格與格之間相隔幾個像素,必填。數值越大顆粒越粗。shape:像素的形狀,可選 square(方形)、circle(圓形)、diamond(菱形),預設 square。size:每個像素實際繪製的大小,可選,預設等於 resolution。offset:位移量,可選,預設 0。單一數值如 15 會做對角線偏移;也可以寫成陣列 [15, 5] 或物件 { x: 15, y: 5 } 分別控制 X 與 Y。alpha:透明度,小數,可選,預設 1。先引入腳本:
<script type="text/javascript" src="https://hovertree.com/texiao/html5/1/js/close-pixelate.js"></script>
然後在圖片元素上呼叫方法:
<img id="pixelExample2" src="pixel-example.jpg" width="300" height="426" />
document.getElementById('pixelExample2').closePixelate( [
{ resolution: 24 }
] );
依 HTML5 規範,瀏覽器基於安全考量,禁止對外部網域托管的圖片呼叫 getImageData(),否則會丟出安全性錯誤。好消息是透過 John Schulz 的貢獻與 Max Novakovic 的 getImageData API,這個限制在腳本內已被繞過,因此跨域圖片也能像素化。實務上仍建議把圖片放在自己的網域,並留意伺服器是否有設定合適的 CORS 標頭。
<!doctype html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>基於HTML5 Canvas實現的圖片馬賽克模糊特效-何問起</title>
<link rel="stylesheet" type="text/css" href="1/css/normalize.css" />
<link rel="stylesheet" type="text/css" href="1/css/default.css">
<style type="text/css">
.thumb
{
margin-left: 5em;
}
.thumb img
{
max-width: 400px;
}
</style>
</head>
<body>
<div style="width:760px;margin:0px auto">
<h2>基於HTML5 Canvas的圖片馬賽克模糊特效 何問起</h2>
<a href="https://hovertree.com">首頁</a> <a href="https://hovertree.com/hvtart/bjae/160ced36cb3a6a86.htm">原文</a> <a href="https://hovertree.com/texiao/html5/1/tupianmohu.rar" target="_self">下載</a>
<a href="https://hovertree.com/texiao/">特效庫</a><br />
</div>
<div class="container">
<div class="content bgcolor-8">
<p class="center">
改變模糊度
<input type="range" min="4" max="100" value="6" id="range" />
<span id="output">6</span></p>
<div class="thumb">
<img src="1/img/1.jpg" id="dolly1" />
<img src="1/img/2.jpg" id="dolly2" />
<img src="1/img/3.jpg" id="dolly3" />
</div>
</div>
</div>
<script src="https://hovertree.com/texiao/html5/1/js/close-pixelate.js"></script>
<script>
window.onload = function () {
var dolly1 = document.getElementById('dolly1')
var dolly2 = document.getElementById('dolly2')
var dolly3 = document.getElementById('dolly3')
var pixelOpts = [{ resolution: 8}]
var pixelDolly1 = dolly1.closePixelate(pixelOpts)
var pixelDolly2 = dolly2.closePixelate(pixelOpts)
var pixelDolly3 = dolly3.closePixelate(pixelOpts)
var range = document.getElementById('range')
var output = document.getElementById('output')
range.addEventListener('change', function (event) {
var res = parseInt(event.target.value, 10)
res = Math.floor(res / 2) * 2
res = Math.max(4, Math.min(100, res))
output.textContent = res
pixelOpts = [{ resolution: res}]
pixelDolly1.render(pixelOpts)
pixelDolly2.render(pixelOpts)
pixelDolly3.render(pixelOpts)
}, false)
}
</script>
</body>
</html>
closePixelate() 會回傳一個物件,上面帶有 render() 方法,可以重複套用不同參數而不必重新初始化。因此拖動 input type="range" 時,只要呼叫 render() 就能即時改變顆粒大小。
中間那三行是防呆處理:Math.floor(res / 2) * 2 把奇數強制轉成偶數,讓格子能對齊;Math.max(4, Math.min(100, res)) 把值夾在 4 到 100 之間,避免使用者塞進不合理數字導致跑不動或看不出差別。
把 shape 改成 circle 會得到半調網點的印刷感,搭配 alpha 低於 1 還能做出疊影;offset 加上位移後,色塊會錯開排列,形成近似故障藝術(glitch art)的觀感。這個效果也很常用在圖片的「點擊揭露」互動:先顯示重度馬賽克,點一下再漸進降低 resolution。