[英]How to make image into an outline with transparent background using JS
您可以使用Konva 自定义滤镜根据需要处理图像。
这是用透明像素替换白色的示例:
// lets define a custom filter:
var MyFilter = function (imageData) {
const tol = 10;
// make all pixels opaque 100%
var nPixels = imageData.data.length;
const { data } = imageData;
for (var i = 0; i < nPixels - 4; i += 4) {
const isWhite =
data[i] > 255 - tol &&
data[i + 1] > 255 - tol &&
data[i + 2] > 255 - tol;
if (isWhite) {
imageData.data[i + 3] = 0;
} else {
// you can replace black with another color
}
}
};
Konva.Image.fromURL("./lion.jpeg", function (image) {
layer.add(image);
image.setAttrs({
x: 80,
y: 30,
draggable: true
});
image.filters([MyFilter]);
image.cache();
layer.draw();
});
演示: https://codesandbox.io/s/konva-remove-white-example-fw6fz?file=/index.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.