A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lang below:

CanvasRenderingContext2D: lang property - Web APIs

CanvasRenderingContext2D: lang property

Limited availability

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The CanvasRenderingContext2D.lang property of the Canvas 2D API gets or sets the language of the canvas drawing context.

Value

The lang property can take one of the following string values:

The default value is inherit.

Description

Sometimes, you need to set a language for a canvas rendering context so that it knows how to render language-dependent features: for example, some fonts have certain characters rendered differently in different languages. An on-screen canvas context (CanvasRenderingContext2D) is always associated with a particular <canvas> element, so whenever you render content using it, it can derive the language from the value of the <canvas> element's lang attribute.

Sometimes, it is necessary to set a language for a canvas rendering context so that it knows how to render language-dependent features such as some fonts. An on-screen canvas context (CanvasRenderingContext2D) is always associated with a particular <canvas> element, so whenever you render content using it, it can derive the language from the value of the <canvas> element's lang attribute.

However, an off-screen canvas context (OffscreenCanvasRenderingContext2D) renders its content before it is associated with a <canvas> element, so it can't derive a rendering language from the lang attribute of the <canvas> element. The lang property addresses this issue, allowing you to set a language directly on a canvas rendering context, whether you are using an on-screen or off-screen canvas.

The inherit value

When the inherit value is used, the language of the canvas context is inherited from the lang attribute of the nearest-available HTML source:

Due to technical limitations, the inherit value behaves differently for on-screen and off-screen canvases:

Examples Basic usage
const canvasElem = document.querySelector("canvas");
const ctx = canvasElem.getContext("2d");

// Get context language; returns "inherit" by default
console.log(ctx.lang);

// Set context language
ctx.lang = "en";
// Logs "en"
console.log(ctx.lang);
Demonstrating canvas context localization support

In this example, we render a text string to a 2D canvas context in a particular font that has language-dependent ligatures. We allow the canvas context's language to be adjusted so you can see the difference in rendering.

HTML

The HTML features a <select> element that allows you to choose a language — en (English) or tr (Turkish) — and a <canvas> element to render to.

<p>
  <label for="lang">Choose language:</label>
  <select id="lang" name="lang">
    <option>en</option>
    <option>tr</option>
  </select>
</p>
<canvas></canvas>
JavaScript

In the JavaScript, we first grab references to the <canvas> element, its CanvasRenderingContext2D, and the <select> element, then load the language-dependant font using the CSS Font Loading API. Once the font is loaded, we run an init() function. This function defines another function — drawText(), which draws some text to the canvas context that uses the loaded font, adds a change event listener to the <select> element, then calls drawText() so that the text is immediately drawn to the canvas when the page first loads.

const canvasElem = document.querySelector("canvas");
const ctx = canvasElem.getContext("2d");

const selectElem = document.querySelector("select");

const latoMediumFontFace = new FontFace(
  // Lato-Medium is a font with language specific ligatures
  "Lato-Medium",
  "url(https://mdn.github.io/shared-assets/fonts/Lato-Medium.ttf)",
);

latoMediumFontFace.load().then((font) => {
  document.fonts.add(font);
  init();
});

function init() {
  function drawText() {
    ctx.clearRect(0, 0, canvasElem.width, canvasElem.height);
    ctx.font = "30px Lato-Medium";
    ctx.color = "black";
    ctx.fillText("finish crafting", 50, 100);
  }

  selectElem.addEventListener("change", () => {
    document.documentElement.lang = selectElem.value;
    drawText();
  });

  drawText();
}

When the <select> value is changed, the change event handler function fires, which:

Result

The example is rendered as follows:

Try changing the document language using the <select> element. When the language is set to English, the font will be rendered with the "fi" ligature. However, when it is set to Turkish, the font will be rendered without the "fi" ligature, because that locale doesn't include it.

Language support for offscreen canvases

This example is the similar to the previous example, except that the font is rendered to a OffscreenCanvasRenderingContext2D then the resulting bitmap is transferred to the on-screen <canvas> to display.

In addition, because an inherited off-screen canvas language is only set once, and not dynamically updated if the inherited lang attribute value is changed, we explicitly set the lang property on the OffscreenCanvasRenderingContext2D instead.

HTML
<p>
  <label for="lang">Choose language:</label>
  <select id="lang" name="lang">
    <option>en</option>
    <option>tr</option>
  </select>
</p>
<canvas></canvas>
JavaScript

The JavaScript works in the same way as the previous example, except that:

const canvasElem = document.querySelector("canvas");
const ctx = canvasElem.getContext("bitmaprenderer");

const offscreen = new OffscreenCanvas(canvasElem.width, canvasElem.height);
const offscreen_ctx = offscreen.getContext("2d");

const selectElem = document.querySelector("select");

const latoMediumFontFace = new FontFace(
  // Lato-Medium is a font with language specific ligatures.
  "Lato-Medium",
  "url(https://mdn.github.io/shared-assets/fonts/Lato-Medium.ttf)",
);

latoMediumFontFace.load().then((font) => {
  document.fonts.add(font);
  init();
});

function init() {
  function drawText() {
    offscreen_ctx.clearRect(0, 0, canvasElem.width, canvasElem.height);
    offscreen_ctx.lang = selectElem.value;
    offscreen_ctx.font = "30px Lato-Medium";
    offscreen_ctx.color = "black";
    offscreen_ctx.fillText("finish crafting", 50, 100);

    const bitmap = offscreen.transferToImageBitmap();
    ctx.transferFromImageBitmap(bitmap);
  }

  selectElem.addEventListener("change", () => {
    drawText();
  });

  drawText();
}
Result

The example is rendered as follows:

Specifications Browser compatibility See also

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4