gpt4 book ai didi

javascript - 使用 Chrome 在本地 Canvas 上绘制图像

转载 作者:太空宇宙 更新时间:2023-11-04 13:29:00 25 4
gpt4 key购买 nike

我有一个必须在本地运行的 HTML5 应用程序。当我运行应用程序并尝试在 Canvas 上加载图像时,在 Chrome 上我收到错误消息:

Unable to get image data from canvas because the canvas has been tainted by cross-origin data

本地文件夹中的 HTML 文档和图像。这是一个将分发给用户的应用程序,因此我无法运行本地服务器。它必须在 Chrome 上按原样运行......我已经阅读了有关此问题的答案,但它们都与与本地文件无关的 CORS 和跨源策略有关...

这种情况的解决方案是什么?如何在 Chrome 上本地加载 Canvas 上的图像?

编辑:这是抛出错误的代码:

var canvas = Matach.createElement(false, "canvas", false, false);           
var context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
context.globalCompositeOperation = 'source-over';

context.save();
context.drawImage(
item.img.img,
-item.img.width / 2,
-item.img.height / 2,
item.img.width,
item.img.height
);

context.restore();

res = context.getImageData(0, 0, canvas.width, canvas.height);

最佳答案

在 chrome 中 getimagedata 有一些安全问题,所以要使用它你必须做一些周转,查看这些帖子以供引用,你会得到解决方案,

canvas getImageData doesn't work when running locally on Windows? (security exception)

Why does HTML Canvas getImageData() not return the exact same values that were just set?

关于javascript - 使用 Chrome 在本地 Canvas 上绘制图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15083805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com