-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
account for device pixel ratio when rendering graphics
- Loading branch information
1 parent
781d683
commit 6691ac4
Showing
5 changed files
with
265 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/attack_flow_builder/src/assets/scripts/BlockDiagram/Utilities/Canvas.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* Resizes a canvas context according to the current screen's pixel ratio. | ||
* @param context | ||
* The context to resize. | ||
* @param width | ||
* The new width of the context. | ||
* @param height | ||
* The new height of the context. | ||
*/ | ||
export function resizeContext( | ||
context: CanvasRenderingContext2D, | ||
width: number, | ||
height: number | ||
) { | ||
context.canvas.width = width * window.devicePixelRatio; | ||
context.canvas.height = height * window.devicePixelRatio; | ||
context.canvas.style.width = `${ width }px`; | ||
context.canvas.style.height = `${ height }px`; | ||
context.scale(window.devicePixelRatio, window.devicePixelRatio); | ||
} | ||
|
||
/** | ||
* Transforms a canvas context according to the current screen's pixel ratio. | ||
* @param context | ||
* The context to resize. | ||
* @param k | ||
* The context's scale. | ||
* @param x | ||
* The context's x translation. | ||
* @param y | ||
* The context's y translation. | ||
*/ | ||
export function transformContext( | ||
context: CanvasRenderingContext2D, | ||
k: number, | ||
x: number, | ||
y: number | ||
) { | ||
k *= window.devicePixelRatio, | ||
x *= window.devicePixelRatio, | ||
y *= window.devicePixelRatio; | ||
context.setTransform( | ||
k, 0, 0, | ||
k, x, y | ||
); | ||
} | ||
|
||
/** | ||
* Resizes and transforms a canvas context according to the current screen's | ||
* pixel ratio. | ||
* @param context | ||
* The context to resize. | ||
* @param width | ||
* The new width of the context. | ||
* @param height | ||
* The new height of the context. | ||
* @param k | ||
* The context's scale. | ||
* @param x | ||
* The context's x translation. | ||
* @param y | ||
* The context's y translation. | ||
*/ | ||
export function resizeAndTransformContext( | ||
context: CanvasRenderingContext2D, | ||
width: number, | ||
height: number, | ||
k: number, | ||
x: number, | ||
y: number | ||
) { | ||
context.canvas.width = width * window.devicePixelRatio; | ||
context.canvas.height = height * window.devicePixelRatio; | ||
context.canvas.style.width = `${ width }px`; | ||
context.canvas.style.height = `${ height }px`; | ||
transformContext(context, k, x, y); | ||
} |
Oops, something went wrong.