strokeStyle Property - Javascript Canvas Reference

Javascript examples for Canvas Reference:strokeStyle


The strokeStyle property sets or gets the color, gradient, or pattern used for strokes. Default value is #000000

JavaScript syntax

context.strokeStyle = color|gradient|pattern;

Property Values

colorA CSS color value that indicates the stroke color of the drawing. Default value is #000000
gradient A gradient object (linear or radial) used to create a gradient stroke
pattern A pattern object used to create a pattern stroke

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>

<canvas id="myCanvas" width="300" height="250" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>


var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

var gradient = ctx.createLinearGradient(0, 0, 170, 0);
gradient.addColorStop("0", "magenta");
gradient.addColorStop("0.5", "blue");
gradient.addColorStop("1.0", "red");

// Fill with gradient
ctx.strokeStyle = gradient;//w w  w  .  ja va2  s  .  com
ctx.lineWidth = 5;
ctx.strokeRect(20, 20, 150, 100);



Related Tutorials