简体   繁体   中英

HTML5 Canvas context globalAlpha not working on image [iOS]

I'm updating the "this.opacity" value to fade an svg logo. The code bellow is working except on iOS(8).

    var size = (this.canvas.width * 0.2) * 0.4;

    this.context.save();
    this.context.globalAlpha = this.opacity;
    this.context.drawImage(this.svg, this.canvas.width * 0.04, this.canvas.width * 0.027, size, size);
    this.context.restore();

Animating the alpha of SVG images is not supported on iOS. But i found a solution. If you create a fullscreen canvas using the following code to scale it:

var ratio = window.devicePixelRatio || 1,
    width = $(window).width(),
    height = $(window).height();

this.canvas.width = width * ratio;
this.canvas.height = height * ratio;
this.canvas.style.width = width + "px";
this.canvas.style.height = height + "px";

this.update();

You will be able to use double sized images to give retina support, that way you don't have to use SVG elements in your canvas (which is IMHO terrible for your memory as well). And the good thing is that images will accept setting the globalAlpha of your context.

Hope the reader is informed enough :-).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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