【问题标题】:How to apply gradient color for an image using createjs?如何使用 createjs 为图像应用渐变颜色?
【发布时间】:2019-05-28 12:51:14
【问题描述】:

如何使用 createjs 应用渐变填充?下面是创建加载图像的代码,我已经使用滤色器更改了颜色,但我想应用渐变颜色

 let rightContainer = new createjs.Container();
                let rightMain = new window.createjs.Bitmap(rightImage);
                // leftMain.scaleX = 800 / leftMain.image.width;
                rightMain.scaleY = 800 / rightMain.image.height;
                rightContainer.addChild(rightMain);
                rightMain.x = 300;
                rightMain.y = 0;
                this.layerImage = rightMain.clone();
                this.layerImage.alpha = 0.15;
                rightContainer.addChild(this.layerImage);
                rightMain.filters = [new window.createjs.ColorFilter(0, 0, 0, 1, 117, 111, 115, 0)];
                rightContainer.main = rightMain;
                rightMain.cache(0, 0, rightMain.image.width, rightMain.image.height);
                rightContainer.visible = false;
                this.stage.addChild(rightContainer);

【问题讨论】:

    标签: javascript html5-canvas createjs


    【解决方案1】:

    你到底想做什么?在经过颜色过滤的图像上应用渐变?

    您可以使用的方法是:

    1. 画一个带有渐变的盒子
    2. 缓存它,以便它可以用作 AlphaMaskFilter
    3. 将其作为过滤器应用于位图
    4. 缓存位图以应用过滤器。

    我做了一个演示来展示它是如何工作的: https://jsfiddle.net/lannymcnie/uog3hkpd/2/

    // Draw a gradient in a shape:
    s.graphics.lf(["#000", "rgba(0,0,0,0)"], [0, 0], 0,0,960,0);
    
    // Cache the shape
    s.cache(0,0,960,400);
    
    // Add the alphamaskfilter + a color adjustment for fun
    var col = new createjs.ColorMatrix().adjustHue(180);
    bmp.filters = [
        new createjs.AlphaMaskFilter(s.cacheCanvas), 
        new createjs.ColorMatrixFilter(col)
    ];
    
    // Cache it to apply filters
    bmp.cache(0,0,960,400);
    

    演示做了一些其他的事情,比如

    • 在下面添加第二个未过滤的 bmp
    • 对渐变比例进行动画处理(需要重新缓存)

    希望对您的问题有所帮助。如果您有任何需要帮助的具体代码或示例,请随时澄清。

    干杯,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-30
      • 1970-01-01
      • 1970-01-01
      • 2018-01-13
      相关资源
      最近更新 更多