canvas下载图片
作者:互联网
canvas下载图片
<!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">
<title>Document</title>
<style>
canvas{ border: 1px solid black; }
</style>
</head>
<body>
<canvas id="myCanvas" width="500" height="500"></canvas>
<a download="canvas.jpg" href="" onclick="downloadpic(this);">下载图片</a>
<script>
var canvas = document.getElementById("myCanvas");
var ox = canvas.width / 2;
var oy = canvas.height / 2;
var ctx = canvas.getContext("2d");
ctx.font = "42px serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText("Hello World", ox, oy);
function downloadpic(e) {
var image = canvas.toDataURL("image/jpg", 1);
e.href = image;
};
</script>
</body>
</html>
标签:canvas,image,ctx,oy,ox,var,下载,图片 来源: https://www.cnblogs.com/ye-hcj/p/10356406.html