尝试将我拖放到画布元素上的图像转换为PNG或Jpeg照片(类似于polyvore的情绪板概念),以便我可以在一个PNG或Jpeg照片中同时查看我放置在画布上的所有图像。这样我就可以保存它或对照片进行任何操作。
但是,当我按保存并尝试使用.alert()方法将数据转换为实际显示保存的图像时,会遇到SecurityError:操作不安全。您有什么想法如何克服此错误以实现目标?谢谢!
这是我的项目链接,以便查看实时效果:http://amechi101.github.io/moodboard/ Html:
但是,当我按保存并尝试使用.alert()方法将数据转换为实际显示保存的图像时,会遇到SecurityError:操作不安全。您有什么想法如何克服此错误以实现目标?谢谢!
这是我的项目链接,以便查看实时效果:http://amechi101.github.io/moodboard/ Html:
<div id="container" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div class="buttonMoodBoard">
<button class="btn btn-primary btn-lg" id="save">Save Moodboard</button>
</div>
Javascript:
var stage = new Kinetic.Stage({
container: 'container',
width: 500,
height:500
});
var layer = new Kinetic.Layer();
uni_width = 120;
function allowDrop(ev) {
ev.preventDefault();
}
function drag(ev) {
ev.dataTransfer.setData("Text", ev.target.id);
}
function drop(ev) {
ev.preventDefault();
data = ev.dataTransfer.getData("Text");
img_received = document.getElementById(data);
ratio = img_received.height / img_received.width;
var c=document.getElementById("container");
drop_x=ev.pageX-c.offsetLeft;
drop_y=ev.pageY-c.offsetTop;
var imageObj = new Image();
imageObj.src = img_received.src;
imageObj.onload = function() {
var new_image = new Kinetic.Image({
x: drop_x - uni_width / 2,
y: drop_y - uni_width * ratio / 2,
image: imageObj,
width: uni_width,
height: uni_width * ratio,
draggable: true
});
// add the shape to the layer
layer.add(new_image);
// add the layer to the stage
stage.add(layer);
};
}
//saving to data base via Json
document.getElementById('save').addEventListener('click', function (canvas) {
var json = stage.toJSON();
var canvas = document.getElementsByTagName("canvas");
var img = canvas[0].toDataURL("image/png");
var alertJson = alert(img);
console.log(json, alertJson);
}, false);
crossorigin = true
- BergiimageObj.src = …
之前,您需要设置imageObj.crossOrigin = "Anonymous";
- 请注意其他域服务器需要支持CORS。 - Bergi