Skip to content

Commit 2841cf4

Browse files
committed
The third argument to DOM.context2d can be an options object with {scale, colorSpace}, allowing the creation of wide-gamut graphics on supported hardware and software.
Note: this necessitates a try/catch because Safari will throw if the os requirements are not met. Tests/demo: https://observablehq.com/@fil/colorspace-display-p3-context2d
1 parent a2d4919 commit 2841cf4

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ html`<canvas width=960 height=500>`
4141

4242
If you are using [2D Canvas](https://www.w3.org/TR/2dcontext/) (rather than [WebGL](https://webglfundamentals.org/)), you should use [DOM.context2d](#DOM_context2d) instead of DOM.canvas for automatic pixel density scaling.
4343

44-
<a href="#DOM_context2d" name="DOM_context2d">#</a> DOM.<b>context2d</b>(<i>width</i>, <i>height</i>[, <i>dpi</i>]) [<>](https://github.com/observablehq/stdlib/blob/master/src/dom/context2d.js "Source")
44+
<a href="#DOM_context2d" name="DOM_context2d">#</a> DOM.<b>context2d</b>(<i>width</i>, <i>height</i>[, <i>options</i>]) [<>](https://github.com/observablehq/stdlib/blob/master/src/dom/context2d.js "Source")
4545

46-
Returns a new canvas context with the specified *width* and *height* and the specified device pixel ratio *dpi*. If *dpi* is not specified, it defaults to [*window*.devicePixelRatio](https://developer.mozilla.org/docs/Web/API/Window/devicePixelRatio). To access the context’s canvas, use [*context*.canvas](https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/canvas). For example, to create a 960×500 canvas:
46+
Returns a new canvas context with the specified *width* and *height* and the specified *options*. If the options are specified as an object, its scale and colorSpace properties are considered. A string or number value specifies the scale. The scale defaults to [*window*.devicePixelRatio](https://developer.mozilla.org/docs/Web/API/Window/devicePixelRatio). The colorSpace defaults to srgb—the other acceptable value is display-p3. To access the context’s canvas, use [*context*.canvas](https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/canvas). For example, to create a 960×500 canvas:
4747

4848
```js
4949
{

src/dom/context2d.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1-
export default function(width, height, dpi) {
2-
if (dpi == null) dpi = devicePixelRatio;
1+
export default function(width, height, options) {
2+
var colorSpace = "srgb";
3+
var scale = devicePixelRatio;
4+
if (options == +options) {
5+
scale = +options;
6+
} else {
7+
if (options.scale) scale = +options.scale;
8+
if (options.colorSpace === "display-p3") colorSpace = options.colorSpace;
9+
}
310
var canvas = document.createElement("canvas");
4-
canvas.width = width * dpi;
5-
canvas.height = height * dpi;
11+
canvas.width = width * scale;
12+
canvas.height = height * scale;
613
canvas.style.width = width + "px";
7-
var context = canvas.getContext("2d");
8-
context.scale(dpi, dpi);
14+
var context;
15+
try {
16+
context = canvas.getContext("2d", {colorSpace});
17+
} catch(e) {
18+
context = canvas.getContext("2d");
19+
}
20+
context.scale(scale, scale);
921
return context;
1022
}

0 commit comments

Comments
 (0)