drawImage() est une méthode clé. Elle peut introduire des images, des toiles et des vidéos, et les redimensionner ou les recadrer.
Code JavaScriptCopier le contenu dans le presse-papiers
-
context.drawImage(img,dx,dy);
Code JavaScriptCopier le contenu dans le presse-papiers
-
context.drawImage(img,dx,dy,dw,dw);
Code JavaScriptCopier le contenu dans le presse-papiers
-
context.drawImage(img,sx,sy,sw,sh,dx,dy,dw,dh
);
Ensuite, essayons de charger une image.
Code JavaScriptCopier le contenu dans le presse-papiers
-
- "zh">
-
"UTF-8">
drawImage()
-
-
- "toile-warp">
- "toile"
- >
Votre navigateur ne prend pas en charge Canvas ? ! Changez-le vite ! !
-
-
<script> </li> <li class="alt">
<span></span> window.onload = </li>
<li>fonction<span></span>(){ </li> <li class="alt">
<span></span> </li>
<li>var<span></span> canvas = document.getElementById(</li>
<li class="alt">"canvas"<span></span> <span class="keyword"> </span>);
<span></span> toile.width = 800 </li> <li>
<span></span> toile.hauteur = 600 <span class="keyword"> </span>
<span></span> <span class="string"></span>var<span></span> context = canvas.getContext(</li>
<li class="alt">"2d"<span></span> </li> <li>);
<span></span> context.fillStyle = </li>
<li class="alt">"#FFF"<span></span> <span class="keyword"> </span>
<span></span> context.fillRect(0,0,800,600); <span class="string"> </span>
<span></span> </li> <li>
<span></span> <span class="string"></span>var<span></span> img = </li>
<li class="alt">nouveau<span></span> Image(); </li> <li>
<span></span> img.src = </li>
<li class="alt">"./images/20-1.jpg"<span></span> <span class="keyword"> </span>
<span></span> img.onload = <span class="keyword"></span>fonction<span></span>(){ </li> <li>
<span></span> context.drawImage(img,200,50); <span class="string"> </span>
<span></span>
</li>
<li class="alt">}; <span> </span>
<span class="keyword"></span></script>
-
-
Résultat de l'exécution :
Créer un cadre photo :
Ici, nous combinons clip(), drawImage() et la courbe de Bézier cubique bezierCurveTo() pour ajouter un cadre photo en forme de cœur au cas ci-dessus~
Code JavaScriptCopier le contenu dans le presse-papiers
-
-
"zh">
-
-
"UTF-8">
-
绘制心形相框
-
-
-
-
"canvas-warp">
-
-
你的浏览器居然不支持Canvas?!赶快换一个吧!!
-
-
-
-
<script> </span> </li>
<li class="alt">
<span> window.onload = </span><span class="keyword">fonction</span><span>(){ </span> </li>
<li>
<span> </span><span class="keyword">var</span><span> canvas = document.getElementById(</span><span class="string">"canvas"</span><span>); </span> </li>
<li class="alt">
<span> canvas.width = 800 ; </span> </li>
<li>
<span> canvas.height = 600 ; </span> </li>
<li class="alt">
<span> </span><span class="keyword">var</span><span> context = canvas.getContext(</span><span class="string">"2d"</span><span>); </span> </li>
<li>
<span> context.fillStyle = </span><span class="string">"#FFF"</span><span>; </span> </li>
<li class="alt">
<span> context.fillRect(0,0,800,600); </span> </li>
<li>
<span> </span> </li>
<li class="alt">
<span> context.beginPath(); </span> </li>
<li>
<span> context.moveTo(400,260); </span> </li>
<li class="alt">
<span> context.bezierCurveTo(450,220,450,300,400,315); </span> </li>
<li>
<span> context.bezierCurveTo(350,300,350,220,400,260); </span> </li>
<li class="alt">
<span> context.clip(); </span> </li>
<li>
<span> context.closePath(); </span> </li>
<li class="alt">
<span> </span> </li>
<li>
<span> </span><span class="keyword">var</span><span> img = </span><span class="keyword">nouveau</span><span> Image(); </span> </li>
<li class="alt">
<span> img.src = </span><span class="string">"./images/20-1.jpg"</span><span>; </span> </li>
<li>
<span> img.onload = </span><span class="keyword">fonction</span><span>(){ </span> </li>
<li class="alt">
<span> context.drawImage(img,348,240,100,100); </span> </li>
<li>
<span> } </span> </li>
<li class="alt">
<span> } ; </span> </li>
<li>
<span></script>
-
-
运行结果:
是不是美美的?好啦,其实在java.awt中,drawImage()也是一个Il s'agit d'une application Java. L'application drawImage() est basée sur la fonction Canvas.图片的处理就很重要了。即使基本功,也是对图片最重要的处理方法。