canvas教程

将HTML5 Canvas的内容保存为图片 图文全成

字号+ 作者:H5之家 来源:H5之家 2017-01-20 09:01 我要评论( )

技术交流学习或者有任何问题欢迎加群 : 154514123 标签: html !-- meta http-equiv="X-UA-Compatible" content="chrome=1" -- meta charset="UTF-8" meta content="width=device-width,user-scalable=no,initial-scale=1" head script window.onload = func

技术交流学习或者有任何问题欢迎加群 : 154514123 

标签:                           

<html>
<!-- <meta http-equiv="X-UA-Compatible" content="chrome=1"> -->
<meta charset="UTF-8">
<meta content="width=device-width,user-scalable=no,initial-scale=1">
<head>
<script>
window.onload = function() {
draw();
var saveButton = document.getElementById("saveImageBtn");
bindButtonEvent(saveButton, "click", saveImageInfo);
var dlButton = document.getElementById("downloadImageBtn");
bindButtonEvent(dlButton, "click", saveAsLocalImage);
};
function draw(){
var canvas = document.getElementById("thecanvas");
var ctx = canvas.getContext("2d");
//设置字体样式
ctx.font = "30px Courier New";
//设置字体填充颜色
ctx.fillStyle = "blue";
//从坐标点(50,50)开始绘制文字
ctx.fillText("CodePlayer+中文测试", 50, 50);

var img = new Image();
img.src = "http://hs.3g.cnfol.com/uh/Imgs/WeiXin/1.jpg";
img.onload = function(){
ctx.drawImage(img,0,50);
}
}

function bindButtonEvent(element, type, handler)
{
if(element.addEventListener) {
element.addEventListener(type, handler, false);
} else {
element.attachEvent(‘on‘+type, handler);
}
}

function saveImageInfo ()
{
var mycanvas = document.getElementById("thecanvas");
var image = mycanvas.toDataURL("image/png");
/*var w=window.open(‘about:blank‘,‘image from canvas‘);*/
document.getElementById("newImg").src = image;
/*w.document.write("<img src=‘"+image+"‘ alt=‘from canvas‘/>");*/
}

function saveAsLocalImage () {
var myCanvas = document.getElementById("thecanvas");

var image = myCanvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
window.location.href=image; // it will save locally
}
</script>
</head>
<body bgcolor="#E6E6FA">
<div>
<canvas></canvas>
<button>Save Image</button>
<button>Download Image</button>

<img src="" >
</div>
</body>
</html>

将HTML5 Canvas的内容保存为图片 图文全成

标签:                           

原文:

技术交流学习或者有任何问题欢迎加群 : 154514123 

广告推荐

讨论区

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • HTML canvas to blob to downloadable file in IE9, 10

    HTML canvas to blob to downloadable file in IE9, 10

    2017-01-20 10:00

  • 数据可视化工程师(Canvas/SVG/OpenGL) 北京裕民东路3号京版信

    数据可视化工程师(Canvas/SVG/OpenGL) 北京裕民东路3号京版信

    2017-01-20 09:00

  • Canvas Draw 3.0.2 加载与工具和技术来创建创造性的内容 MAC下载

    Canvas Draw 3.0.2 加载与工具和技术来创建创造性的内容 MAC下载

    2017-01-19 11:01

  • Canvas的基本分析报告

    Canvas的基本分析报告

    2017-01-19 10:00

网友点评