简体   繁体   English

canvas.drawImage .toDataURL

[英]canvas.drawImage .toDataURL

So far i have this code for add a little watermark to a image and drawimage it to a canvas, now i'm trying to get that canvas data to DataURL, for some reason it's not working as it supposed to be 到目前为止,我已经有了这段代码来为图像添加一些水印并将其绘制到画布上,现在我正尝试将画布数据获取到DataURL,由于某种原因,它无法正常工作

could anyone give me a idea why ? 谁能给我个个主意吗? and maybe how to fix it and get URI data of it 以及如何修复它并获取其URI数据

here's my code 这是我的代码

JS JS

var canvas = document.getElementById('canvas1');
var context = canvas.getContext('2d');

var watermark = new Image();
watermark.src = "http://dummyimage.com/80x80/red/ffffff";

var img = new Image();
img.src = "http://dummyimage.com/500x700/303030/ffffff";

context.drawImage(img, 0, 0);
context.drawImage(watermark,0,0,50,50);

HTML 的HTML

<canvas width="500" height="700" id="canvas1"></canvas>

tried with context.toDataURL('image/png'); 尝试使用context.toDataURL('image/png'); , but it doesn't return ,但不会返回

Here's a link to my bin 这是我的垃圾箱的链接

toDataURL method exists on the canvas and not the context. toDataURL方法存在于画布上,而不存在于上下文中。

The next error that you'll get after fixing that is: 修复后,您将收到的下一个错误是:

Uncaught SecurityError: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.

The image must exist on the same domain to fix that. 该映像必须存在于同一域中才能解决。

You may also have to preload the images. 您可能还必须预加载图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM