如何为Canvas元素添加Screen混合模式?解决Animate导出问题
你遇到的问题是因为Adobe Animate的GUI没有直接支持设置mix-blend-mode: screen,但我们可以通过手动修改生成的CreateJS代码来实现和Google Web Designer版本一致的效果。以下是具体步骤:
步骤1:为渐变元件添加Screen合成模式
CreateJS的DisplayObject有一个compositeOperation属性,对应Canvas 2D的globalCompositeOperation,而CSS的mix-blend-mode: screen正好对应Canvas的"screen"合成值。
找到代码中渐变元件的初始化部分(在lib.index构造函数的// GRADIENT注释下方),在设置透明度的代码后添加一行:
this.instance.compositeOperation = "screen";
修改后的代码片段如下:
// GRADIENT this.instance = new lib.GRADIENT(); this.instance.parent = this; this.instance.setTransform(535,669.5,1,1,0,0,0,535,669.5); this.instance.alpha = 0.5; // 新增:设置Screen混合模式 this.instance.compositeOperation = "screen"; this.timeline.addTween(cjs.Tween.get(this.instance).to({x:-235,y:-69.5},89,cjs.Ease.quartInOut).wait(4));
步骤2:确保渐变层在照片层上方
混合模式需要渐变层绘制在照片层之上才能生效,但当前代码中照片层(instance_1)是最后创建的,会默认显示在渐变层上方。我们需要调整层级:
在照片元件的初始化代码(// PHOTO注释下方)末尾,添加一行代码将照片层移到最底层:
this.setChildIndex(this.instance_1, 0);
修改后的照片层代码片段:
// PHOTO this.instance_1 = new lib.PHOTO(); this.instance_1.parent = this; this.instance_1.setTransform(150,300,1,1,0,0,0,150,300); this.timeline.addTween(cjs.Tween.get(this.instance_1).wait(93)); // 新增:将照片层置于最底层,确保渐变层在上方 this.setChildIndex(this.instance_1, 0);
验证效果
修改完成后,保存HTML文件并在浏览器中打开,渐变层会以50%透明度加上Screen混合模式的效果叠加在照片上,和Google Web Designer制作的A1版本效果一致。
原理说明:CreateJS的compositeOperation属性控制元素绘制到画布时的合成方式,"screen"模式会将渐变层的颜色与下方照片层的颜色按照Screen混合公式计算,实现你需要的视觉效果,和CSS的mix-blend-mode: screen行为一致。
内容的提问来源于stack exchange,提问作者frogseatflies

