function createContext(canvas, attributes) {
	let context;

	if (attributes.context === "2d") {
		// 2d/canvas mode
		context = canvas.getContext(attributes.context, {
			willReadFrequently: attributes.willReadFrequently,
		});
	} else if (attributes.context === "webgl") {
		const attr = {
			alpha: attributes.transparent,
			antialias: attributes.antiAlias,
			depth: attributes.depth,
			stencil: true,
			preserveDrawingBuffer: false,
			premultipliedAlpha: attributes.transparent
				? attributes.premultipliedAlpha
				: false,
			powerPreference: attributes.powerPreference,
			failIfMajorPerformanceCaveat: attributes.failIfMajorPerformanceCaveat,
		};

		// attempt to create a WebGL2 context unless not requested
		if (attributes.preferWebGL1 !== true) {
			context = canvas.getContext("webgl2", attr);
			if (context) {
				WebGLVersion = 2;
			}
		}

		// fallback to WebGL1
		if (!context) {
			WebGLVersion = 1;
			context =
				canvas.getContext("webgl", attr) ||
				canvas.getContext("experimental-webgl", attr);
		}

		if (!context) {
			throw new Error("A WebGL context could not be created.");
		}
	} else {
		throw new Error("Invalid context type. Must be one of '2d' or 'webgl'");
	}

	// set the context size
	return context;
}
additional import for TypeScript
@import CanvasRenderer from "./../canvas/canvas_renderer.js";
@import WebGLRenderer from "./../webgl/webgl_renderer.js";