Multiple image patterns in 2d context of HTML5 canvas? - Javascript Canvas

Javascript examples for Canvas:image

Description

Multiple image patterns in 2d context of HTML5 canvas?

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://code.jquery.com/jquery-1.7.1.js"></script> 
      <script type="text/javascript">
    $(window).load(function(){/*w ww  .  j  av  a 2s .  c  o  m*/
var A = $('#a')[0],
    B = $('#b')[0],
    C = $('#c')[0],
    ctx;
ctx = A.getContext('2d');
ctx.fillStyle = 'blue';
ctx.fillRect(0, 0, 5, 5);
ctx.fillRect(5, 5, 5, 5);
ctx = B.getContext('2d');
ctx.fillStyle = 'red';
ctx.fillRect(5, 0, 5, 5);
ctx.fillRect(0, 5, 5, 5);
ctx = C.getContext('2d');
var patternA = ctx.createPattern(A, 'repeat');
ctx.fillStyle = patternA;
ctx.fillRect(100,100,20,20);
var patternB = ctx.createPattern(B, "repeat");
ctx.fillStyle = patternB;
ctx.fillRect(150,100,20,20)
    });

      </script> 
   </head> 
   <body>
       A: 
      <canvas id="a" width="10" height="10"></canvas>
       B: 
      <canvas id="b" width="10" height="10"></canvas> 
      <br>
      <br> 
      <canvas id="c" width="400" height="400"></canvas>  
   </body>
</html>

Related Tutorials