当前位置:网站首页>canvas 高斯模糊效果
canvas 高斯模糊效果
2022-08-05 00:15:00 【最凶残的小海豹】
// imgData:canvas 的 getImageData 方法返回值
// radius:模糊的半径
function gaussBlur(imgData, radius) {
radius *= 3; //不知为什么,我的模糊半径是 css中 filter:bulr 值的三倍时效果才一致。
//Copy图片内容
let pixes = new Uint8ClampedArray(imgData.data);
const width = imgData.width;
const height = imgData.height;
let gaussMatrix = [],
gaussSum,
x, y,
r, g, b, a,
i, j, k,
w;
radius = Math.floor(radius);
const sigma = radius / 3;
a = 1 / (Math.sqrt(2 * Math.PI) * sigma);
b = -1 / (2 * sigma * sigma);
//生成高斯矩阵
for (i = -radius; i <= radius; i++) {
gaussMatrix.push(a * Math.exp(b * i * i));
}
//x 方向一维高斯运算
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
r = g = b = a = gaussSum = 0;
for (j = -radius; j <= radius; j++) {
k = x + j;
if (k >= 0 && k < width) {
i = (y * width + k) * 4;
w = gaussMatrix[j + radius];
r += pixes[i] * w;
g += pixes[i + 1] * w;
b += pixes[i + 2] * w;
a += pixes[i + 3] * w;
gaussSum += w;
}
}
i = (y * width + x) * 4;
//计算加权均值
imgData.data.set([r, g, b, a].map(v => v / gaussSum), i);
}
}
pixes.set(imgData.data);
//y 方向一维高斯运算
for (x = 0; x < width; x++) {
for (y = 0; y < height; y++) {
r = g = b = a = gaussSum = 0;
for (j = -radius; j <= radius; j++) {
k = y + j;
if (k >= 0 && k < height) {
i = (k * width + x) * 4;
w = gaussMatrix[j + radius];
r += pixes[i] * w;
g += pixes[i + 1] * w;
b += pixes[i + 2] * w;
a += pixes[i + 3] * w;
gaussSum += w;
}
}
i = (y * width + x) * 4;
imgData.data.set([r, g, b, a].map(v => v / gaussSum), i);
}
}
return imgData;
};
详细内容请看下文:
本文转载:https://blog.csdn.net/xuanhun521/article/details/109362541
边栏推荐
猜你喜欢
随机推荐
jenkins send mail system configuration
怎么将自己新文章自动推送给自己的粉丝(巨简单,学不会来打我)
一、爬虫基本概念
测试经理要不要做测试执行?
KT6368A蓝牙的认证问题_FCC和BQB_CE_KC认证或者其它说明
2022 Niu Ke Summer Multi-School Training Camp 5 (BCDFGHK)
Couple Holding Hands [Greedy & Abstract]
【CVA估值训练营】财务建模指南——第一讲
The master teaches you the 3D real-time character production process, the game modeling process sharing
E - Distance Sequence (前缀和优化dp
性能测试如何准备测试数据
Essential knowledge for entry-level 3D game modelers
【unity编译器扩展之模型动画拷贝】
Develop a SpaceX website based on the Appian low-code platform
The applicable scenarios and common product types of the KT148A electronic voice chip ic solution
游戏3D建模入门,有哪些建模软件可以选择?
00、数组及字符串常用的 API(详细剖析)
gorm的Raw与scan
统计单词(DAY 101)华中科技大学考研机试题
【Valentine's Day special effects】--Canvas realizes full screen love









