婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > 將HTML5 Canvas的內容保存為圖片借助toDataURL實現

將HTML5 Canvas的內容保存為圖片借助toDataURL實現

熱門標簽:臨海地圖標注app 四川移動電銷外呼客戶管理系統 百度地圖標注為什么總是封號 怎么做百度地圖標注 400開頭的電話好申請不 智能芯電話機器人 小朱地圖標注 咸陽穩定外呼系統軟件 地圖標注柱狀圖
主要思想是借助Canvas自己的API - toDataURL()來實現,整個實現
HTML + JavaScript的代碼很簡單。

復制代碼
代碼如下:

<html>
<meta http-equiv="X-UA-Compatible" content="chrome=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.fillStyle = "rgba(125, 46, 138, 0.5)";
ctx.fillRect(25,25,100,100);
ctx.fillStyle = "rgba( 0, 146, 38, 0.5)";
ctx.fillRect(58, 74, 125, 100);
ctx.fillStyle = "rgba( 0, 0, 0, 1)"; // black color
ctx.fillText("Gloomyfish - Demo", 50, 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');
w.document.write("<img src='"+image+"' alt='from canvas'/>");
}
function saveAsLocalImage () {
var myCanvas = document.getElementById("thecanvas");
// here is the most important part because if you dont replace you will get a DOM 18 exception.
// var image = myCanvas.toDataURL("image/png").replace("image/png", "image/octet-stream;Content-Disposition: attachment;filename=foobar.png");
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 width=200 height=200 id="thecanvas"></canvas>
<button id="saveImageBtn">Save Image</button>
<button id="downloadImageBtn">Download Image</button>
</div>
</body>
</html>

運行效果如下

標簽:平頂山 陜西 公主嶺 黃石 黃石 平涼 山南 南平

巨人網絡通訊聲明:本文標題《將HTML5 Canvas的內容保存為圖片借助toDataURL實現》,本文關鍵詞  將,HTML5,Canvas,的,內容,保,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《將HTML5 Canvas的內容保存為圖片借助toDataURL實現》相關的同類信息!
  • 本頁收集關于將HTML5 Canvas的內容保存為圖片借助toDataURL實現的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 彭州市| 抚宁县| 通河县| 景东| 琼海市| 广饶县| 柳林县| 仙居县| 芦山县| 闽侯县| 大埔县| 兰坪| 玉门市| 手机| 平湖市| 平武县| 长岛县| 方山县| 文登市| 泗阳县| 大足县| 理塘县| 区。| 大渡口区| 津市市| 南岸区| 甘泉县| 龙南县| 邹城市| 九台市| 恩施市| 和田县| 两当县| 平利县| 新安县| 藁城市| 丰城市| 始兴县| 竹北市| 涟水县| 玉溪市|