SHOW:
|
|
- or go back to the newest paste.
| 1 | Игра "Найди слово из спам-листа" | |
| 2 | [CODE] | |
| 3 | javascript:( | |
| 4 | function() {
| |
| 5 | ||
| 6 | // getting an image | |
| 7 | var img = document.getElementsByTagName('img')[0];
| |
| 8 | var already_restored = (document.getElementsByTagName('canvas').length > 0);
| |
| 9 | if (img && !already_restored) {
| |
| 10 | ||
| 11 | // creating an empty canvas | |
| 12 | var link = document.createElement('a');
| |
| 13 | var canvas = document.createElement('canvas');
| |
| 14 | var ctx = canvas.getContext('2d');
| |
| 15 | ||
| 16 | // adding canvas to the document | |
| 17 | link.appendChild(canvas); | |
| 18 | document.body.appendChild(link); | |
| 19 | ||
| 20 | //setting canvas size to img's fullsize | |
| 21 | canvas.width = img.naturalWidth; | |
| 22 | canvas.height = img.naturalHeight; | |
| 23 | ||
| 24 | // drawing the image on the canvas | |
| 25 | ctx.drawImage(img, 0, 0); | |
| 26 | // adding an ability to download-on-click | |
| 27 | link.addEventListener('click', function() {
| |
| 28 | downloadCanvas(this, canvas, 'restored_image.png'); | |
| 29 | }, false); | |
| 30 | ||
| 31 | // replacing image by canvas | |
| 32 | img.remove(); | |
| 33 | canvas.style = ` | |
| 34 | border: black solid 2px; | |
| 35 | text-align: center; | |
| 36 | margin: auto; | |
| 37 | left: 0px; | |
| 38 | right: 0px; | |
| 39 | top: 0px; | |
| 40 | bottom: 0px; | |
| 41 | position: absolute; | |
| 42 | `; | |
| 43 | } | |
| 44 | function downloadCanvas(link, canvas, filename) {
| |
| 45 | link.href = canvas.toDataURL(); | |
| 46 | link.download = filename; | |
| 47 | } | |
| 48 | ||
| 49 | } | |
| 50 | )(); | |
| 51 | [/CODE] |