Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

怎么给图片添加圆角 #1231

Closed
17602260250 opened this issue Apr 27, 2023 · 2 comments
Closed

怎么给图片添加圆角 #1231

17602260250 opened this issue Apr 27, 2023 · 2 comments

Comments

@17602260250
Copy link

怎么给图片添加圆角

现在用的这个方法可以把图片切成圆形
jimp.circle();

但是这个只能把图片切成圆形

有方法可以给图片上下左右添加圆角吗

@hipstersmoothie
Copy link
Collaborator

There currently isn't an option to add border radius to a square. You could probably implement this as a plugin.

@tsoil
Copy link

tsoil commented Mar 12, 2024

  // 设置圆角的半径
  const radius = 100;
  // 计算圆角的半径

  const radiusSquared = radius ** 2;

  // 计算角的距离
  const c1 = radius - 0.5;
  const c2 = image.bitmap.width - radius - 1 + 0.5;
  const c3 = image.bitmap.height - radius - 1 + 0.5;

  // 循环处理每个像素
  image.scan(0, 0, image.bitmap.width, image.bitmap.height, (x, y, idx) => {
    // 计算当前像素到四个角的距离的平方
    const distanceSquared = (x < radius ? c1 - x : x >= c2 ? x - c2 : 0) ** 2 + (y < radius ? c1 - y : y >= c3 ? y - c3 : 0) ** 2;
    // 判断是否在圆角范围内,并根据对称性进行处理
    if (distanceSquared > radiusSquared) {
      image.bitmap.data[idx + 3] = 0; // 设置透明度为0,即完全透明
    }
  });
  初始图片要大不然有锯齿严重,然后再resize

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants