Skip to content

Commit 96fa21a

Browse files
committed
pass-through
1 parent 2841cf4 commit 96fa21a

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ If you are using [2D Canvas](https://www.w3.org/TR/2dcontext/) (rather than [Web
4343

4444
<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 *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:
46+
Returns a new canvas context with the specified *width* and *height* and the specified *options*. The scale property of the options object defines the device pixel ratio, and defaults to [*window*.devicePixelRatio](https://developer.mozilla.org/docs/Web/API/Window/devicePixelRatio). The remaining options are passed through as contextAttributes to [getContext](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/getContext). As a shorthand notation, *options* having only scale can be specified as a number. 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
{

Diff for: src/dom/context2d.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
export default function(width, height, options) {
2-
var colorSpace = "srgb";
32
var scale = devicePixelRatio;
4-
if (options == +options) {
3+
if (options == null) {
4+
options = {};
5+
} else if (options == +options) {
56
scale = +options;
7+
options = {};
68
} else {
79
if (options.scale) scale = +options.scale;
8-
if (options.colorSpace === "display-p3") colorSpace = options.colorSpace;
10+
delete options.scale;
911
}
1012
var canvas = document.createElement("canvas");
1113
canvas.width = width * scale;
1214
canvas.height = height * scale;
1315
canvas.style.width = width + "px";
1416
var context;
15-
try {
16-
context = canvas.getContext("2d", {colorSpace});
17-
} catch(e) {
18-
context = canvas.getContext("2d");
19-
}
17+
context = canvas.getContext("2d", options);
2018
context.scale(scale, scale);
2119
return context;
2220
}

0 commit comments

Comments
 (0)