fix: cross origin issue for image

This commit is contained in:
chenshenhai 2022-10-22 15:32:49 +08:00
parent 6f0a575239
commit 5f6523164d
3 changed files with 20 additions and 1 deletions

View file

@ -27,6 +27,10 @@
<body>
<div id="mount"></div>
<div>
<button id="btn">Export</button>
</div>
<div id="preview"></div>
<script type="module" src="./main.js"></script>
</body>

View file

@ -7,7 +7,8 @@ var opts = {
contextWidth: 300,
contextHeight: 200,
devicePixelRatio: 4,
}
};
// var config = {
// elementWrapper: {
// controllerSize: 4,
@ -29,3 +30,16 @@ const idraw = new iDraw(
}
);
idraw.setData(data);
const btn = document.querySelector('#btn');
btn.addEventListener('click', () => {
idraw.exportDataURL({type: "image/png"}).then((dataURL) => {
const preview = document.querySelector('#preview');
preview.innerHTML = `<img width="300" src="${dataURL}">`;
}).catch((err) => {
console.log(err);
});
});

View file

@ -4,6 +4,7 @@ const { Image } = window;
export function loadImage(src: string): Promise<HTMLImageElement> {
return new Promise((resolve, reject) => {
const img = new Image;
img.setAttribute('crossOrigin', 'anonymous');
img.onload = function() {
resolve(img);
};