Canvas 输出位图

一 介绍
toDataURL()是canvas中将图片的二进制格式转为dataURL格式使用的方法。
dataURL是将数据按base64进行编码,它是一种保存二进制的方式。
下面通过这种方法输出位图。
 
二 代码下载
Html代码   收藏代码
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <meta name="author" content="Yeeku.H.Lee(CrazyIt.org)" />  
  5.     <meta http-equiv="Content-Type" content="text/html; charset=GBK" />  
  6.     <title> 位图输出 </title>  
  7. </head>  
  8. <body>  
  9. <h2> 位图输出 </h2>  
  10. <canvas id="mc" width="360" height="280"  
  11.     style="border:1px solid black"></canvas>  
  12. <img id="result" src="" alt="图片输出"/>  
  13. <script type="text/javascript">  
  14.     // 获取canvas元素对应的DOM对象  
  15.     var canvas = document.getElementById('mc');  
  16.     // 获取在canvas上绘图的CanvasRenderingContext2D对象  
  17.     var ctx = canvas.getContext('2d');  
  18.     ctx.save();  
  19.     ctx.translate(30 , 20);  
  20.     var image = new Image();  
  21.     image.src = "wjc.gif";  
  22.     image.onload = function()  
  23.     { 下载
  24.         // 创建位图填充  
  25.         imgPattern = ctx.createPattern(image, "repeat");  
  26.         // 设置使用位图填充作为填充颜色  
  27.         ctx.fillStyle = imgPattern;  
  28.         // 填充一个矩形  
  29.         ctx.fillRect(0 , 0 , 160 , 80);  
  30.         // 恢复坐标系统  
  31.         ctx.restore();  
  32.         // 平移坐标系统  
  33.         ctx.translate(280 , 160)  
  34.         ctx.beginPath();  
  35.         // 添加圆弧  
  36.         ctx.arc(0 , 0  , 80 , 0 , Math.PI * 2 , true);  
  37.         ctx.closePath();  
  38.         ctx.lineWidth = 12;  
  39.         // 设置使用位图填充作为边框颜色  
  40.         ctx.strokeStyle = imgPattern;  
  41.         ctx.stroke();  
  42.         // 使用img元素来显示Canvas的输出结果  
  43.         document.getElementById("result").src = canvas.toDataURL("image/png");  
  44.     }  
  45. </script>  
  46. </body>  
  47. </html>  
 
三 运行结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值