HTMLCanvasElement.getContext() - Web APIs | MDN
文章推薦指數: 80 %
Note: The identifier "experimental-webgl" is used in new implementations of WebGL. These implementations have either not reached test suite ... SkiptomaincontentSkiptoselectlanguageReferencesWebAPIsHTMLCanvasElementHTMLCanvasElement.getContext()ArticleActionsEnglish(US)SyntaxExamplesSpecificationsBrowsercompatibilitySeealsoRelatedTopicsCanvasAPIHTMLCanvasElementPropertiesheight mozOpaquewidthMethods captureStream()getContext() mozFetchAsStream() mozGetAsFile()toBlob()toDataURL() transferControlToOffscreen()EventswebglcontextlostInheritance:HTMLElementElementNodeEventTargetRelatedpagesforCanvasAPICanvasCanvasGradientCanvasImageSourceCanvasPatternCanvasRenderingContext2DImageBitmapImageBitmapFactoriesImageBitmapRenderingContextImageDataOffscreenCanvasPath2DTextMetricsSyntaxExamplesSpecificationsBrowsercompatibilitySeealsoHTMLCanvasElement.getContext() The HTMLCanvasElement.getContext()methodreturnsadrawing contextonthecanvas,ornullifthecontextidentifierisnot supported,orthecanvashasalreadybeensettoadifferentcontextmode. Latercallstothismethodonthesamecanvaselement,withthesame contextTypeargument,willalwaysreturnthesamedrawingcontextinstance aswasreturnedthefirsttimethemethodwasinvoked.Itisnotpossibletogeta differentdrawingcontextobjectonagivencanvaselement. Syntaxvarctx=canvas.getContext(contextType); varctx=canvas.getContext(contextType,contextAttributes); Parameters contextType IsaDOMStringcontainingthecontextidentifierdefiningthedrawing contextassociatedtothecanvas.Possiblevaluesare: "2d",leadingtothecreationofa CanvasRenderingContext2Dobjectrepresentingatwo-dimensional renderingcontext. "webgl"(or"experimental-webgl")whichwillcreatea WebGLRenderingContextobjectrepresentingathree-dimensional renderingcontext.ThiscontextisonlyavailableonbrowsersthatimplementWebGLversion1(OpenGLES2.0). "webgl2"whichwillcreateaWebGL2RenderingContext objectrepresentingathree-dimensionalrenderingcontext.Thiscontextisonly availableonbrowsersthatimplementWebGL version2(OpenGLES3.0). "bitmaprenderer"whichwillcreatean ImageBitmapRenderingContextwhichonlyprovidesfunctionalityto replacethecontentofthecanvaswithagivenImageBitmap. Note:Theidentifier"experimental-webgl"isused innewimplementationsofWebGL.Theseimplementationshaveeithernotreached testsuiteconformance,orthegraphicsdriversontheplatformarenotyet stable.TheKhronosGroupcertifiesWebGL implementationsundercertainconformance rules. contextAttributes Youcanuseseveralcontextattributeswhencreatingyourrenderingcontext,for example: constgl=canvas.getContext('webgl',{ antialias:false, depth:false }); 2dcontextattributes: alpha:Booleanthatindicatesifthecanvas containsanalphachannel.Ifsettofalse,thebrowsernowknows thatthebackdropisalwaysopaque,whichcanspeedupdrawingoftransparent contentandimages. desynchronized:Booleanthathintstheuseragent toreducethelatencybydesynchronizingthecanvaspaintcyclefromtheevent loop (Geckoonly) willReadFrequently:Booleanthatindicateswhether ornotalotofread-backoperationsareplanned.Thiswillforcetheuseofa software(insteadofhardwareaccelerated)2Dcanvasandcansavememorywhen callinggetImageData() frequently.Thisoptionisonlyavailable,iftheflag gfx.canvas.willReadFrequently.enableissettotrue (which,bydefault,isonlythecaseforB2G/FirefoxOS). (Blinkonly)storage: Stringthatindicateswhichstorageisused("persistent"bydefault). WebGLcontextattributes: alpha:Booleanthatindicatesifthecanvas containsanalphabuffer. depth:Booleanthatindicatesthatthedrawing bufferisrequestedtohaveadepthbufferofatleast16bits. stencil:Booleanthatindicatesthatthedrawing bufferisrequestedtohaveastencilbufferofatleast8bits. desynchronized:Booleanthathintstheuseragent toreducethelatencybydesynchronizingthecanvaspaintcyclefromtheevent loop antialias:Booleanthatindicateswhetherornot toperformanti-aliasingifpossible. failIfMajorPerformanceCaveat:Booleanthat indicatesifacontextwillbecreatedifthesystemperformanceisloworifno hardwareGPUisavailable. powerPreference:Ahinttotheuseragent indicatingwhatconfigurationofGPUissuitablefortheWebGLcontext.Possible valuesare: "default":LettheuseragentdecidewhichGPUconfigurationis mostsuitable.Thisisthedefaultvalue. "high-performance":Prioritizesrenderingperformanceover powerconsumption. "low-power":Prioritizespowersavingoverrendering performance. premultipliedAlpha:Booleanthatindicatesthat thepagecompositorwillassumethedrawingbuffercontainscolorswith pre-multipliedalpha. preserveDrawingBuffer:Ifthevalueistruethe bufferswillnotbeclearedandwillpreservetheirvaluesuntilclearedor overwrittenbytheauthor. xrCompatible:Booleanthathintstotheuseragent touseacompatiblegraphicsadapterforan immersiveXRdevice.Settingthis synchronousflagatcontextcreationisdiscouraged;rathercalltheasynchronous WebGLRenderingContext.makeXRCompatible()methodthemomentyou intendtostartanXRsession. ReturnvalueArenderingcontextwhichiseithera CanvasRenderingContext2Dfor"2d", WebGLRenderingContextfor"webgl"and "experimental-webgl", WebGL2RenderingContextfor"webgl2"or ImageBitmapRenderingContextfor"bitmaprenderer". IfthecontextTypedoesn'tmatchapossibledrawingcontext,ordiffers fromthefirstcontextTyperequested,nullisreturned. ExamplesGiventhis Youcangeta2dcontextofthecanvaswiththefollowingcode: varcanvas=document.getElementById('canvas'); varctx=canvas.getContext('2d'); console.log(ctx);//CanvasRenderingContext2D{/*...*/} Nowyouhavethe2Drendering contextforacanvasandyoucandrawwithinit.SpecificationsSpecificationHTMLStandard#dom-canvas-getcontext-devBrowsercompatibilityBCDtablesonlyloadinthebrowserSeealso CanvasRenderingContext2D.getContextAttributes() WebGLRenderingContext.getContextAttributes() Theinterfacedefiningit,HTMLCanvasElement. OffscreenCanvas.getContext() Availablerenderingcontexts:CanvasRenderingContext2D, WebGLRenderingContextandWebGL2RenderingContextand ImageBitmapRenderingContext. Foundaproblemwiththispage?EditonGitHubSourceonGitHubReportaproblemwiththiscontentonGitHubWanttofixtheproblemyourself?SeeourContributionguide.Lastmodified:Feb18,2022,byMDNcontributors
延伸文章資訊
- 1HTMLCanvasElement.getContext() - Web APIs | MDN
Note: The identifier "experimental-webgl" is used in new implementations of WebGL. These implemen...
- 2canvas.getContext( 'experimental-webgl' ) 是什么意思?
早期WebGL的context,还不能通过正式的名称webgl来获取,必须使用experimental-webgl来获取context对象。 但是,随着技术的发展更新,后来就可以通过正式 ...
- 3取得WebGL drawing context繪製在canvas上 - gists · GitHub
function initWebGL(canvas) {. var gl;. try {. gl = canvas.getContext("experimental-webgl");. /**....
- 4webgl (原生)基础入门指南【一】 - SegmentFault 思否
也行你已经有threejs或其他3d场景开发框架的使用经验,却不知道webgl原生到底 ... getContext("experimental-webgl"); } catch(e) {} /...
- 5HTMLCanvasElement.getContext() - Web API 接口参考| MDN
"webgl" (或 "experimental-webgl" ) 这将创建一个 WebGLRenderingContext 三维渲染上下文对象。只在实现WebGL 版本1(OpenGL ES ...