如何保存带有两个图像的画布
how to save canvas with two image
>我有一个画布,其中一张图像作为背景显示,另一张图像放置在该图像的中心。 现在我想保存完整的画布。我最近的代码没有保存它。
您可以在此处查看演示 [ http://jsfiddle.net/himani/gqc9b0qd/3/]
var can = document.getElementsByTagName('canvas')[0];
var ctx = can.getContext('2d');
ctx.strokeStyle = '#f00';
ctx.lineWidth = 6;
ctx.lineJoin = 'round';
ctx.strokeRect(40,100,150,100);
var angleInDegrees=0;
var img = document.getElementsByTagName('img')[0];
img.src="http://media1.santabanta.com/full1/Miscellaneous/Cartoon%20Characters/cartoon-characters-47a.jpg";
ctx.drawImage(img,40,100,150,100);
function drawRotated(degrees){
ctx.clearRect(0,0,300,300);
ctx.save();
ctx.translate(40+150/2,100+100/2);
ctx.rotate(degrees*Math.PI/180);
ctx.strokeRect(-150/2,-100/2,150,100);
ctx.drawImage(img,-150/2,-100/2,150,100);
ctx.restore();
}
$("#clockwise").click(function(){
angleInDegrees+=30;
drawRotated(angleInDegrees);
});
$("#save").click(function(){
var ua = window.navigator.userAgent;
if (ua.indexOf("Chrome") > 0) {
// save image without file type
var canvas = document.getElementsByTagName('canvas')[0];
document.location.href = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
// save image as png
var link = document.createElement('a');
link.download = "test1.png";
link.href = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");;
link.click();
}
else {
alert("Please use Chrome");
}
});
如果要
使用 toDataUrl 从跨源请求导出图像,请尝试在图像上定义 crossOrigin 属性。
var img = document.getElementsByTagName('img')[0];
img.crossOrigin = "anonymous";
img.src="http://media1.santabanta.com/full1/Miscellaneous/Cartoon%20Characters/cartoon-characters-47a.jpg";
img.onload = function () {
ctx.drawImage(img,40,100,150,100);
};
它对我来说效果很好。有关此属性和可用值的更多信息,请查看 https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Img#Attributes
相关文章:
- 单击时切换两个图像
- 一个具有两个图像按钮的表单在只能按下一个按钮的情况下发送两个按钮值
- 使用Javascript对象为具有两个背景图像的DIV设置样式时出现问题
- 如何通过一个onclick事件更改两个单独的图像
- 如何使用 jQuery 交换网页中的两个图像(连续两次点击)
- 选择最短的旋转来排列两个图像
- 在jQuery中创建两个图像之间的行
- 将两个图像合并为一个,并使用javascript进行溢出隐藏
- 淡入淡出两个图像[JavaScript]
- 从两个标记中查找静态地图图像坐标
- 带有两个带过渡的图像的随机幻灯片
- 单击按钮时交换两个图像
- 如何将两个渐弱的图像居中
- 任何比较两个不同位图图像并在javascript中检测不同区域的方法
- 一键在一个窗口中打开两个不同的链接图像
- 可以在Javascript中比较两个图像
- 如何保存带有两个图像的画布
- 如何查看两个图像源是否相等
- 如何在两个图像之间添加空间
- 一次两个滑动图像