對不起,如果這屬於數學論壇。我在HTML5s canvas元素中有一個圖像圓角,我想讓圓角透明。我有以下代碼:檢查點是否超出曲線
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是否落在拐角處的最佳方式。任何人都可以幫忙嗎?
喬
P.S.我知道角落的半徑 – JoeS 2012-04-08 19:14:45