refactor: base64 to blob

This commit is contained in:
Tristan 2019-09-13 17:06:41 +08:00
parent 4adb4c35f8
commit a61a308b6a
2 changed files with 26 additions and 22 deletions

View File

@ -3,25 +3,6 @@ import html2canvas from 'html2canvas';
import styled from 'styled-components';
function saveAs(uri, filename) {
var link = document.createElement('a');
if (typeof link.download === 'string') {
link.href = uri;
link.download = filename;
//Firefox requires the link to be in the body
document.body.appendChild(link);
//simulate click
link.click();
//remove the link when done
document.body.removeChild(link);
} else {
window.open(uri);
}
}
const Wrapper = styled.button`
cursor: pointer;
position: absolute;
@ -61,18 +42,40 @@ const generateImage = (ele, name, isWeixin = false) => {
tmp.classList.add('starting');
tmp.style.height = window.innerHeight + 'px';
}
},
scale: window.devicePixelRatio * 4
}).then(function(canvas) {
console.log(canvas);
if (isWeixin) {
console.log('weixin');
let img = document.createElement('img');
img.classList.add('downloadImg');
img.src = canvas.toDataURL('image/png');
canvas.toBlob(blob => {
const {
URL: { createObjectURL }
} = window;
img.src = createObjectURL(blob);
});
ele.classList.add('img');
ele.appendChild(img);
} else {
saveAs(canvas.toDataURL(), `${name}-${new Date().getTime()}.png`);
canvas.toBlob(blob => {
const {
URL: { createObjectURL, revokeObjectURL },
setTimeout
} = window;
const url = createObjectURL(blob);
const anchor = document.createElement('a');
anchor.setAttribute('href', url);
anchor.setAttribute('download', `${name}-${new Date().getTime()}.png`);
anchor.click();
setTimeout(() => {
revokeObjectURL(url);
}, 100);
});
// saveAs(canvas.toDataURL(), `${name}-${new Date().getTime()}.png`);
}
ele.classList.remove('starting');
});

View File

@ -59,6 +59,7 @@ const Wrapper = styled.section`
&.btns {
margin-top: 2rem;
flex-direction: row;
justify-content: center;
text-shadow: none;
> span:not(:first-child) {
padding-left: 0.4rem;