Canvas drawImage() मेथड
HTML5 Canvas API का drawImage() मेथड इमेज, कैनवास या वीडियो को कैनवास पर ड्रॉ करता है। इमेज के भाग भी ड्रॉ कर सकते हैं और आकार बदल सकते हैं।
पैरामीटर्स:
| क्र.सं. | पैरामीटर और विवरण |
|---|---|
| 1 | img - इमेज, कैनवास या वीडियो एलिमेंट |
| 2 | sx - क्लिपिंग शुरू करने का X निर्देशांक (वैकल्पिक) |
| 3 | sy - क्लिपिंग शुरू करने का Y निर्देशांक (वैकल्पिक) |
| 4 | swidth - क्लिप्ड इमेज की चौड़ाई (वैकल्पिक) |
| 5 | sheight - क्लिप्ड इमेज की ऊंचाई (वैकल्पिक) |
| 6 | x - कैनवास पर प्लेसमेंट X निर्देशांक |
| 7 | y - कैनवास पर प्लेसमेंट Y निर्देशांक |
| 8 | width - उपयोग की जाने वाली इमेज की चौड़ाई |
| 9 | height - उपयोग की जाने वाली इमेज की ऊंचाई |
उदाहरण (एनिमेटेड पैटर्न)
<!DOCTYPE HTML> <html>
<head>
<script type="text/javascript">
var pattern = new Image();
function animate() {
pattern.src = '/html5/images/pattern.jpg';
setInterval(drawShape, 100);
}
function drawShape() {
var canvas = document.getElementById('newCanvas');
if (canvas.getContext) {
var ctx = canvas.getContext('2d');
ctx.fillStyle = 'rgba(0,0,0,0.4)';
ctx.strokeStyle = 'rgba(0,153,255,0.4)';
ctx.save();
ctx.translate(150,150);
var time = new Date();
ctx.rotate( ((2*Math.PI)/6)*time.getSeconds() + ((2*Math.PI)/6000)*time.getMilliseconds() );
ctx.translate(0,28.5);
ctx.drawImage(pattern,-3.5,-3.5);
ctx.restore();
} else {
alert('You need Safari or Firefox 1.5+ to see this demo.');
}
}
</script>
</head>
<body onload="animate();">
<canvas id="newCanvas" width="400" height="400"></canvas>
</body>
</html>