Menu

Chrome 66 Beta: CSS Typed Object Model, Async Clipboard API, AudioWorklet

March 21st, 2018

Unless otherwise noted, changes described below apply to the newest Chrome Beta channel release for Android, Chrome OS, Linux, macOS, and Windows. View a complete list of the features in Chrome 66 on ChromeStatus.

The ImageBitmap Rendering Context for <canvas>

Historically, rendering an image to a canvas has involved first creating an <img> tag and then rendering its contents to a canvas. This causes multiple copies of the image to be stored in memory. A new rendering context streamlines the display of ImageBitmap objects by avoiding memory duplication and rendering them more efficiently.

This example shows how to do this using an ImageBitmapRenderingContext. This essentially transfers ownership of an image's pixels. This example does so from a blob to a <canvas>, but pixels can be moved between <canvas> elements as well. Note that the blob is compressed so it is not a full copy in memory.

const image = await createImageBitmap(imageBlob);
const canvas = document.createElement('canvas');
const context = canvas.getContext('bitmaprenderer');
context.transferFromImageBitmap(image);
canvas.toBlob((outputJPEGBlob) => {
// Do something with outputJPEGBlob.
}, 'image/jpeg');

If this were done without createImageBitmap(), the imageBlob would be lazily decoded, which would cause jank. On the other hand createImageBitmap() is asynchronous which allows you to decode it completely before using it and avoiding jank. For example, a WebGL game could use this to load new textures on the fly as gameplay progresses.

CSS Typed Object Model

Historically, developers wanting to manipulate CSS properties have had to manipulate strings only for the browser to then convert it back to a typed representation. What made things worse was when developers tried to read the value of a CSS property in Javascript, this typed value was converted back to a string.

In version 66, Chrome implements the CSS Typed Object Model (OM) Level 1, a part of Houdini, for a subset of CSS properties. Typed OM reduces this burden on both the developer and browser by exposing CSS values as typed JavaScript objects rather than strings. Along with allowing the performant manipulation of values assigned to CSS properties, Typed OM allows developers to write more maintainable and easy to understand code.

A brief example illustrates the point. Previously if I wanted to set the opacity of an element I could do this:

el.style.opacity = 0.3;
el.style.opacity === "0.3"
With CSSOM:
el.attributeStyleMap.set("opacity", CSS.number("0.3"));
el.attributeStyleMap.get("opacity").value === 0.3

The returned values above are of type CSSUnitValue, which are easier to manipulate than strings.

Asynchronous Clipboard API

The new asynchronous clipboard API provides a promise-based means of reading from and writing to the clipboard. It's simpler than the old execCommand('copy') API released in Chrome 43 and integrates with the Permissions API. Future Chrome releases will also support copy/paste of richer types of data, including images. To get a taste of this API, lets do simple write and read operations with text.

try {
  await navigator.clipboard.writeText("Hello, clipboard.");
} catch { 
  console.error("Unable to write to clipboard.");
}

Similarly, to read text back:

const data = await navigator.clipboard.readText();
console.log("From the clipboard:", data);

For more information, including how to use security and permissions with the API, read Unblocking Clipboard Access and check out our sample.

AudioWorklet

The legacy ScriptProcessorNode was asynchronous and required thread hops, which could produce an unstable audio output. The AudioWorklet object provides a new synchronous JavaScript execution context which allows developers to programmatically control audio without additional latency and higher stability in the output audio.

You can see example code in action along with other examples at Google Chrome Labs.

In addition to AudioWorklet, other worklet API are being built. PaintWorklet was released in Chrome 65/Opera 52. An AnimationWorklet is planned. ScriptProcessorNode will be deprecated some time after AudioWorklet ships.

Other Features in this Release

Blink > Animation

The add and accumulate compositing operations are intended for building modularized animations. The add and accumulate keywords will be supported in Chrome soon. Until then, they will no longer throw errors. This is to maintain compatibility with Firefox and other implementations.

Blink > CSS

CSS has two new features.

  • The mathematical expressions calc(), min(), and max() are now supported in media queries, as required by the CSS Values and Units Module Level 4 specification. This change brings them in line with other types of rules where these functions are allowed anywhere a number is allowed.
  • Floating point values are now allowed in the rgb() and rgba() functions.

Blink > Feature Policy

By default, the deviceorientation, deviceorientationabsolute, and devicemotion events are now restricted to top-level document and same-origin subframes, the same as if the feature policy for those features were set to 'self'. To modify this behavior, explicitly enable or disable the specific feature.

Blink > File API

The File API now results in a network error instead of a 404 when attempting to read from an invalid or non-existing blob URL.

Blink > Forms

HTML forms have two new features.

  • The <textarea> element and the <select> element now support the autocomplete attribute as required by the specification.
  • A mutable checkbox now fires three events as required by the HTML specification: a click event, then an input event, then a change event. Formerly only the click and change events were fired.

Blink > Fullscreen

If a page in fullscreen mode opens a popup and calls window.focus(), that page exits full screen. This does not occur if the popup receives focus some other way.

Blink > GetUserMedia

A new method on the MediaStreamTrack interface called getCapabilities() returns a MediaTrackCapabilities object, which specifies the values or range of values which each constrianable property. Capabilities vary by device.

Blink > JavaScript

There are several JavaScript changes.

  • The Function.prototype.toString() function now returns exactly what is written in the source code. This includes whitespace and other text that may have been used. For example, if there is a comment between the function keyword and the function name, the comment is now returned in addition to the keyword and name.
  • JSON is now a syntactic subset of ECMAScript, which allows line separator (U+2028) and paragraph separator (U+2029) symbols in string literals.
  • The catch clause of a try statement can now be used without a parameter.
  • String.prototype.trimStart() and String.prototype.trimEnd() are now available as the standards-based way of trimming whitespace from strings, in addition to String.prototype.trim() which was already implemented. The non-standard trimLeft() and trimRight() remain as aliases of the new methods for backward compatibility.
  • The Array.prototype.values() method returns a new array iterator object that contains the values for each index in the array.

Blink > Layout

Layout has two new features.

  • The grid prefix has been removed from the CSS gutter properties:
    • grid-gap becomes gap
    • grid-row-gap becomes row-gap
    • grid-column-gap becomes column-gap
The default value for all three is normal and the prefixed properties are aliases of the new ones. Note that column-gap property already exists and is used by css-multicol.
  • Elements with the display properties table-row, table-row-group, table-header-group, table-footer-group, table-cell, and table-caption that have a transform property are now containing blocks for fixed position elements. Blink currently does not make <tr>, <tbody>, <tfoot>, and <thead> be containing blocks for fixed-position elements.

Blink > Media

Media has two new features.

  • As announced earlier, autoplay is now allowed only when either the media won't play sound, after the user clicks or taps on the site, or (on desktop) if the user has previously shown an interest in media on the site. This will reduce unexpected video playbacks with sound when first opening a web page.
  • The Media Capabilities, Decoding Info API allows websites to get more information about the decoding abilities of the client. This enables more informed selection of media streams for the user, avoiding scenarios such as where the client is unable to smoothly and power-efficiently decode a resolution that might have been picked based only on available bandwidth and screen size.

Blink > Network

The Fetch API has two new features.

  • The Request object now supports a keepalive property which allows a fetch to continue after a tab is closed. This feature is invoked by passing a boolean in the constructor's initialization object. Its value can be read back from the object itself. This property can be used with sendBeacon() as well.
  • New AbortSignal and AbortController interfaces allow a fetch to be canceled. To accomplish this create an AbortController object and pass its signal property as an option to fetch. Calling abortController.abort() cancels the fetch. There's more information in our abortable fetch article, but a small code example is shown below.
const controller = new AbortController();
const signal = controller.signal;
const requestPromise = fetch(url, { signal });
// Abort the fetch:
controller.abort();

Blink > ServiceWorker

Service workers have two changes.

  • A service worker can no longer respond to a request whose mode is same-origin with a response whose type is CORS. This is a security measure recently added to the Fetch specification.
  • FetchEvent.clientId now returns an empty string instead of null when it isn't set. For example, this can occur during a navigation request.

Blink > WebRTC

Chrome now supports the RTCRtpSender.dtmf attribute per the specification. This replaces the CreateDTMFSender() function which has not yet been deprecated.

Deprecations and Interoperability Improvements

Blink > CSS

The object-position and perspective-origin properties no longer accepts three-part values such as top right 20%. This change also applies to basic shapes and gradients. Valid position values must always have 1, 2, or 4 parts.

Blink > HTML

Following a specification change, ImageCapture.prototype.setOptions() has been removed.

Blink > Input

Following a specification change, document.createTouch() and document.createTouchList() have been removed.

Blink > Web Audio

Following a specification change, automatic dezippering of AudioParam.prototype.value changes was removed from Chrome. If you need to smooth the value of AudioParam changes, use AudioParam.prototype.setTargetAtTime().

Posted by Naina Raisinghani