我使用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