我使用context-blender以固定顏色對html背景圖像的前192個像素應用多重效果,以實現頁面標題上的透明效果。html5圖像裁剪
在html上我有2個畫布。一個用於圖像的部分以應用乘法效果,另一個用於顏色。
在JavaScript中,我與得到的背景圖像顏色的帆布的顏色和兩個畫布寬度設置爲window.innerWidth後:
imageObj.src = $('html').css('background-image').replace(/^url|[\(\)]/g, '');
現在問題來了。我想繪製圖像的裁剪圖像到圖像畫布,以便我可以應用乘法效果。我試圖做到以下幾點:
imageObj.onload = function(){
// getting the background-image height
var imageHeight = window.innerWidth * imageObj.height/imageObj.width;
// get the corresponding pixels of the source image that correspond to the first 192 pixels of the background-image
var croppedHeight = 192 * imageObj.height/imageHeight;
// draw the image to the canvas
imageCanvas.drawImage(imageObj, 0, 0, imageObj.width, croppedHeight, 0, 0, window.innerWidth, 192);
// apply the multiply effect
colorCanvas.blendOnto(imageCanvas, 'multiply');
}
但我做錯了,得到裁剪的高度。
例:對於一個1536x1152圖像和1293x679瀏覽器的容器中,我得到了源中值裁剪高度是230,但以獲得正確的作物我需要用的東西約296
編輯:
我使用的背景大小:蓋在CSS創建背景圖像
EDIT2:
我創建了一個fiddle來說明問題。如果取消註釋//cHeight *= magicConstant;
這一行,裁剪的圖像看起來好多了,但事情沒有意義。我刪除了對提琴手的乘法效果,但這不是重現問題所必需的。我還注意到,如果從URL中刪除第二個畫布,行爲會發生變化。
順便說一句,這種行爲發生在谷歌鉻,但我認爲同樣的事情發生在Safari和Firefox。
你能在jsfiddle.net上創建一個例子嗎?可以幫助我們爲你提供一個答案。此外,行爲(獲得230像素vs 296像素)在多個瀏覽器中是否一致? – 2012-04-16 11:24:56
@MarcGagne我在問題 – dcarneiro 2012-04-16 16:25:56