<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>圖像</title> <style> canvas{ border:1px solid red; } </style> <script> window.onload = function(){ var canvas = document.getElementById('canvas'); var context = canvas.getContext('2d'); // 繪製圖像 // 1.建立圖像對象 img節點 var img = new Image(); // var img1 = document.createElement('img'); img.src = '../1.jpg'; img.onload = function(){ // 參數:img節點,開始繪製的位置x,y context.drawImage(img,0,0); // 參數:img節點,開始繪製的位置x,y,寬,高 // context.drawImage(img,0,0,200,200); // 參數:img節點,局部位置x,y,寬,高,目標位置x,y,寬,高 context.drawImage(img,345,100,100,100,500,0,200,200); } } </script> </head> <body> <canvas id="canvas" width="700px" height="400px"></canvas> </body> </html>