1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| <!DOCTYPE html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <script type="text/javascript"> <!-- function init() { //1. 获取canvas元素 var cv = document.getElementById("cv"); //2. 获取2D上下文 var ctx = cv.getContext('2d'); //3. 新建一个Image对象 var img = new Image(); //4. 设置Image的src img.src = "http://lorempixel.com/400/400/sports/"; //5. 确定Image加载完毕后将Image画到canvas上 img.onload = () => { ctx.drawImage(img, 0, 0, 400, 400); } } //--> </script> <title>Document</title> </head>
<body onload="init()"> <canvas id="cv" width="600" height="600"></canvas>
</body>
</html>
|