//canvas转换成图片下载
document.getElementById("download").onclick=function(){
canvas = document.getElementById("myCanvas");
var dataURL = canvas.toDataURL("image/png");
var a = document.getElementById("download");
a.setAttribute("href",dataURL);
}
function drawHuaban(){
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.fillStyle = "black";
var maxWidth = document.body.clientWidth;
var maxHeight = document.body.clientHeight;
ctx.fillRect(0,0,800,500);
var onoff = false;
var oldx = -10;
var oldy = -10;
ctx.lineWidth = 20;
ctx.strokeStyle = "white";
canvas.addEventListener("mousemove",drawA,true);
canvas.addEventListener("mousedown",down,false);
canvas.addEventListener("mouseup",up,false);
canvas.addEventListener("mouseleave",leave,false);
function down(e){
onoff = true;
oldx = e.pageX-10;
oldy = e.pageY-10;
}
function leave(){
onoff = false;
}
function up(){
onoff = false;
}
function drawA(event){
if(onoff == true){
var x = event.pageX;
var y = event.pageY;
console.log("x:"+x+";y:"+y);
ctx.lineCap = "round";
ctx.beginPath();
ctx.moveTo(oldx,oldy);
ctx.lineTo(x,y);
ctx.stroke();
oldx = x;
oldy = y;
}
}
}
- THE END -
最后修改:2019年12月26日
非特殊说明,本博所有文章均为博主原创。
如若转载,请注明出处:https://www.95app.top/canvas%e5%ae%9e%e7%8e%b0%e7%94%bb%e6%9d%bf/
共有 0 条评论