当前位置:网站首页>canvas Gaussian blur effect
canvas Gaussian blur effect
2022-08-05 00:21:00 【The most brutal baby seals】
// imgData:canvas 的 getImageData 方法返回值
// radius:模糊的半径
function gaussBlur(imgData, radius) {
radius *= 3; //不知为什么,My blur radius is css中 filter:bulr The effect is the same when the value is tripled.
//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
边栏推荐
猜你喜欢

《MySQL入门很轻松》第2章:MySQL管理工具介绍

测试经理要不要做测试执行?

KT148A voice chip ic working principle and internal architecture description of the chip

2022杭电多校第三场 K题 Taxi

【LeetCode】Summary of Two Pointer Problems

Mysql_14 存储引擎

"WEB Security Penetration Testing" (28) Burp Collaborator-dnslog out-band technology

Redis visual management software Redis Desktop Manager2022

gorm joint table query - actual combat

软件开发工具的技术要素
随机推荐
【数据挖掘概论】数据挖掘的简单描述
【idea】idea配置sql格式化
leetcode: 269. The Martian Dictionary
tiup telemetry
Software testing interview questions: test life cycle, the test process is divided into several stages, and the meaning of each stage and the method used?
TinyMCE disable escape
Mysql based
More than 2022 cattle school training topic Link with the second L Level Editor I
Redis visual management software Redis Desktop Manager2022
How to automatically push my new articles to my fans (very simple, can't learn to hit me)
典型相关分析CCA计算过程
《WEB安全渗透测试》(28)Burp Collaborator-dnslog外带技术
【unity编译器扩展之模型动画拷贝】
lua 如何 实现一个unity协程的工具
导入JankStats检测卡帧库遇到问题记录
软件测试面试题:您以往所从事的软件测试工作中,是否使用了一些工具来进行软件缺陷(Bug)的管理?如果有,请结合该工具描述软件缺陷(Bug)跟踪管理的流程?
jenkins send mail system configuration
leetcode: 267. Palindromic permutations II
怎样进行在不改变主线程执行的时候,进行日志的记录
简单的顺序结构程序(C语言)