HTML Canvas:更新 globalAlpha 属性

发布于 2024-11-16 05:55:41 字数 1342 浏览 3 评论 0原文

我正在尝试在绘制后更新画布元素的 globalAlpha 。画布包含一个图像和两个文本。我在底部显示了代码。

之后我想将 globalAlpha 属性从 0.6 更改为 1。考虑到性能,我怎样才能最优雅地做到这一点?

        var ctx = canvasElement.getContext("2d");
        ctx.globalAlpha = 0.6;

        //background gradient
        var gradient = ctx.createLinearGradient(0, 0, 0, options["height"]);
        gradient.addColorStop(0, options["colorStop0"]);
        gradient.addColorStop(1, options["colorStop1"]);
        ctx.fillStyle = gradient;
        ctx.fillRect(0, 0, options["width"], options["height"]);

        //program
        ctx.font = options.programText["font"];
        ctx.fillStyle = options.programText["color"];
        ctx.fillText(prog.title, options.programText["x"], options.programText["y"]);

        //channel number
        ctx.font = options.channelNumber["font"];
        ctx.fillStyle = options.channelNumber["color"];
        ctx.fillText(item_index + 1, options.channelNumber["x"], options.channelNumber["y"]);

        //channel logo
        var channelLogo = new Image();
        channelLogo.onload = function () {
            var clX = options.channelLogo["x"];
            var clY = options.channelLogo["y"];
            ctx.drawImage(channelLogo, clX, clY, channelLogo.width, channelLogo.height);
        };
        channelLogo.src = this.getChannelLogo(channel);

I'm trying to update the globalAlpha of a canvas element after its painted. The canvas contains an image, and two texts. I show the code at the bottom.

I want to change the globalAlpha property from 0.6 to 1 afterwards. How can i do this most elegantly with performance in mind?

        var ctx = canvasElement.getContext("2d");
        ctx.globalAlpha = 0.6;

        //background gradient
        var gradient = ctx.createLinearGradient(0, 0, 0, options["height"]);
        gradient.addColorStop(0, options["colorStop0"]);
        gradient.addColorStop(1, options["colorStop1"]);
        ctx.fillStyle = gradient;
        ctx.fillRect(0, 0, options["width"], options["height"]);

        //program
        ctx.font = options.programText["font"];
        ctx.fillStyle = options.programText["color"];
        ctx.fillText(prog.title, options.programText["x"], options.programText["y"]);

        //channel number
        ctx.font = options.channelNumber["font"];
        ctx.fillStyle = options.channelNumber["color"];
        ctx.fillText(item_index + 1, options.channelNumber["x"], options.channelNumber["y"]);

        //channel logo
        var channelLogo = new Image();
        channelLogo.onload = function () {
            var clX = options.channelLogo["x"];
            var clY = options.channelLogo["y"];
            ctx.drawImage(channelLogo, clX, clY, channelLogo.width, channelLogo.height);
        };
        channelLogo.src = this.getChannelLogo(channel);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

Bonjour°[大白 2024-11-23 05:55:41

绘制形状后,您无法更新其 globalAlpha。

更重要的是,绘制后您无法“更新”任何东西。 Canvas 中没有更新。您必须重新绘制属性已更改的事物。

因此,在您的情况下,您只需清除画布并重新绘制所有内容,并将 globalAlpha 设置回 1。

You cannot update the globalAlpha of a shape after it is painted.

More to the point, you cannot "update" anything after it is painted. There is no updating in Canvas. You must instead redraw the thing(s) with the attributes changed.

So in your case you will simply want to clear your canvas and redraw everything with globalAlpha set back to 1.

不必你懂 2024-11-23 05:55:41

您可以在绘画后通过进行像素操作来更新画布。

使用 getImageData &画布上下文的 putImageData 方法。

但是画布上下文中没有像“changeAlpha(x,y,width,height,alphaValue)”这样的方法。

我在我的应用程序中也面临同样的问题。

You can update canvas after painting by just doing pixel manipulation.

Using the getImageData & putImageData method of context of canvas.

But there is not any method like "changeAlpha(x,y,width,height,alphaValue)" of context of canvas.

Same kind of problem i am also facing in my app.

桃扇骨 2024-11-23 05:55:41

如果您的 Alpha 达到 100%,您可能只需重新绘制它,而无需重新绘制整个场景(覆盖之前的场景)。唯一的风险是如果您正在制作曲线,则会出现一些抗锯齿伪影。

然而,如果你的阿尔法下降,你必须按照西蒙所说的那样 - 重新绘制整个场景。

If your alpha is going up to 100%, you can probably just repaint it without redrawing the whole scene (overwriting what was there previously). The only risk is some antialiasing artifacts if you're doing curves.

If you alpha is going down, however, you have to do as Simon says - repaint the whole scene.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文