observablehq / stdlib

The Observable standard library.
https://observablehq.com/@observablehq/standard-library
ISC License
966 stars 83 forks source link

Override context.resetTransform in DOM.context2d? #16

Closed mbostock closed 5 years ago

mbostock commented 6 years ago

If you use DOM.context2d when dpi is not 1, you probably never want to say:

context.resetTransform();

Instead you want to say

context.resetTransform();
context.scale(dpi, dpi);

Do we want to monkey-patch context.resetTransform on the returned instance to do this automatically? Or is calling context.resetTransform considered user error?

mootari commented 6 years ago

resetTransform() should not be monkeypatched. When using drawImage() to combine multiple canvas outputs into one, the target canvas context must not have any transforms. Otherwise any source image/canvas will be scaled. Calling resetTransform() on the output canvas is the only way to prevent this without losing the convenience of using DOM.context2d().

I've created a simplified example here: http://jsfiddle.net/0v9g3x8k/