Canvas gradients are the colors that can be used to fill or stroke of shapes, instead of solid colors, Gradient is a color which changes from one color to another color.
There are two types of gradinets they are
<canvas id="Canvas" width="300" height="250" style ="border:5px solid orange;"></canvas>
<script>
var canvas = document.getElementById('Canvas');
var context = canvas.getContext('2d');
context.rect(10, 10, 200, 150);
var linear = context.createLinearGradient(0, 0, 200, 150);
linear.addColorStop(0, 'orange');
linear.addColorStop(1, 'blue');
context.fillStyle = linear;
context.fill();
</script>
In the above code snippet we have defined the linear gradient with two colors we have canvas element as with the width of 300 and height of 250 and id as "canvas".
context.createLinearGradient(0, 0, 200, 150);
- defines the value of linear gradient rectangle values output
We can have the linear gradient with multi colors
<canvas id="Canvas" width="400" height="300" style="border:2px solid green"></canvas>
<script>
var canvas = document.getElementById('Canvas');
var context = canvas.getContext('2d');
context.rect(10, 10, 300, 250);
var linear = context.createLinearGradient(60, 20, 100, 250);
linear.addColorStop(0, 'black');
linear.addColorStop(0.3, 'lime');
linear.addColorStop(0.6, 'red');
linear.addColorStop(1, 'blue');
context.fillStyle = linear;
context.fill();
</script>
Linear gradient with multicolors
context.createLinearGradient(60, 20, 100, 250);
- defines the value of linear gradient rectangle values output
It is used to create circular gradient
<canvas id="Canvas" width="200" height="200"></canvas>
<script>
var canvas = document.getElementById('Canvas');
var context = canvas.getContext('2d');
context.rect(0, 0, 600, 250);
var radial = context.createRadialGradient(100, 100, 50, 100, 100, 100);
radial.addColorStop(1, 'pink');
radial.addColorStop(0, 'yellow');
radial.addColorStop(1, 'green');
context.fillStyle = radial;
context.fill();
</script>
In the above code snippet we have defined the radial gradient with width as 200 and height as 200
context.createRadialGradient(100, 100, 50, 100, 100, 100);
- It defines the radialgradient with values as (x1, y1, r1) and (x2, y2,r2).output
Creating circular radial pattern
<canvas id="Canvas" width="200" height="200"></canvas>
<script>
var canvas = document.getElementById('Canvas');
var context = canvas.getContext('2d');
context.rect(0, 0, 600, 250);
var radial = context.createRadialGradient(100, 100, 50, 100, 100, 100);
radial.addColorStop(1, 'yellow');
radial.addColorStop(0, 'red');
radial.addColorStop(0.3, 'blue');
context.fillStyle = radial;
context.fill();
</script>
In the above code snippet we have defined the radial gradient with width as 200 and height as 200
context.createRadialGradient(100, 100, 50, 100, 100, 100);
- It defines the radialgradient with values as (x1, y1, r1) and (x2, y2,r2).output
Gradient text is used to add the color to the text
<canvas id="myCanvas" width="300" height="300"></canvas> <script> var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext('2d'); ctx.shadowColor = "rgb(29, 289, 10)"; ctx.shadowOffsetX = 20; ctx.shadowOffsetY = 20 ctx.shadowBlur = 10; ctx.font = "50px arial"; var gradient = ctx.createLinearGradient(0, 0, 150, 100); gradient.addColorStop(1, "rgb(255, 10, 18)"); gradient.addColorStop(0, "rgb(55, 53, 151)"); ctx.fillStyle = gradient; ctx.fillText("TechFunda", 10, 50); </script>
In the above code snippet we have defined the gradient text which is used to add the color to the text we have defined the shadow of the text with the color and setting the positions of the shadow text in the canvas with id as mycanvas and with as "300" and height as "300"
ctx.filltext= ("TechFunda, 10, 50);
output
Views: 5954 | Post Order: 37