YouTip LogoYouTip

Met Canvas Createlineargradient

# HTML canvas createLinearGradient() Method [![Image 9: Canvas Object Reference Manual](#) Canvas Object](#) ## Example Define a gradient from black to white (from left to right) and use it as the fill style for a rectangle: YourbrowserdoesnotsupporttheHTML5canvastag. JavaScript: var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); var grd=ctx.createLinearGradient(0,0,170,0); grd.addColorStop(0,"black"); grd.addColorStop(1,"white"); ctx.fillStyle=grd; ctx.fillRect(20,20,150,100); [Try it Β»](#) * * * ## Browser Support ![Image 10: Internet Explorer](#)![Image 11: Firefox](#)![Image 12: Opera](#)![Image 13: Google Chrome](#)![Image 14: Safari](#) Internet Explorer 9, Firefox, Opera, Chrome, and Safari support the createLinearGradient() method. **Note:** Internet Explorer 8 and earlier versions do not support the element. * * * ## Definition and Usage The createLinearGradient() method creates a linear gradient object. Gradients can be used to fill rectangles, circles, lines, text, and more. **Tip:** Use this object as the value for the (#) or (#) property. **Tip:** Use the [addColorStop()](#) method to specify different colors and where to position those colors in the gradient object. | JavaScript Syntax: | _context_.createLinearGradient(_x0,y0,x1,y1_); | | --- | ## Parameter Values | Parameter | Description | | --- | --- | | _x0_ | The x-coordinate of the gradient's starting point | | _y0_ | The y-coordinate of the gradient's starting point | | _x1_ | The x-coordinate of the gradient's ending point | | _y1_ | The y-coordinate of the gradient's ending point | * * * ![Image 15: Example](#) ## More Examples ## Example Define a gradient (from top to bottom) and use it as the fill style for a rectangle: Yourbrowserdoesnotsupportthecanvastag. JavaScript: var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); var my_gradient=ctx.createLinearGradient(0,0,0,170); my_gradient.addColorStop(0,"black"); my_gradient.addColorStop(1,"wh
← Met Canvas CreatepatternProp Canvas Shadowoffsety β†’