当前位置:网站首页>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
边栏推荐
- 测试经理要不要做测试执行?
- 软件开发工具的技术要素
- What is next-generation modeling (with learning materials)
- Couple Holding Hands [Greedy & Abstract]
- 国内网站用香港服务器会被封吗?
- About I double-checked and reviewed the About staff page, returning an industry question
- 电赛必备技能___定时ADC+DMA+串口通信
- 2022牛客多校训练第二场 J题 Link with Arithmetic Progression
- TinyMCE disable escape
- 机器学习(公式推导与代码实现)--sklearn机器学习库
猜你喜欢
matlab中rcosdesign函数升余弦滚降成型滤波器
子连接中的参数传递
[230] Execute command error after connecting to Redis MISCONF Redis is configured to save RDB snapshots
仿网易云音乐小程序-uniapp
jenkins send mail system configuration
QSunSync Qiniu cloud file synchronization tool, batch upload
[230]连接Redis后执行命令错误 MISCONF Redis is configured to save RDB snapshots
怎样进行在不改变主线程执行的时候,进行日志的记录
Essential knowledge for entry-level 3D game modelers
英特尔WiFi 7产品将于2024年亮相 最高速度可达5.8Gbps
随机推荐
jenkins send mail system configuration
元宇宙:未来我们的每一个日常行为是否都能成为赚钱工具?
国内网站用香港服务器会被封吗?
leetcode经典例题——单词拆分
[idea] idea configures sql formatting
软件测试面试题:测试生命周期,测试过程分为几个阶段,以及各阶段的含义及使用的方法?
2022杭电多校第三场 L题 Two Permutations
数据类型及输入输出初探(C语言)
Getting started with 3D modeling for games, what modeling software can I choose?
RK3399平台开发系列讲解(内核调试篇)2.50、嵌入式产品启动速度优化
关于使用read table 语句
D - I Hate Non-integer Number (选数的计数dp
软件开发工具的技术要素
oracle创建用户
网站最终产品页使用单一入口还是多入口?
Software Testing Interview Questions: What aspects should be considered when designing test cases, i.e. what aspects should different test cases test against?
2022 Multi-school Second Session K Question Link with Bracket Sequence I
Mysql_12 多表查询
Software Testing Interview Questions: What's the Key to a Good Test Plan?
软件测试面试题:BIOS, Fat, IDE, Sata, SCSI, Ntfs windows NT?