简体   繁体   中英

HTML5 canvas faster fillText() vs drawImage()

I have a digital clock that is running and being updated every 10 milliseconds. In each draw call I am using this:

    var gradient = clockContext.createLinearGradient(0, 0, 0, this.digitWidth);
    gradient.addColorStop(0.15, "rgb(255, 252, 52)");
    gradient.addColorStop(0.15, "rgb(245, 127, 26)");
    gradient.addColorStop(1, "rgb(248, 159, 52)");
    clockContext.fillStyle = gradient;
    clockContext.lineWidth = 1;
    clockContext.lineStyle = "#000000";
    clockContext.fillText(time, (this.digitWidth * i) + e + s, 46);
    clockContext.strokeText(time, (this.digitWidth * i) + e + s, 46);

Now is this quicker or slow than creating a PNG of the numbers 0 - 9, caching each one and then using drawImage() on each draw call?

drawImage is always, always faster than fillText . It can be 100+ times faster depending on how the text is constructed.

I did a meandering analysis here not long ago:

http://simonsarris.com/blog/322-canvas-drawtext-considered-harmful

Here's a simple jsperf example: http://jsperf.com/image-vs-text

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