-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pass-through options from DOM.context2d to getContext #273
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,10 +1,15 @@ | ||||||||||||||
export default function(width, height, dpi) { | ||||||||||||||
if (dpi == null) dpi = devicePixelRatio; | ||||||||||||||
var canvas = document.createElement("canvas"); | ||||||||||||||
canvas.width = width * dpi; | ||||||||||||||
canvas.height = height * dpi; | ||||||||||||||
export default function (width, height, options = {}) { | ||||||||||||||
const {scale = devicePixelRatio, ...contextOptions} = !isNaN(options) | ||||||||||||||
? {...(options != null && {scale: options})} | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no need for the destructuring here, and the null check should be explicit. If options is undefined, we already get
Suggested change
|
||||||||||||||
: options; | ||||||||||||||
Comment on lines
+2
to
+4
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 0 is currently working as before (in the sense that it returns a canvas of size 0)—it would be simpler if we didn't support this "feature". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the additional null check in the earlier comment we can remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (btw, that parseFloat was the result of a brainfart - I meant to write |
||||||||||||||
const canvas = document.createElement("canvas"); | ||||||||||||||
canvas.width = width * scale; | ||||||||||||||
canvas.height = height * scale; | ||||||||||||||
canvas.style.width = width + "px"; | ||||||||||||||
var context = canvas.getContext("2d"); | ||||||||||||||
context.scale(dpi, dpi); | ||||||||||||||
const context = canvas.getContext( | ||||||||||||||
"2d", | ||||||||||||||
options === null ? options : contextOptions | ||||||||||||||
); | ||||||||||||||
context.scale(scale, scale); | ||||||||||||||
return context; | ||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we list both signatures here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's there: "As a shorthand notation, options having only scale can be specified as a number." (could be phrased better?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel the shorthand should be mentioned earlier and emphasized, since it's the format that you'll encounter throughout Observable and the notation that you'll commonly use to disable dpr scaling.