Photoshop 裡的羽化(Feather)能讓色塊邊緣柔和地漸變透明。在瀏覽器中,可以用 CSS 的 alpha 漸變遮罩搭配定時器做出一個類似效果的動畫。下面這段代碼示範了一個白色柔光圓點在頁面上移動,並以 clip 區域逐步收放來模擬羽化的視覺感受。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>用 JavaScript 模擬 PS 裡的羽化效果</title>
</head>
<body onload="setValues()">
<center>
<SCRIPT language="JavaScript">
<!-- Beginning of JavaScript
var x, y
var marginbottom
var marginleft = 0
var margintop = 0
var marginright
var cliptop
var clipbottom
var clipleft
var clipright
var clippoints
var ballheight = 150
var ballwidth = 150
var imageheight = 525
var imagewidth = 457
var tempo = 25
var stepx = 12
var stepy = 6
var timer
function setValues() {
if (document.all) {
marginbottom = imageheight - ballheight
marginright = imagewidth - ballwidth
document.all.ball.style.posLeft = randommaker(400)
document.all.ball.style.posTop = 0
document.all.textcontent.style.posLeft = 0
document.all.textcontent.style.posTop = 0
document.all.ball.style.filter =
"alpha(opacity=0,finishopacity=100,style=2,startX=0px,startY=0px,finishX=100px,finishY=100px)"
moveball()
}
}
// randomfunction
function randommaker(range) {
rand = Math.floor(range * Math.random())
return rand
}
function moveball() {
checkposition()
if (document.all) {
document.all.ball.style.posLeft += stepx
document.all.ball.style.posTop += stepy
cliptop = document.all.ball.style.posTop
clipbottom = cliptop + ballheight
clipleft = document.all.ball.style.posLeft
clipright = clipleft + ballwidth
clippoints = "rect(" + cliptop + " " + clipright + " " + clipbottom + " " + clipleft + ")"
document.all.textcontent.style.clip = clippoints
timer = setTimeout("moveball()", tempo)
}
}
function checkposition() {
if (document.all) {
if (document.all.ball.style.posLeft >= marginright) {
stepx = stepx * -1
document.all.ball.style.posLeft -= 10
}
if (document.all.ball.style.posLeft <= marginleft) {
stepx = stepx * -1
document.all.ball.style.posLeft += 10
}
if (document.all.ball.style.posTop >= marginbottom) {
stepy = stepy * -1
document.all.ball.style.posTop -= 10
}
if (document.all.ball.style.posTop <= margintop) {
stepy = stepy * -1
document.all.ball.style.posTop += 10
}
}
}
// - End of JavaScript -->
</SCRIPT>
<span id="textcontent" style="LEFT: -5000px; POSITION: absolute; TOP: -2000px">
<IMG border="0" src="https://files.jb51.net/upload/201112/20111220011256875.jpg">
</span>
<span id="ball" style="BACKGROUND-COLOR: white; HEIGHT: 150px; POSITION: absolute; TOP: -50px; WIDTH: 150px"></span>
</center>
</body>
</html>
alpha 漸變滤镜:filter: alpha(opacity=0, finishopacity=100, style=2, ...) 是 IE 專屬的漸變透明效果。style=2 表示從中心向外擴散的圓形漸變,起始透明度 0、結束 100,於是白色方塊看起來像一個邊緣柔和的光暈——這就是「羽化」的視覺來源。
clip 裁剪:用 clip: rect(top right bottom left) 把可視區域限制在圖片範圍內,讓光暈移動到邊界時不會溢出畫面。
邊界反彈:checkposition() 每次移動前檢查是否超出四邊,超出就把 stepx 或 stepy 取負號並回退 10 像素,形成碰撞反彈的效果。
必須誠實說明:這份代碼依賴 document.all、IE 的 filter: alpha() 以及 posLeft/posTop,全部只在舊版 IE 有效,在現代瀏覽器中完全不會動。它更像是歷史文獻而非可用代碼。
同樣的效果,用現代技術只需幾行 CSS:
/* 用径向渐变直接做出羽化圆点,不需要任何滤镜 */
.feather-ball {
width: 150px;
height: 150px;
border-radius: 50%;
background: radial-gradient(circle,
rgba(255,255,255,1) 0%,
rgba(255,255,255,0.6) 40%,
rgba(255,255,255,0) 70%);
pointer-events: none;
}
如果是要對圖片本身做羽化,則用 mask-image:
img {
-webkit-mask-image: radial-gradient(ellipse at center, #000 50%, transparent 75%);
mask-image: radial-gradient(ellipse at center, #000 50%, transparent 75%);
}
移動則改用 transform: translate() 搭配 CSS transition 或 requestAnimationFrame,比 setTimeout 定時器更順滑,也不會阻塞主線程。
核心觀念沒變——羽化本質上是「透明度沿空間漸變」,只是實作工具從私有滤镜換成了標準的漸變與遮罩。