HTML5 ক্যানভাসের সাথে একটি প্যাটার্ন তৈরি করতে নিম্নলিখিত পদ্ধতিটি ব্যবহার করুন:createPattern(image, repetition)− এই পদ্ধতিটি প্যাটার্ন তৈরি করতে একটি চিত্র ব্যবহার করবে। দ্বিতীয় যুক্তিটি নিম্নলিখিত মানগুলির মধ্যে একটি সহ একটি স্ট্রিং হতে পারে:পুনরাবৃত্তি, পুনরাবৃত্তি-x, পুনরাবৃত্তি-y এবং নো-পুনরাবৃত্তি৷ যদি খালি স্ট্রিং বা নাল নির্দিষ্ট করা হয়, তাহলে পুনরাবৃত্তি অনুমান করা হবে।
উদাহরণ
কীভাবে একটি প্যাটার্ন তৈরি করতে হয় তা শিখতে আপনি নিম্নলিখিত কোডটি চালানোর চেষ্টা করতে পারেন −
<!DOCTYPE HTML>
<html>
<head>
<style>
#test {
width:100px;
height:100px;
margin: 0px auto;
}
</style>
<script>
function drawShape(){
// get the canvas element using the DOM
var canvas = document.getElementById('mycanvas');
// Make sure we don't execute when canvas isn't supported
if (canvas.getContext){
// use getContext to use the canvas for drawing
var ctx = canvas.getContext('2d');
// create new image object to use as pattern
var img = new Image();
img.src = 'images/pattern.jpg';
img.onload = function(){
// create pattern
var ptrn = ctx.createPattern(img,'repeat');
ctx.fillStyle = ptrn;
ctx.fillRect(0,0,150,150);
}
} else {
alert('You need Safari or Firefox 1.5+ to see this demo.');
}
}
</script>
</head>
<body id = "test" onload = "drawShape();">
<canvas id = "mycanvas"></canvas>
</body>
</html>