Home

How to Build a Pattern on JavaScript's Canvas for HTML5 and CSS3 Programming

|
Updated:  
2016-03-26 13:15:52
|
HTML5 and CSS3 All-in-One For Dummies
Explore Book
Buy On Amazon

A pattern is used in JavaScript to define an image to be used as a fill or stroke. You can use any image on your HTML5 page as a pattern, but it's generally best to find or create an image that is designed to be tiled. Many sources of tiled patterns exist on the web as well.

After you've got an image you want to use as a fill pattern, here's how to implement it in the tag:

image0.jpg
 function draw(){
 //from pattern.html
 var drawing = document.getElementById("drawing");
 var con = drawing.getContext("2d");
 var texture = document.getElementById("texture");
 pFill = con.createPattern(texture, "repeat");
 con.fillStyle = pFill;
 con.fillRect(10,150,190,150);
 con.font = "40px sans-serif";
 con.fillText("Pattern!", 20, 80);
 con.strokeStyle = pFill;
 con.lineWidth = 5;
 con.strokeRect(10, 10, 180, 100);
 } // end draw

A pattern is simply an image. Building a pattern is relatively straightforward:

  1. Get access to an image.

    You'll need a JavaScript image object to serve as the basis of your pattern. There's a number of ways to do this, but the easiest is to create the image somewhere in your HTML, hide it with the display:none style, and use the standard document.getElementById technique to get access to your image.

  2. Create a variable for the pattern.

    Like gradients, pattern fills can be reused, so store the pattern in a variable for later reuse.

  3. Build the pattern.

    The context's createpattern() method creates a pattern from an image.

  4. Specify the pattern's repeat parameter.

    The second parameter indicates how the pattern will repeat. The default value is repeat, which repeats the pattern in both the X and Y axis indefinitely. If your pattern is not tiled, you will see a visible seam where the pattern repeats. You can also set the repeat value to repeat-x, repeat-y, and no-repeat.

  5. Apply the pattern variable to the fillStyle or strokeStyle.

    Assign the pattern variable to the context's and then perform any fill operation to draw in the pattern.

About This Article

This article is from the book: 

About the book author:

Andy Harris earned a degree in Special Education from Indiana University/Purdue University–Indianapolis (IUPUI). He taught young adults with severe disabilities for several years. He also taught himself enough computer programming to support his teaching habit with freelance programming.
Those were the exciting days when computers started to have hard drives, and some computers connected to each other with arcane protocols. He taught programming in those days because it was fun.
Eventually, Andy decided to teach computer science full time, and he still teaches at IUPUI. He lectures in the applied computing program and runs the streaming media lab. He also teaches classes in whatever programming language is in demand at the time. He has developed a large number of online video-based courses and international distance education projects.
Andy has written several books on various computing topics and languages including Java, C#, mobile computing, JavaScript, and PHP/MySQL.
Andy welcomes comments and suggestions about his books. He can be reached at [email protected].