繁体   English   中英

HTML 5画布填充颜色

[英]HTML 5 canvas fill color

我的以下脚本有问题

var ctx = demo.getContext('2d'),
    w = demo.width,
    h = demo.height,
    img = new Image,
    url = 'http://i.imgur.com/1xweuKj.png',
    grd = ctx.createLinearGradient(0, 0, 0, h);

grd.addColorStop(0, 'rgb(0, 130, 230)');
grd.addColorStop(1, 'rgb(0, 20, 100)');

img.onload = loop;
img.crossOrigin = 'anonymous';
img.src = url;

function loop(x) {
    /// since we will change composite mode and clip:
    ctx.save();

    /// clear background with black
    ctx.fillStyle = '#000';
    ctx.fillRect(0, 0, w, h);

    /// remove waveform, change composite mode
    ctx.globalCompositeOperation = 'destination-atop';
    ctx.drawImage(img, 0, 0, w, h);

    /// fill new alpha, same mode, different region.
    /// as this will remove anything else we need to clip it
    ctx.fillStyle = grd;
    ctx.rect(0, 0, x, h);
    ctx.clip();    /// et clipping
    ctx.fill();    /// use the same rect to fill

    /// remove clip and use default composite mode
    ctx.restore();

    /// loop until end
}

当我执行loop(40)它可以工作,然后如果我执行loop(50)则它也可以工作,但是当我想提供比当前值小的值(例如loop(20) )时,它不起作用。

有人可以让我知道这里出什么问题吗?

它适用于所有增加的值,但不适用于减少的值。

检查jsfiddle

我想出了解决方法,现在可以正常工作了。

我是通过ctx.beginPath();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM