-Slightly modified/extended versions of these examples (and more) can be found in
- e.g. this codepen collection.
-
-## Platform support for a text detector ## {#example-feature-detection}
-
```js
-if (window.TextDetector == undefined) {
+if (!('TextDetector' in window)) {
console.error('Text Detection not supported on this platform');
+} else {
+ const languages = ['en', 'es']; // English and Spanish
+ TextDetector.availability({ languages: languages }).then(availability => {
+ if (availability === 'unavailable') {
+ console.log('Not all of the requested languages are supported.');
+ return;
+ }
+
+ if (availability === 'downloadable') {
+ console.log('Languages need to be downloaded first.');
+ } else if (availability === 'downloading') {
+ console.log('Languages are currently being downloaded.');
+ } else {
+ console.log('All requested languages are supported.');
+ }
+
+ // Now you can create a TextDetector with the supported languages.
+ // If the status was 'downloadable' or 'downloading', create() will wait
+ // for the download to finish before resolving.
+ TextDetector.create({ languages: languages }).then(detector => {
+ // ... use the detector
+ });
+ });
}
```
## Text Detection ## {#example-text-detection}
-
```js
-let textDetector = new TextDetector();
-// Assuming |theImage| is e.g. a <img> content, or a Blob.
-
-textDetector.detect(theImage)
-.then(detectedTextBlocks => {
- for (const textBlock of detectedTextBlocks) {
- console.log(
- 'text @ (${textBlock.boundingBox.x}, ${textBlock.boundingBox.y}), ' +
- 'size ${textBlock.boundingBox.width}x${textBlock.boundingBox.height}');
+(async () => {
+ // Assuming |theImage| is e.g. a
![]()
content, or a Blob.
+ try {
+ // The legacy synchronous constructor is still supported,
+ // but the async create() method is recommended.
+ // let textDetector = new TextDetector();
+
+ let textDetector = await TextDetector.create();
+
+ const detectedTextBlocks = await textDetector.detect(theImage);
+ for (const textBlock of detectedTextBlocks) {
+ console.log(
+ `text @ (${textBlock.boundingBox.x}, ${textBlock.boundingBox.y}), ` +
+ `size ${textBlock.boundingBox.width}x${textBlock.boundingBox.height}`);
+ }
+ } catch (e) {
+ console.error("Text Detection failed, boo.", e);
}
-}).catch(() => {
- console.error("Text Detection failed, boo.");
-})
+})();
```
@@ -154,6 +224,9 @@ spec: html
text: allowed to show a popup
text: in parallel
text: incumbent settings object
+spec: writing-assistance-apis
+ type: enum
+ text: Availability