对不起,如果这属于数学论坛。我在 HTML5s 画布元素中有一个图像圆角,我想让圆角透明。我有以下代码:
var cornersImgData = tempContext.getImageData(0, 0, img.width, img.height);
var topLeft = getPixel(cornersImgData, 0, 0);
var topRight = getPixel(cornersImgData, cornersImgData.width - 1, 0);
var bottomLeft = getPixel(cornersImgData, 0, cornersImgData.height - 1);
// Check that the rounded corners have actually been applied (e.g. make sure the user hasn't just clicked the button and then undo)
var bottomRight = getPixel(cornersImgData, cornersImgData.width - 1, cornersImgData.height - 1);
if (('rgb(' + topLeft[0] + ', ' + topLeft[1] + ', ' + topLeft[2] + ')' == _roundedCornersColour) ||
('rgb(' + topRight[0] + ', ' + topRight[1] + ', ' + topRight[2] + ')' == _roundedCornersColour) ||
('rgb(' + bottomLeft[0] + ', ' + bottomLeft[1] + ', ' + bottomLeft[2] + ')' == _roundedCornersColour) ||
('rgb(' + bottomRight[0] + ', ' + bottomRight[1] + ', ' + bottomRight[2] + ')' == _roundedCornersColour))
{
for (var x = 0; x < cornersImgData.width; x++)
{
for (var y = 0; y < cornersImgData.height; y++)
{
var colour = getPixel(cornersImgData, x, y);
if ('rgb(' + colour[0] + ', ' + colour[1] + ', ' + colour[2] + ')' == _roundedCornersColour)
{
setPixel(cornersImgData, x, y, colour[0], colour[1], colour[2], 0);
}
}
}
}
这可行,但是因为我要替换 _roundedCornersColour 的每个实例,所以有时我最终会替换图像本身中的几个像素。我的高中数学有点生疏,我想不出最好的方法来确定 x 和 y 是否落在角落应该在的位置之外。有人可以帮忙吗?
乔