canvas Realizes Image Layout Filling Function

  • 2021-07-16 01:40:06
  • OfStack

In this paper, we share the specific code of canvas image layout filling for your reference. The specific contents are as follows


<!DOCTYPE html> 
<html> 
<head lang="en"> 
  <meta charset="UTF-8"> 
  <title>canvas- Image layout filling </title> 
  <style> 
    #canvas{ 
      border: 1px solid palevioletred; 
    } 
  </style> 
</head> 
<body> 
  <canvas id="canvas" width="600px" height="600px"> 
  </canvas> 
  <script> 
    // Get to canvas Element  
    var canvas = document.getElementById('canvas'); 
    // Get canvas Drawing environment in  
    var context = canvas.getContext('2d'); 
 
    // Create Image Object  
    var img = new Image(); 
    //alert(img); 
    // Introduce pictures URL 
    img.src = "./image/huiji.png"; 
    window.onload = function (){ 
      // Create a fill rule  .createPattern( Image object ,' Rules ');  No. 1 2 Parameters: repeat , no-repeat , repeat-x,repeat-y; 
       var pattern = context.createPattern(img,'no-repeat'); 
      // Setting fill properties  
      context.fillStyle = pattern; 
      context.fillRect(10,10,canvas.width,canvas.height); 
    } 
  </script> 
</body> 
</html> 

Related articles: