Canvas 显示图片
这是一个简单的API应用,看看代码就一目了然了:
<!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>
如代码注释所言,整个过程就是:
1、准备好画板
2、准备好图片
3、图片准备好后贴到画布上
原文地址:http://blog.techcave.cn/2017/09/13/Canvas%E6%98%BE%E7%A4%BA%E5%9B%BE%E7%89%87/
相关推荐
星星有所不知 2020-10-12
MIKUScallion 2020-07-05
jinxiutong 2020-05-10
MIKUScallion 2020-04-11
MIKUScallion 2020-02-22
jinxiutong 2020-02-13
大地飞鸿 2020-02-11
jinxiutong 2020-02-10
大地飞鸿 2020-11-12
jinxiutong 2020-07-26
songfens 2020-07-05
songfens 2020-06-11
songfens 2020-06-08
northwindx 2020-05-31
northwindx 2020-05-31
northwindx 2020-05-27
northwindx 2020-05-25