Detecting canvas text API support (Opera Mini)
Asked Answered
L

4

4

Although Opera Mini does not display canvas text, a typical test indicates that it has an implementation of the text API functions. Is there an alternative technique to check for support?

Current method:

var context = document.createElement("canvas").getContext("2d");
var canvasTextSupport = typeof context.fillText == "function"; // true in Opera Mini

An example you can use to see if text shown: tutorialspoint

Lofton answered 20/9, 2012 at 10:13 Comment(2)
That's pretty bad on Opera's side — to expose unimplemented method. Have you tried creating a small canvas, filling it with text (just 1 character), and checking if any of the pixels are non-"empty"? It would indicate that text was rendered.Tremolite
@Tremolite sounds promising I will try it out tomorrow and post result.Lofton
L
2

As suggested by @kangax, you can test by drawing text to a canvas and then making sure that pixels have been drawn.

var canvasTextSupported = function() {
  var canvas = document.createElement("canvas");  
  var context = canvas.getContext("2d");

  context.fillText("X", 5, 5);     
  var imageData = context.getImageData(0, 0, 10, 10);

  for(var i = 0, l = imageData.data.length; i < l; i++) {
    if(imageData.data[i] !== 0)
      return true;
  }
  return false;
}
Lofton answered 26/9, 2012 at 16:39 Comment(0)
W
2

You can use a custom build of the Modernizr library to do that!

http://modernizr.com/

Warrick answered 20/9, 2012 at 10:47 Comment(1)
Are you sure? I think modernizr also falsely claims support (open haz.io in OMini).Lofton
L
2

As suggested by @kangax, you can test by drawing text to a canvas and then making sure that pixels have been drawn.

var canvasTextSupported = function() {
  var canvas = document.createElement("canvas");  
  var context = canvas.getContext("2d");

  context.fillText("X", 5, 5);     
  var imageData = context.getImageData(0, 0, 10, 10);

  for(var i = 0, l = imageData.data.length; i < l; i++) {
    if(imageData.data[i] !== 0)
      return true;
  }
  return false;
}
Lofton answered 26/9, 2012 at 16:39 Comment(0)
H
1

As I was using canvas to detect fonts, with errors thrown, a more compact alternative I found is:

var canvasTextSupported = function() {
  var cvs = document.createElement("canvas");  
  var ctx = cvs.getContext("2d");
  return 'measureText' in ctx && ctx.measureText("") !== undefined;
}

Firefox 3.0.x doesn't have measureText. And in Opera Mini measureText() returns undefined.

Hound answered 3/6, 2015 at 6:23 Comment(0)
S
0

You can use canisuse.js script to detect if your browsers supports canvas text API or not

caniuse.canvasTextApi();
Sauer answered 16/6, 2016 at 6:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.