After creating the canvas, you have to get the WebGL context. The first thing to do to obtain a WebGL drawing context is to get the id of the current canvas ...
Home
CodingGround
Jobs
Whiteboard
Tools
Business
Teachwithus
WebGLTutorial
WebGL-Home
WebGL-Introduction
WebGL-Html5CanvasOverview
WebGL-Basics
WebGL-GraphicsPipeline
WebGLApplication
WebGL-SampleApplication
WebGL-Context
WebGL-Geometry
WebGL-Shaders
AssociatingAttributes&BufferObjects
WebGL-DrawingaModel
WebGLExamples
WebGL-DrawingPoints
WebGL-DrawingaTriangle
WebGL-ModesofDrawing
WebGL-DrawingaQuad
WebGL-Colors
WebGL-Translation
WebGL-Scaling
WebGL-Rotation
WebGL-CubeRotation
WebGL-InteractiveCube
WebGLUsefulResources
WebGL-QuickGuide
WebGL-UsefulResources
WebGL-Discussion
SelectedReading
UPSCIASExamsNotes
Developer'sBestPractices
QuestionsandAnswers
EffectiveResumeWriting
HRInterviewQuestions
ComputerGlossary
WhoisWho
WebGL-Context
Advertisements
PreviousPage
NextPage
TowriteaWebGLapplication,firststepistogettheWebGLrenderingcontextobject.ThisobjectinteractswiththeWebGLdrawingbufferandcancallalltheWebGLmethods.ThefollowingoperationsareperformedtoobtaintheWebGLcontext−
CreateanHTML-5canvas
GetthecanvasID
ObtainWebGL
CreatingHTML-5CanvasElement
InChapter5,wediscussedhowtocreateanHTML-5canvaselement.WithinthebodyoftheHTML-5document,writeacanvas,giveitaname,andpassitasaparametertotheattributeid.Youcandefinethedimensionsofthecanvasusingthewidthandheightattributes(optional).
Example
Thefollowingexampleshowshowtocreateacanvaselementwiththedimensions500×500.WehavecreatedabordertothecanvasusingCSSforvisibility.Copyandpastethefollowingcodeinafilewiththenamemy_canvas.html.
LiveDemo
Itwillproducethefollowingresult−
GettheCanvasID
Aftercreatingthecanvas,youhavetogettheWebGLcontext.ThefirstthingtodotoobtainaWebGLdrawingcontextistogettheidofthecurrentcanvaselement.
CanvasIDisacquiredbycallingtheDOM(DocumentObjectModel)methodgetElementById().Thismethodacceptsastringvalueasparameter,sowepassthenameofthecurrentcanvastoit.
Forexample,ifthecanvasnameismy_canvas,thencanvasIDisobtainedasshownbelow−
varcanvas=document.getElementById('my_Canvas');
GettheWebGLDrawingContext
TogettheWebGLRenderingContextobject(orWebGLDrawingcontextobjectorsimplyWebGLcontext),callthegetContext()methodofthecurrentHTMLCanvasElement.ThesyntaxofgetContext()isasfollows−
canvas.getContext(contextType,contextAttributes);
Passthestringswebglorexperimental-webglasthecontentType.ThecontextAttributesparameterisoptional.(Whileproceedingwiththisstep,makesureyourbrowserimplementsWebGLversion1(OpenGLES2.0)).
ThefollowingcodesnippetshowshowtoobtaintheWebGLrenderingcontext.Hereglisthereferencevariabletotheobtainedcontextobject.
varcanvas=document.getElementById('my_Canvas');
vargl=canvas.getContext('experimental-webgl');
WebGLContextAttributes
TheparameterWebGLContextAttributesisnotmandatory.ThisparameterprovidesvariousoptionsthatacceptBooleanvaluesaslistedbelow−
Sr.No.
Attributes&Description
1
Alpha
Ifitsvalueistrue,itprovidesanalphabuffertothecanvas.
Bydefault,itsvalueistrue.
2
depth
Ifitsvalueistrue,youwillgetadrawingbufferwhichcontainsadepthbufferofatleast16bits.
Bydefault,itsvalueistrue.
3
stencil
Ifitsvalueistrue,youwillgetadrawingbufferwhichcontainsastencilbufferofatleast8bits.
Bydefault,itsvalueisfalse.
4
antialias
Ifitsvalueistrue,youwillgetadrawingbufferwhichperformsanti-aliasing.
Bydefault,itsvalueistrue.
5
premultipliedAlpha
Ifitsvalueistrue,youwillgetadrawingbufferwhichcontainscolorswithpre-multipliedalpha.
Bydefault,itsvalueistrue.
6
preserveDrawingBuffer
Ifitsvalueistrue,thebufferswillnotbeclearedandwillpreservetheirvaluesuntilclearedoroverwrittenbytheauthor.
Bydefault,itsvalueisfalse.
ThefollowingcodesnippetshowshowtocreateaWebGLcontextwithastencilbuffer,whichwillnotperformanti-aliasing.
varcanvas=document.getElementById('canvas1');
varcontext=canvas.getContext('webgl',{antialias:false,stencil:true});
AtthetimeofcreatingtheWebGLRenderingContext,adrawingbufferiscreated.TheContextobjectmanagesOpenGLstateandrenderstothedrawingbuffer.
WebGLRenderingContext
ItistheprincipalinterfaceinWebGL.ItrepresentstheWebGLdrawingcontext.ThisinterfacecontainsallthemethodsusedtoperformvarioustasksontheDrawingbuffer.Theattributesofthisinterfacearegiveninthefollowingtable.
Sr.No.
Attributes&Description
1
Canvas
Thisisareferencetothecanvaselementthatcreatedthiscontext.
2
drawingBufferWidth
Thisattributerepresentstheactualwidthofthedrawingbuffer.ItmaydifferfromthewidthattributeoftheHTMLCanvasElement.
3
drawingBufferHeight
Thisattributerepresentstheactualheightofthedrawingbuffer.ItmaydifferfromtheheightattributeoftheHTMLCanvasElement.
PreviousPage
PrintPage
NextPage
Advertisements