Skip to content

Commit e0533db

Browse files
authored
Merge pull request #5006 from Tyriar/tyriar/4995
Set up shared context on browser even if process exists
2 parents b7b9d27 + 37efeeb commit e0533db

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

src/common/Color.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* @license MIT
44
*/
55

6-
import { isNode } from 'common/Platform';
76
import { IColor, IColorRGB } from 'common/Types';
87

98
let $r = 0;
@@ -117,9 +116,10 @@ export namespace color {
117116
* '#rrggbbaa').
118117
*/
119118
export namespace css {
119+
// Attempt to set get the shared canvas context
120120
let $ctx: CanvasRenderingContext2D | undefined;
121121
let $litmusColor: CanvasGradient | undefined;
122-
if (!isNode) {
122+
try {
123123
// This is guaranteed to run in the first window, so document should be correct
124124
const canvas = document.createElement('canvas');
125125
canvas.width = 1;
@@ -133,6 +133,9 @@ export namespace css {
133133
$litmusColor = $ctx.createLinearGradient(0, 0, 1, 1);
134134
}
135135
}
136+
catch {
137+
// noop
138+
}
136139

137140
/**
138141
* Converts a css string to an IColor, this should handle all valid CSS color strings and will

src/common/Platform.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ interface INavigator {
1414
declare const navigator: INavigator;
1515
declare const process: unknown;
1616

17-
export const isNode = (typeof process !== 'undefined') ? true : false;
17+
export const isNode = (typeof process !== 'undefined' && 'title' in (process as any)) ? true : false;
1818
const userAgent = (isNode) ? 'node' : navigator.userAgent;
1919
const platform = (isNode) ? 'node' : navigator.platform;
2020

0 commit comments

Comments
 (0)