Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Игра "Найди слово из спам-листа"
- [CODE]
- javascript:(
- function() {
- // getting an image
- var img = document.getElementsByTagName('img')[0];
- var already_restored = (document.getElementsByTagName('canvas').length > 0);
- if (img && !already_restored) {
- // creating an empty canvas
- var link = document.createElement('a');
- var canvas = document.createElement('canvas');
- var ctx = canvas.getContext('2d');
- // adding canvas to the document
- link.appendChild(canvas);
- document.body.appendChild(link);
- //setting canvas size to img's fullsize
- canvas.width = img.naturalWidth;
- canvas.height = img.naturalHeight;
- // drawing the image on the canvas
- ctx.drawImage(img, 0, 0);
- // adding an ability to download-on-click
- link.addEventListener('click', function() {
- downloadCanvas(this, canvas, 'restored_image.png');
- }, false);
- // replacing image by canvas
- img.remove();
- canvas.style = `
- border: black solid 2px;
- text-align: center;
- margin: auto;
- left: 0px;
- right: 0px;
- top: 0px;
- bottom: 0px;
- position: absolute;
- `;
- }
- function downloadCanvas(link, canvas, filename) {
- link.href = canvas.toDataURL();
- link.download = filename;
- }
- }
- )();
- [/CODE]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement