问题已解决。谢谢dystroy。
.htaccess
Header set Access-Control-Allow-Origin http://mysite-domain
Javascript
将图片转换为画布(Canvas)
this.imageToCanvas = function()
{
var self = this,
img = new Image();
img.src = document.getElementById("IMAGE").src;
img.crossOrigin = "anonymous";
img.onload = function(){
var canvas = document.getElementById("CANVAS");
if ( ! canvas || ! canvas.getContext ) { return false; }
var ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, img.width, img.height);
self.draw();
};
};
在画布上绘制一些东西。
this.draw = function()
{
var self = this,
text = "stackoverflow",
img = new Image();
img.crossOrigin = "anonymous";
img.src = document.getElementById("SECOND-IMAGE").src;
img.onload = function(){
var canvas = document.getElementById("CANVAS");
if ( ! canvas || ! canvas.getContext ) { return false; }
var ctx = canvas.getContext('2d');
ctx.drawImage(img, 10, 10, img.width, img.height);
ctx.save();
ctx.font = "italic bold 32px 'Times New Roman'";;
ctx.fillStyle = "#0067ef";
ctx.fillText(text, 50, 50);
ctx.restore();
self.canvasToImage(canvas);
};
};
将canvas转换回图像
this.canvasToImage = function(_canvas)
{
var img = new Image();
img.id = "IMAGE";
img.src = _canvas.toDataURL();
$("#" + _canvas.id).replaceWith(img);
};