It's quite surprising that this question hasn't been closed yet, given that the answers provided are mostly subjective, with the exception of the following paragraph.
After reviewing the WHATWG HTML Living Standard, there doesn't seem to be a clear reason stated for the positioning of createLinearGradient and other objects specific to the CanvasRenderingContext2D
. The only requirement seems to be that these methods are integral to the interface.
The remainder of my response is based on personal perspective.
Interestingly, the gradients don't appear to necessitate a 2D context or maintain any connection to the canvas or 2D context. This implies that they can be easily removed (along with the canvas & 2D context) for disposal by garbage collection. It suggests that there may not be any behind-the-scenes resource management related to the gradients.
In my opinion, the rationale for their placement lies in the fact that without the 2D context, the gradient serves no purpose. While there may be situations where style-related objects are desired before creating a 2D context, it shouldn't pose a significant issue since the gradient is unusable without a 2D context, and a proxy object could potentially fulfill interim requirements.
I do wonder if the created gradient object could have been an SVG.linearGradient
interface, similar to how
CanvasRenderingContext2D.currentTransform
functions as an
SVG.matrix
interface. This would facilitate resource-sharing between SVG and the 2D context, aligning with their interconnected nature.
Since the standard mandates that these methods must be supported by the 2D context, implementing them elsewhere would likely be redundant.