Skip to content

Commit

Permalink
refactor(webgl): remove adaptDPI()
Browse files Browse the repository at this point in the history
BREAKING CHANGE: re-use adaptDPI() from new @thi.ng/adapt-dpi pkg

- update deps
  • Loading branch information
postspectacular committed Jun 6, 2020
1 parent 2b89ad4 commit 6d49da6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 28 deletions.
1 change: 1 addition & 0 deletions packages/webgl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"typescript": "^3.9.2"
},
"dependencies": {
"@thi.ng/adapt-dpi": "^0.0.1",
"@thi.ng/api": "^6.11.0",
"@thi.ng/associative": "^4.0.11",
"@thi.ng/binary": "^2.0.7",
Expand Down
33 changes: 5 additions & 28 deletions packages/webgl/src/canvas.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { adaptDPI } from "@thi.ng/adapt-dpi";
import { isString } from "@thi.ng/checks";
import { error } from "./error";
import type { WeblGLCanvasOpts } from "./api/canvas";
import type { WebGLExtensionMap } from "./api/ext";
import { error } from "./error";

const defaultOpts: WebGLContextAttributes = {
alpha: true,
antialias: true,
depth: true,
premultipliedAlpha: true,
preserveDrawingBuffer: false,
stencil: false
stencil: false,
};

export const glCanvas = (opts: Partial<WeblGLCanvasOpts> = {}) => {
Expand All @@ -26,7 +27,7 @@ export const glCanvas = (opts: Partial<WeblGLCanvasOpts> = {}) => {
opts.version === 2 ? "webgl2" : "webgl",
{
...defaultOpts,
...opts.opts
...opts.opts,
}
);
if (!gl) {
Expand All @@ -37,7 +38,7 @@ export const glCanvas = (opts: Partial<WeblGLCanvasOpts> = {}) => {
return {
canvas,
gl,
ext: getExtensions(gl, opts.ext!)
ext: getExtensions(gl, opts.ext!),
};
};

Expand All @@ -55,27 +56,3 @@ export const getExtensions = <K extends keyof WebGLExtensionMap>(
}
return ext;
};

/**
* Sets the canvas size to given `width` & `height` and adjusts style to
* compensate for HDPI devices. Note: For 2D canvases, this will
* automatically clear any prior canvas content.
*
* @param canvas -
* @param width - uncompensated pixel width
* @param height - uncompensated pixel height
*/
export const adaptDPI = (
canvas: HTMLCanvasElement,
width: number,
height: number
) => {
const dpr = window.devicePixelRatio || 1;
if (dpr != 1) {
canvas.style.width = `${width}px`;
canvas.style.height = `${height}px`;
}
canvas.width = width * dpr;
canvas.height = height * dpr;
return dpr;
};

0 comments on commit 6d49da6

Please sign in to comment.