Javascript EaselJS或TweenMax中带Alpha滤镜的遮罩
我试图在我的图片上方做一个光泽效果,我只是希望这个效果应该显示在我图片的内容中,而不是所有的容器中。我已经试了很多次了,但我无法使用Alphamask频道。如果有人能帮助我并解释我做错了什么,我将不胜感激 这是我的例子:Javascript EaselJS或TweenMax中带Alpha滤镜的遮罩,javascript,filter,mask,createjs,gsap,Javascript,Filter,Mask,Createjs,Gsap,我试图在我的图片上方做一个光泽效果,我只是希望这个效果应该显示在我图片的内容中,而不是所有的容器中。我已经试了很多次了,但我无法使用Alphamask频道。如果有人能帮助我并解释我做错了什么,我将不胜感激 这是我的例子: var stage = new createjs.Stage("canvas"); createjs.Ticker.addEventListener("tick", tick); var imgSheen = new createjs.Shape(); var bmp = n
var stage = new createjs.Stage("canvas");
createjs.Ticker.addEventListener("tick", tick);
var imgSheen = new createjs.Shape();
var bmp = new createjs.Bitmap("http://upload.wikimedia.org/wikipedia/pt/0/02/Homer_Simpson_2006.png");
imgSheen.graphics.beginLinearGradientFill(['rgba(255,255,255,0)', '#000', 'rgba(255,255,255,0)'], [0, 0.5, 1], 0, 0, 50, 0).drawRect(0, 0, 100, 600);
imgSheen.rotation = 18;
imgSheen.filters = [
new createjs.AlphaMapFilter(bmp)
];
stage.addChild(bmp, imgSheen);
function tick(event) {
createjs.Tween.get(imgSheen).to({x:300}, 4000);
stage.update();
}
链接:我想我知道你想做什么,要做到这一点有几个步骤。在画布中,Alpha遮罩比动画/Flash更复杂,因此设置更困难。为效果设置动画也很困难 正确应用过滤器
cacheCanvas
传递给AlphaMaskFilter,将其应用于bmp,然后缓存bmp以应用过滤器imgSheen
。它是应用于位图的过滤器的一部分,不应可见imgSheen.cache(0,0,100,400); // Cache the sheen so it can be used in the filter
bmp.filters = [
new createjs.AlphaMaskFilter(imgSheen.cacheCanvas) // Use the right filter
];
bmp.cache(0,0,bmp.image.width,bmp.image.height); // Apply the filter
stage.addChild(bmp); // Don't add imgSheen to the stage
c = new createjs.Container();
c.addChild(imgSheen);
createjs.Tween.get(imgSheen, {loop:true}).to({x:200}, 2000, createjs.Ease.quadInOut)
.on("change", function() {
c.cache(0,0,250,400);
bmp.cache(0,0,bmp.image.width,bmp.image.height);
});
设置效果动画
要添加动画,必须更改过滤器的位置。这很困难,因为:
imgSheen
可以自由移动,我们可以随时更新容器缓存imgSheen.cache(0,0,100,400); // Cache the sheen so it can be used in the filter
bmp.filters = [
new createjs.AlphaMaskFilter(imgSheen.cacheCanvas) // Use the right filter
];
bmp.cache(0,0,bmp.image.width,bmp.image.height); // Apply the filter
stage.addChild(bmp); // Don't add imgSheen to the stage
c = new createjs.Container();
c.addChild(imgSheen);
createjs.Tween.get(imgSheen, {loop:true}).to({x:200}, 2000, createjs.Ease.quadInOut)
.on("change", function() {
c.cache(0,0,250,400);
bmp.cache(0,0,bmp.image.width,bmp.image.height);
});
只是提醒一下,每帧更新一次缓存并不理想。最好制作一个覆盖图像的反向渐变,而不是遮罩它(并融入背景)。您可以遮罩渐变和图像,以将其约束到图像大小
希望有帮助!这是一个多么漂亮的解决方案